Skip to content

[12.x] Support rehash on login #1743

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

Closed
wants to merge 4 commits into from
Closed
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
16 changes: 15 additions & 1 deletion src/Bridge/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Passport\Bridge;

use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Facades\Auth;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use RuntimeException;
Expand All @@ -16,15 +17,24 @@ class UserRepository implements UserRepositoryInterface
*/
protected $hasher;

/**
* Indicates if passwords should be rehashed on login if needed.
*
* @var bool
*/
protected $rehashOnLogin;

/**
* Create a new repository instance.
*
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
* @param bool $rehashOnLogin
* @return void
*/
public function __construct(Hasher $hasher)
public function __construct(Hasher $hasher, bool $rehashOnLogin = true)
{
$this->hasher = $hasher;
$this->rehashOnLogin = $rehashOnLogin;
}

/**
Expand Down Expand Up @@ -64,6 +74,10 @@ public function getUserEntityByUserCredentials($username, $password, $grantType,
return;
}

if ($this->rehashOnLogin && method_exists(Auth::createUserProvider($provider), 'rehashPasswordIfRequired')) {
Auth::createUserProvider($provider)->rehashPasswordIfRequired($user, ['password' => $password]);
}

return new User($user->getAuthIdentifier());
}
}
4 changes: 3 additions & 1 deletion src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ protected function makeRefreshTokenGrant()
protected function makePasswordGrant()
{
$grant = new PasswordGrant(
$this->app->make(Bridge\UserRepository::class),
$this->app->make(Bridge\UserRepository::class, [
'rehashOnLogin' => $this->app['config']->get('hashing.rehash_on_login', true),
]),
$this->app->make(Bridge\RefreshTokenRepository::class)
);

Expand Down
75 changes: 75 additions & 0 deletions tests/Feature/AccessTokenControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\CarbonImmutable;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Foundation\Application;
use Laravel\Passport\Client;
use Laravel\Passport\Database\Factories\ClientFactory;
use Laravel\Passport\Passport;
Expand Down Expand Up @@ -274,6 +275,80 @@ public function testGettingCustomResponseType()
$this->assertArrayHasKey('id_token', $decodedResponse);
$this->assertSame('foo_bar_open_id_token', $decodedResponse['id_token']);
}

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

$this->withoutExceptionHandling();

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

Passport::enablePasswordGrant();

$password = 'foobar123';
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make($password, ['rounds' => 6]),
]);

/** @var Client $client */
$client = ClientFactory::new()->asPasswordClient()->create(['user_id' => $user->getKey()]);

$response = $this->post(
'/oauth/token',
[
'grant_type' => 'password',
'client_id' => $client->getKey(),
'client_secret' => $client->secret,
'username' => $user->email,
'password' => $password,
]
);

$response->assertOk();

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

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

$this->withoutExceptionHandling();

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

Passport::enablePasswordGrant();

$password = 'foobar123';
$user = UserFactory::new()->create([
'email' => '[email protected]',
'password' => $this->app->make(Hasher::class)->make($password, ['rounds' => 6]),
]);

/** @var Client $client */
$client = ClientFactory::new()->asPasswordClient()->create(['user_id' => $user->getKey()]);

$response = $this->post(
'/oauth/token',
[
'grant_type' => 'password',
'client_id' => $client->getKey(),
'client_secret' => $client->secret,
'username' => $user->email,
'password' => $password,
]
);

$response->assertOk();

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

class IdTokenResponse extends \League\OAuth2\Server\ResponseTypes\BearerTokenResponse
Expand Down