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 cross-origin requests refused by default.
  • Tokens done right: secure generation, hash-only storage, checksums, JWTValidator for provider JWTs (JWKS, rotation, aud/iss/typ), 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, dependency_overrides included.

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.2.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.2.0
File Size Uploaded Provenance Actions
fastapi_multiauth-1.2.0.tar.gz 60.3 kB With provenance Details for fastapi_multiauth-1.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 135.0 kB

Release files / fastapi_multiauth-1.2.0.tar.gz

Download URL fastapi_multiauth-1.2.0.tar.gz
Size 60.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a98eb77b8e861d9f188dd1bea8e389b4a20f4106b09d82b8326bb6f483e447e5
BLAKE2b-256 checksum
How to use checksums
6101084c02a01efd64b4322146b03f8e03f17faa0898a0856d8a1cedc4256fc6
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 Nov 11, 2026.

Transparency log

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

Download URL fastapi_multiauth-1.2.0-py3-none-any.whl
Size 74.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c170231edaf1e5869c9c6510ac45e70aa0d05e964aa281f51816aa7ed0546254
BLAKE2b-256 checksum
How to use checksums
7cf9fc1b2e3db4dcfb0e1e4c5b2a3030f3f8eabe7afea3617bc482d63734e217
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 Nov 11, 2026.

Transparency log

Security for fastapi-multiauth 1.2.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.2.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 Nov 11, 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