Skip to main content

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.0

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.0
File Size Uploaded Provenance Actions
fastapi_multiauth-1.0.0.tar.gz 52.9 kB With provenance Details for fastapi_multiauth-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 119.8 kB

Release files / fastapi_multiauth-1.0.0.tar.gz

Download URL fastapi_multiauth-1.0.0.tar.gz
Size 52.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fd15f7b1413abbd810fb0200a7b33973e74ef5990c8158ba18bc1725afa33160
BLAKE2b-256 checksum
How to use checksums
86e2b8a83dffa43068e71a14f41e4e6ba3db24b90c699a7e9fde612b7e437045
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 29, 2026.

Transparency log

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

Download URL fastapi_multiauth-1.0.0-py3-none-any.whl
Size 67.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
234b108b223407d5c0b659d0c7afe937d5f7287eda49976efa4a94059ca97297
BLAKE2b-256 checksum
How to use checksums
16680a832e9099838edbd48a7a8f83eb2b5b2e07bda2650530243e12fda3237c
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 29, 2026.

Transparency log

Security for fastapi-multiauth 1.0.0

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.0.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 29, 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