Skip to content

Added config option for custom OTP window #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/TwoFactorAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function qrCodeUrl($companyName, $companyEmail, $secret)
*/
public function verify($secret, $code)
{
if (is_int($customWindow = config('fortify-options.two-factor-authentication.window'))) {
$this->engine->setWindow($customWindow);
}

$timestamp = $this->engine->verifyKeyNewer(
$secret, $code, optional($this->cache)->get($key = 'fortify.2fa_codes.'.md5($code))
);
Expand Down
1 change: 1 addition & 0 deletions stubs/fortify.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
// 'window' => 0,
]),
],

Expand Down
35 changes: 35 additions & 0 deletions tests/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,41 @@ public function test_two_factor_challenge_can_be_passed_via_code()
->assertSessionMissing('login.id');
}

public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
{
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);

//Setting window to 0 should mean any old OTP is instantly invalid
app('config')->set('fortify.features', [
Features::twoFactorAuthentication(['window' => 0]),
]);

$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench'])->run();

$tfaEngine = app(Google2FA::class);
$userSecret = $tfaEngine->generateSecretKey();
$currentTs = $tfaEngine->getTimestamp();
$previousOtp = $tfaEngine->oathTotp($userSecret, $currentTs - 1);

$user = TestTwoFactorAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => encrypt($userSecret),
]);

$response = $this->withSession([
'login.id' => $user->id,
'login.remember' => false,
])->withoutExceptionHandling()->post('/two-factor-challenge', [
'code' => $previousOtp,
]);

$response->assertRedirect('/two-factor-challenge')
->assertSessionHas('login.id');
}

public function test_two_factor_challenge_can_be_passed_via_recovery_code()
{
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
Expand Down