Skip to main content

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.

Dependabot Updates PyPI version Python versions License: MIT

Features

  • Protocol-Based User Fetching — Complete inversion of control via UserLoader and IdentityLoader protocols.
  • Plug-and-Play Configuration — Centralized settings via Pydantic (AuthConfig). Configure once, inject everywhere.
  • Pluggable Transports — Extensible Transport protocol with a built-in BearerTransport.
  • Automatic OpenAPI/Swagger UI Support — Integrated security schemes with "Authorize" button in Swagger UI.
  • Built-in Password Hashing & JWT Crypto — Argon2 via pwdlib and utilities for creating/decoding access and refresh tokens.
  • RBACrequire_roles and require_permissions dependencies with customizable user model field names.
  • Secure RouterSecureAPIRouter secures 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


Download files

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

Source Distribution

fauth-0.5.5.tar.gz (96.3 kB view details)

Uploaded Source

Built Distribution

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

fauth-0.5.5-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

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

Hashes for fauth-0.5.5.tar.gz
Algorithm Hash digest
SHA256 3dbfa0d5e15dd516e018b4e4fa0108bad6aca25fa0c26187f8eb15b34a16c5ca
MD5 d7d5691b65f1a6776b69b46ac2ebc1f3
BLAKE2b-256 5948291643305adb5e26f36b65505378294b8d5c843b86dc02f5d1b262085392

See more details on using hashes here.

Provenance

The following attestation bundles were made for fauth-0.5.5.tar.gz:

Publisher: cicd.yml on justmatias/fauth

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

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

Hashes for fauth-0.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2df1e6ef57de85d00413bfae1812bced45929ec8be9189aef913f41ada658cc4
MD5 3454011a8181854dbc25747b19d036c5
BLAKE2b-256 4aba4fdb0a8f654c7398a690f57e91b3c0ddf4c1cd5689406f4abe87001f5487

See more details on using hashes here.

Provenance

The following attestation bundles were made for fauth-0.5.5-py3-none-any.whl:

Publisher: cicd.yml on justmatias/fauth

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