Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

FastAPI MultiAuth

Composable authentication sources for FastAPI: bring your own validator, combine multiple schemes with MultiAuth, and get correct OpenAPI documentation by construction.

CI codecov CodSpeed Badge ty uv Ruff Python 3.11+ License: MIT


Documentation: https://fastapi-multiauth.d3vyce.fr

Source Code: https://github.com/d3vyce/fastapi-multiauth


You write the function that decides who a credential belongs to; the library extracts the credential, combines schemes, and declares them in OpenAPI. No user model, no database coupling, no ready-made login routes.

Installation

uv add fastapi-multiauth
uv add "fastapi-multiauth[jwt]"    # JWTValidator, HTTPDPoPAuth
uv add "fastapi-multiauth[oauth]"  # OAuth 2.0 / OIDC login helpers

Quick start

import os

from fastapi import FastAPI, Security

from fastapi_multiauth import (
    APIKeyCookieAuth,
    HTTPBearerAuth,
    MultiAuth,
    UnauthorizedError,
    hash_token,
)

SECRET_KEY = os.environ["SECRET_KEY"]
USERS = {"1": {"id": "1", "name": "alice"}}
TOKENS = {hash_token("user_alice-token"): "1"}


async def validate_token(token: str) -> dict:
    user_id = TOKENS.get(hash_token(token))
    if user_id is None:
        raise UnauthorizedError()
    return USERS[user_id]


async def validate_session(user_id: str) -> dict:
    if user_id not in USERS:
        raise UnauthorizedError()
    return USERS[user_id]


bearer = HTTPBearerAuth(validator=validate_token, prefix="user_")
session = APIKeyCookieAuth(
    name="__Host-session", validator=validate_session, secret_key=SECRET_KEY
)
auth = MultiAuth(bearer, session)

app = FastAPI()


@app.get("/me")
async def me(user: dict = Security(auth)):
    return user

Set SECRET_KEY to at least 32 random bytes, for example the output of python -c "import secrets; print(secrets.token_urlsafe(32))".

Features

  • Every standard scheme: bearer tokens with Stripe-style prefixes, signed cookie sessions, API keys, HTTP Basic, OAuth 2.0 and OpenID Connect.
  • MultiAuth: several schemes on one route, all documented in OpenAPI, plus optional() for routes that also serve anonymous callers.
  • Stateless sessions: HMAC-signed cookies with expiry, key rotation, opt-in revocable session ids, and csrf=True to refuse cross-origin requests.
  • Tokens done right: secure generation, hash-only storage, checksums, JWTValidator for provider JWTs (JWKS, rotation, aud/iss), IntrospectionValidator for opaque ones, and HTTPDPoPAuth for tokens bound to the client's key.
  • Signed webhooks: HMACSignatureAuth checks the sender's signature over the raw body, with timestamp tolerance and secret rotation.
  • Scopes and step-up: fail-closed scope checks, and fresh() / stepped_up() with RFC 9470 challenges for sensitive routes.
  • Second factor: TOTP helpers that refuse a replayed code, and the login level signed into the session cookie.
  • OAuth login helpers: discovery, CSRF state, PKCE, code exchange and ID token validation.
  • Observable: reason-coded security logs and auth_surface(app) to audit what guards every route.

See the comparison for how it sits next to fastapi.security, fastapi-users, AuthX and Authlib, and the security model for what it guarantees.

Contributing

Contributions are welcome: see CONTRIBUTING.md and the Code of Conduct. Report vulnerabilities privately, as the security policy describes.

License

This project is licensed under the MIT License.

Metadata

Release files for fastapi-multiauth 1.0.0rc2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-multiauth 1.0.0rc2
File Size Uploaded Provenance Actions
fastapi_multiauth-1.0.0rc2.tar.gz 53.2 kB With provenance Details for fastapi_multiauth-1.0.0rc2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-multiauth 1.0.0rc2
File Interpreter ABI Platform Actions
fastapi_multiauth-1.0.0rc2-py3-none-any.whl Python 3 none any Details for fastapi_multiauth-1.0.0rc2-py3-none-any.whl

Total release size: 120.5 kB

Release files / fastapi_multiauth-1.0.0rc2.tar.gz

Download URL fastapi_multiauth-1.0.0rc2.tar.gz
Size 53.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1e2ded710b251c404da150a60ee0c5e37b43e0e2c8f063b1c4ff069fec1b8118
BLAKE2b-256 checksum
How to use checksums
98f6fd5ba1599ba3bb5568bc99cccbfbfc5dec5275c2fca773842bab98528f5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / fastapi_multiauth-1.0.0rc2-py3-none-any.whl

Download URL fastapi_multiauth-1.0.0rc2-py3-none-any.whl
Size 67.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5fb5519f1304a4f64dadea63ef827591d97a39972a89ad0af728ca4790d04d1
BLAKE2b-256 checksum
How to use checksums
d9b8b4823dbcad18a82407c99db2bcabb92dbefb98572380d561de3a66dd734b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for fastapi-multiauth 1.0.0rc2

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastapi_multiauth-1.0.0rc2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page