Skip to content

Commit

Permalink
[1.x] Add 2fa Events (#338)
Browse files Browse the repository at this point in the history
* [2.x] Events for Two Factor Authentication (#162)

* added events

* dispatch events

* added tests for events

* Style CI fixes

* rename events

* fix usages

(cherry picked from commit 8105a1a)

* [2.x] Dispatch Event when Two Factor Challenged (#239)

* dispatch event when two factor challenged and test

* use statements styleci

(cherry picked from commit b490728)

Co-authored-by: danclaytondev <daniel@cl8on.co.uk>
  • Loading branch information
joelbutcher and danclaytondev authored Jan 7, 2022
1 parent fb2b788 commit a717da4
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Actions/DisableTwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Laravel\Fortify\Actions;

use Laravel\Fortify\Events\TwoFactorAuthenticationDisabled;

class DisableTwoFactorAuthentication
{
/**
Expand All @@ -16,5 +18,7 @@ public function __invoke($user)
'two_factor_secret' => null,
'two_factor_recovery_codes' => null,
])->save();

TwoFactorAuthenticationDisabled::dispatch($user);
}
}
3 changes: 3 additions & 0 deletions src/Actions/EnableTwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Laravel\Fortify\Contracts\TwoFactorAuthenticationProvider;
use Laravel\Fortify\Events\TwoFactorAuthenticationEnabled;
use Laravel\Fortify\RecoveryCode;

class EnableTwoFactorAuthentication
Expand Down Expand Up @@ -40,5 +41,7 @@ public function __invoke($user)
return RecoveryCode::generate();
})->all())),
])->save();

TwoFactorAuthenticationEnabled::dispatch($user);
}
}
3 changes: 3 additions & 0 deletions src/Actions/GenerateNewRecoveryCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Fortify\Actions;

use Illuminate\Support\Collection;
use Laravel\Fortify\Events\RecoveryCodesGenerated;
use Laravel\Fortify\RecoveryCode;

class GenerateNewRecoveryCodes
Expand All @@ -20,5 +21,7 @@ public function __invoke($user)
return RecoveryCode::generate();
})->all())),
])->save();

RecoveryCodesGenerated::dispatch($user);
}
}
3 changes: 3 additions & 0 deletions src/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
28 changes: 28 additions & 0 deletions src/Events/RecoveryCodesGenerated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Fortify\Events;

use Illuminate\Foundation\Events\Dispatchable;

class RecoveryCodesGenerated
{
use Dispatchable;

/**
* The user instance.
*
* @var \App\Models\User
*/
public $user;

/**
* Create a new event instance.
*
* @param \App\Models\User $user
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
}
8 changes: 8 additions & 0 deletions src/Events/TwoFactorAuthenticationChallenged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Fortify\Events;

class TwoFactorAuthenticationChallenged extends TwoFactorAuthenticationEvent
{
//
}
8 changes: 8 additions & 0 deletions src/Events/TwoFactorAuthenticationDisabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Fortify\Events;

class TwoFactorAuthenticationDisabled extends TwoFactorAuthenticationEvent
{
//
}
8 changes: 8 additions & 0 deletions src/Events/TwoFactorAuthenticationEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Fortify\Events;

class TwoFactorAuthenticationEnabled extends TwoFactorAuthenticationEvent
{
//
}
28 changes: 28 additions & 0 deletions src/Events/TwoFactorAuthenticationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Fortify\Events;

use Illuminate\Foundation\Events\Dispatchable;

abstract class TwoFactorAuthenticationEvent
{
use Dispatchable;

/**
* The user instance.
*
* @var \App\Models\User
*/
public $user;

/**
* Create a new event instance.
*
* @param \App\Models\User $user
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
}
6 changes: 6 additions & 0 deletions tests/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions tests/RecoveryCodeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions tests/TwoFactorAuthenticationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
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;

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();

Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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);
Expand Down

0 comments on commit a717da4

Please sign in to comment.