No project description provided
Project description
Starlette OAuth2
A Startlette 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.0.tar.gz
.
File metadata
- Download URL: starlette-oauth2-api-0.1.0.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 | 4ca63d51b3df0e2673991621dd7b7eb116ebfaf06cbd765ddd2a9fcc2e6bd048 |
|
MD5 | 3d8ec7db100fea9ab4c1ddb0848980f9 |
|
BLAKE2b-256 | 00cae7df283c0190e8470f334f1f71fb98efb8f7d2d72231474ee91f23771428 |