Middleware¶
sentinel_auth.middleware
¶
Starlette/FastAPI middleware for JWT access token validation.
Add this middleware to your FastAPI app to automatically validate
JWT tokens on incoming requests and populate request.state.user
with an AuthenticatedUser instance.
JWTAuthMiddleware(app, public_key=None, jwks_url=None, algorithm='RS256', audience='sentinel:access', exclude_paths=None, allowed_workspaces=None)
¶
Bases: BaseHTTPMiddleware
Validates JWT access tokens and sets request.state.user.
For each incoming request (except excluded paths), this middleware:
- Extracts the
Authorization: Bearer <token>header - Decodes and validates the JWT using the provided public key
- Sets
request.state.userto anAuthenticatedUserinstance - Returns 401 if the token is missing, expired, or invalid
Provide either public_key (PEM string) or jwks_url to
specify how the signing key is obtained. When jwks_url is given
the key is fetched lazily on the first request and cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
The ASGI application to wrap. |
required |
public_key
|
str | None
|
RSA public key (PEM format) used to verify JWT signatures.
Obtain this from the identity service's |
None
|
jwks_url
|
str | None
|
URL of the Sentinel JWKS endpoint (e.g.
|
None
|
algorithm
|
str
|
JWT signing algorithm. Defaults to |
'RS256'
|
audience
|
str
|
Expected JWT audience claim. Defaults to |
'sentinel:access'
|
exclude_paths
|
list[str] | None
|
List of path prefixes to skip authentication for.
Defaults to |
None
|
allowed_workspaces
|
set[str] | None
|
Optional set of workspace IDs (as strings) that
are permitted to access this service. |
None
|
Example using JWKS (recommended)::
app.add_middleware(
JWTAuthMiddleware,
jwks_url="http://localhost:9003/.well-known/jwks.json",
)
Example using PEM file::
from pathlib import Path
app.add_middleware(
JWTAuthMiddleware,
public_key=Path("keys/public.pem").read_text(),
)