Models¶
SQLModel database models with relationships and validation.
All models are located in src/models/ and represent database tables. They use SQLModel (combining SQLAlchemy and Pydantic) for both ORM functionality and data validation.
User Model¶
src.models.user.User ¶
Bases: DashtamBase
Application user model with JWT authentication support.
Represents a user who can authenticate and manage multiple financial provider instances. Supports JWT authentication with email/password, email verification, and account security features.
Attributes:
| Name | Type | Description |
|---|---|---|
email |
str
|
User's email address (unique, used for login). |
name |
str
|
User's full name. |
password_hash |
Optional[str]
|
Bcrypt hash of user's password. |
email_verified |
bool
|
Whether email address has been verified. |
email_verified_at |
Optional[datetime]
|
Timestamp of email verification. |
failed_login_attempts |
int
|
Count of consecutive failed login attempts. |
account_locked_until |
Optional[datetime]
|
Timestamp until which account is locked (after too many failures). |
last_login_at |
Optional[datetime]
|
Timestamp of last successful login. |
last_login_ip |
Optional[str]
|
IP address of last successful login. |
is_active |
bool
|
Whether account is active (for soft deletion/suspension). |
providers |
List[Provider]
|
List of provider instances owned by user. |
refresh_tokens |
List[RefreshToken]
|
List of active refresh tokens for user sessions. |
email_verification_tokens |
List[EmailVerificationToken]
|
List of email verification tokens. |
password_reset_tokens |
List[PasswordResetToken]
|
List of password reset tokens. |
Source code in src/models/user.py
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 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 | |
Attributes¶
Functions¶
ensure_timezone_aware
classmethod
¶
Ensure datetime fields are timezone-aware (UTC).
Source code in src/models/user.py
reset_failed_login_attempts ¶
increment_failed_login_attempts ¶
Increment failed login attempts and lock account if threshold exceeded.
Source code in src/models/user.py
Provider Model¶
src.models.provider.Provider ¶
Bases: DashtamBase
User's provider instances.
Each row represents a user's connection to a financial provider. Users can have multiple instances of the same provider with different aliases (e.g., "Schwab Personal" and "Schwab 401k").
Attributes:
| Name | Type | Description |
|---|---|---|
user_id |
UUID
|
The user who owns this provider instance. |
provider_key |
str
|
The provider identifier (must match registry). |
alias |
str
|
User's custom name for this instance. |
is_active |
bool
|
Whether this instance is currently active. |
metadata |
bool
|
Additional user-specific metadata. |
Source code in src/models/provider.py
Attributes¶
needs_reconnection
property
¶
Check if this provider needs to be reconnected.
Functions¶
validate_provider_key
classmethod
¶
Validate that provider_key exists in the registry.
This validation happens at the application level. The actual check against the registry should be done in the service layer.
Source code in src/models/provider.py
ProviderConnection Model¶
src.models.provider.ProviderConnection ¶
Bases: DashtamBase
Connection details for a provider instance.
Each provider instance has exactly one connection that manages the authentication state and sync schedule.
Attributes:
| Name | Type | Description |
|---|---|---|
provider_id |
UUID
|
The provider instance this connection belongs to. |
status |
ProviderStatus
|
Current status of the connection. |
connected_at |
Optional[datetime]
|
When the connection was established. |
last_sync_at |
Optional[datetime]
|
When data was last synced. |
next_sync_at |
Optional[datetime]
|
When the next sync should occur. |
sync_frequency_minutes |
int
|
How often to sync data. |
error_message |
Optional[str]
|
Last error message if any. |
error_count |
int
|
Number of consecutive errors. |
accounts_count |
int
|
Number of accounts accessible. |
accounts_list |
List[str]
|
List of account IDs accessible. |
Source code in src/models/provider.py
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
Functions¶
ensure_timezone_aware
classmethod
¶
Ensure datetime fields are timezone-aware (UTC).
Source code in src/models/provider.py
mark_connected ¶
Mark the connection as successfully connected.
Source code in src/models/provider.py
schedule_next_sync ¶
mark_sync_successful ¶
Mark a sync as successful and schedule next one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accounts
|
Optional[List[str]]
|
Optional list of account IDs that were synced. |
None
|
Source code in src/models/provider.py
mark_sync_failed ¶
Mark a sync as failed and increment error count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_message
|
str
|
Description of the error that occurred. |
required |
Source code in src/models/provider.py
ProviderToken Model¶
src.models.provider.ProviderToken ¶
Bases: DashtamBase
OAuth tokens for a provider connection.
Each connection has exactly one token record that stores encrypted OAuth credentials. Tokens are automatically refreshed when expired.
The tokens are encrypted before storage for security. The encryption is handled by the TokenService layer.
Attributes:
| Name | Type | Description |
|---|---|---|
connection_id |
UUID
|
The connection this token belongs to. |
access_token_encrypted |
str
|
Encrypted OAuth access token. |
refresh_token_encrypted |
Optional[str]
|
Encrypted OAuth refresh token. |
id_token |
Optional[str]
|
JWT ID token if provided (for identifying unique connections). |
token_type |
str
|
Type of token (usually 'Bearer'). |
expires_at |
Optional[datetime]
|
When the access token expires. |
scope |
Optional[str]
|
OAuth scopes granted. |
last_refreshed_at |
Optional[datetime]
|
When token was last refreshed. |
refresh_count |
int
|
Number of times refreshed. |
Source code in src/models/provider.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
Attributes¶
is_expired
property
¶
Check if the access token is expired.
Returns:
| Type | Description |
|---|---|
bool
|
True if the token has expired, False otherwise. |
is_expiring_soon
property
¶
Check if token expires within 5 minutes.
Returns:
| Type | Description |
|---|---|
bool
|
True if the token expires within 5 minutes, False otherwise. |
Functions¶
ensure_timezone_aware
classmethod
¶
Ensure datetime fields are timezone-aware (UTC).
Source code in src/models/provider.py
update_tokens ¶
update_tokens(
access_token_encrypted: str,
refresh_token_encrypted: Optional[str] = None,
expires_in: Optional[int] = None,
id_token: Optional[str] = None,
) -> None
Update tokens from a refresh response.
Handles token rotation if the provider sends a new refresh token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token_encrypted
|
str
|
New encrypted access token. |
required |
refresh_token_encrypted
|
Optional[str]
|
New encrypted refresh token (if rotated). |
None
|
expires_in
|
Optional[int]
|
Token lifetime in seconds. |
None
|
id_token
|
Optional[str]
|
New ID token if provided. |
None
|
Source code in src/models/provider.py
RefreshToken Model¶
src.models.user.RefreshToken ¶
Bases: DashtamBase
Refresh token for JWT authentication with session management.
Stores refresh tokens with rotation support. Each token is hashed before storage and can be revoked for logout or security events. Each refresh token represents a user session on a specific device.
Session Management: - Users can view all active sessions (devices) - Users can revoke sessions individually or in bulk - Email alerts sent for new sessions from new devices/locations - Device fingerprinting for session hijacking detection
Attributes:
| Name | Type | Description |
|---|---|---|
user_id |
UUID
|
ID of user who owns this token. |
token_hash |
str
|
Bcrypt hash of the refresh token. |
expires_at |
datetime
|
Token expiration timestamp (30 days). |
revoked_at |
Optional[datetime]
|
Timestamp when token was revoked (logout). |
is_revoked |
bool
|
Whether token has been revoked. |
device_info |
Optional[str]
|
Information about device/browser. |
ip_address |
Optional[str]
|
IP address where token was issued. |
user_agent |
Optional[str]
|
User agent string of client. |
last_used_at |
Optional[datetime]
|
Timestamp of last token use (refresh). |
location |
Optional[str]
|
User-friendly location from IP geolocation. |
fingerprint |
Optional[str]
|
SHA256 hash of device fingerprint. |
is_trusted_device |
bool
|
User-marked trusted device flag. |
user |
User
|
User who owns this token. |
Source code in src/models/auth.py
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 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 | |
Attributes¶
Functions¶
ensure_timezone_aware
classmethod
¶
Ensure datetime fields are timezone-aware (UTC).
Source code in src/models/auth.py
ProviderAuditLog Model¶
src.models.provider.ProviderAuditLog ¶
Bases: DashtamBase
Audit log for provider operations.
Tracks all significant operations on provider connections for debugging, security, and compliance purposes. This provides a complete audit trail of all provider-related activities.
Attributes:
| Name | Type | Description |
|---|---|---|
connection_id |
UUID
|
The connection this log entry relates to. |
user_id |
UUID
|
User who performed the action. |
action |
str
|
Type of action performed. |
details |
Dict[str, Any]
|
Additional details about the action. |
ip_address |
Optional[str]
|
IP address of the request. |
user_agent |
Optional[str]
|
User agent string. |
Source code in src/models/provider.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
Functions¶
log_action
classmethod
¶
log_action(
connection_id: UUID,
user_id: UUID,
action: str,
details: Optional[Dict[str, Any]] = None,
ip_address: Optional[str] = None,
user_agent: Optional[str] = None,
) -> ProviderAuditLog
Factory method to create an audit log entry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id
|
UUID
|
The connection this action relates to. |
required |
user_id
|
UUID
|
User who performed the action. |
required |
action
|
str
|
Description of the action. |
required |
details
|
Optional[Dict[str, Any]]
|
Additional context about the action. |
None
|
ip_address
|
Optional[str]
|
Client IP address if available. |
None
|
user_agent
|
Optional[str]
|
Client user agent if available. |
None
|
Returns:
| Type | Description |
|---|---|
ProviderAuditLog
|
New audit log entry (not yet persisted). |