Skip to content

Commit

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

* dispatch events

* added tests for events

* Style CI fixes

* rename events

* fix usages
  • Loading branch information
joelbutcher authored Dec 18, 2020
1 parent 55c625c commit 8105a1a
Show file tree
Hide file tree
Showing 9 changed files with 99 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);
}
}
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/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/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 8105a1a

Please sign in to comment.