jwt_auth_middleware
- class auth_middleware.jwt_auth_middleware.AuthFailure(status_code: int, reason: Literal['invalid_token', 'bad_scheme', 'internal', 'unavailable'])[source]
Bases:
objectWhat an
error_responderis told about a rejected request.reasonis meant for logs and metrics; the exception text is never passed, and by default nothing of it reaches the response body.
- class auth_middleware.jwt_auth_middleware.JwtAuthMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], auth_provider: JWTProvider, *args: Any, canonical_bearer_scheme: bool = False, path_prefix: str | None = None, error_responder: Callable[[Request, AuthFailure], Response] | None = None, strict: bool = False, **kwargs: Any)[source]
Bases:
BaseHTTPMiddlewareJWT Authorization middleware for FastAPI Adds the current user to the request state.
Every option below is keyword-only and defaults to the historical behaviour.
- Parameters:
app – The ASGI application.
auth_provider – The JWT provider validating tokens.
canonical_bearer_scheme – Treat the
Authorizationscheme case-insensitively (RFC 7235):bearer/BEARERare rewritten toBearerbefore validation. Other schemes keep their own rejection status.path_prefix – Only requests whose path equals the prefix or starts with
prefix + "/"are authenticated; others skip the middleware entirely andrequest.state.current_useris not set for them. Useful when other apps are mounted inside this one with their own middleware.error_responder – Builds the response for invalid token, bad scheme and internal errors from
(request, AuthFailure). The default keeps the{"detail": ...}JSON bodies.strict – Raise
RuntimeErrorat construction whenAUTH_MIDDLEWARE_DISABLEDis set or the provider’s settings havejwt_token_verification_disabled=True.
- __init__(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]], auth_provider: JWTProvider, *args: Any, canonical_bearer_scheme: bool = False, path_prefix: str | None = None, error_responder: Callable[[Request, AuthFailure], Response] | None = None, strict: bool = False, **kwargs: Any) None[source]