Skip to content

Commit

Permalink
[PasswordHasher] Fix missing PasswordHasherAwareInterface allowed typ…
Browse files Browse the repository at this point in the history
…e in signatures
  • Loading branch information
chalasr committed Jun 11, 2021
1 parent f7063bc commit 517555f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
3 changes: 1 addition & 2 deletions Hasher/PasswordHasherFactoryInterface.php
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
6 changes: 3 additions & 3 deletions Hasher/UserPasswordHasher.php
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
6 changes: 3 additions & 3 deletions Hasher/UserPasswordHasherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*
* @author Ariel Ferrandini <[email protected]>
*
* @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
18 changes: 7 additions & 11 deletions Tests/Hasher/PasswordHasherFactoryTest.php
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

0 comments on commit 517555f

Please sign in to comment.