Skip to content

Commit 75ea742

Browse files
committed
feature #40267 [Security] Decouple passwords from UserInterface (chalasr)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Security] Decouple passwords from UserInterface | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | #23081, helps with #39308 | License | MIT | Doc PR | todo This PR addresses a long-standing issue of the Security component: UserInterface is coupled to passwords. It does it by moving the `getPassword()` method from `UserInterface` to a `PasswordAuthenticatedUserInterface`, and the `getSalt()` method to a `LegacyPasswordAuthenticatedUserInterface`. Steps: - In 5.3, we add the new interface and, at places where password-based authentication happens, trigger deprecation notices when a `UserInterface` object does not implement the new interface(s). The UserInterface is kept as-is until 6.0. - In 6.0, we can remove the methods from `UserInterface` as well as support for using password authentication with user objects not implementing the new interface(s). As a side-effect, some password-related interfaces (`UserPasswordHasherInterface` and `PasswordUpgraderInterface`) must change their signatures to type-hint against the new interface. That is done in a BC way, which is to make the concerned methods virtual until 6.0, with deprecation notices triggered from callers and concrete implementations. Benefits: In 6.0, applications that use password-less authentication (e.g. login links) won't need to write no-op `getPassword()` and `getSalt()` in order to fulfil the `UserInterface` contract. For applications that do use password-based authentication, they will need to opt-in explicitly by implementing the relevant interface(s). This build on great discussions with @wouterj and @nicolas-grekas, and it is part of the overall rework of the Security component. Commits ------- 2764225a38 [Security] Decouple passwords from UserInterface
2 parents b91f7cc + d98d3e0 commit 75ea742

12 files changed

+130
-22
lines changed

Authentication/Provider/DaoAuthenticationProvider.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111

1212
namespace Symfony\Component\Security\Core\Authentication\Provider;
1313

14+
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
1415
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1516
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1617
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
1718
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1819
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
20+
use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
21+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1922
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
2023
use Symfony\Component\Security\Core\User\UserCheckerInterface;
2124
use Symfony\Component\Security\Core\User\UserInterface;
2225
use Symfony\Component\Security\Core\User\UserProviderInterface;
23-
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
2426

2527
/**
2628
* DaoAuthenticationProvider uses a UserProviderInterface to retrieve the user
@@ -67,11 +69,20 @@ protected function checkAuthentication(UserInterface $user, UsernamePasswordToke
6769
throw new BadCredentialsException('The presented password is invalid.');
6870
}
6971

72+
if (!$user instanceof PasswordAuthenticatedUserInterface) {
73+
trigger_deprecation('symfony/security-core', '5.3', 'Using password-based authentication listeners while not implementing "%s" interface from class "%s" is deprecated.', PasswordAuthenticatedUserInterface::class, get_debug_type($user));
74+
}
75+
76+
$salt = $user->getSalt();
77+
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
78+
trigger_deprecation('symfony/security-core', '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));
79+
}
80+
7081
// deprecated since Symfony 5.3
7182
if ($this->hasherFactory instanceof EncoderFactoryInterface) {
7283
$encoder = $this->hasherFactory->getEncoder($user);
7384

74-
if (!$encoder->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt())) {
85+
if (!$encoder->isPasswordValid($user->getPassword(), $presentedPassword, $salt)) {
7586
throw new BadCredentialsException('The presented password is invalid.');
7687
}
7788

@@ -84,12 +95,12 @@ protected function checkAuthentication(UserInterface $user, UsernamePasswordToke
8495

8596
$hasher = $this->hasherFactory->getPasswordHasher($user);
8697

87-
if (!$hasher->verify($user->getPassword(), $presentedPassword, $user->getSalt())) {
98+
if (!$hasher->verify($user->getPassword(), $presentedPassword, $salt)) {
8899
throw new BadCredentialsException('The presented password is invalid.');
89100
}
90101

91102
if ($this->userProvider instanceof PasswordUpgraderInterface && $hasher->needsRehash($user->getPassword())) {
92-
$this->userProvider->upgradePassword($user, $hasher->hash($presentedPassword, $user->getSalt()));
103+
$this->userProvider->upgradePassword($user, $hasher->hash($presentedPassword, $salt));
93104
}
94105
}
95106
}

Authentication/Token/AbstractToken.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Authentication\Token;
1313

1414
use Symfony\Component\Security\Core\User\EquatableInterface;
15+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1516
use Symfony\Component\Security\Core\User\UserInterface;
1617

1718
/**
@@ -262,10 +263,12 @@ private function hasUserChanged(UserInterface $user): bool
262263
return !(bool) $this->user->isEqualTo($user);
263264
}
264265

266+
// @deprecated since Symfony 5.3, check for PasswordAuthenticatedUserInterface on both user objects before comparing passwords
265267
if ($this->user->getPassword() !== $user->getPassword()) {
266268
return true;
267269
}
268270

271+
// @deprecated since Symfony 5.3, check for LegacyPasswordAuthenticatedUserInterface on both user objects before comparing salts
269272
if ($this->user->getSalt() !== $user->getSalt()) {
270273
return true;
271274
}

Encoder/UserPasswordEncoder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Security\Core\Encoder;
1313

1414
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
15+
use Symfony\Component\Security\Core\User\LegacyPasswordAuthenticatedUserInterface;
16+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1517
use Symfony\Component\Security\Core\User\UserInterface;
1618

1719
trigger_deprecation('symfony/security-core', '5.3', sprintf('The "%s" class is deprecated, use "%s" instead.', UserPasswordEncoder::class, UserPasswordHasher::class));
@@ -39,6 +41,15 @@ public function encodePassword(UserInterface $user, string $plainPassword)
3941
{
4042
$encoder = $this->encoderFactory->getEncoder($user);
4143

44+
if (!$user instanceof PasswordAuthenticatedUserInterface) {
45+
trigger_deprecation('symfony/password-hasher', '5.3', 'Not implementing the "%s" interface while using "%s" is deprecated, the "%s" class should implement it.', PasswordAuthenticatedUserInterface::class, __CLASS__, get_debug_type($user));
46+
}
47+
48+
$salt = $user->getSalt();
49+
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
50+
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));
51+
}
52+
4253
return $encoder->encodePassword($plainPassword, $user->getSalt());
4354
}
4455

Tests/Authentication/Provider/DaoAuthenticationProviderTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
16+
use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
17+
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
1518
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
1619
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1720
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
@@ -23,9 +26,6 @@
2326
use Symfony\Component\Security\Core\User\UserCheckerInterface;
2427
use Symfony\Component\Security\Core\User\UserInterface;
2528
use Symfony\Component\Security\Core\User\UserProviderInterface;
26-
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
27-
use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
28-
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
2929

3030
class DaoAuthenticationProviderTest extends TestCase
3131
{
@@ -380,4 +380,5 @@ public function eraseCredentials()
380380
}
381381
interface PasswordUpgraderProvider extends UserProviderInterface, PasswordUpgraderInterface
382382
{
383+
public function upgradePassword(UserInterface $user, string $newHashedPassword): void;
383384
}

Tests/User/ChainUserProviderTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
1616
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1717
use Symfony\Component\Security\Core\User\ChainUserProvider;
18+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1819
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
1920
use Symfony\Component\Security\Core\User\User;
2021
use Symfony\Component\Security\Core\User\UserInterface;
@@ -251,14 +252,14 @@ public function testPasswordUpgrades()
251252
{
252253
$user = new User('user', 'pwd');
253254

254-
$provider1 = $this->createMock(PasswordUpgraderInterface::class);
255+
$provider1 = $this->getMockForAbstractClass(MigratingProvider::class);
255256
$provider1
256257
->expects($this->once())
257258
->method('upgradePassword')
258259
->willThrowException(new UnsupportedUserException('unsupported'))
259260
;
260261

261-
$provider2 = $this->createMock(PasswordUpgraderInterface::class);
262+
$provider2 = $this->getMockForAbstractClass(MigratingProvider::class);
262263
$provider2
263264
->expects($this->once())
264265
->method('upgradePassword')
@@ -269,3 +270,8 @@ public function testPasswordUpgrades()
269270
$provider->upgradePassword($user, 'foobar');
270271
}
271272
}
273+
274+
abstract class MigratingProvider implements PasswordUpgraderInterface
275+
{
276+
abstract public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void;
277+
}

User/ChainUserProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function refreshUser(UserInterface $user)
7373

7474
foreach ($this->providers as $provider) {
7575
try {
76-
if (!$provider->supportsClass(\get_class($user))) {
76+
if (!$provider->supportsClass(get_debug_type($user))) {
7777
continue;
7878
}
7979

@@ -110,10 +110,16 @@ public function supportsClass(string $class)
110110
}
111111

112112
/**
113+
* @param PasswordAuthenticatedUserInterface $user
114+
*
113115
* {@inheritdoc}
114116
*/
115-
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
117+
public function upgradePassword($user, string $newEncodedPassword): void
116118
{
119+
if (!$user instanceof PasswordAuthenticatedUserInterface) {
120+
trigger_deprecation('symfony/security-core', '5.3', 'The "%s::upgradePassword()" method expects an instance of "%s" as first argument, the "%s" class should implement it.', PasswordUpgraderInterface::class, PasswordAuthenticatedUserInterface::class, get_debug_type($user));
121+
}
122+
117123
foreach ($this->providers as $provider) {
118124
if ($provider instanceof PasswordUpgraderInterface) {
119125
try {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\User;
13+
14+
/**
15+
* For users that can be authenticated using a password/salt couple.
16+
*
17+
* Once all password hashes have been upgraded to a modern algorithm via password migrations,
18+
* implement {@see PasswordAuthenticatedUserInterface} instead.
19+
*
20+
* @author Robin Chalas <[email protected]>
21+
*/
22+
interface LegacyPasswordAuthenticatedUserInterface extends PasswordAuthenticatedUserInterface
23+
{
24+
/**
25+
* Returns the salt that was originally used to hash the password.
26+
*/
27+
public function getSalt(): ?string;
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\User;
13+
14+
/**
15+
* For users that can be authenticated using a password.
16+
*
17+
* @author Robin Chalas <[email protected]>
18+
* @author Wouter de Jong <[email protected]>
19+
*/
20+
interface PasswordAuthenticatedUserInterface
21+
{
22+
/**
23+
* Returns the hashed password used to authenticate the user.
24+
*
25+
* Usually on authentication, a plain-text password will be compared to this value.
26+
*
27+
* @return string|null The hashed password or null (if not set or erased)
28+
*/
29+
public function getPassword(): ?string;
30+
}

User/PasswordUpgraderInterface.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313

1414
/**
1515
* @author Nicolas Grekas <[email protected]>
16+
*
17+
* @method void upgradePassword(PasswordAuthenticatedUserInterface|UserInterface $user, string $newHashedPassword) Upgrades the hashed password of a user, typically for using a better hash algorithm.
18+
* This method should persist the new password in the user storage and update the $user object accordingly.
19+
* Because you don't want your users not being able to log in, this method should be opportunistic:
20+
* it's fine if it does nothing or if it fails without throwing any exception.
1621
*/
1722
interface PasswordUpgraderInterface
1823
{
19-
/**
20-
* Upgrades the hashed password of a user, typically for using a better hash algorithm.
21-
*
22-
* This method should persist the new password in the user storage and update the $user object accordingly.
23-
* Because you don't want your users not being able to log in, this method should be opportunistic:
24-
* it's fine if it does nothing or if it fails without throwing any exception.
25-
*/
26-
public function upgradePassword(UserInterface $user, string $newHashedPassword): void;
2724
}

User/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Fabien Potencier <[email protected]>
2020
*/
21-
final class User implements UserInterface, EquatableInterface
21+
final class User implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface
2222
{
2323
private $username;
2424
private $password;

0 commit comments

Comments
 (0)