application.commands.handlers.authenticate_user_handler¶
src.application.commands.handlers.authenticate_user_handler
¶
Authenticate user handler.
Single responsibility: Verify user credentials. Does NOT create sessions or generate tokens (CQRS separation).
Flow: 1. Emit UserLoginAttempted event 2. Find user by email 3. Check account exists 4. Check email verified 5. Check account not locked 6. Check account active 7. Verify password 8. Reset failed login counter on success 9. Emit UserLoginSucceeded event 10. Return Success(AuthenticatedUser)
On failure: - Increment failed login counter (if password wrong) - Emit UserLoginFailed event - Return Failure(error)
Architecture: - Application layer ONLY imports from domain layer (entities, protocols, events) - NO infrastructure imports (repositories are injected via protocols) - Handler orchestrates business logic without knowing persistence details
Classes¶
AuthenticationError
¶
Authentication-specific error reasons.
Source code in src/application/commands/handlers/authenticate_user_handler.py
AuthenticateUserHandler
¶
Handler for user authentication command.
Single responsibility: Verify user credentials and return user data. Does NOT create sessions or generate tokens.
Follows hexagonal architecture: - Application layer (this handler) - Domain layer (User entity, protocols) - Infrastructure layer (repositories via dependency injection)
Source code in src/application/commands/handlers/authenticate_user_handler.py
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 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 | |
Functions¶
__init__
¶
__init__(
user_repo: UserRepository,
password_service: PasswordHashingProtocol,
event_bus: EventBusProtocol,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_repo
|
UserRepository
|
User repository for persistence. |
required |
password_service
|
PasswordHashingProtocol
|
Password hashing/verification service. |
required |
event_bus
|
EventBusProtocol
|
Event bus for publishing domain events. |
required |
Source code in src/application/commands/handlers/authenticate_user_handler.py
handle
async
¶
Handle user authentication command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
AuthenticateUser
|
AuthenticateUser command (email and password). |
required |
request
|
Request | None
|
Optional FastAPI Request for IP/user agent tracking (PCI-DSS 10.2.7). |
None
|
Returns:
| Type | Description |
|---|---|
Result[AuthenticatedUser, str]
|
Success(AuthenticatedUser) on successful authentication. |
Result[AuthenticatedUser, str]
|
Failure(error_message) on failure. |
Side Effects
- Publishes UserLoginAttempted event (always).
- Publishes UserLoginSucceeded event (on success).
- Publishes UserLoginFailed event (on failure).
- Updates User failed_login_attempts on wrong password.
Source code in src/application/commands/handlers/authenticate_user_handler.py
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 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 | |