Transports¶
litestar_auth.authentication.transport
¶
Move credentials between client and server (Bearer header vs HTTP-only cookies).
Transports are composed with a :class:~litestar_auth.authentication.strategy.Strategy
inside an :class:~litestar_auth.authentication.backend.AuthenticationBackend.
BearerTransport
¶
Bases: Transport
Transport that reads tokens from the Authorization header.
read_token(connection)
async
¶
Return the bearer token from the Authorization header when present.
Source code in litestar_auth/authentication/transport/bearer.py
set_login_token(response, token)
¶
Store the issued bearer token in the response body.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The mutated response. |
Source code in litestar_auth/authentication/transport/bearer.py
set_logout(response)
¶
Clear the response body because bearer transport keeps no client state.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The mutated response. |
Source code in litestar_auth/authentication/transport/bearer.py
CookieTransport(*, cookie_name='litestar_auth', max_age=None, path='/', domain=None, secure=True, httponly=True, samesite='lax', allow_insecure_cookie_auth=False, refresh_max_age=None)
¶
Bases: Transport
Transport that stores authentication tokens in HTTP cookies.
Initialize the cookie transport configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cookie_name
|
str
|
Name of the auth cookie. |
'litestar_auth'
|
max_age
|
int | None
|
Optional cookie max-age in seconds. |
None
|
path
|
str
|
Cookie path. |
'/'
|
domain
|
str | None
|
Optional cookie domain. |
None
|
secure
|
bool
|
Whether to set the Secure attribute. |
True
|
httponly
|
bool
|
Whether to set the HttpOnly attribute on the auth cookie. |
True
|
samesite
|
SameSitePolicy
|
SameSite policy for cookies. |
'lax'
|
allow_insecure_cookie_auth
|
bool
|
When |
False
|
refresh_max_age
|
int | None
|
Optional cookie max-age in seconds for the refresh-token
cookie. When |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/authentication/transport/cookie.py
refresh_cookie_name
property
¶
Return the cookie key used to carry refresh tokens in cookie flows.
clear_refresh_token(response)
¶
Expire the refresh-token cookie immediately.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The mutated response. |
Source code in litestar_auth/authentication/transport/cookie.py
read_logout_token(connection)
async
¶
Return the access-token cookie value to invalidate during logout.
Logout token sourcing is explicit here: cookie logout invalidates the access-token cookie and does not read refresh-token cookies.
Source code in litestar_auth/authentication/transport/cookie.py
read_token(connection)
async
¶
Return the authentication token from the configured cookie.
set_login_token(response, token)
¶
Persist the issued token in the configured cookie.
Security
When this transport is used for browser-based authentication, you MUST
pair it with an explicit CSRF protection mechanism (for example, a
separate CSRF cookie and a required X-CSRF-Token header on state-changing
requests). This is especially important when samesite=\"none\" is used
for cross-site scenarios, because browsers will attach cookies
automatically to cross-origin requests.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The mutated response. |
Source code in litestar_auth/authentication/transport/cookie.py
set_logout(response)
¶
Remove the access-token cookie by expiring it immediately.
Note
This transport-level method clears only the access-token cookie.
The refresh-token cookie is cleared by
:meth:AuthenticationBackend.logout, which calls
:meth:clear_refresh_token after this method.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The mutated response. |
Source code in litestar_auth/authentication/transport/cookie.py
set_refresh_token(response, refresh_token)
¶
Persist a refresh token in a dedicated HttpOnly cookie.
Note
This library intentionally treats refresh tokens as a separate artifact from the access-token cookie used for request authentication.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The mutated response. |