|
17 | 17 | use App\libs\Auth\Models\UserRecoveryCode; |
18 | 18 | use App\libs\Auth\Models\UserTrustedDevice; |
19 | 19 | use App\Services\Auth\IDeviceTrustService; |
| 20 | +use App\Services\Auth\ITwoFactorAuditService; |
20 | 21 | use Auth\AuthHelper; |
21 | 22 | use Auth\User; |
22 | 23 | use Illuminate\Support\Facades\App; |
@@ -239,6 +240,58 @@ public function testTrustedDeviceCookieBypassesMFA(): void |
239 | 240 | $this->assertTrue(Auth::check(), 'a valid trusted-device cookie must bypass MFA'); |
240 | 241 | } |
241 | 242 |
|
| 243 | + // ------------------------------------------------------------------------- |
| 244 | + // post-verify transaction boundary (Task 5: device-trust atomic, audit best-effort) |
| 245 | + // ------------------------------------------------------------------------- |
| 246 | + |
| 247 | + public function testAuditFailureDoesNotBlockLogin(): void |
| 248 | + { |
| 249 | + // Audit is best-effort: a failure emitting challenge_succeeded must NOT |
| 250 | + // 500 a user whose OTP is already redeemed and session established. |
| 251 | + $auditMock = \Mockery::mock(ITwoFactorAuditService::class); |
| 252 | + $auditMock->shouldReceive('log') |
| 253 | + ->andReturnUsing(function (User $user, string $eventType) { |
| 254 | + // Allow challenge_issued (postLogin) so the challenge is created; |
| 255 | + // blow up only on the post-success event. |
| 256 | + if ($eventType === TwoFactorAuditLog::EventChallengeSucceeded) { |
| 257 | + throw new \Exception('audit sink unavailable'); |
| 258 | + } |
| 259 | + }); |
| 260 | + $this->app->instance(ITwoFactorAuditService::class, $auditMock); |
| 261 | + |
| 262 | + $this->postLogin(self::ADMIN_EMAIL, self::SEED_PASSWORD); |
| 263 | + $code = $this->latestOtpCode(self::ADMIN_EMAIL); |
| 264 | + |
| 265 | + $response = $this->verify($code); |
| 266 | + |
| 267 | + $this->assertEquals(302, $response->getStatusCode(), 'a best-effort audit failure must not fail the login'); |
| 268 | + $this->assertTrue(Auth::check(), 'session must be established despite the audit failure'); |
| 269 | + } |
| 270 | + |
| 271 | + public function testDeviceTrustFailureDoesNotBlockLogin(): void |
| 272 | + { |
| 273 | + // Device-trust enrollment is best-effort: by the time it runs the OTP is |
| 274 | + // already redeemed and the session established, so a failure must NOT 500 |
| 275 | + // the user (which would lock them out on retry against a now-burned OTP), |
| 276 | + // and the pending MFA state must still be cleared. |
| 277 | + $deviceTrustMock = \Mockery::mock(IDeviceTrustService::class); |
| 278 | + // Gate path: no cookie -> not trusted, so the challenge is still issued. |
| 279 | + $deviceTrustMock->shouldReceive('isDeviceTrusted')->andReturn(false); |
| 280 | + // Enrollment blows up AFTER the OTP has been redeemed and the session set. |
| 281 | + $deviceTrustMock->shouldReceive('trustDevice') |
| 282 | + ->andThrow(new \Exception('trusted-device store unavailable')); |
| 283 | + $this->app->instance(IDeviceTrustService::class, $deviceTrustMock); |
| 284 | + |
| 285 | + $this->postLogin(self::ADMIN_EMAIL, self::SEED_PASSWORD); |
| 286 | + $code = $this->latestOtpCode(self::ADMIN_EMAIL); |
| 287 | + |
| 288 | + $response = $this->verify($code, true); // trust_device = true |
| 289 | + |
| 290 | + $this->assertEquals(302, $response->getStatusCode(), 'a best-effort device-trust failure must not fail the login'); |
| 291 | + $this->assertTrue(Auth::check(), 'session must be established despite the device-trust failure'); |
| 292 | + $this->assertNull(Session::get('2fa_pending_user_id'), 'pending MFA state must be cleared even when device-trust enrollment fails'); |
| 293 | + } |
| 294 | + |
242 | 295 | // ------------------------------------------------------------------------- |
243 | 296 | // recovery codes |
244 | 297 | // ------------------------------------------------------------------------- |
@@ -311,6 +364,21 @@ public function testVerifyRateLimitBlocksAfterThreshold(): void |
311 | 364 | $this->assertSame('mfa_rate_limit', $payload['error_code']); |
312 | 365 | } |
313 | 366 |
|
| 367 | + public function testRecoveryRateLimitBlocksAfterThreshold(): void |
| 368 | + { |
| 369 | + $this->postLogin(self::ADMIN_EMAIL, self::SEED_PASSWORD); |
| 370 | + |
| 371 | + $max = (int) Config::get('two_factor.rate_limit.max_attempts'); |
| 372 | + for ($i = 0; $i < $max; $i++) { |
| 373 | + $this->recovery('bad-recovery-' . $i); |
| 374 | + } |
| 375 | + |
| 376 | + $response = $this->recovery('bad-recovery-final'); |
| 377 | + $this->assertResponseStatus(429); |
| 378 | + $payload = json_decode($response->getContent(), true); |
| 379 | + $this->assertSame('mfa_rate_limit', $payload['error_code']); |
| 380 | + } |
| 381 | + |
314 | 382 | public function testResendRateLimitBlocksAfterThreshold(): void |
315 | 383 | { |
316 | 384 | $this->postLogin(self::ADMIN_EMAIL, self::SEED_PASSWORD); |
|
0 commit comments