Authentication (middleware, authenticator, backend)¶
litestar_auth.authentication
¶
Authentication package.
AuthenticationBackend(*, name, transport, strategy)
¶
Compose a transport and strategy into a reusable auth backend.
Store backend components used for auth flows.
Source code in litestar_auth/authentication/backend.py
authenticate(connection, user_manager)
async
¶
Resolve a user from the current request via transport and strategy.
Returns:
| Type | Description |
|---|---|
UP | None
|
Authenticated user or |
Source code in litestar_auth/authentication/backend.py
login(user)
async
¶
Issue a token through the configured strategy and transport.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
Response mutated by the configured transport for login. |
Source code in litestar_auth/authentication/backend.py
logout(user, token)
async
¶
Invalidate a token and clear transport-managed state.
When the transport is a :class:CookieTransport, the refresh-token
cookie is also expired so the browser does not retain it after logout.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
Response mutated by the configured transport for logout. |
Source code in litestar_auth/authentication/backend.py
terminate_session(connection, user)
async
¶
Terminate the current authenticated session for a connection.
This method orchestrates logout in one explicit place by reading the
current transport token and delegating token invalidation plus transport
cleanup to logout.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
Response mutated by the configured transport for logout. |
Raises:
| Type | Description |
|---|---|
NotAuthorizedException
|
If the current transport token is unavailable. |
Source code in litestar_auth/authentication/backend.py
with_session(session)
¶
Return a backend whose strategy is rebound to the provided session when supported.
Source code in litestar_auth/authentication/backend.py
Authenticator(backends, user_manager)
¶
Try configured authentication backends in order.
Store backends and the user manager used for token resolution.
Source code in litestar_auth/authentication/authenticator.py
authenticate(connection)
async
¶
Return the first authenticated user and backend name.
Returns:
| Type | Description |
|---|---|
UP | None
|
Tuple of authenticated user and backend name, or |
str | None
|
when no backend resolves the request. |
Source code in litestar_auth/authentication/authenticator.py
LitestarAuthMiddleware(app, *, get_request_session, authenticator_factory, auth_cookie_names=frozenset(), exclude=None, exclude_from_auth_key='exclude_from_auth', exclude_http_methods=None, scopes=None)
¶
Bases: AbstractAuthenticationMiddleware
Resolve request users through an authenticator built with the request-scoped DB session.
Initialize the middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
ASGI app to wrap. |
required |
get_request_session
|
RequestSessionProvider
|
Returns the shared request |
required |
authenticator_factory
|
AuthenticatorFactory[UP, ID]
|
Factory that binds the request-local session into an authenticator. |
required |
auth_cookie_names
|
frozenset[bytes]
|
Cookie names that should count as auth credentials when present. |
frozenset()
|
exclude
|
str | list[str] | None
|
Optional route patterns excluded from middleware processing. |
None
|
exclude_from_auth_key
|
str
|
Route opt key used to bypass auth. |
'exclude_from_auth'
|
exclude_http_methods
|
Sequence[Method] | None
|
Optional HTTP methods excluded from auth. |
None
|
scopes
|
Scopes | None
|
Optional ASGI scope types handled by the middleware. |
None
|
Source code in litestar_auth/authentication/middleware.py
authenticate_request(connection)
async
¶
Authenticate the request and return the resolved user or None.
Returns:
| Type | Description |
|---|---|
AuthenticationResult
|
Authentication result containing the resolved user and backend name. |