Authentication support for layab.
Project description
Authentication for layab
Provides a decorator to ensure that, in a context of a Flask
server, a valid OAuth2 token was received.
As expected by the HTTP specification, token is extracted from Authorization
header and must be prefixed with Bearer
.
If validation fails, an werkzeug.exceptions.Unauthorized
exception is raised.
Otherwise user details are stored in flask.g.current_user
, this variable is an instance of the User
class,
it contains name
property holding the authenticated user name (extracted from the upn field inside the token).
Decorator works fine on flask-restplus
methods as well.
Below is a sample Flask
application with an endpoint requesting a Microsoft issued OAuth2 token.
import flask
import layabauth
app = flask.Flask(__name__)
@app.route("/my_endpoint")
@layabauth.requires_authentication("https://sts.windows.net/common/discovery/keys")
def my_endpoint():
return "OK"
app.run()
OpenAPI
You can generate OpenAPI security
definition thanks to layabauth.authorizations
.
You can generate OpenAPI method security
thanks to layabauth.method_authorizations
Testing
Authentication can be mocked using layabauth.testing.auth_mock
pytest
fixture.
upn
pytest
fixture returning the UPN located in token used in tests must be provided.
from layabauth.testing import *
@pytest.fixture
def upn():
return "TEST@email.com"
def test_authentication(auth_mock):
pass
How to install
- python 3.6+ must be installed
- Use pip to install module:
python -m pip install layabauth
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for layabauth-3.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4997c89b95a601921603709e810bbedfbff6040cf3105aa09bece5b9acfeb037 |
|
MD5 | 99772b3dba3328cdea80e2512f238f4a |
|
BLAKE2b-256 | 89738114bf4ebb2750ba33faf8ea895eecb52278291db554786d7376324eaed7 |