Skip to content

Commit

Permalink
[1.x] Add TwoFactorAuthenticationFailed event (#558)
Browse files Browse the repository at this point in the history
* Add two factor authentication failed event

* Add new line

* styleci

* styleci

* extends TwoFactorAuthenticationEvent
  • Loading branch information
antergos98 authored Jul 22, 2024
1 parent 6761f04 commit 33f8af0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Events/TwoFactorAuthenticationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Fortify\Events;

class TwoFactorAuthenticationFailed extends TwoFactorAuthenticationEvent
{
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laravel\Fortify\Contracts\TwoFactorChallengeViewResponse;
use Laravel\Fortify\Contracts\TwoFactorLoginResponse;
use Laravel\Fortify\Events\RecoveryCodeReplaced;
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
use Laravel\Fortify\Http\Requests\TwoFactorLoginRequest;

class TwoFactorAuthenticatedSessionController extends Controller
Expand Down Expand Up @@ -61,6 +62,8 @@ public function store(TwoFactorLoginRequest $request)

event(new RecoveryCodeReplaced($user, $code));
} elseif (! $request->hasValidCode()) {
event(new TwoFactorAuthenticationFailed($user));

return app(FailedTwoFactorLoginResponse::class)->toResponse($request);
}

Expand Down
5 changes: 5 additions & 0 deletions tests/AuthenticatedSessionControllerWithTwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
use Laravel\Fortify\Features;
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
use Orchestra\Testbench\Attributes\DefineEnvironment;
Expand Down Expand Up @@ -200,6 +201,8 @@ public function test_two_factor_authentication_preserves_remember_me_selection()

public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
{
Event::fake();

// Setting window to 0 should mean any old OTP is instantly invalid
Features::twoFactorAuthentication(['window' => 0]);

Expand All @@ -222,6 +225,8 @@ public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
'code' => $previousOtp,
]);

Event::assertDispatched(TwoFactorAuthenticationFailed::class);

$response->assertRedirect('/two-factor-challenge')
->assertSessionHas('login.id')
->assertSessionHasErrors(['code']);
Expand Down

0 comments on commit 33f8af0

Please sign in to comment.