Skip to main content

Easy auth0.com integration for FastAPI and PyJWT

Project description

Description

this is a FORK of the original fastapi-auth0 with python-jose swapped to PyJWT (as in the auth0 package itself)

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-pyjwt

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_pyjwt-0.6.3.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_auth0_pyjwt-0.6.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_auth0_pyjwt-0.6.3.tar.gz.

File metadata

  • Download URL: fastapi_auth0_pyjwt-0.6.3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastapi_auth0_pyjwt-0.6.3.tar.gz
Algorithm Hash digest
SHA256 d6ba029b60cfff2c890056cd5fb7bc6074f40403958681aeb85a68f8d55a7151
MD5 4af52aadf41702bf19625ab8c2149550
BLAKE2b-256 0815879389637c778bc5901dcb8d787086f03ac8d44c450ec3479b204008677c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_auth0_pyjwt-0.6.3.tar.gz:

Publisher: package.yml on spawn-guy/fastapi-auth0

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_auth0_pyjwt-0.6.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_auth0_pyjwt-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a32c8068783fa6d875b0093af5b484ddbc080de275c0925f112a56f49a9cb370
MD5 3c323cd2ac43701fd75c23a014f5897e
BLAKE2b-256 c78415fb72ea3b356467acb1798e5a15212e8216ab7b0ec7d90a7703c09c92c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_auth0_pyjwt-0.6.3-py3-none-any.whl:

Publisher: package.yml on spawn-guy/fastapi-auth0

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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