User manager¶
litestar_auth.manager
¶
User-management business logic for litestar-auth.
BaseUserManager(user_db, *, oauth_account_store=None, password_helper=None, verification_token_secret=None, reset_password_token_secret=None, verification_token_lifetime=DEFAULT_VERIFY_TOKEN_LIFETIME, reset_password_token_lifetime=DEFAULT_RESET_PASSWORD_TOKEN_LIFETIME, id_parser=None, password_validator=None, reset_verification_on_email_change=True, totp_secret_key=None, backends=(), login_identifier='email')
¶
Bases: _UserLifecycleManagerProtocol[UP, ID], _AccountTokensManagerProtocol[UP, ID], _TotpSecretsManagerProtocol[UP]
Coordinate user persistence, password hashing, and account tokens.
Initialize the user manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_db
|
BaseUserStore[UP, ID]
|
Persistence backend used to load and update users. |
required |
oauth_account_store
|
BaseOAuthAccountStore[UP, ID] | None
|
Optional persistence backend used for linked OAuth accounts. |
None
|
password_helper
|
PasswordHelper | None
|
Password hasher/verifier implementation. |
None
|
verification_token_secret
|
str | None
|
Secret used to sign email-verification tokens.
If omitted, |
None
|
reset_password_token_secret
|
str | None
|
Secret used to sign password-reset tokens.
If omitted, |
None
|
verification_token_lifetime
|
timedelta
|
Lifetime applied to verification tokens. |
DEFAULT_VERIFY_TOKEN_LIFETIME
|
reset_password_token_lifetime
|
timedelta
|
Lifetime applied to password-reset tokens. |
DEFAULT_RESET_PASSWORD_TOKEN_LIFETIME
|
id_parser
|
Callable[[str], ID] | None
|
Optional callable that converts JWT |
None
|
password_validator
|
Callable[[str], None] | None
|
Optional callable used to validate plain-text passwords. |
None
|
reset_verification_on_email_change
|
bool
|
Whether email changes should clear |
True
|
totp_secret_key
|
str | None
|
Optional Fernet key used to encrypt stored TOTP secrets. |
None
|
backends
|
tuple[object, ...]
|
Session-bound auth backends when constructed via |
()
|
login_identifier
|
LoginIdentifier
|
Which field |
'email'
|
Source code in litestar_auth/manager.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
authenticate(identifier, password, *, login_identifier=None)
async
¶
Return the matching user when credentials are valid.
After successful verification, if the stored hash is deprecated (e.g. bcrypt), the password hash is upgraded to the current algorithm (e.g. Argon2) in the DB. If the DB update fails, login still succeeds without upgrading the hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
Email or username string, depending on |
required |
password
|
str
|
Plain-text password. |
required |
login_identifier
|
LoginIdentifier | None
|
Lookup mode; defaults to :attr: |
None
|
Source code in litestar_auth/manager.py
create(user_create, *, safe=True, allow_privileged=False)
async
¶
Create a new user, hashing the provided password before persistence.
Returns:
| Type | Description |
|---|---|
UP
|
The newly created user. |
Source code in litestar_auth/manager.py
delete(user_id)
async
¶
forgot_password(email)
async
¶
Trigger the forgot-password flow without revealing whether a user exists.
get(user_id)
async
¶
Return a user by identifier.
Returns:
| Type | Description |
|---|---|
UP | None
|
The matching user when one exists, otherwise |
list_users(*, offset, limit)
async
¶
Return paginated users and the total available count.
on_after_delete(user)
async
¶
on_after_forgot_password(user, token)
async
¶
Hook invoked after a forgot-password request is processed.
SECURITY: When user is None, the email did not match any account.
To prevent user enumeration via timing, your implementation MUST perform
equivalent I/O in both cases (e.g., always enqueue a background task,
whether or not an email will actually be sent). Do NOT conditionally
skip work based on whether user is None.
Source code in litestar_auth/manager.py
on_after_login(user)
async
¶
on_after_register(user, token)
async
¶
on_after_request_verify_token(user, token)
async
¶
on_after_reset_password(user)
async
¶
on_after_update(user, update_dict)
async
¶
on_after_verify(user)
async
¶
on_before_delete(user)
async
¶
read_totp_secret(secret)
async
¶
Return a plain-text TOTP secret from storage.
Returns:
| Type | Description |
|---|---|
str | None
|
Plain-text secret, or |
Source code in litestar_auth/manager.py
request_verify_token(email)
async
¶
Generate a new verification token for an existing unverified user.
require_account_state(user, *, require_verified=False)
¶
Validate account-state policy for authenticated flows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
UP
|
User to validate. |
required |
require_verified
|
bool
|
When |
False
|
Source code in litestar_auth/manager.py
reset_password(token, password)
async
¶
Reset a user's password using a signed reset token.
Returns:
| Type | Description |
|---|---|
UP
|
The updated user instance. |
set_totp_secret(user, secret)
async
¶
Store or clear the TOTP secret directly, bypassing None-filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
UP
|
The user whose TOTP secret should be updated. |
required |
secret
|
str | None
|
New secret string, or |
required |
Returns:
| Type | Description |
|---|---|
UP
|
The updated user instance. |
Source code in litestar_auth/manager.py
update(user_update, user)
async
¶
Update mutable user fields, hashing passwords when provided.
Fields with None values in user_update are treated as absent and
will not overwrite existing data. To explicitly clear a nullable
field, use a dedicated method (e.g. set_totp_secret(user, None)).
Returns:
| Type | Description |
|---|---|
UP
|
The updated user, or the original user when there are no changes. |
Source code in litestar_auth/manager.py
verify(token)
async
¶
Mark a user as verified using a signed verification token.
Returns:
| Type | Description |
|---|---|
UP
|
The verified user instance. |
write_verify_token(user)
¶
Return a signed email-verification token for a user.
Returns:
| Type | Description |
|---|---|
str
|
A short-lived verification token. |