domain.value_objects.provider_credentials¶
src.domain.value_objects.provider_credentials
¶
Authentication-agnostic encrypted credentials value object.
Immutable value object that stores encrypted credential data as an opaque blob. The domain layer has no knowledge of the credential format - infrastructure layer handles encryption/decryption based on the credential_type hint.
Reference
- docs/architecture/provider-domain-model.md
Usage
from src.domain.value_objects import ProviderCredentials from src.domain.enums import CredentialType
credentials = ProviderCredentials( encrypted_data=encrypted_blob, credential_type=CredentialType.OAUTH2, expires_at=datetime.now(UTC) + timedelta(hours=1), )
if credentials.is_expired(): # Need to refresh or re-authenticate
Classes¶
ProviderCredentials
dataclass
¶
Authentication-agnostic encrypted credentials.
Stores encrypted credential data as an opaque blob. The domain layer treats this as a black box - only the infrastructure layer understands the internal format based on credential_type.
This design supports multiple authentication mechanisms (OAuth2, API keys, link tokens, certificates) without domain layer changes.
Attributes:
| Name | Type | Description |
|---|---|---|
encrypted_data |
bytes
|
Encrypted credential blob (opaque to domain). |
credential_type |
CredentialType
|
Type hint for infrastructure to route handling. |
expires_at |
datetime | None
|
When credentials expire (None = never expires). |
Immutability
Frozen dataclass ensures credentials cannot be modified after creation. To update credentials, create a new ProviderCredentials instance.
Security
- Domain never sees raw credentials
- Encryption/decryption happens at infrastructure layer
- Credentials excluded from logging and events
Example
from datetime import UTC, datetime, timedelta creds = ProviderCredentials( ... encrypted_data=b"encrypted_oauth_tokens", ... credential_type=CredentialType.OAUTH2, ... expires_at=datetime.now(UTC) + timedelta(hours=1), ... ) creds.is_expired() False
Source code in src/domain/value_objects/provider_credentials.py
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 | |
Functions¶
__post_init__
¶
Validate credentials after initialization.
Raises:
| Type | Description |
|---|---|
ValueError
|
If encrypted_data is empty or invalid. |
Source code in src/domain/value_objects/provider_credentials.py
is_expired
¶
Check if credentials have expired.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if credentials are past expiration time. False if no expiration set or not yet expired. |
Source code in src/domain/value_objects/provider_credentials.py
is_expiring_soon
¶
Check if credentials will expire within threshold.
Useful for proactive refresh before expiration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
timedelta
|
Time window to check. Defaults to 5 minutes. |
timedelta(minutes=5)
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if credentials will expire within threshold. False if no expiration set or expiration is further out. |
Source code in src/domain/value_objects/provider_credentials.py
time_until_expiry
¶
Get time remaining until credentials expire.
Returns:
| Type | Description |
|---|---|
timedelta | None
|
timedelta | None: Time until expiration, or None if no expiration. Returns zero timedelta if already expired. |
Source code in src/domain/value_objects/provider_credentials.py
supports_refresh
¶
Check if credential type supports automatic refresh.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if credentials can be refreshed without user action. |
Source code in src/domain/value_objects/provider_credentials.py
__repr__
¶
Return repr for debugging.
Note: Does NOT include encrypted_data for security.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
String representation without sensitive data. |
Source code in src/domain/value_objects/provider_credentials.py
__str__
¶
Return string representation.
Note: Does NOT include encrypted_data for security.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Human-readable string without sensitive data. |