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), 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.1.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.1.0
File Size Uploaded Provenance Actions
fastapi_multiauth-1.1.0.tar.gz 56.7 kB With provenance Details for fastapi_multiauth-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 127.4 kB

Release files / fastapi_multiauth-1.1.0.tar.gz

Download URL fastapi_multiauth-1.1.0.tar.gz
Size 56.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c81542ea6605acedc213885191a9ff34ee825f908edb4972140cce6e092af089
BLAKE2b-256 checksum
How to use checksums
4a0b01f4b0cde52a3bf6212c71391b6ef0afb3c3e68fe8c0298f72a7bc2db041
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 3, 2026.

Transparency log

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

Download URL fastapi_multiauth-1.1.0-py3-none-any.whl
Size 70.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a0107e2b97ae2c1496acf53f475a2fcc2155ccaab260350310b2c1f4c53fb81c
BLAKE2b-256 checksum
How to use checksums
eb8d5f1f8f1d3b1295ba284f2bb8106f90b5c91290989f60b8d857e1d77f167a
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 3, 2026.

Transparency log

Security for fastapi-multiauth 1.1.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.1.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 3, 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