Skip to content

[PasswordHasher] Fix missing PasswordHasherAwareInterface allowed type #41678

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 1 commit into from
Jun 11, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* PasswordHasherFactoryInterface to support different password hashers for different user accounts.
Expand All @@ -26,7 +25,7 @@ interface PasswordHasherFactoryInterface
/**
* Returns the password hasher to use for the given user.
*
* @param PasswordAuthenticatedUserInterface|UserInterface|string $user A PasswordAuthenticatedUserInterface/UserInterface instance or a class name
* @param PasswordHasherAwareInterface|PasswordAuthenticatedUserInterface|string $user
*
* @throws \RuntimeException When no password hasher could be found for the user
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(PasswordHasherFactoryInterface $hasherFactory)
}

/**
* @param PasswordAuthenticatedUserInterface $user
* @param PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user
*/
public function hashPassword($user, string $plainPassword): string
{
Expand All @@ -54,7 +54,7 @@ public function hashPassword($user, string $plainPassword): string
}

/**
* @param PasswordAuthenticatedUserInterface $user
* @param PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user
*/
public function isPasswordValid($user, string $plainPassword): bool
{
Expand All @@ -80,7 +80,7 @@ public function isPasswordValid($user, string $plainPassword): bool
}

/**
* @param PasswordAuthenticatedUserInterface $user
* @param PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user
*/
public function needsRehash($user): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*
* @author Ariel Ferrandini <arielferrandini@gmail.com>
*
* @method string hashPassword(PasswordAuthenticatedUserInterface $user, string $plainPassword) Hashes the plain password for the given user.
* @method bool isPasswordValid(PasswordAuthenticatedUserInterface $user, string $plainPassword) Checks if the plaintext password matches the user's password.
* @method bool needsRehash(PasswordAuthenticatedUserInterface $user) Checks if an encoded password would benefit from rehashing.
* @method string hashPassword(PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user, string $plainPassword) Hashes the plain password for the given user.
* @method bool isPasswordValid(PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user, string $plainPassword) Checks if the plaintext password matches the user's password.
* @method bool needsRehash(PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user) Checks if an encoded password would benefit from rehashing.
*/
interface UserPasswordHasherInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

class PasswordHasherFactoryTest extends TestCase
{
public function testGetHasherWithMessageDigestHasher()
{
$factory = new PasswordHasherFactory([UserInterface::class => [
$factory = new PasswordHasherFactory([PasswordAuthenticatedUserInterface::class => [
'class' => MessageDigestPasswordHasher::class,
'arguments' => ['sha512', true, 5],
]]);

$hasher = $factory->getPasswordHasher($this->createMock(UserInterface::class));
$hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
$expectedHasher = new MessageDigestPasswordHasher('sha512', true, 5);

$this->assertEquals($expectedHasher->hash('foo', 'moo'), $hasher->hash('foo', 'moo'));
Expand All @@ -41,22 +41,18 @@ public function testGetHasherWithMessageDigestHasher()
public function testGetHasherWithService()
{
$factory = new PasswordHasherFactory([
UserInterface::class => new MessageDigestPasswordHasher('sha1'),
PasswordAuthenticatedUserInterface::class => new MessageDigestPasswordHasher('sha1'),
]);

$hasher = $factory->getPasswordHasher($this->createMock(UserInterface::class));
$expectedHasher = new MessageDigestPasswordHasher('sha1');
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));

$hasher = $factory->getPasswordHasher(new InMemoryUser('user', 'pass'));
$hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
$expectedHasher = new MessageDigestPasswordHasher('sha1');
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));
}

public function testGetHasherWithClassName()
{
$factory = new PasswordHasherFactory([
UserInterface::class => new MessageDigestPasswordHasher('sha1'),
PasswordAuthenticatedUserInterface::class => new MessageDigestPasswordHasher('sha1'),
]);

$hasher = $factory->getPasswordHasher(SomeChildUser::class);
Expand Down Expand Up @@ -208,7 +204,7 @@ public function testLegacyEncoderClass()
}
}

class SomeUser implements UserInterface
class SomeUser implements PasswordAuthenticatedUserInterface
{
public function getRoles(): array
{
Expand Down