infrastructure.persistence.repositories.password_reset_token_repository¶
src.infrastructure.persistence.repositories.password_reset_token_repository
¶
PasswordResetTokenRepository - SQLAlchemy implementation for password reset token persistence.
Handles CRUD operations for password reset tokens with expiration checks.
Classes¶
PasswordResetTokenRepository
¶
SQLAlchemy implementation for password reset token persistence.
Manages password reset tokens with support for: - Token creation and storage - Token validation (lookup by token string) - One-time use enforcement (mark as used) - IP address and user agent tracking
Attributes:
| Name | Type | Description |
|---|---|---|
session |
SQLAlchemy async session for database operations. |
Example
async with get_session() as session: ... repo = PasswordResetTokenRepository(session) ... token = await repo.find_by_token("abc123...")
Source code in src/infrastructure/persistence/repositories/password_reset_token_repository.py
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 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 | |
Functions¶
__init__
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
SQLAlchemy async session. |
required |
save
async
¶
save(
user_id: UUID,
token: str,
expires_at: datetime,
ip_address: str | None = None,
user_agent: str | None = None,
) -> PasswordResetTokenData
Create new password reset token in database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
token
|
str
|
Random hex token (64 characters). |
required |
expires_at
|
datetime
|
Token expiration timestamp (15 minutes). |
required |
ip_address
|
str | None
|
IP address of requester (for audit). |
None
|
user_agent
|
str | None
|
User agent of requester (for audit). |
None
|
Returns:
| Type | Description |
|---|---|
PasswordResetTokenData
|
Created PasswordResetTokenData. |
Source code in src/infrastructure/persistence/repositories/password_reset_token_repository.py
find_by_token
async
¶
Find password reset token by token string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The reset token string. |
required |
Returns:
| Type | Description |
|---|---|
PasswordResetTokenData | None
|
PasswordResetTokenData if found and not used, None otherwise. |
Source code in src/infrastructure/persistence/repositories/password_reset_token_repository.py
mark_as_used
async
¶
Mark password reset token as used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
UUID
|
Token's unique identifier. |
required |
Source code in src/infrastructure/persistence/repositories/password_reset_token_repository.py
delete_expired_tokens
async
¶
Delete expired password reset tokens.
Cleanup task to remove old tokens (typically run hourly).
Returns:
| Type | Description |
|---|---|
int
|
Number of tokens deleted. |
Source code in src/infrastructure/persistence/repositories/password_reset_token_repository.py
find_by_user_id
async
¶
Find all password reset tokens for a user.
Useful for debugging, admin views, or detecting abuse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
list[PasswordResetTokenData]
|
List of PasswordResetTokenData. |
Source code in src/infrastructure/persistence/repositories/password_reset_token_repository.py
count_recent_requests
async
¶
Count password reset requests since a given time.
Used for rate limiting (e.g., max 3 requests per hour).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
since
|
datetime
|
Start time for counting. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of reset requests since the given time. |