Skip to content

Commit ec9159e

Browse files
committed
feature #31597 [Security] add MigratingPasswordEncoder (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Security] add MigratingPasswordEncoder | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Split from #31153: the proposed `MigratingPasswordEncoder` is able to validate password using a chain of encoders, and encodes new them using the best-provided algorithm. This chained encoder is used when the "auto" algorithm is configured. This is seamless for 4.3 app. Commits ------- 765f14c [Security] add MigratingPasswordEncoder
2 parents 8d359b2 + 765f14c commit ec9159e

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added method `needsRehash()` to `PasswordEncoderInterface` and `UserPasswordEncoderInterface`
8+
* Added `MigratingPasswordEncoder`
89

910
4.3.0
1011
-----

src/Symfony/Component/Security/Core/Encoder/EncoderFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,17 @@ private function createEncoder(array $config)
8585
private function getEncoderConfigFromAlgorithm($config)
8686
{
8787
if ('auto' === $config['algorithm']) {
88-
$config['algorithm'] = SodiumPasswordEncoder::isSupported() ? 'sodium' : 'native';
88+
$encoderChain = [];
89+
// "plaintext" is not listed as any leaked hashes could then be used to authenticate directly
90+
foreach ([SodiumPasswordEncoder::isSupported() ? 'sodium' : 'native', 'pbkdf2', $config['hash_algorithm']] as $algo) {
91+
$config['algorithm'] = $algo;
92+
$encoderChain[] = $this->createEncoder($config);
93+
}
94+
95+
return [
96+
'class' => MigratingPasswordEncoder::class,
97+
'arguments' => $encoderChain,
98+
];
8999
}
90100

91101
switch ($config['algorithm']) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Encoder;
13+
14+
/**
15+
* Hashes passwords using the best available encoder.
16+
* Validates them using a chain of encoders.
17+
*
18+
* /!\ Don't put a PlaintextPasswordEncoder in the list as that'd mean a leaked hash
19+
* could be used to authenticate successfully without knowing the cleartext password.
20+
*
21+
* @author Nicolas Grekas <p@tchwork.com>
22+
*/
23+
final class MigratingPasswordEncoder extends BasePasswordEncoder implements SelfSaltingEncoderInterface
24+
{
25+
private $bestEncoder;
26+
private $extraEncoders;
27+
28+
public function __construct(PasswordEncoderInterface $bestEncoder, PasswordEncoderInterface ...$extraEncoders)
29+
{
30+
$this->bestEncoder = $bestEncoder;
31+
$this->extraEncoders = $extraEncoders;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function encodePassword($raw, $salt)
38+
{
39+
return $this->bestEncoder->encodePassword($raw, $salt);
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function isPasswordValid($encoded, $raw, $salt)
46+
{
47+
if ($this->bestEncoder->isPasswordValid($encoded, $raw, $salt)) {
48+
return true;
49+
}
50+
51+
if (!$this->bestEncoder->needsRehash($encoded)) {
52+
return false;
53+
}
54+
55+
foreach ($this->extraEncoders as $encoder) {
56+
if ($encoder->isPasswordValid($encoded, $raw, $salt)) {
57+
return true;
58+
}
59+
}
60+
61+
return false;
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function needsRehash(string $encoded): bool
68+
{
69+
return $this->bestEncoder->needsRehash($encoded);
70+
}
71+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Encoder;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Encoder\MigratingPasswordEncoder;
16+
use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder;
17+
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
18+
19+
class MigratingPasswordEncoderTest extends TestCase
20+
{
21+
public function testValidation()
22+
{
23+
$bestEncoder = new NativePasswordEncoder(4, 12000, 4);
24+
25+
$extraEncoder = $this->getMockBuilder(TestPasswordEncoderInterface::class)->getMock();
26+
$extraEncoder->expects($this->never())->method('encodePassword');
27+
$extraEncoder->expects($this->never())->method('isPasswordValid');
28+
$extraEncoder->expects($this->never())->method('needsRehash');
29+
30+
$encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder);
31+
32+
$this->assertTrue($encoder->needsRehash('foo'));
33+
34+
$hash = $encoder->encodePassword('foo', 'salt');
35+
$this->assertFalse($encoder->needsRehash($hash));
36+
37+
$this->assertTrue($encoder->isPasswordValid($hash, 'foo', 'salt'));
38+
$this->assertFalse($encoder->isPasswordValid($hash, 'bar', 'salt'));
39+
}
40+
41+
public function testFallback()
42+
{
43+
$bestEncoder = new NativePasswordEncoder(4, 12000, 4);
44+
45+
$extraEncoder1 = $this->getMockBuilder(TestPasswordEncoderInterface::class)->getMock();
46+
$extraEncoder1->expects($this->any())
47+
->method('isPasswordValid')
48+
->with('abc', 'foo', 'salt')
49+
->willReturn(true);
50+
51+
$encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder1);
52+
53+
$this->assertTrue($encoder->isPasswordValid('abc', 'foo', 'salt'));
54+
55+
$extraEncoder2 = $this->getMockBuilder(TestPasswordEncoderInterface::class)->getMock();
56+
$extraEncoder2->expects($this->any())
57+
->method('isPasswordValid')
58+
->willReturn(false);
59+
60+
$encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder2);
61+
62+
$this->assertFalse($encoder->isPasswordValid('abc', 'foo', 'salt'));
63+
64+
$encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder2, $extraEncoder1);
65+
66+
$this->assertTrue($encoder->isPasswordValid('abc', 'foo', 'salt'));
67+
}
68+
}
69+
70+
interface TestPasswordEncoderInterface extends PasswordEncoderInterface
71+
{
72+
public function needsRehash(string $encoded): bool;
73+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy