Skip to content

Fire ValidTwoFactorAuthenticationCodeProvided Event when 2FA session is authenticated #559

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 3 commits into from
Aug 1, 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/ValidTwoFactorAuthenticationCodeProvided.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Fortify\Events;

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

class TwoFactorAuthenticatedSessionController extends Controller
Expand Down Expand Up @@ -67,6 +68,8 @@ public function store(TwoFactorLoginRequest $request)
return app(FailedTwoFactorLoginResponse::class)->toResponse($request);
}

event(new ValidTwoFactorAuthenticationCodeProvided($user));

$this->guard->login($user, $request->remember());

$request->session()->regenerate();
Expand Down
9 changes: 9 additions & 0 deletions tests/AuthenticatedSessionControllerWithTwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
use Laravel\Fortify\Events\ValidTwoFactorAuthenticationCodeProvided;
use Laravel\Fortify\Features;
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
use Orchestra\Testbench\Attributes\DefineEnvironment;
Expand Down Expand Up @@ -156,6 +157,8 @@ public function test_does_not_rehash_user_password_when_redirecting_to_two_facto

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

$tfaEngine = app(Google2FA::class);
$userSecret = $tfaEngine->generateSecretKey();
$validOtp = $tfaEngine->getCurrentOtp($userSecret);
Expand All @@ -174,6 +177,8 @@ public function test_two_factor_challenge_can_be_passed_via_code()
'code' => $validOtp,
]);

Event::assertDispatched(ValidTwoFactorAuthenticationCodeProvided::class);

$response->assertRedirect('/home')
->assertSessionMissing('login.id');
}
Expand Down Expand Up @@ -234,6 +239,8 @@ public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()

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

$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
Expand All @@ -248,6 +255,8 @@ public function test_two_factor_challenge_can_be_passed_via_recovery_code()
'recovery_code' => 'valid-code',
]);

Event::assertDispatched(ValidTwoFactorAuthenticationCodeProvided::class);

$response->assertRedirect('/home')
->assertSessionMissing('login.id');
$this->assertNotNull(Auth::getUser());
Expand Down