Validate and extract information from OAuth2 token.
Project description
Validate and extract information from OAuth2 token
Validating an OAuth2 token
import oauth2helper
headers = {"Authorization": "Bearer YOUR_OAUTH2_TOKEN"}
my_token = headers.get('Authorization')[7:]
# Will raise InvalidTokenError or InvalidKeyError in case validation failed
oauth2helper.validate(my_token, "https://provider_url/common/discovery/keys")
Extracting user from a OAuth2 token
import oauth2helper
headers = {"Authorization": "Bearer YOUR_OAUTH2_TOKEN"}
my_token = headers.get('Authorization')[7:]
json_header, json_body = oauth2helper.validate(my_token, "https://provider_url/common/discovery/keys")
username = oauth2helper.user_name(json_body)
Starlette
A Starlette AuthenticationMiddleware
backend is available.
from starlette.applications import Starlette
from starlette.authentication import requires
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware
from oauth2helper.starlette import OAuth2IdTokenBackend, unauthorized
backend = OAuth2IdTokenBackend(
identity_provider_url="https://identity_provider_url",
# You can extract scopes per user and validate them on @requires decorator
scopes_retrieval=lambda json_body: ["authenticated"],
)
app = Starlette(middleware=[Middleware(AuthenticationMiddleware, backend=backend, on_error=unauthorized)])
@app.route("/authenticated_endpoint")
@requires(scopes=["authenticated"])
def endpoint(request):
pass # Implement your own logic
How to install
- python 3.6+ must be installed
- Use pip to install module:
python -m pip install oauth2helper
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
oauth2helper-3.4.0.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for oauth2helper-3.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2377d787048ef9cda96d8c8d21f6d811e1a125d38ad34e043250bb742a16991 |
|
MD5 | 8f74bce3b377635a8b4949bfe45b0039 |
|
BLAKE2b-256 | 130f0e7de0f8ba1df844cec353693f5389c7a536007b0bf3d2ff76be9a1bb2ff |