No project description provided
Project description
Starlette OAuth2
A Starlette middleware for authentication and authorization through JWT.
This middleware is intended to add authentication and authorization to an API (e.g. FastAPI) through an external auth provider (e.g. Microsoft AD, Auth0).
This middleware depends only on python-jose
, which it uses to decode and validate JWT.
How to install
pip install starlette-oauth2-api
How to use
from starlette.applications import Starlette
from starlette_oauth2_api import AuthenticateMiddleware
app = Starlette()
app.add_middleware(AuthenticateMiddleware,
{
'example': {
'uri': f'https://example.com/tenant-id/v2.0/.well-known/openid-configuration',
'issuer': settings.EXAMPLE_ISSUER,
'audience': settings.EXAMPLE_AUDIENCE,
}
},
public_paths={'/'},
)
At this point, every route except /
requires an authorization: Bearer {token}
where token
must:
- be a JWT
- be issued by
issuer
to the audienceaudience
- be signed by one of the keys declared in
jwks
of the response ofhttps://example.com/tenant-id/v2.0/.well-known/openid-configuration
- not have expired
Failing any of the conditions above returns a 401 response, failing to contain the header with Bearer
returns a 400 response.
When the request is valid, the Middlware adds all claims in the JWT to oauth2-claims
, which can be accessed using
...
def home(request):
...
request.scope['oauth2-claims']
...
In particular, if your auth provider provides custom claims, you can use these for authorization.
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
File details
Details for the file starlette-oauth2-api-0.1.1.tar.gz
.
File metadata
- Download URL: starlette-oauth2-api-0.1.1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7a3f713ca90be621b8d6a8ea6c2db2a3f28b81f575300a736f6147c9572431b |
|
MD5 | 3276a3d24cd6e633a5bbe41c2e444891 |
|
BLAKE2b-256 | a25f5780ab251e0741216140058f96911dadef500bf28e29c8bd83052749af47 |