Transports¶
The Transport protocol defines how credentials move between client and server: reading token material from an incoming request and attaching issued tokens to responses. Implementations decide where credentials live on the wire; a paired strategy decides how tokens are validated or minted.
BearerTransport uses the Authorization: Bearer <token> header for API-style clients. CookieTransport stores tokens in HTTP-only cookies for browser sessions, which affects CSRF posture when cookies are used without a separate header secret. ApiKeyTransport accepts formatted API keys through either Authorization: Bearer ak_... or X-API-Key; it also parses Authorization: LSA1-HMAC-SHA256 ... for signed API-key requests.
Transports are always composed with a Strategy inside an AuthenticationBackend: the backend wires one transport to one strategy so login, logout, and per-request authentication share the same token semantics. For how backends fit together and when to pick Bearer vs cookies, see Backends: transports and strategies. For cookie-based setups, review Cookie transport and CSRF.
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.
ApiKeyTransport(*, prefix=API_KEY_PREFIX)
¶
Bases: Transport
Transport that reads canonical API keys from bearer auth or X-API-Key.
Store the accepted API-key prefix.
Source code in litestar_auth/authentication/transport/api_key.py
read_token(connection)
async
¶
Return an API-key token from Authorization or X-API-Key headers.
Source code in litestar_auth/authentication/transport/api_key.py
set_login_token(response, token)
¶
Reject login issuance because API keys are not login-flow tokens.
Raises:
| Type | Description |
|---|---|
TokenError
|
Always, because API keys are not issued by login. |
Source code in litestar_auth/authentication/transport/api_key.py
set_logout(response)
¶
Leave logout responses unchanged because API keys keep no client state.
Returns:
| Type | Description |
|---|---|
Response[Any]
|
The original response. |
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(config=None, **options)
¶
Bases: Transport
Transport that stores authentication tokens in HTTP cookies.
Initialize the cookie transport configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
CookieTransportConfig | None
|
Cookie transport configuration. Omit for secure defaults. |
None
|
**options
|
Unpack[CookieTransportConfigOptions]
|
Individual cookie transport settings. Do not combine with
|
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
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_refresh_token(connection)
async
¶
Return the refresh token from the dedicated refresh-token cookie.
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. |
Source code in litestar_auth/authentication/transport/cookie.py
CookieTransportConfig(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)
dataclass
¶
Configuration for :class:CookieTransport.