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
Metadata
Release files for oauth2helper 3.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| oauth2helper-3.4.0.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| oauth2helper-3.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / oauth2helper-3.4.0.tar.gz
| Download URL | oauth2helper-3.4.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f5141fbe61a91abe0b5269bd9c0596be1a8a0983051fe91bb909f34540c6df63
|
|
BLAKE2b-256 checksum How to use checksums |
5e26fae8395ec1e2e6a2f11b3fcb62b1c256dab7e4105cc39b9f5c89b5b375ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1
|
Release files / oauth2helper-3.4.0-py3-none-any.whl
| Download URL | oauth2helper-3.4.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d2377d787048ef9cda96d8c8d21f6d811e1a125d38ad34e043250bb742a16991
|
|
BLAKE2b-256 checksum How to use checksums |
130f0e7de0f8ba1df844cec353693f5389c7a536007b0bf3d2ff76be9a1bb2ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1
|