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.

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.1.0.tar.gz (29.1 kB 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.1.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_betterauth-0.1.0.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • 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.1.0.tar.gz
Algorithm Hash digest
SHA256 3ef45b6c871a901ff149390987f52c49a6a37a79fc8e9c349e5aad8b2cf981f4
MD5 f7c89eac07533f3df943fe65045b295d
BLAKE2b-256 e1105d073c7cd4dddeda7baa8ad009bb9341e6b61508db8d12e549dd9b6133d2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_betterauth-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f64110ba3e19db471ee9d6a9e19c7880212b18caeec37d400f888409bd2ea5a6
MD5 1a2ed93271b6f693b96b6c98bd761e4a
BLAKE2b-256 ecb1c91cb6ca1e89fc9511032c65d2edc0c34b305b89c67be807f832fbd8bd73

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