domain.protocols.email_verification_token_repository¶
src.domain.protocols.email_verification_token_repository
¶
EmailVerificationTokenRepository protocol (port) for domain layer.
This protocol defines the interface for email verification token persistence that the domain layer needs. Infrastructure provides concrete implementations.
Following hexagonal architecture: - Domain defines what it needs (protocol/port) - Infrastructure provides implementation (adapter) - Domain has no knowledge of how tokens are stored
Reference
- docs/architecture/authentication-architecture.md (Lines 307-336)
Classes¶
EmailVerificationTokenData
dataclass
¶
Data transfer object for email verification token information.
Used by protocol methods to return token data without exposing infrastructure model classes to domain/application layers.
Source code in src/domain/protocols/email_verification_token_repository.py
EmailVerificationTokenRepository
¶
Bases: Protocol
Protocol for email verification token persistence operations.
Defines the contract that all email verification token repository implementations must satisfy. Used for email verification flow during user registration.
Token Lifecycle
- Created during registration (24-hour expiration)
- Validated during email verification
- Marked as used after successful verification
- Expired tokens cleaned up periodically
Implementations
- EmailVerificationTokenRepository (SQLAlchemy): src/infrastructure/persistence/repositories/
Source code in src/domain/protocols/email_verification_token_repository.py
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 | |
Functions¶
save
async
¶
Create new email verification token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
token
|
str
|
Random hex token (64 characters, 32 bytes). |
required |
expires_at
|
datetime
|
Token expiration timestamp (typically 24 hours from now). |
required |
Returns:
| Type | Description |
|---|---|
EmailVerificationTokenData
|
Created EmailVerificationTokenData. |
Source code in src/domain/protocols/email_verification_token_repository.py
find_by_token
async
¶
Find email verification token by token string.
Only returns tokens that have NOT been used (used_at IS NULL). Does NOT check expiration - caller must verify expires_at.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The verification token string (64-char hex). |
required |
Returns:
| Type | Description |
|---|---|
EmailVerificationTokenData | None
|
EmailVerificationTokenData if found and not used, None otherwise. |
Source code in src/domain/protocols/email_verification_token_repository.py
mark_as_used
async
¶
Mark email verification token as used.
Sets used_at to current timestamp. Ensures token cannot be reused.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
UUID
|
Token's unique identifier. |
required |
Raises:
| Type | Description |
|---|---|
NoResultFound
|
If token with given ID doesn't exist. |
Example
token = await repo.find_by_token("abc123...") await repo.mark_as_used(token.id)
Token now has used_at set, won't be returned by find_by_token¶
Source code in src/domain/protocols/email_verification_token_repository.py
delete_expired_tokens
async
¶
Delete expired email verification tokens.
Cleanup task to remove old tokens (typically run daily via cron). Deletes tokens where expires_at < current timestamp.
Returns:
| Type | Description |
|---|---|
int
|
Number of tokens deleted. |
Example
Run as daily cleanup task¶
deleted_count = await repo.delete_expired_tokens() logger.info(f"Deleted {deleted_count} expired verification tokens")
Source code in src/domain/protocols/email_verification_token_repository.py
find_by_user_id
async
¶
Find all email verification tokens for a user.
Useful for debugging or admin views. Returns all tokens (used and unused) ordered by creation date (newest first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User's unique identifier. |
required |
Returns:
| Type | Description |
|---|---|
list[EmailVerificationTokenData]
|
List of EmailVerificationTokenData (may be empty). |