Controllers¶
HTTP controller factories and request/response payload types for advanced wiring. Most applications rely on LitestarAuth plus the canonical litestar_auth.oauth.create_provider_oauth_controller(...) login helper instead of mounting these directly.
Use this module when you need the explicit escape hatch: custom OAuth path prefixes, direct user-manager wiring, or another non-canonical route table.
litestar_auth.controllers
¶
Public controller exports.
This module is the stable import location for controller factories and their
request/response payload types. litestar_auth.payloads is the authoritative
payload boundary; root-package and controller-module re-exports remain
available for compatibility.
ForgotPassword
¶
Bases: Struct
Payload used to request a reset-password token.
LoginCredentials
¶
Bases: Struct
Login payload accepted by the auth controller.
RefreshTokenRequest
¶
Bases: Struct
Refresh payload accepted by the auth controller.
RequestVerifyToken
¶
Bases: Struct
Payload used to request a fresh email-verification token.
ResetPassword
¶
Bases: Struct
Payload used to reset a password with a previously issued token.
TotpConfirmEnableRequest
¶
Bases: Struct
Payload for confirming TOTP enrollment (phase 2).
TotpConfirmEnableResponse
¶
Bases: Struct
Response returned when 2FA is successfully confirmed and persisted.
TotpDisableRequest
¶
Bases: Struct
Payload for disabling 2FA.
TotpEnableResponse
¶
Bases: Struct
Response returned when 2FA enrollment is initiated (phase 1).
The secret is not yet persisted. The client must confirm enrollment via
/enable/confirm with a valid TOTP code to activate 2FA.
TotpUserManagerProtocol
¶
Bases: AccountStateValidatorProvider[UP], Protocol
User-manager behavior required by the TOTP controller.
TotpVerifyRequest
¶
Bases: Struct
Payload for completing 2FA login verification.
VerifyToken
¶
Bases: Struct
Payload used to complete an email-verification flow.
create_auth_controller(*, backend, rate_limit_config=None, enable_refresh=False, requires_verification=False, login_identifier='email', totp_pending_secret=None, totp_pending_lifetime=_DEFAULT_PENDING_TOKEN_LIFETIME, path='/auth')
¶
Return a controller subclass bound to the provided backend (DI user manager).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
AuthenticationBackend[UP, ID]
|
Auth backend used to issue and invalidate tokens. |
required |
rate_limit_config
|
AuthRateLimitConfig | None
|
Optional auth-endpoint rate-limiter configuration. |
None
|
enable_refresh
|
bool
|
When |
False
|
requires_verification
|
bool
|
When |
False
|
login_identifier
|
LoginIdentifier
|
Which user attribute is used to interpret the login
|
'email'
|
totp_pending_secret
|
str | None
|
When set, enables 2FA support. Login returns an
intermediate pending token when the user has TOTP configured.
Must match the value passed to |
None
|
totp_pending_lifetime
|
timedelta
|
Maximum age of the intermediate pending token. |
_DEFAULT_PENDING_TOKEN_LIFETIME
|
path
|
str
|
Base route prefix for the generated controller. |
'/auth'
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass with backend-specific login and logout handlers. |
Examples:
from litestar_auth.controllers.auth import create_auth_controller
AuthController = create_auth_controller(
backend=backend,
path="/auth",
)
Source code in litestar_auth/controllers/auth.py
create_oauth_associate_controller(*, provider_name, user_manager=None, user_manager_dependency_key=None, oauth_client, redirect_base_url, path='/auth/associate', cookie_secure=True)
¶
Return a controller for linking an OAuth account to the authenticated user.
Both /authorize and /callback are protected by is_authenticated. Callback upserts the OAuth account for request.user and does not create new users.
Provide either user_manager (for direct use) or user_manager_dependency_key (for plugin use with a request-scoped dependency).
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Generated controller class mounted under the provider-specific path. |
Source code in litestar_auth/controllers/oauth.py
create_oauth_controller(*, provider_name, backend, user_manager, oauth_client, redirect_base_url, path='/auth/oauth', cookie_secure=True, associate_by_email=False, trust_provider_email_verified=False)
¶
Return a controller subclass bound to one OAuth provider.
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Generated controller class mounted under the provider-specific path. |
Source code in litestar_auth/controllers/oauth.py
create_register_controller(*, rate_limit_config=None, path='/auth', user_read_schema=UserRead, user_create_schema=UserCreate)
¶
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 registration responses. |
UserRead
|
user_create_schema
|
type[Struct]
|
Custom msgspec struct used for registration requests. |
UserCreate
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing the registration endpoint. |
Examples:
class ExtendedUserCreate(msgspec.Struct):
email: str
password: str
bio: str
class ExtendedUserRead(msgspec.Struct):
id: uuid.UUID
email: str
is_active: bool
is_verified: bool
is_superuser: bool
bio: str
controller = create_register_controller(
user_create_schema=ExtendedUserCreate,
user_read_schema=ExtendedUserRead,
)
Source code in litestar_auth/controllers/register.py
create_reset_password_controller(*, rate_limit_config=None, path='/auth', user_read_schema=UserRead)
¶
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
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing reset-password endpoints. |
Source code in litestar_auth/controllers/reset.py
create_totp_controller(*, backend, user_manager_dependency_key, used_tokens_store=None, pending_jti_store=None, require_replay_protection=True, rate_limit_config=None, requires_verification=False, totp_pending_secret, totp_enable_requires_password=True, totp_issuer='litestar-auth', totp_algorithm='SHA256', totp_pending_lifetime=None, id_parser=None, path='/auth/2fa')
¶
Return a controller with TOTP enable/verify/disable endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
AuthenticationBackend[UP, ID]
|
Auth backend used to issue tokens after successful TOTP verification. |
required |
user_manager_dependency_key
|
str
|
Litestar DI key / handler parameter name for the request-scoped user manager. |
required |
used_tokens_store
|
UsedTotpCodeStore | None
|
Optional replay-protection cache for successful |
None
|
pending_jti_store
|
JWTDenylistStore | None
|
Optional denylist store used to reject replayed
pending-token JTIs after successful |
None
|
require_replay_protection
|
bool
|
When enabled, the controller refuses to start without a used-token replay store outside testing mode. |
True
|
rate_limit_config
|
AuthRateLimitConfig | None
|
Optional auth-endpoint rate-limiter configuration. |
None
|
requires_verification
|
bool
|
When |
False
|
totp_pending_secret
|
str
|
Shared secret for signing and verifying pending-2FA JWTs.
Must match the value passed to |
required |
totp_enable_requires_password
|
bool
|
When |
True
|
totp_issuer
|
str
|
Issuer label shown inside authenticator-app QR codes. |
'litestar-auth'
|
totp_algorithm
|
TotpAlgorithm
|
Hash algorithm used for TOTP generation and verification. |
'SHA256'
|
totp_pending_lifetime
|
timedelta | None
|
Unused; kept for API symmetry with
|
None
|
id_parser
|
Callable[[str], ID] | None
|
Optional callable that converts the JWT |
None
|
path
|
str
|
Base route prefix for the generated controller. |
'/auth/2fa'
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass with TOTP management endpoints. |
Examples:
from litestar_auth.controllers.totp import create_totp_controller
totp_controller_cls = create_totp_controller(
backend=backend,
user_manager_dependency_key="litestar_auth_user_manager",
totp_pending_secret=settings.totp_pending_secret,
)
Source code in litestar_auth/controllers/totp.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
create_users_controller(*, id_parser=None, path='/users', default_limit=50, max_limit=100, hard_delete=False, user_read_schema=UserRead, user_update_schema=UserUpdate)
¶
Return a controller subclass that resolves the user manager via Litestar DI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_parser
|
Callable[[str], ID] | None
|
Optional callable that converts path IDs into manager IDs. |
None
|
path
|
str
|
Base route prefix for the generated controller. |
'/users'
|
default_limit
|
int
|
Default page size for list responses. |
50
|
max_limit
|
int
|
Maximum allowed page size for list responses. |
100
|
hard_delete
|
bool
|
When |
False
|
user_read_schema
|
type[Struct]
|
Custom msgspec struct used for public user responses. |
UserRead
|
user_update_schema
|
type[Struct]
|
Custom msgspec struct used for update requests. |
UserUpdate
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing self-service and admin user endpoints. |
Examples:
class ExtendedUserRead(msgspec.Struct):
id: uuid.UUID
email: str
is_active: bool
is_verified: bool
is_superuser: bool
bio: str
class ExtendedUserUpdate(msgspec.Struct, omit_defaults=True):
email: str | None = None
password: str | None = None
bio: str | None = None
controller = create_users_controller(
user_read_schema=ExtendedUserRead,
user_update_schema=ExtendedUserUpdate,
)
Source code in litestar_auth/controllers/users.py
create_verify_controller(*, rate_limit_config=None, path='/auth', user_read_schema=UserRead)
¶
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
|
Returns:
| Type | Description |
|---|---|
type[Controller]
|
Controller subclass exposing verification-related endpoints. |