Skip to main content

Description

Integrate FastAPI with https://auth0.com in a simple and elegant way. Get automatic Swagger UI support for the implicit scheme (along others), which means that signing in using social providers is only a few clicks away with no additional code.

Installation

  • pip install fastapi-auth0

Requirements

Reading auth0 docs is recommended in order to understand the following concepts:

  • API's and audience
  • Applications
  • Grant types
  • Permissions and scopes

This library cannot do magic if the auth0 tenant is not configured correctly!

Email field requirements

In order to get email for Auth0User, the API must have "openid profile email" permission and the rule "Add email to access token" must be added with the matching namespace, see tests. The security is not affected in any way if we don't do this, but we need to if we want to know the user email's address. Otherwise, email field will always be None.

Swagger UI login requirements

In order to utilize the interactive docs for the implicit flow, the callback url must be registered on the auth0 dashboard. For swagger this url is {SWAGGER_DOCS_URL}/oauth2-redirect, so if you are running FastAPI on localhost:8000, that becomes http://localhost:8000/docs/oauth2-redirect. Add it to "Allowed Callback URLs" for the application which you intend to login with (the client_id you input for Auth0ImplicitBearer authorization).

In order to logout and login with another user, it's necessary to manually call GET https://{auth0_domain}/v2/logout, becacause the Swagger UI logout button is not able to clear 3rd party session / cookies.

Example usage

from fastapi import FastAPI, Depends, Security
from fastapi_auth0 import Auth0, Auth0User

auth = Auth0(domain='your-tenant.auth0.com', api_audience='your-api-identifier', scopes={'read:blabla': ''})
app = FastAPI()

@app.get("/public")
def get_public():
    return {"message": "Anonymous user"}

@app.get("/secure", dependencies=[Depends(auth.implicit_scheme)])
def get_secure(user: Auth0User = Security(auth.get_user, scopes=['read:blabla'])):
    return {"message": f"{user}"}

Example user responses:

id='Art2l2uCeCQk5zDVbZzNZmQkLJXLd9Uy@clients' permissions=['read:blabla'] email=None               # user is M2M app
id='auth0|5fe72b8eb2ac50006f725451' permissions=['read:blabla'] email='some.user@outlook.com'      # user signed up using auth0 database
id='google-oauth2|115595596713285791346' permissions=['read:blabla'] email='other.user@gmail.com'  # user signed up using google

Video tutorial

https://youtu.be/cGRdFjgAy9s

Metadata

Release files for fastapi-auth0 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-auth0 0.5.0
File Size Uploaded
fastapi-auth0-0.5.0.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-auth0 0.5.0
File Interpreter ABI Platform
fastapi_auth0-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / fastapi-auth0-0.5.0.tar.gz

Download URL fastapi-auth0-0.5.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5513880b59b4aad0e60c159af519f29d49c14993adc84e9193a7bf47dddd5a1d
BLAKE2b-256 checksum
How to use checksums
b23089260d69537363b0d9d9deedf5c9a59dd38d65eee3899fe576efb239269b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / fastapi_auth0-0.5.0-py3-none-any.whl

Download URL fastapi_auth0-0.5.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73c6ad0cf1803c8eb7251186030c0948cfd3d5aae87918ae3947e826a04dfd8b
BLAKE2b-256 checksum
How to use checksums
607b113a42c4b2d6e93567f116d4443ad62d09acca17ee37902b1fe7ab9e4619
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page