Skip to content

About

Passkeys / WebAuthn authentication for FastAPI — secure by default, storage-agnostic, async-native.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

fastapi-passkeys

fastapi-passkeys

Passkeys / WebAuthn authentication for FastAPI — phishing-resistant passwordless login with cloned-authenticator detection, single-use challenges, and strict origin checks, without locking into a specific auth library or ORM.

PyPI version Python versions License: MIT CI Code style: ruff Typed: mypy strict
GitHub stars Open issues Last commit

If this project helps you, please ⭐ star the repo — it really helps others find it.  ·  Tweet


Table of contents

Status

0.2.0 — Alpha. Full registration + authentication ceremonies, clone detection, SQLAlchemy/Redis adapters, and WebAuthn Level 3 features (hints, Signal API, related origins). 0.2.0 is a security-hardening release — see the audit report. The public API may change before 1.0. See CHANGELOG.md and the roadmap.

Why

Passwords are a liability and WebAuthn is easy to get subtly wrong — dropped signature counters, replayable challenges, missing origin checks. fastapi-passkeys owns the hard, security-sensitive part and stays out of the way of your idea of a session:

  • Secure by default — single-use, TTL-bound challenges; strict origin/RP validation checked at startup; registration bound to the signed-in user; userHandle ownership and backup-flag checks; and monotonic sign_count cloned-authenticator detection (the prior Django solution stores no counter).
  • Auth-agnostic — it verifies the passkey and hands you the user; you mint whatever session or JWT you like via an on_authenticated hook.
  • Storage-abstracted — credentials live behind an async CredentialRepository protocol (in-memory, stateless, SQLAlchemy 2.0, and Redis adapters included), with a shipped contract test-suite for your own.
  • Async-native and fully typed (mypy --strict, py.typed).

Install

pip install fastapi-passkeys
# optional extras:
pip install "fastapi-passkeys[sqlalchemy]"   # SQLAlchemy 2.0 async credential repo
pip install "fastapi-passkeys[redis]"        # Redis challenge store (atomic single-use)

Quickstart

from fastapi import FastAPI, Request

from fastapi_passkeys import (
    AuthenticationResult,
    Passkeys,
    PasskeyConfig,
    PasskeyUser,
)
from fastapi_passkeys.contrib import InMemoryCredentialRepository


async def get_user(request: Request) -> PasskeyUser:
    # Return the *authenticated* user — from your session or a verified sign-up
    # token. Never trust an email/user id sent by the client: whoever this
    # returns is who the new passkey is attached to.
    account = await current_account(request)  # your auth; raise HTTPException(401) if none
    return PasskeyUser(
        id=account.passkey_id,          # opaque, stable, 1-64 bytes (e.g. a UUID) — not an email
        name=account.email,             # shown in the passkey picker
        display_name=account.full_name,
    )


async def on_authenticated(request: Request, result: AuthenticationResult) -> dict:
    # The passkey is verified — now mint *your* session or token.
    return {"access_token": issue_token(result.user_id)}


passkeys = Passkeys(
    config=PasskeyConfig(
        rp_id="example.com",
        rp_name="Example",
        expected_origins=["https://example.com"],
    ),
    credential_repository=InMemoryCredentialRepository(),
    get_user=get_user,
    on_authenticated=on_authenticated,
)

app = FastAPI()
app.include_router(passkeys.router, prefix="/auth/passkeys", tags=["passkeys"])
passkeys.install_exception_handlers(app)

Each ceremony is a begin → finish pair: begin returns the options your frontend passes to navigator.credentials.create() / .get() plus an opaque state handle; echo that state back to finish with the authenticator's response. No server session is required between the calls. A full, runnable browser demo lives in examples/app.py.

Need full control? Skip the router and drive passkeys.registration / passkeys.authentication (the services) from your own endpoints.

Endpoints

Method Path Purpose
POST /register/begin Start registration; returns creation options + state
POST /register/finish Verify attestation and store the credential
POST /authenticate/begin Start authentication (usernameless or with userId)
POST /authenticate/finish Verify assertion; runs your on_authenticated hook
GET /credentials List the current user's passkeys
GET /credentials/signals WebAuthn Signal API payloads for the current user
PATCH /credentials/{id} Rename a passkey (404 if not yours)
DELETE /credentials/{id} Revoke a passkey (404 if not yours)

passkeys.well_known_router (mounted without a prefix) serves /.well-known/webauthn when related_origins is configured.

get_user decides whose account a passkey is attached to. Resolve it from your session — never from an id the client sends.

Run the demo with Docker

docker run --rm -p 8000:8000 ghcr.io/javlondevv/fastapi-passkeys

Then open http://localhost:8000 and register / sign in with a real authenticator. This runs examples/app.py with in-memory storage, so data resets on restart. Keep host port 8000: the demo's origin is http://localhost:8000.

Storage backends

Backend Import Extra
In-memory credentials fastapi_passkeys.contrib.InMemoryCredentialRepository —
In-memory challenges fastapi_passkeys.contrib.InMemoryChallengeStore —
Stateless challenges fastapi_passkeys.contrib.StatelessChallengeStore —
SQLAlchemy credentials fastapi_passkeys.contrib.sqlalchemy.SqlAlchemyCredentialRepository [sqlalchemy]
Redis challenges fastapi_passkeys.contrib.redis.RedisChallengeStore [redis]

Implement the CredentialRepository / ChallengeStore protocols for any other store (SQLModel, Tortoise, Beanie, …) and validate it with the shipped contract suite in fastapi_passkeys.testing.

Security

  • Challenges are CSPRNG-generated, bound to user + ceremony, TTL-enforced server-side, and single-use (in-memory and Redis stores; per-process for the stateless store).
  • Registration is bound to the user who started it; assertions must come from the credential's owner (userHandle), with BE/BS flag validation.
  • Clone detection stores and enforces the signature counter, with strict-reject, flag-disable, or audit-only policies — all audited.
  • Origins & RP ID are normalized and validated at startup; multiple origins, Android app origins, and related origins are supported.
  • No secrets are logged — audit events carry identifiers and outcomes only.

See the security model for the full picture, including the stateless-store tradeoff. Report vulnerabilities privately via SECURITY.md.

Documentation

Full docs: https://javlondevv.github.io/fastapi-passkeys/

Roadmap

  • 0.1 — core ceremonies, clone detection, router + services, in-memory / stateless / SQLAlchemy / Redis adapters, contract suite, docs site.
  • 0.2 — security hardening from a full audit, WebAuthn L3 hints / Signal API / related origins, credential limits, conditional-UI example, rate-limiting and deployment guidance.
  • 0.3 — soft-disable credentials (with migrations), per-user random user handles, optional FIDO MDS3 attestation + authenticator names, SQLModel / Tortoise / Beanie adapters.
  • 1.0 — API freeze + semver guarantee.

Contributing

Contributions are very welcome! See CONTRIBUTING.md for the dev setup and checks. Good entry points are the issues labelled good first issue and help wanted. Please also read our Code of Conduct.

Support the project

The simplest way to help is a ⭐ star — it boosts visibility for everyone.

Embed a live star button on your own site or docs with GitHub Buttons:

<!-- Place once, before </body> -->
<a class="github-button"
   href="https://github.com/javlondevv/fastapi-passkeys"
   data-icon="octicon-star"
   data-size="large"
   data-show-count="true"
   aria-label="Star javlondevv/fastapi-passkeys on GitHub">Star</a>
<script async defer src="https://buttons.github.io/buttons.js"></script>

License

MIT — see LICENSE.

About

Passkeys / WebAuthn authentication for FastAPI — secure by default, storage-agnostic, async-native.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages