infrastructure.persistence.models.security_config¶
src.infrastructure.persistence.models.security_config
¶
Security configuration database model for token breach rotation.
This module defines the SecurityConfig model - a singleton table (single row) storing global security settings for token version management.
Security
- global_min_token_version: Minimum acceptable token version (incremented on breach)
- grace_period_seconds: Time to allow old tokens after rotation (gradual rollout)
- Single row constraint: Only one config row allowed (id=1)
Reference
- docs/architecture/token-breach-rotation-architecture.md
Classes¶
SecurityConfig
¶
Bases: BaseMutableModel
Security configuration model for token version management.
This is a singleton table - only one row with id=1 is allowed. Stores global security settings used during token validation.
Note
This model uses Integer ID (not UUID like other models) because the singleton pattern requires a fixed ID of 1. The id column overrides the UUID from BaseMutableModel.
Token Validation Rule
token_version >= max(global_min_token_version, user.min_token_version)
Use Cases
- Database breach: Increment global_min_token_version to invalidate ALL tokens
- Grace period: Allow old tokens for N seconds during rotation
Fields
id: Always 1 (singleton constraint, Integer NOT UUID) created_at: When config was created (from BaseMutableModel) updated_at: When config was last modified (from BaseMutableModel) global_min_token_version: Minimum acceptable token version globally grace_period_seconds: Seconds to allow old tokens after rotation last_rotation_at: When global rotation was last triggered last_rotation_reason: Why global rotation was triggered
Constraints
- single_row: CHECK (id = 1) ensures only one config row
Example
Get config (should always exist)¶
result = await session.execute( select(SecurityConfig).where(SecurityConfig.id == 1) ) config = result.scalar_one()
Trigger global rotation¶
config.global_min_token_version += 1 config.last_rotation_at = datetime.now(UTC) config.last_rotation_reason = "Database breach detected" await session.commit()
Source code in src/infrastructure/persistence/models/security_config.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 | |
Functions¶
__repr__
¶
String representation for debugging.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Human-readable representation of security config. |