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.

Releasing

Publishing a GitHub release uploads the package to PyPI, via the publish.yml workflow and PyPI's trusted publishing:

  1. Bump version in pyproject.toml and commit
  2. Tag that commit with the same version, prefixed with v, and push: git tag v0.2.1 && git push github master --tags
  3. Create a release from the tag: gh release create v0.2.1 --generate-notes

Alternatively, from the GitHub UI, steps 2 and 3 are one: after pushing the version bump to master, go to Releases → Draft a new release, type the new tag (e.g. v0.2.1) under "Choose a tag" and select "Create new tag on publish", with master as the target, then publish the release.

The workflow fails without publishing if the tag doesn't match the version in pyproject.toml.

Metadata

Release files for fastapi-oidc-jwks 0.2.1

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.1
File Size Uploaded
fastapi_oidc_jwks-0.2.1.tar.gz 4.9 kB Details

Built distribution (wheel)

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

Total release size: 11.0 kB

Release files / fastapi_oidc_jwks-0.2.1.tar.gz

Download URL fastapi_oidc_jwks-0.2.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
06c860f7a3e1ddf571deaa2528f805f7f096684d3e433150d31b100e09d25329
BLAKE2b-256 checksum
How to use checksums
9e2a16183fffc195d34044193a6fe698ab17e7cc8d43c8f00a464777590fadd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 18, 2026.

Transparency log

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

Download URL fastapi_oidc_jwks-0.2.1-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
11f9d5e2034422163e89d8ed93889b13905842f3a97745cfa8859587684329c8
BLAKE2b-256 checksum
How to use checksums
4f6ddd1aecff1cbf6322e706776210d4648f33bf74330714e8afbc6a574c8e72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 18, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

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