8000 minor #41640 [PasswordHasher] Add union types (ValentineBoineau) · symfony/symfony@e5bbf62 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5bbf62

Browse files
committed
minor #41640 [PasswordHasher] Add union types (ValentineBoineau)
This PR was merged into the 6.0 branch. Discussion ---------- [PasswordHasher] Add union types | Q | A | ------------- | --- | Branch? | 6.0 | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Extracted from #41424 Commits ------- 8628479 [PasswordHasher] Add union types
2 parents 9021b0a + 8628479 commit e5bbf62

File tree

5 files changed

+26
-78
lines changed

5 files changed

+26
-78
lines changed

src/Symfony/Component/PasswordHasher/Hasher/PasswordHasherFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
1717
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
1818
use Symfony\Component\Security\Core\Encoder\PasswordHasherAdapter;
19+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1920

2021
/**
2122
* A generic hasher factory implementation.
@@ -38,7 +39,7 @@ public function __construct(array $passwordHashers)
3839
/**
3940
* {@inheritdoc}
4041
*/
41-
public function getPasswordHasher($user): PasswordHasherInterface
42+
public function getPasswordHasher(string|PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user): PasswordHasherInterface
4243
{
4344
$hasherKey = null;
4445

src/Symfony/Component/PasswordHasher/Hasher/PasswordHasherFactoryInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ interface PasswordHasherFactoryInterface
2525
/**
2626
* Returns the password hasher to use for the given user.
2727
*
28-
* @param PasswordHasherAwareInterface|PasswordAuthenticatedUserInterface|string $user
29-
*
3028
* @throws \RuntimeException When no password hasher could be found for the user
3129
*/
32-
public function getPasswordHasher($user): PasswordHasherInterface;
30+
public function getPasswordHasher(string|PasswordAuthenticatedUserInterface|PasswordHasherAwareInterface $user): PasswordHasherInterface;
3331
}

src/Symfony/Component/PasswordHasher/Hasher/UserPasswordHasher.php

Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
1515
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
16-
use Symfony\Component\Security\Core\User\UserInterface;
1716

1817
/**
1918
* Hashes passwords based on the user and the PasswordHasherFactory.
@@ -31,43 +30,23 @@ public function __construct(PasswordHasherFactoryInterface $hasherFactory)
3130
$this->hasherFactory = $hasherFactory;
3231
}
3332

34-
/**
35-
* @param PasswordAuthenticatedUserInterface $user
36-
*/
37-
public function hashPassword($user, string $plainPassword): string
33+
public function hashPassword(PasswordAuthenticatedUserInterface $user, string $plainPassword): string
3834
{
39-
if (!$user instanceof PasswordAuthenticatedUserInterface) {
40-
if (!$user instanceof UserInterface) {
41-
throw new \TypeError(sprintf('Expected an instance of "%s" as first argument, but got "%s".', UserInterface::class, get_debug_type($user)));
42-
}
43-
trigger_deprecation('symfony/password-hasher', '5.3', 'The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.', __METHOD__, PasswordAuthenticatedUserInterface::class, get_debug_type($user));
44-
}
45-
46-
$salt = $user->getSalt();
47-
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
48-
trigger_deprecation('symfony/password-hasher', '5.3', 'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.', LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
35+
$salt = null;
36+
if ($user instanceof LegacyPasswordAuthenticatedUserInterface) {
37+
$salt = $user->getSalt();
4938
}
5039

5140
$hasher = $this->hasherFactory->getPasswordHasher($user);
5241

5342
return $hasher->hash($plainPassword, $salt);
5443
}
5544

56-
/**
57-
* @param PasswordAuthenticatedUserInterface $user
58-
*/
59-
public function isPasswordValid($user, string $plainPassword): bool
45+
public function isPasswordValid(PasswordAuthenticatedUserInterface $user, string $plainPassword): bool
6046
{
61-
if (!$user instanceof PasswordAuthenticatedUserInterface) {
62-
if (!$user instanceof UserInterface) {
63-
throw new \TypeError(sprintf('Expected an instance of "%s" as first argument, but got "%s".', UserInterface::class, get_debug_type($user)));
64-
}
65-
trigger_deprecation('symfony/password-hasher', '5.3', 'The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.', __METHOD__, PasswordAuthenticatedUserInterface::class, get_debug_type($user));
66-
}
67-
68-
$salt = $user->getSalt();
69-
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
70-
trigger_deprecation('symfony/password-hasher', '5.3', 'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.', LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
47+
$salt = null;
48+
if ($user instanceof LegacyPasswordAuthenticatedUserInterface) {
49+
$salt = $user->getSalt();
7150
}
7251

7352
if (null === $user->getPassword()) {
@@ -79,22 +58,12 @@ public function isPasswordValid($user, string $plainPassword): bool
7958
return $hasher->verify($user->getPassword(), $plainPassword, $salt);
8059
}
8160

82-
/**
83-
* @param PasswordAuthenticatedUserInterface $user
84-
*/
85-
public function needsRehash($user): bool
61+
public function needsRehash(PasswordAuthenticatedUserInterface $user): bool
8662
{
8763
if (null === $user->getPassword()) {
8864
return false;
8965
}
9066

91-
if (!$user instanceof PasswordAuthenticatedUserInterface) {
92-
if (!$user instanceof UserInterface) {
93-
throw new \TypeError(sprintf('Expected an instance of "%s" as first argument, but got "%s".', UserInterface::class, get_debug_type($user)));
94-
}
95-
trigger_deprecation('symfony/password-hasher', '5.3', 'The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.', __METHOD__, PasswordAuthenticatedUserInterface::class, get_debug_type($user));
96-
}
97-
9867
$hasher = $this->hasherFactory->getPasswordHasher($user);
9968

10069
return $hasher->needsRehash($user->getPassword());

src/Symfony/Component/PasswordHasher/Hasher/UserPasswordHasherInterface.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@
1717
* Interface for the user password hasher service.
1818
*
1919
* @author Ariel Ferrandini <arielferrandini@gmail.com>
20-
*
21-
* @method string hashPassword(PasswordAuthenticatedUserInterface $user, string $plainPassword) Hashes the plain password for the given user.
22-
* @method bool isPasswordValid(PasswordAuthenticatedUserInterface $user, string $plainPassword) Checks if the plaintext password matches the user's password.
23-
* @method bool needsRehash(PasswordAuthenticatedUserInterface $user) Checks if an encoded password would benefit from rehashing.
2420
*/
2521
interface UserPasswordHasherInterface
2622
{
23+
/**
24+
* Hashes the plain password for the given user.
25+
*/
26+
public function hashPassword(PasswordAuthenticatedUserInterface $user, string $plainPassword): string;
27+
28+
/**
29+
* Checks if the plaintext password matches the user's password.
30+
*/
31+
public function isPasswordValid(PasswordAuthenticatedUserInterface $user, string $plainPassword): bool;
32+
33+
/**
34+
* Checks if an encoded password would benefit from rehashing.
35+
*/
36+
public function needsRehash(PasswordAuthenticatedUserInterface $user): bool;
2737
}

src/Symfony/Component/PasswordHasher/Tests/Hasher/UserPasswordHasherTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,6 @@ class UserPasswordHasherTest extends TestCase
2626
{
2727
use ExpectDeprecationTrait;
2828

29-
/**
30-
* @group legacy
31-
*/
32-
public function testHashWithNonPasswordAuthenticatedUser()
33-
{
34-
$this->expectDeprecation('Since symfony/password-hasher 5.3: Returning a string from "getSalt()" without implementing the "Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface" interface is deprecated, the "%s" class should implement it.');
35-
36-
$userMock = $this->createMock('Symfony\Component\Security\Core\User\UserInterface');
37-
$userMock->expects($this->any())
38-
->method('getSalt')
39-
->willReturn('userSalt');
40-
41-
$mockHasher = $this->createMock(PasswordHasherInterface::class);
42-
$mockHasher->expects($this->any())
43-
->method('hash')
44-
->with($this->equalTo('plainPassword'), $this->equalTo('userSalt'))
45-
->willReturn('hash');
46-
47-
$mockPasswordHasherFactory = $this->createMock(PasswordHasherFactoryInterface::class);
48-
$mockPasswordHasherFactory->expects($this->any())
49-
->method('getPasswordHasher')
50-
->with($this->equalTo($userMock))
51-
->willReturn($mockHasher);
52-
53-
$passwordHasher = new UserPasswordHasher($mockPasswordHasherFactory);
54-
55-
$encoded = $passwordHasher->hashPassword($userMock, 'plainPassword');
56-
$this->assertEquals('hash', $encoded);
57-
}
58-
5929
public function testHash()
6030
{
6131
$userMock = $this->createMock(TestPasswordAuthenticatedUser::class);

0 commit comments

Comments
 (0)
0