Skip to main content

FastAPI Auth Middleware for Clerk (https://clerk.com)

Project description

FastAPI Clerk Auth Middleware

FastAPI Auth Middleware for Clerk

Easily setup authentication on your API routes using your Clerk JWKS endpoint.

Install

pip install fastapi-clerk-auth

Basic Usage

from fastapi import FastAPI, Depends
from fastapi_clerk_auth import ClerkConfig, ClerkHTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder

app = FastAPI()

clerk_config = ClerkConfig(jwks_url="https://example.com/.well-known/jwks.json") # Use your Clerk JWKS endpoint

clerk_auth_guard = ClerkHTTPBearer(config=clerk_config)

@app.get("/")
async def read_root(credentials: HTTPAuthorizationCredentials | None = Depends(clerk_auth_guard)):
    return JSONResponse(content=jsonable_encoder(credentials))

The returned credentials model will either be of type None or HTTPAuthorizationCredentials. If the model is populated it will have the following properties:

  • scheme
    • Indicates the scheme of the Authorization header (Bearer)
  • credentials
    • Raw token received from the Authorization header
  • decoded
    • The payload of the decoded token

Auto Errors

By default the middleware automatically returns 403 errors if the token is missing or invalid. You can disable that behavior by setting the following:

clerk_config = ClerkConfig(jwks_url="https://example.com/.well-known/jwks.json", auto_error=False)

This will allow requests to reach the endpoint for additional logic or error handling.

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_clerk_auth-0.0.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

fastapi_clerk_auth-0.0.5-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_clerk_auth-0.0.5.tar.gz.

File metadata

  • Download URL: fastapi_clerk_auth-0.0.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for fastapi_clerk_auth-0.0.5.tar.gz
Algorithm Hash digest
SHA256 14acf42231ce4b0eb273c6338d02322ba63357de844bf39e0e78327b3b78d1cf
MD5 8a7d9e74a687248ec969733841153d5a
BLAKE2b-256 381931f45cade8d729828ecf541dfe585e5282c4b18d8f13d0518d82297e0bea

See more details on using hashes here.

File details

Details for the file fastapi_clerk_auth-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_clerk_auth-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 54a37c542ebd0205fa773f5e0dd13f4ad782680a26585ac7008585e308fcb4f7
MD5 0db38a68032cd51377d3509825a6eab6
BLAKE2b-256 166ed85e5b2fe04cb267a812ee98d3330531200b4391f29f2e398924e8dca4b4

See more details on using hashes here.

Supported by

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