middleware#
- class litestar.middleware.AbstractAuthenticationMiddleware#
Bases:
ABCAbstract AuthenticationMiddleware that allows users to create their own AuthenticationMiddleware by extending it and overriding
AbstractAuthenticationMiddleware.authenticate_request().- __init__(app: ASGIApp, exclude: str | list[str] | None = None, exclude_from_auth_key: str = 'exclude_from_auth', exclude_http_methods: Sequence[Method] | None = None, scopes: Scopes | None = None) None#
Initialize
AbstractAuthenticationMiddleware.- Parameters:
app¶ – An ASGIApp, this value is the next ASGI handler to call in the middleware stack.
exclude¶ – A pattern or list of patterns to skip in the authentication middleware.
exclude_from_auth_key¶ – An identifier to use on routes to disable authentication for a particular route.
exclude_http_methods¶ – A sequence of http methods that do not require authentication.
scopes¶ – ASGI scopes processed by the authentication middleware.
- abstract async authenticate_request(connection: ASGIConnection) AuthenticationResult#
Receive the http connection and return an
AuthenticationResult.Notes
This method must be overridden by subclasses.
- Parameters:
connection¶ – An
ASGIConnectioninstance.- Raises:
NotAuthorizedException | PermissionDeniedException – if authentication fails.
- Returns:
An instance of
AuthenticationResult.
- class litestar.middleware.AbstractMiddleware#
Bases:
objectAbstract middleware providing base functionality common to all middlewares, for dynamically engaging/bypassing the middleware based on paths,
opt-keys and scope types.When implementing new middleware, this class should be used as a base.
- __init__(app: ASGIApp, exclude: str | list[str] | None = None, exclude_opt_key: str | None = None, scopes: Scopes | None = None) None#
Initialize the middleware.
- Parameters:
app¶ – The
nextASGI app to call.exclude¶ – A pattern or list of patterns to match against a request’s path. If a match is found, the middleware will be skipped.
exclude_opt_key¶ – An identifier that is set in the route handler
optkey which allows skipping the middleware.scopes¶ – ASGI scope types, should be a set including either or both ‘ScopeType.HTTP’ and ‘ScopeType.WEBSOCKET’.
- abstract async __call__(scope: Scope, receive: Receive, send: Send) None#
Execute the ASGI middleware.
Called by the previous middleware in the stack if a response is not awaited prior.
Upon completion, middleware should call the next ASGI handler and await it - or await a response created in its closure.
- class litestar.middleware.DefineMiddleware#
Bases:
objectContainer enabling passing
*argsand**kwargsto Middleware class constructors and factory functions.- __init__(middleware: Callable[..., ASGIApp], *args: Any, **kwargs: Any) None#
Initialize
DefineMiddleware.- Parameters:
Notes
The callable will be passed a kwarg
app, which is the next ASGI app to call in the middleware stack. It therefore must define such a kwarg.
- __call__(app: ASGIApp) ASGIApp#
Call the middleware constructor or factory.
- Parameters:
app¶ – An ASGIApp, this value is the next ASGI handler to call in the middleware stack.
- Returns:
Calls
DefineMiddleware.middlewareand returns the ASGIApp created.
- class litestar.middleware.MiddlewareProtocol#
Bases:
ProtocolAbstract middleware protocol.
- async __call__(scope: Scope, receive: Receive, send: Send) None#
Execute the ASGI middleware.
Called by the previous middleware in the stack if a response is not awaited prior.
Upon completion, middleware should call the next ASGI handler and await it - or await a response created in its closure.
- __init__(*args, **kwargs)#