Skip to content

[1.x] Add TwoFactorAuthenticationFailed event #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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