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.6.tar.gz (96.6 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.6-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file fauth-0.5.6.tar.gz.

File metadata

  • Download URL: fauth-0.5.6.tar.gz
  • Upload date:
  • Size: 96.6 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.6.tar.gz
Algorithm Hash digest
SHA256 e93553e50c067f8fdfb8121e29402ed1170282e99243cacb9515a4cc5428f8cd
MD5 04fdefc72b219112899583195f7e923e
BLAKE2b-256 e64eb716389fd28155e9c363014ff40c436dfe5a0e91055481eccf8f8f1a006c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fauth-0.5.6.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.6-py3-none-any.whl.

File metadata

  • Download URL: fauth-0.5.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0a6e87159fd279e10fffbe4198eeb12533a35f83ff234cd489052a03b10a5f9e
MD5 e94a1a0f7d3d4ce2bef645bb5e7599a8
BLAKE2b-256 77e20ac0acd9aa6e7c17c6d96fa0ce304f156b82a21e6f27e83cf698592f694b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fauth-0.5.6-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