Skip to content

Commit

Permalink
Move config option to Fortify feature
Browse files Browse the repository at this point in the history
  • Loading branch information
danjohnson95 committed Jul 11, 2024
1 parent 91278c5 commit d74919c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,17 @@ public static function resetUserPasswordsUsing(string $callback)
app()->singleton(ResetsUserPasswords::class, $callback);
}

/**
* Determine if Fortify is enforcing two factor authentication.
*
* @return bool
*/
public static function enforcesTwoFactorAuthentication()
{
return Features::enabled(Features::twoFactorAuthentication()) &&
Features::optionEnabled(Features::twoFactorAuthentication(), 'enforced');
}

/**
* Determine if Fortify is confirming two factor authentication configurations.
*
Expand Down
2 changes: 1 addition & 1 deletion src/TwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait TwoFactorAuthenticatable
*/
public function twoFactorAuthenticationEnforced()
{
return config('fortify.enforce_two_factor_auth');
return Fortify::enforcesTwoFactorAuthentication();
}

/**
Expand Down
1 change: 1 addition & 0 deletions stubs/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
'enforced' => false,
// 'window' => 0,
]),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
use PragmaRX\Google2FA\Google2FA;

#[WithMigration]
#[DefineEnvironment('withTwoFactorAuthentication')]
#[WithConfig('auth.providers.users.model', UserWithTwoFactor::class)]
#[WithConfig('fortify.enforce_two_factor_auth', true)]
class AuthenticatedSessionControllerWithEnforcedTwoFactorTest extends OrchestraTestCase
{
use RefreshDatabase;

#[DefineEnvironment('withConfirmedEnforcedTwoFactorAuthentication')]
public function test_user_is_redirected_to_setup_page_when_two_factor_is_enforced()
{
Event::fake();
Expand All @@ -40,6 +39,7 @@ public function test_user_is_redirected_to_setup_page_when_two_factor_is_enforce
Event::assertDispatched(TwoFactorAuthenticationSetupRequired::class);
}

#[DefineEnvironment('withConfirmedEnforcedTwoFactorAuthentication')]
public function test_json_response_contains_data_required_for_setting_up_two_factor_when_enforced()
{
Event::fake();
Expand Down Expand Up @@ -68,6 +68,7 @@ public function test_json_response_contains_data_required_for_setting_up_two_fac
]);
}

#[DefineEnvironment('withConfirmedEnforcedTwoFactorAuthentication')]
public function test_user_is_redirected_to_challenge_when_two_factor_is_already_setup()
{
Event::fake();
Expand All @@ -77,6 +78,7 @@ public function test_user_is_redirected_to_challenge_when_two_factor_is_already_
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => 'test-secret',
'two_factor_confirmed_at' => now(),
]);

$response = $this->withoutExceptionHandling()->post('/login', [
Expand All @@ -89,7 +91,7 @@ public function test_user_is_redirected_to_challenge_when_two_factor_is_already_
$response->assertRedirect('/two-factor-challenge');
}

#[DefineEnvironment('withConfirmedTwoFactorAuthentication')]
#[DefineEnvironment('withConfirmedEnforcedTwoFactorAuthentication')]
public function test_two_factor_is_confirmed_when_feature_enabled_after_successful_setup()
{
Event::fake();
Expand Down Expand Up @@ -120,7 +122,7 @@ public function test_two_factor_is_confirmed_when_feature_enabled_after_successf
$this->assertNotNull($user->two_factor_confirmed_at);
}

#[DefineEnvironment('withConfirmedTwoFactorAuthentication')]
#[DefineEnvironment('withConfirmedEnforcedTwoFactorAuthentication')]
public function test_user_is_redirected_to_home_when_two_factor_is_successfully_set_up()
{
Event::fake();
Expand Down Expand Up @@ -150,7 +152,7 @@ public function test_user_is_redirected_to_home_when_two_factor_is_successfully_
->assertSessionMissing('login.id');
}

#[DefineEnvironment('withConfirmedTwoFactorAuthentication')]
#[DefineEnvironment('withConfirmedEnforcedTwoFactorAuthentication')]
public function test_setup_fails_if_confirm_two_factor_is_enabled_and_code_is_incorrect()
{
Event::fake();
Expand Down
10 changes: 10 additions & 0 deletions tests/OrchestraTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ protected function withoutTwoFactorAuthentication($app)
$config->set('fortify.features', $features);
});
}

protected function withConfirmedEnforcedTwoFactorAuthentication($app)
{
$app['config']->set('fortify.features', [
Features::twoFactorAuthentication([
'confirm' => true,
'enforced' => true,
]),
]);
}
}

0 comments on commit d74919c

Please sign in to comment.