infrastructure.persistence.repositories.refresh_token_repository¶
src.infrastructure.persistence.repositories.refresh_token_repository
¶
RefreshTokenRepository - SQLAlchemy implementation for refresh token persistence.
Handles CRUD operations for refresh tokens with automatic expiration checks.
Classes¶
RefreshTokenRepository
¶
SQLAlchemy implementation for refresh token persistence.
Manages refresh tokens with support for: - Token creation and storage - Token validation (hash lookup) - Token rotation (delete old, create new) - Session-based revocation
Attributes:
| Name | Type | Description |
|---|---|---|
session |
SQLAlchemy async session for database operations. |
Example
async with get_session() as session: ... repo = RefreshTokenRepository(session) ... token = await repo.find_by_token_hash(token_hash)
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
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 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 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 220 221 222 223 224 225 226 227 | |
Functions¶
__init__
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
AsyncSession
|
SQLAlchemy async session. |
required |
save
async
¶
save(
user_id: UUID,
token_hash: str,
session_id: UUID,
expires_at: datetime,
*,
token_version: int = 1,
global_version_at_issuance: int = 1
) -> RefreshTokenData
Create new refresh token in database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
token_hash
|
str
|
Bcrypt hash of the refresh token. |
required |
session_id
|
UUID
|
Associated session ID. |
required |
expires_at
|
datetime
|
Token expiration timestamp. |
required |
token_version
|
int
|
Token version at issuance (for breach rotation). |
1
|
global_version_at_issuance
|
int
|
Global min version when issued. |
1
|
Returns:
| Type | Description |
|---|---|
RefreshTokenData
|
Created RefreshTokenData. |
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
find_by_token_hash
async
¶
Find refresh token by hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_hash
|
str
|
Bcrypt hash of the token. |
required |
Returns:
| Type | Description |
|---|---|
RefreshTokenData | None
|
RefreshTokenData if found and not revoked, None otherwise. |
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
find_by_id
async
¶
Find refresh token by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
UUID
|
Token's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
RefreshTokenData | None
|
RefreshTokenData if found, None otherwise. |
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
update_last_used
async
¶
Update last_used_at timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
UUID
|
Token's unique identifier. |
required |
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
delete
async
¶
Delete refresh token (for rotation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
UUID
|
Token's unique identifier. |
required |
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
revoke_by_session
async
¶
Revoke all refresh tokens for a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
UUID
|
Session ID to revoke tokens for. |
required |
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
revoke_all_for_user
async
¶
Revoke all refresh tokens for a user.
Used when password changes or user logs out of all devices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
reason
|
str
|
Reason for revocation (for audit). |
'user_requested'
|
Source code in src/infrastructure/persistence/repositories/refresh_token_repository.py
find_by_token_verification
async
¶
find_by_token_verification(
token: str, verify_fn: Callable[[str, str], bool]
) -> RefreshTokenData | None
Find refresh token by verifying against stored hashes.
Since bcrypt hashes are non-deterministic, we iterate through active tokens and verify each one against the provided token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
Plain refresh token from user request. |
required |
verify_fn
|
Callable[[str, str], bool]
|
Function to verify token against hash (token, hash) -> bool. |
required |
Returns:
| Type | Description |
|---|---|
RefreshTokenData | None
|
RefreshTokenData if found and verified, None otherwise. |