Skip to main content

FastAPI helpers for Better Auth JWT verification

Project description

fastapi-betterauth

FastAPI helpers for Better Auth JWT verification.

The package verifies Better Auth JWTs with PyJWT and JWKS, then exposes the verified claims either through a plain Python verifier or a FastAPI dependency.

Installation

pip install fastapi-betterauth

For FastAPI dependency helpers:

pip install "fastapi-betterauth[fastapi]"

Usage

from fastapi_betterauth import BetterAuthVerifier

verifier = BetterAuthVerifier("https://your-app.example.com")

claims = verifier.validate_token(token)

By default, the verifier:

  • reads keys from /api/auth/jwks
  • verifies the EdDSA algorithm
  • expects iss to match base_url
  • expects aud to match base_url

FastAPI

from typing import Annotated

from fastapi import Depends, FastAPI
from fastapi_betterauth import BetterAuthVerifier

app = FastAPI()
verifier = BetterAuthVerifier("https://your-app.example.com")
current_user = verifier.fastapi_dependency()


@app.get("/me")
def me(claims: Annotated[dict, Depends(current_user)]):
    return claims

Invalid tokens are converted to 401 Unauthorized responses.

Configuration

from fastapi_betterauth import BetterAuthVerifier

verifier = BetterAuthVerifier(
    "https://your-app.example.com",
    jwks_path="/api/auth/jwks",
    issuer="https://your-app.example.com",
    audience="https://your-app.example.com",
    algorithms=("EdDSA",),
    cache_jwk_set=True,
    lifespan=300,
)

Set issuer=None or audience=None to disable that specific validation.

Compatibility API

The older module-level style is still available:

from fastapi_betterauth import init_client, validate_token

init_client("https://your-app.example.com")
claims = validate_token(token)

Prefer BetterAuthVerifier for new code because it avoids global state and works better for tests, multiple apps, and multi-tenant services.

FastAPI demo server

This repository includes a small FastAPI demo in examples/fastapi_demo.py. Use it to test a real bearer token issued by your Better Auth application.

BETTER_AUTH_BASE_URL="http://localhost:3000" uv run python -m uvicorn examples.fastapi_demo:app --reload

The demo reads JWKS from ${BETTER_AUTH_BASE_URL}/api/auth/jwks by default. Override that path with BETTER_AUTH_JWKS_PATH if your Better Auth app uses a different route.

Public route:

curl http://localhost:8000/public

Protected route:

curl \
  -H "Authorization: Bearer $BETTER_AUTH_TOKEN" \
  http://localhost:8000/me

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_betterauth-0.2.1.tar.gz (35.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_betterauth-0.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_betterauth-0.2.1.tar.gz.

File metadata

  • Download URL: fastapi_betterauth-0.2.1.tar.gz
  • Upload date:
  • Size: 35.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_betterauth-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4a20e87c77bdd2bba9da5a3ab08020639c0a1983fcbcdfb882d83d0d25f98840
MD5 4185d876ebeae9adc37152bbe44dbabe
BLAKE2b-256 cbf41b27ca6382c2ad93afb9527691fa355569e6f35a04ad3f39bf28222c15e6

See more details on using hashes here.

File details

Details for the file fastapi_betterauth-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_betterauth-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_betterauth-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1cca5103e936fcd7dd0bbbefa10b4450dabe186c4225184f9dc3853a3122a554
MD5 e68b2747b0177a105cf9f03642fe64e6
BLAKE2b-256 d218c982c9a9d89f0a8681f7cd8f9a2ae5248b083029e6983190732a75e437fd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page