domain.entities.security_config¶
src.domain.entities.security_config
¶
SecurityConfig domain entity for token breach rotation.
Pure business logic, no framework dependencies.
Token Breach Rotation
- global_min_token_version: Minimum acceptable token version globally
- grace_period_seconds: Time window to allow old tokens after rotation
Classes¶
SecurityConfig
dataclass
¶
Security configuration domain entity.
Singleton configuration for global token version management. Used during token validation to check minimum version requirements.
Token Validation Rule
token_version >= max(global_min_token_version, user.min_token_version)
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
Always 1 (singleton). |
global_min_token_version |
int
|
Minimum acceptable token version globally. |
grace_period_seconds |
int
|
Seconds to allow old tokens after rotation. |
last_rotation_at |
datetime | None
|
When global rotation was last triggered. |
last_rotation_reason |
str | None
|
Why global rotation was triggered. |
created_at |
datetime
|
When config was created. |
updated_at |
datetime
|
When config was last modified. |
Example
config = SecurityConfig( ... id=1, ... global_min_token_version=1, ... grace_period_seconds=300, ... last_rotation_at=None, ... last_rotation_reason=None, ... created_at=datetime.now(UTC), ... updated_at=datetime.now(UTC), ... ) config.is_within_grace_period(rotation_time) True
Source code in src/domain/entities/security_config.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 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 | |
Functions¶
is_within_grace_period
¶
Check if current time is within grace period after last rotation.
Grace period allows gradual token invalidation rather than immediate mass logout of all users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference_time
|
datetime
|
Current time to check against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if within grace period, False otherwise. Returns False if no rotation has occurred. |
Example
config.last_rotation_at = datetime.now(UTC) config.grace_period_seconds = 300 config.is_within_grace_period(datetime.now(UTC)) True # Within 5 minutes
Source code in src/domain/entities/security_config.py
should_reject_token_version
¶
should_reject_token_version(
token_version: int,
user_min_version: int,
current_time: datetime,
) -> bool
Check if a token should be rejected based on version requirements.
Implements the token validation rule with grace period support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_version
|
int
|
Version of the token being validated. |
required |
user_min_version
|
int
|
User's minimum acceptable token version. |
required |
current_time
|
datetime
|
Current time for grace period check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if token should be rejected, False if valid. |
Example
config.global_min_token_version = 2 config.should_reject_token_version( ... token_version=1, ... user_min_version=1, ... current_time=datetime.now(UTC), ... ) True # Token version 1 < global minimum 2