Skip to content

Commit 983daf0

Browse files
Fire ValidTwoFactorAuthenticationCodeProvided Event when 2FA session is authenticated (#559)
* Add TwoFactorAuthenticationVerified * Update Tests * Rename Event
1 parent 57c28ff commit 983daf0

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Laravel\Fortify\Events;
4+
5+
class ValidTwoFactorAuthenticationCodeProvided extends TwoFactorAuthenticationEvent
6+
{
7+
//
8+
}

src/Http/Controllers/TwoFactorAuthenticatedSessionController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Laravel\Fortify\Contracts\TwoFactorLoginResponse;
1111
use Laravel\Fortify\Events\RecoveryCodeReplaced;
1212
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
13+
use Laravel\Fortify\Events\ValidTwoFactorAuthenticationCodeProvided;
1314
use Laravel\Fortify\Http\Requests\TwoFactorLoginRequest;
1415

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

71+
event(new ValidTwoFactorAuthenticationCodeProvided($user));
72+
7073
$this->guard->login($user, $request->remember());
7174

7275
$request->session()->regenerate();

tests/AuthenticatedSessionControllerWithTwoFactorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Facades\Hash;
1010
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
1111
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
12+
use Laravel\Fortify\Events\ValidTwoFactorAuthenticationCodeProvided;
1213
use Laravel\Fortify\Features;
1314
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
1415
use Orchestra\Testbench\Attributes\DefineEnvironment;
@@ -156,6 +157,8 @@ public function test_does_not_rehash_user_password_when_redirecting_to_two_facto
156157

157158
public function test_two_factor_challenge_can_be_passed_via_code()
158159
{
160+
Event::fake();
161+
159162
$tfaEngine = app(Google2FA::class);
160163
$userSecret = $tfaEngine->generateSecretKey();
161164
$validOtp = $tfaEngine->getCurrentOtp($userSecret);
@@ -174,6 +177,8 @@ public function test_two_factor_challenge_can_be_passed_via_code()
174177
'code' => $validOtp,
175178
]);
176179

180+
Event::assertDispatched(ValidTwoFactorAuthenticationCodeProvided::class);
181+
177182
$response->assertRedirect('/home')
178183
->assertSessionMissing('login.id');
179184
}
@@ -234,6 +239,8 @@ public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
234239

235240
public function test_two_factor_challenge_can_be_passed_via_recovery_code()
236241
{
242+
Event::fake();
243+
237244
$user = UserWithTwoFactor::forceCreate([
238245
'name' => 'Taylor Otwell',
239246
'email' => '[email protected]',
@@ -248,6 +255,8 @@ public function test_two_factor_challenge_can_be_passed_via_recovery_code()
248255
'recovery_code' => 'valid-code',
249256
]);
250257

258+
Event::assertDispatched(ValidTwoFactorAuthenticationCodeProvided::class);
259+
251260
$response->assertRedirect('/home')
252261
->assertSessionMissing('login.id');
253262
$this->assertNotNull(Auth::getUser());

0 commit comments

Comments
 (0)