Skip to main content

No project description provided

Project description

pipeline status coverage report

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()
        'example': {
            'uri': f'',
            'issuer': settings.EXAMPLE_ISSUER,
            'audience': settings.EXAMPLE_AUDIENCE,

At this point, every route except / requires an authorization: Bearer {token} where token must:

  • be a JWT
  • be issued by issuer to the audience audience
  • be signed by one of the keys declared in jwks of the response of
  • 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):

In particular, if your auth provider provides custom claims, you can use these for authorization.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for starlette-oauth2-api, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size starlette-oauth2-api-0.1.2.tar.gz (3.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page