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
- Latest beta:
pip install -i https://test.pypi.org/simple/ 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).
Unfortunately, it's not possible to logout and login with another user before the token expires. This is a limitation of Swagger UI as the logout button only clears the access token locally, but doesn't call any url.
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
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
Built Distribution
File details
Details for the file fastapi-auth0-0.1.5.tar.gz
.
File metadata
- Download URL: fastapi-auth0-0.1.5.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a00fa94310382409cdf63cff38420d86b49d6c415177c9aca988e4d0aec06a62 |
|
MD5 | 4e63f74fc7fa7348172918dc587b32d7 |
|
BLAKE2b-256 | e4c939be3def435f0f6f4c4c92bade2d84918088d6f0b337cf5e5eeb71b69138 |
File details
Details for the file fastapi_auth0-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: fastapi_auth0-0.1.5-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b150fa14b6d9da85d0218cc0b1f36c3a34fcfe77707ee39fd107b9a43c77cc02 |
|
MD5 | bc8f5ddd3445744813d5bd8633553ea1 |
|
BLAKE2b-256 | 324ef75e76d3620c8612b9a0fc776461099dd704398af15f4f67bb72442fe7b8 |