Controllers¶
HTTP controller factories for advanced wiring. Most applications rely on LitestarAuth plus OAuthConfig for the default plugin-owned OAuth route table instead of mounting these directly.
Use this module when you need direct controller ownership: custom OAuth path prefixes, direct user-manager wiring, or another custom route table.
Import built-in request and response payload types from litestar_auth.payloads.
For direct auth-controller assembly, AuthControllerConfig(...) carries the same
settings accepted by create_auth_controller(...) as keyword arguments.
For direct OAuth login-controller assembly, OAuthControllerConfig(...) carries the same
settings accepted by create_oauth_controller(...) as keyword arguments.
For direct OAuth account-linking assembly, OAuthAssociateControllerConfig(...) carries
the same settings accepted by create_oauth_associate_controller(...) as keyword arguments.
For direct registration-controller assembly, RegisterControllerConfig(...) carries the same
settings accepted by create_register_controller(...) as keyword arguments.
For direct users-controller assembly, UsersControllerConfig(...) carries the same settings
accepted by create_users_controller(...) as keyword arguments.
For direct API-key-controller assembly, ApiKeysControllerConfig(...) carries the same settings
accepted by create_api_keys_controllers(...) as keyword arguments. Most apps should still prefer
LitestarAuthConfig.api_keys, which wires the backend, store, rate-limit slot, OpenAPI security,
and generated controllers together.
Manual cookie-auth route tables must declare their CSRF posture explicitly:
create_auth_controller(..., csrf_protection_managed_externally=True) means
your application has already installed CSRF protection for those routes. The
plugin-owned route table handles this through LitestarAuthConfig.csrf_secret.
litestar_auth.controllers
¶
Public controller factory exports.
Use litestar_auth.payloads for built-in request and response payload types.
ApiKeysControllerConfig(id_parser=None, rate_limit_config=None, path='/api-keys', users_path='/users', security=None, require_step_up_on_create=True, signing_enabled=False, totp_stepup_policy=dict())
dataclass
¶
Configuration for :func:create_api_keys_controllers.
AuthControllerConfig(backend, rate_limit_config=None, enable_refresh=False, requires_verification=True, login_identifier='email', totp_pending_secret=None, totp_pending_lifetime=_DEFAULT_PENDING_TOKEN_LIFETIME, totp_pending_require_client_binding=True, path='/auth', unsafe_testing=False, csrf_protection_managed_externally=False, security=None)
dataclass
¶
Configuration for :func:create_auth_controller.
OAuthAssociateControllerConfig(provider_name, oauth_client, redirect_base_url, oauth_flow_cookie_secret, user_manager=None, user_manager_dependency_key=None, path='/auth/associate', cookie_secure=True, security=None, totp_stepup_policy=dict())
dataclass
¶
Configuration for :func:create_oauth_associate_controller.
OAuthControllerConfig(provider_name, backend, user_manager, oauth_client, redirect_base_url, oauth_flow_cookie_secret, path='/auth/oauth', cookie_secure=True, oauth_scopes=None, associate_by_email=False, trust_provider_email_verified=False, totp_stepup_policy=dict())
dataclass
¶
Configuration for :func:create_oauth_controller.
RegisterControllerConfig(rate_limit_config=None, path='/auth', user_read_schema=UserRead, user_create_schema=UserCreate, register_minimum_response_seconds=DEFAULT_REGISTER_MINIMUM_RESPONSE_SECONDS, unsafe_testing=False)
dataclass
¶
Configuration for :func:create_register_controller.
SessionDevicesControllerConfig(backend, path='/auth', security=None)
dataclass
¶
Configuration for :func:create_session_devices_controller.
TotpControllerOptions
¶
Bases: TypedDict
Keyword options accepted by :func:create_totp_controller.
TotpUserManagerProtocol
¶
Bases: AccountStateValidatorProvider[UP], Protocol
User-manager behavior required by the TOTP controller.
password_helper
property
¶
Return the configured password helper used for recovery-code hashing.
recovery_code_lookup_secret
property
¶
Return the HMAC lookup key for recovery-code verification.
authenticate(identifier, password, *, login_identifier=None)
async
¶
Re-authenticate the current user (e.g. password step-up for /enable).
consume_recovery_code_by_lookup(user, lookup_hex)
async
¶
find_recovery_code_hash_by_lookup(user, lookup_hex)
async
¶
get(user_id)
async
¶
has_recent_totp_verification(user, session_id)
async
¶
issue_totp_stepup_verification(user, session_id, *, ttl_seconds)
async
¶
Store a recent TOTP verification marker for the current session.
on_after_login(user)
async
¶
read_totp_secret(secret)
async
¶
set_recovery_code_hashes(user, code_index)
async
¶
UsersControllerConfig(id_parser=None, rate_limit_config=None, path='/users', default_limit=50, max_limit=100, hard_delete=False, user_read_schema=UserRead, user_update_schema=UserUpdate, admin_user_update_schema=AdminUserUpdate, unsafe_testing=False, security=None, totp_stepup_policy=dict())
dataclass
¶
Configuration for :func:create_users_controller.
create_api_keys_controllers(*, config=None, **options)
¶
Return self-service and admin API-key controller classes.
Returns:
| Type | Description |
|---|---|
list[type[Controller]]
|
Generated self-service and admin controller classes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/controllers/api_keys.py
create_auth_controller(*, config=None, **options)
¶
Return a controller subclass bound to the provided backend (DI user manager).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AuthControllerConfig[UP, ID] | None
|
Auth controller configuration. |
None
|
**options
|
Unpack[AuthControllerOptions[UP, ID]]
|
Individual auth controller settings. Do not combine with
|
{}
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass with backend-specific login and logout handlers. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/controllers/auth.py
create_oauth_associate_controller(*, config=None, **options)
¶
Return a controller for linking an OAuth account to the authenticated user.
Both /authorize and /callback are protected by is_authenticated. Callback
validates account state, then upserts the OAuth account for request.user and
does not create new users. The generated flow enforces RFC 7636 PKCE S256:
manual clients must accept code_challenge / code_challenge_method on
authorization and code_verifier on token exchange.
Provide either user_manager (for direct use) or user_manager_dependency_key
(for plugin use with a request-scoped dependency). user_manager_dependency_key
must be a valid non-keyword Python identifier because Litestar injects
dependencies by matching keys to callback parameter names.
redirect_base_url must use a non-loopback https:// origin; the
manual controller API does not expose a debug or testing override for
insecure callback origins.
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Generated controller class mounted under the provider-specific path. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/controllers/oauth.py
create_oauth_controller(*, config=None, **options)
¶
Return a controller subclass bound to one OAuth provider.
The authorize endpoint uses only server-configured oauth_scopes. Runtime
scope-query overrides are rejected. redirect_base_url must use a
non-loopback https:// origin; the manual controller API does not expose
a debug or testing override for insecure callback origins. The generated
flow enforces RFC 7636 PKCE S256: manual clients must accept
code_challenge / code_challenge_method on authorization and
code_verifier on token exchange.
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Generated controller class mounted under the provider-specific path. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/controllers/oauth.py
create_register_controller(*, config=None, **options)
¶
Return a controller subclass for enumeration-resistant public registration.
Domain failures collapse to HTTP 400 with ErrorCode.REGISTER_FAILED and
successful/domain-failed attempts are padded to the configured minimum
duration.
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing the registration endpoint. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/controllers/register.py
create_reset_password_controller(*, rate_limit_config=None, path='/auth', user_read_schema=UserRead, unsafe_testing=False)
¶
Return a controller subclass that resolves the user manager via Litestar DI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate_limit_config
|
AuthRateLimitConfig | None
|
Optional auth-endpoint rate-limiter configuration. |
None
|
path
|
str
|
Base route prefix for the generated controller. |
'/auth'
|
user_read_schema
|
type[Struct]
|
Custom msgspec struct used for public reset-password responses. |
UserRead
|
unsafe_testing
|
bool
|
Explicit test-only override that allows response schemas with sensitive fields for isolated fixtures. |
False
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing reset-password endpoints. |
Source code in litestar_auth/controllers/reset.py
create_session_devices_controller(config=None, **options)
¶
Return a controller exposing authenticated refresh-session management routes.
Raises:
| Type | Description |
|---|---|
TypeError
|
If both |
Source code in litestar_auth/controllers/session_devices.py
create_totp_controller(**options)
¶
Return a controller with TOTP management and login-completion endpoints.
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass with TOTP management endpoints. |
Source code in litestar_auth/controllers/totp.py
create_users_controller(*, config=None, **options)
¶
Return the users controller subclass wired for Litestar DI.
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing self-service profile, reverified password rotation, |
type[Controller]
|
and admin user endpoints. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/controllers/users.py
create_verify_controller(*, rate_limit_config=None, path='/auth', user_read_schema=UserRead, verify_minimum_response_seconds=DEFAULT_MINIMUM_RESPONSE_SECONDS, request_verify_minimum_response_seconds=DEFAULT_MINIMUM_RESPONSE_SECONDS, unsafe_testing=False)
¶
Return a controller subclass that resolves the user manager via Litestar DI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate_limit_config
|
AuthRateLimitConfig | None
|
Optional auth-endpoint rate-limiter configuration. When provided,
|
None
|
path
|
str
|
Base route prefix for the generated controller. |
'/auth'
|
user_read_schema
|
type[Struct]
|
Custom msgspec struct used for public verification responses. |
UserRead
|
verify_minimum_response_seconds
|
float
|
Minimum wall-clock duration for verify responses. |
DEFAULT_MINIMUM_RESPONSE_SECONDS
|
request_verify_minimum_response_seconds
|
float
|
Minimum wall-clock duration for request-verify-token responses. |
DEFAULT_MINIMUM_RESPONSE_SECONDS
|
unsafe_testing
|
bool
|
Explicit test-only override that allows response schemas with sensitive fields for isolated fixtures. |
False
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing verification-related endpoints. |