FastAPI Third Party Auth
Documentation: https://fastapi-third-party-auth.readthedocs.io/
Source Code: https://github.com/aiwizo/fastapi-third-party-auth
Simple library for using a third party authentication service with FastAPI. Verifies and decrypts 3rd party OpenID Connect tokens to protect your endpoints.
Easily used with authentication services such as:
- Keycloak (open source)
- SuperTokens (open source)
- Auth0
- Okta
FastAPI's generated interactive documentation supports the grant flows:
GrantType.AUTHORIZATION_CODE
GrantType.IMPLICIT
GrantType.PASSWORD
GrantType.CLIENT_CREDENTIALS
Installation
poetry add fastapi-third-party-auth
Or, for the old-timers:
pip install fastapi-third-party-auth
Usage
See this example for how to use
docker-compose to set up authentication with fastapi-third-party-auth +
Keycloak.
Standard usage
from fastapi import Depends
from fastapi import FastAPI
from fastapi import Security
from fastapi import status
from fastapi_third_party_auth import Auth
from fastapi_third_party_auth import GrantType
from fastapi_third_party_auth import KeycloakIDToken
auth = Auth(
openid_connect_url="http://localhost:8080/auth/realms/my-realm/.well-known/openid-configuration",
issuer="http://localhost:8080/auth/realms/my-realm", # optional, verification only
client_id="my-client", # optional, verification only
scopes=["email"], # optional, verification only
grant_types=[GrantType.IMPLICIT], # optional, docs only
idtoken_model=KeycloakIDToken, # optional, verification only
)
app = FastAPI(
title="Example",
version="dev",
dependencies=[Depends(auth)],
)
@app.get("/protected")
def protected(id_token: KeycloakIDToken = Security(auth.required)):
return dict(message=f"You are {id_token.email}")
Optional: Custom token validation
The IDToken class will accept any number of extra fields but you can also validate fields in the token like this:
class MyAuthenticatedUser(IDToken):
custom_field: str
custom_default: float = 3.14
auth = Auth(
...,
idtoken_model=MyAuthenticatedUser,
)
Metadata
Release files for fastapi-third-party-auth 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi-third-party-auth-0.1.2.tar.gz | 7.6 kB | Without provenance | Details for fastapi-third-party-auth-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_third_party_auth-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for fastapi_third_party_auth-0.1.2-py3-none-any.whl |
Total release size: 15.9 kB
Release files / fastapi-third-party-auth-0.1.2.tar.gz
| Download URL | fastapi-third-party-auth-0.1.2.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
01afc17ca5c6b0a80782805aa5cfdedbae3b26db52d47e1a5d87a50e9e1ae44a
|
|
BLAKE2b-256 checksum How to use checksums |
8ccbed450b4e58bd9c406e79f6b5444c990e73b2520586079cfb3f5b39304cb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.1 CPython/3.9.14 Linux/5.15.0-1020-azure
|
Release files / fastapi_third_party_auth-0.1.2-py3-none-any.whl
| Download URL | fastapi_third_party_auth-0.1.2-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7f8c1828615b1672c60a92dd74d1d7af4bdbe6fa8e5601da135ca9e75e987042
|
|
BLAKE2b-256 checksum How to use checksums |
62112e374b0c6c14047281ec37dd1f9cedc98158a5e700b974e097340412d0c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.1 CPython/3.9.14 Linux/5.15.0-1020-azure
|
Security for fastapi-third-party-auth 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.