API Reference
Password reset tokens module for user password recovery.
This module provides CRUD operations, email messaging, and utilities for password reset token management.
Exports
- CRUD: create_password_reset_token, get_password_reset_token_by_hash, claim_password_reset_token, mark_password_reset_token_used, mark_user_password_reset_tokens_used, delete_expired_password_reset_tokens
- Schemas: PasswordResetToken, PasswordResetRequest, PasswordResetConfirm, PasswordResetResponse
- Models: PasswordResetToken (ORM model)
PasswordResetConfirm
Bases: BaseModel
Request schema for confirming a password reset.
Attributes:
| Name | Type | Description |
|---|---|---|
token |
StrictStr
|
The reset token received by the user. |
new_password |
StrictStr
|
The new password to set. |
Source code in backend/app/password_reset_tokens/schema.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
PasswordResetRequest
Bases: BaseModel
Request schema for initiating a password reset.
Attributes:
| Name | Type | Description |
|---|---|---|
email |
EmailStr
|
Email address for the reset. |
Source code in backend/app/password_reset_tokens/schema.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
PasswordResetResponse
Bases: BaseModel
Response schema for password reset operations.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
StrictStr
|
Informational message for the client. |
Source code in backend/app/password_reset_tokens/schema.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
PasswordResetToken
Bases: BaseModel
Schema representing a password reset token record.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
StrictStr
|
Unique identifier for the token. |
user_id |
StrictInt
|
ID of the user who requested the reset. |
token_hash |
StrictStr
|
Hashed token value. |
created_at |
datetime
|
Timestamp when the token was created. |
expires_at |
datetime
|
Timestamp when the token expires. |
used |
StrictBool
|
Whether the token has already been used. |
Source code in backend/app/password_reset_tokens/schema.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
PasswordResetTokenModel
Bases: Base
Password reset token database model.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
Mapped[str]
|
Unique token identifier (string, 64 chars). |
user_id |
Mapped[int]
|
ID of the user who owns the token. |
token_hash |
Mapped[str]
|
Hashed password reset token. |
created_at |
Mapped[datetime]
|
Token creation date. |
expires_at |
Mapped[datetime]
|
Token expiration date. |
used |
Mapped[bool]
|
Whether the token has been used. |
users |
Mapped[Users]
|
Relationship to the Users model. |
Source code in backend/app/password_reset_tokens/models.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
claim_password_reset_token
claim_password_reset_token(token_hash, db)
Atomically claim a valid password reset token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_hash
|
str
|
SHA-256 hash of the plaintext reset token. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
User ID owning the claimed token, or None if the token is missing, |
int | None
|
expired, or already used. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
500 error if database operation fails. |
Source code in backend/app/password_reset_tokens/crud.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
create_password_reset_token
create_password_reset_token(token, db)
Create and persist a new password reset token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
PasswordResetToken
|
Schema object with token data to persist. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
PasswordResetToken
|
The persisted PasswordResetToken ORM instance. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
500 error if database operation fails. |
Source code in backend/app/password_reset_tokens/crud.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
delete_expired_password_reset_tokens
delete_expired_password_reset_tokens(db)
Delete all expired password reset tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of deleted rows. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
500 error if database operation fails. |
Source code in backend/app/password_reset_tokens/crud.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
get_password_reset_token_by_hash
get_password_reset_token_by_hash(token_hash, db)
Retrieve an unused, unexpired token matching the given hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_hash
|
str
|
The hashed token value to look up. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
PasswordResetToken | None
|
The matching PasswordResetToken if found and valid, None otherwise. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
500 error if database query fails. |
Source code in backend/app/password_reset_tokens/crud.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
mark_password_reset_token_used
mark_password_reset_token_used(token_id, db)
Mark a password reset token as used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
str
|
The unique identifier of the token to mark. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
PasswordResetToken | None
|
Updated PasswordResetToken instance if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
500 error if database operation fails. |
Source code in backend/app/password_reset_tokens/crud.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
mark_user_password_reset_tokens_used
mark_user_password_reset_tokens_used(user_id, db)
Mark all unused password reset tokens for a user as used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
User ID whose reset tokens should be invalidated. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of rows marked as used. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
500 error if database operation fails. |
Source code in backend/app/password_reset_tokens/crud.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |