diff --git a/src/Actions/DisableTwoFactorAuthentication.php b/src/Actions/DisableTwoFactorAuthentication.php index 50543d8f..11bfde89 100644 --- a/src/Actions/DisableTwoFactorAuthentication.php +++ b/src/Actions/DisableTwoFactorAuthentication.php @@ -2,6 +2,8 @@ namespace Laravel\Fortify\Actions; +use Laravel\Fortify\Events\TwoFactorAuthenticationDisabled; + class DisableTwoFactorAuthentication { /** @@ -16,5 +18,7 @@ public function __invoke($user) 'two_factor_secret' => null, 'two_factor_recovery_codes' => null, ])->save(); + + TwoFactorAuthenticationDisabled::dispatch($user); } } diff --git a/src/Actions/EnableTwoFactorAuthentication.php b/src/Actions/EnableTwoFactorAuthentication.php index e399a074..cab10ad6 100644 --- a/src/Actions/EnableTwoFactorAuthentication.php +++ b/src/Actions/EnableTwoFactorAuthentication.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Laravel\Fortify\Contracts\TwoFactorAuthenticationProvider; +use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled; use Laravel\Fortify\RecoveryCode; class EnableTwoFactorAuthentication @@ -40,5 +41,7 @@ public function __invoke($user) return RecoveryCode::generate(); })->all())), ])->save(); + + TwoFactorAuthenticationEnabled::dispatch($user); } } diff --git a/src/Actions/GenerateNewRecoveryCodes.php b/src/Actions/GenerateNewRecoveryCodes.php index 3d83b710..1b87a337 100644 --- a/src/Actions/GenerateNewRecoveryCodes.php +++ b/src/Actions/GenerateNewRecoveryCodes.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Actions; use Illuminate\Support\Collection; +use Laravel\Fortify\Events\RecoveryCodesGenerated; use Laravel\Fortify\RecoveryCode; class GenerateNewRecoveryCodes @@ -20,5 +21,7 @@ public function __invoke($user) return RecoveryCode::generate(); })->all())), ])->save(); + + RecoveryCodesGenerated::dispatch($user); } } diff --git a/src/Actions/RedirectIfTwoFactorAuthenticatable.php b/src/Actions/RedirectIfTwoFactorAuthenticatable.php index bc851821..12a7e003 100644 --- a/src/Actions/RedirectIfTwoFactorAuthenticatable.php +++ b/src/Actions/RedirectIfTwoFactorAuthenticatable.php @@ -5,6 +5,7 @@ use Illuminate\Auth\Events\Failed; use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Validation\ValidationException; +use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged; use Laravel\Fortify\Fortify; use Laravel\Fortify\LoginRateLimiter; use Laravel\Fortify\TwoFactorAuthenticatable; @@ -132,6 +133,8 @@ protected function twoFactorChallengeResponse($request, $user) 'login.remember' => $request->filled('remember'), ]); + TwoFactorAuthenticationChallenged::dispatch($user); + return $request->wantsJson() ? response()->json(['two_factor' => true]) : redirect()->route('two-factor.login'); diff --git a/src/Events/RecoveryCodesGenerated.php b/src/Events/RecoveryCodesGenerated.php new file mode 100644 index 00000000..e5c56999 --- /dev/null +++ b/src/Events/RecoveryCodesGenerated.php @@ -0,0 +1,28 @@ +user = $user; + } +} diff --git a/src/Events/TwoFactorAuthenticationChallenged.php b/src/Events/TwoFactorAuthenticationChallenged.php new file mode 100644 index 00000000..3694f565 --- /dev/null +++ b/src/Events/TwoFactorAuthenticationChallenged.php @@ -0,0 +1,8 @@ +user = $user; + } +} diff --git a/tests/AuthenticatedSessionControllerTest.php b/tests/AuthenticatedSessionControllerTest.php index 842bbc6c..60d3e21d 100644 --- a/tests/AuthenticatedSessionControllerTest.php +++ b/tests/AuthenticatedSessionControllerTest.php @@ -5,8 +5,10 @@ use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Foundation\Auth\User; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Schema; use Laravel\Fortify\Contracts\LoginViewResponse; +use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged; use Laravel\Fortify\Features; use Laravel\Fortify\FortifyServiceProvider; use Laravel\Fortify\LoginRateLimiter; @@ -48,6 +50,8 @@ public function test_user_can_authenticate() public function test_user_is_redirected_to_challenge_when_using_two_factor_authentication() { + Event::fake(); + app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class); $this->loadLaravelMigrations(['--database' => 'testbench']); @@ -69,6 +73,8 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe ]); $response->assertRedirect('/two-factor-challenge'); + + Event::assertDispatched(TwoFactorAuthenticationChallenged::class); } public function test_user_can_authenticate_when_two_factor_challenge_is_disabled() diff --git a/tests/RecoveryCodeControllerTest.php b/tests/RecoveryCodeControllerTest.php index 483c3ac4..fddc207f 100644 --- a/tests/RecoveryCodeControllerTest.php +++ b/tests/RecoveryCodeControllerTest.php @@ -3,12 +3,16 @@ namespace Laravel\Fortify\Tests; use Illuminate\Foundation\Auth\User; +use Illuminate\Support\Facades\Event; +use Laravel\Fortify\Events\RecoveryCodesGenerated; use Laravel\Fortify\FortifyServiceProvider; class RecoveryCodeControllerTest extends OrchestraTestCase { public function test_new_recovery_codes_can_be_generated() { + Event::fake(); + $this->loadLaravelMigrations(['--database' => 'testbench']); $this->artisan('migrate', ['--database' => 'testbench'])->run(); @@ -24,6 +28,8 @@ public function test_new_recovery_codes_can_be_generated() $response->assertStatus(200); + Event::assertDispatched(RecoveryCodesGenerated::class); + $user->fresh(); $this->assertNotNull($user->two_factor_recovery_codes); diff --git a/tests/TwoFactorAuthenticationControllerTest.php b/tests/TwoFactorAuthenticationControllerTest.php index 5bc20c7b..77195341 100644 --- a/tests/TwoFactorAuthenticationControllerTest.php +++ b/tests/TwoFactorAuthenticationControllerTest.php @@ -3,6 +3,9 @@ namespace Laravel\Fortify\Tests; use Illuminate\Foundation\Auth\User; +use Illuminate\Support\Facades\Event; +use Laravel\Fortify\Events\TwoFactorAuthenticationDisabled; +use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled; use Laravel\Fortify\FortifyServiceProvider; use Laravel\Fortify\TwoFactorAuthenticatable; @@ -10,6 +13,8 @@ class TwoFactorAuthenticationControllerTest extends OrchestraTestCase { public function test_two_factor_authentication_can_be_enabled() { + Event::fake(); + $this->loadLaravelMigrations(['--database' => 'testbench']); $this->artisan('migrate', ['--database' => 'testbench'])->run(); @@ -25,6 +30,8 @@ public function test_two_factor_authentication_can_be_enabled() $response->assertStatus(200); + Event::assertDispatched(TwoFactorAuthenticationEnabled::class); + $user->fresh(); $this->assertNotNull($user->two_factor_secret); @@ -35,6 +42,8 @@ public function test_two_factor_authentication_can_be_enabled() public function test_two_factor_authentication_can_be_disabled() { + Event::fake(); + $this->loadLaravelMigrations(['--database' => 'testbench']); $this->artisan('migrate', ['--database' => 'testbench'])->run(); @@ -52,6 +61,8 @@ public function test_two_factor_authentication_can_be_disabled() $response->assertStatus(200); + Event::assertDispatched(TwoFactorAuthenticationDisabled::class); + $user->fresh(); $this->assertNull($user->two_factor_secret);