Skip to main content

FastAPI OIDC JWKS

A FastAPI dependency used to verify a user's OAuth access token with JWKS.

Installation

pip install fastapi-oidc-jwks

Usage example

from fastapi import FastAPI, APIRouter, Depends
from fastapi_oidc_jwks import AuthDependency

app = FastAPI()

auth = AuthDependency("Your OIDC provider's JWKS URI here")

router = APIRouter(dependencies=[Depends(auth)])

@router.get("/user")
async def handle(user: dict = Depends(auth)):
    return {"user": user}

app.include_router(router)

Options

AuthDependency(jwks_uri, issuer=None, audience=None): the token's iss and aud claims are only checked when issuer and audience are set. With Keycloak, issuer is the realm's URL (e.g. https://keycloak.example.com/realms/myrealm).

Responses:

  • 401 if the token is missing, invalid, expired or has no exp claim
  • 503 if the JWKS can't be fetched from the identity provider

Verifying a token outside HTTP requests

verify_token returns the token's claims, or raises jwt.PyJWTError. For example, to authenticate Socket.IO connections:

import jwt
import socketio

@sio.event
async def connect(sid, environ, data):
    try:
        auth.verify_token((data or {}).get("token") or "")
    except jwt.PyJWTError:
        raise socketio.exceptions.ConnectionRefusedError("unauthorized")

TokenVerifier(jwks_uri, issuer=None, audience=None) provides verify_token without FastAPI's dependency.

Middleware

AuthMiddleware authenticates every request instead, and puts the token's claims in request.state.user:

from fastapi.middleware.cors import CORSMiddleware
from fastapi_oidc_jwks import AuthMiddleware

app.add_middleware(
    AuthMiddleware,
    jwks_url="Your OIDC provider's JWKS URI here",
    exclude_paths=["/docs", "/openapi.json"],
)
# Added after AuthMiddleware, so that it runs first: 401 responses then get CORS headers too
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_headers=["*"], allow_methods=["*"])

OPTIONS requests (CORS preflight) and the paths in exclude_paths are passed through without authentication.

Metadata

Release files for fastapi-oidc-jwks 0.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-oidc-jwks 0.2.0
File Size Uploaded
fastapi_oidc_jwks-0.2.0.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-oidc-jwks 0.2.0
File Interpreter ABI Platform
fastapi_oidc_jwks-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / fastapi_oidc_jwks-0.2.0.tar.gz

Download URL fastapi_oidc_jwks-0.2.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
86c3db316d6d375c03113877b2c1b287b4a74fafe8a98b1ad43a1c39ac5e7cf6
BLAKE2b-256 checksum
How to use checksums
679b7cad508981fd9ce35395abf57688307611fb3b90e1d13822986a00d36de2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / fastapi_oidc_jwks-0.2.0-py3-none-any.whl

Download URL fastapi_oidc_jwks-0.2.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f7e72c9e8c1c0f0538b35280e13a6832c85a4ac226646e03f8667a217bde130
BLAKE2b-256 checksum
How to use checksums
bd1d59926cbd396aaeb88af02bdd68d8288a51a523549dd7fdb2bda8537c6450
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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