Skip to content
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

[1.x] Rehash password if required when user uses two factor #557

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/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ protected function validateCredentials($request)

$this->throwFailedAuthenticationException($request);
}

if (config('hashing.rehash_on_login', true) && method_exists($this->guard->getProvider(), 'rehashPasswordIfRequired')) {
$this->guard->getProvider()->rehashPasswordIfRequired($user, ['password' => $request->password]);
}
});
}

Expand Down
53 changes: 53 additions & 0 deletions tests/AuthenticatedSessionControllerWithTwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Laravel\Fortify\Tests;

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Features;
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
Expand Down Expand Up @@ -100,6 +102,57 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled
$response->assertRedirect('/home');
}

public function test_rehash_user_password_when_redirecting_to_two_factor_challenge_if_rehashing_on_login_is_enabled()
{
if (version_compare(Application::VERSION, '11.0.0', '<')) {
$this->markTestSkipped('Only on Laravel 11 and later');
}

$this->app['config']->set('hashing.rehash_on_login', true);

$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => Hash::make('secret', ['rounds' => 6]),
'two_factor_secret' => 'test-secret',
]);

$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);

$response->assertRedirect('/two-factor-challenge');

$this->assertNotSame($user->password, $user->fresh()->password);
$this->assertTrue(Hash::check('secret', $user->fresh()->password));
}

public function test_does_not_rehash_user_password_when_redirecting_to_two_factor_challenge_if_rehashing_on_login_is_disabled()
{
if (version_compare(Application::VERSION, '11.0.0', '<')) {
$this->markTestSkipped('Only on Laravel 11 and later');
}

$this->app['config']->set('hashing.rehash_on_login', false);

$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => Hash::make('secret', ['rounds' => 6]),
'two_factor_secret' => 'test-secret',
]);

$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);

$response->assertRedirect('/two-factor-challenge');

$this->assertSame($user->password, $user->fresh()->password);
}

public function test_two_factor_challenge_can_be_passed_via_code()
{
$tfaEngine = app(Google2FA::class);
Expand Down