Skip to main content

Easy auth0.com integration for FastAPI

Project description

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

Project details


Download files

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

Source Distribution

fastapi-auth0-0.2.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

fastapi_auth0-0.2.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-auth0-0.2.1.tar.gz.

File metadata

  • Download URL: fastapi-auth0-0.2.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for fastapi-auth0-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c66a48d1c62bcaee162798e637887f8e1a840c9fa3fb72b46fa4e179f4bc1ff7
MD5 0db90523364d7db98c534d6bf6bf1b81
BLAKE2b-256 0cee072e62061fcef1523ae39796858b57feace75a3d679c48f19de4221d288f

See more details on using hashes here.

File details

Details for the file fastapi_auth0-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_auth0-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for fastapi_auth0-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2cf9e6c4fe94b75e91edc8d688dfe730b588ebdb74a5e472636a1ca08cdcf766
MD5 cc3d066b35132c6a08b0a6230ba2282c
BLAKE2b-256 52dc7949fd899eb76730b835fec9ebfe654f1c9295b2082f4f639ad2d6b06261

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page