TOTP¶
litestar_auth.totp
¶
Time-based one-time password helpers.
InMemoryUsedTotpCodeStore(*, clock=time.monotonic, max_entries=50000)
¶
Async-safe in-memory replay cache for successful TOTP verifications.
Not safe for multi-process or multi-host deployments; use :class:RedisUsedTotpCodeStore
for shared storage (e.g. multi-worker or multi-pod).
Store the monotonic clock and initialize cache state.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in litestar_auth/totp.py
mark_used(user_id, counter, ttl_seconds)
async
¶
Store a used code pair until its TTL elapses.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in litestar_auth/totp.py
RedisUsedTotpCodeStore(*, redis, key_prefix=DEFAULT_TOTP_USED_KEY_PREFIX)
¶
Redis-backed replay store for TOTP codes; safe for multi-worker and multi-pod deployments.
Store the Redis client and key prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis
|
RedisUsedTotpCodeStoreClient
|
Async Redis client supporting |
required |
key_prefix
|
str
|
Prefix for replay keys; keys are |
DEFAULT_TOTP_USED_KEY_PREFIX
|
Source code in litestar_auth/totp.py
mark_used(user_id, counter, ttl_seconds)
async
¶
Atomically record a used (user_id, counter) pair via SET key 1 NX PX ttl_ms.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in litestar_auth/totp.py
RedisUsedTotpCodeStoreClient
¶
Bases: Protocol
Minimal Redis client interface for TOTP replay store (SET key value NX PX ttl_ms).
set(name, value, *, nx=False, px=None)
async
¶
Set a key; with nx=True only set if not exists; px is TTL in milliseconds.
Returns:
| Type | Description |
|---|---|
bool | None
|
|
Source code in litestar_auth/totp.py
SecurityWarning
¶
Bases: UserWarning
Warning emitted for security-sensitive insecure defaults (TOTP, plugin startup, etc.).
UsedTotpCodeStore
¶
Bases: Protocol
Persistence for used TOTP codes keyed by user and counter.
mark_used(user_id, counter, ttl_seconds)
async
¶
Atomically record a (user_id, counter) pair when unused.
Returns:
| Type | Description |
|---|---|
bool
|
|
generate_totp_secret(algorithm=TOTP_ALGORITHM)
¶
Generate a base32-encoded TOTP secret sized to the algorithm's HMAC output.
Per RFC 4226 Section 4, the shared secret length should match the HMAC
output length: 20 bytes for SHA-1, 32 bytes for SHA-256, 64 bytes for
SHA-512. The SECRET_BYTES constant is retained for backward
compatibility but is no longer the sole source of truth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
TotpAlgorithm
|
TOTP hash algorithm; determines secret byte length. |
TOTP_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
str
|
A random base32 secret without RFC padding. |
Source code in litestar_auth/totp.py
generate_totp_uri(secret, email, issuer, *, algorithm=TOTP_ALGORITHM)
¶
Build an otpauth URI suitable for QR-code generation.
Returns:
| Type | Description |
|---|---|
str
|
An |
Source code in litestar_auth/totp.py
verify_totp(secret, code, *, algorithm=TOTP_ALGORITHM)
¶
Validate a TOTP code for the current time window only.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in litestar_auth/totp.py
verify_totp_with_store(secret, code, *, user_id, used_tokens_store=None, algorithm=TOTP_ALGORITHM, require_replay_protection=True)
async
¶
Validate a TOTP code and optionally reject same-window replays.
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If |
Source code in litestar_auth/totp.py
litestar_auth.totp_flow
¶
TOTP login-flow orchestration for pending-token issue and verification.
InvalidTotpCodeError
¶
Bases: Exception
Raised when a TOTP code cannot complete the pending login flow.
InvalidTotpPendingTokenError
¶
Bases: Exception
Raised when a pending TOTP token is invalid or expired.
PendingTotpLogin(user, pending_jti, expires_at)
dataclass
¶
Decoded pending-login state required to finish a TOTP handshake.
TotpFlowUserManagerProtocol
¶
Bases: Protocol
User-manager behavior required by TOTP login-flow orchestration.
get(user_id)
async
¶
TotpLoginFlowService(*, user_manager, totp_pending_secret, totp_pending_lifetime=_DEFAULT_PENDING_TOKEN_LIFETIME, totp_algorithm='SHA256', require_replay_protection=True, used_tokens_store=None, pending_jti_store=None, id_parser=None)
¶
Issue and verify pending TOTP login challenges.
Bind the dependencies used by the pending-login handshake.
Source code in litestar_auth/totp_flow.py
authenticate_pending_login(*, pending_token, code, validate_user=None)
async
¶
Validate a pending-login token plus TOTP code and return the resolved user.
Returns:
| Type | Description |
|---|---|
UP
|
The user resolved from the verified pending-login challenge. |
Raises:
| Type | Description |
|---|---|
InvalidTotpCodeError
|
If the TOTP code is invalid or TOTP is not enabled. |
Source code in litestar_auth/totp_flow.py
issue_pending_token(user)
async
¶
Return a pending-login JWT when the user has TOTP enabled.