Ergonomic, lightweight JWT authentication for FastAPI. Secure your routes instantly with plug-and-play dependency injection
Project description
FAuth
An ergonomic, plug-and-play authentication library for FastAPI.
fauth eliminates boilerplate around JWT, password hashing, user fetching, and Role-Based Access Control (RBAC) by leveraging FastAPI's Dependency Injection (Depends), Pydantic models, and Python Protocols.
Features
- Protocol-Based User Fetching — Complete inversion of control via
UserLoaderandIdentityLoaderprotocols. - Plug-and-Play Configuration — Centralized settings via Pydantic (
AuthConfig). Configure once, inject everywhere. - Pluggable Transports — Extensible
Transportprotocol with a built-inBearerTransport. - Automatic OpenAPI/Swagger UI Support — Integrated security schemes with "Authorize" button in Swagger UI.
- Built-in Password Hashing & JWT Crypto — Argon2 via
pwdliband utilities for creating/decoding access and refresh tokens. - RBAC —
require_rolesandrequire_permissionsdependencies with customizable user model field names. - Secure Router —
SecureAPIRoutersecures all its routes automatically. - Structured Logging — Built-in
structlog-based logging. - Type Safe — Fully annotated for MyPy and IDE integration.
Installation
pip install fauth
Or with uv:
uv add fauth
Quick Start
from pydantic import BaseModel
from fastapi import FastAPI, Depends
from fauth import AuthConfig, AuthProvider, TokenPayload, hash_password
class User(BaseModel):
id: str
username: str
hashed_password: str
is_active: bool = True
roles: list[str] = []
permissions: list[str] = []
DB: dict[str, User] = {
"user-123": User(id="user-123", username="alice", hashed_password=hash_password("s3cret"), roles=["admin"]),
}
async def load_user(payload: TokenPayload) -> User | None:
return DB.get(payload.sub)
async def load_identity(identifier: str) -> User | None:
return next((u for u in DB.values() if u.username == identifier), None)
config = AuthConfig(secret_key="my-super-secret-key")
auth: AuthProvider[User] = AuthProvider(config=config, user_loader=load_user, identity_loader=load_identity)
app = FastAPI()
@app.post("/login")
async def login(username: str, password: str):
user = await auth.authenticate(username, password)
return await auth.login(sub=user.id)
@app.get("/me")
async def get_me(user: User = Depends(auth.require_user)):
return {"message": f"Hello {user.username}"}
:book: Full documentation at fauth.readthedocs.io
License
MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fauth-0.5.5.tar.gz.
File metadata
- Download URL: fauth-0.5.5.tar.gz
- Upload date:
- Size: 96.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3dbfa0d5e15dd516e018b4e4fa0108bad6aca25fa0c26187f8eb15b34a16c5ca
|
|
| MD5 |
d7d5691b65f1a6776b69b46ac2ebc1f3
|
|
| BLAKE2b-256 |
5948291643305adb5e26f36b65505378294b8d5c843b86dc02f5d1b262085392
|
Provenance
The following attestation bundles were made for fauth-0.5.5.tar.gz:
Publisher:
cicd.yml on justmatias/fauth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fauth-0.5.5.tar.gz -
Subject digest:
3dbfa0d5e15dd516e018b4e4fa0108bad6aca25fa0c26187f8eb15b34a16c5ca - Sigstore transparency entry: 1429415408
- Sigstore integration time:
-
Permalink:
justmatias/fauth@fe436adb901eec6304915331dd8e28f96835af94 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/justmatias
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cicd.yml@fe436adb901eec6304915331dd8e28f96835af94 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fauth-0.5.5-py3-none-any.whl.
File metadata
- Download URL: fauth-0.5.5-py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2df1e6ef57de85d00413bfae1812bced45929ec8be9189aef913f41ada658cc4
|
|
| MD5 |
3454011a8181854dbc25747b19d036c5
|
|
| BLAKE2b-256 |
4aba4fdb0a8f654c7398a690f57e91b3c0ddf4c1cd5689406f4abe87001f5487
|
Provenance
The following attestation bundles were made for fauth-0.5.5-py3-none-any.whl:
Publisher:
cicd.yml on justmatias/fauth
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fauth-0.5.5-py3-none-any.whl -
Subject digest:
2df1e6ef57de85d00413bfae1812bced45929ec8be9189aef913f41ada658cc4 - Sigstore transparency entry: 1429415413
- Sigstore integration time:
-
Permalink:
justmatias/fauth@fe436adb901eec6304915331dd8e28f96835af94 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/justmatias
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cicd.yml@fe436adb901eec6304915331dd8e28f96835af94 -
Trigger Event:
push
-
Statement type: