8000 Fix usage of PasswordHasherAdapter in PasswordHasherFactory · symfony/symfony@8ee821e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ee821e

Browse files
peter17pyguerder
authored andcommitted
Fix usage of PasswordHasherAdapter in PasswordHasherFactory
Using migrate_from with a PasswordEncoderInterface resulted in: Argument 1 passed to Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory::createHasher() must be of 10000 the type array, object given, called in /var/www/html/vendor/symfony/password-hasher/Hasher/PasswordHasherFactory.php on line 157 Because getHasherConfigFromAlgorithm would access it before it is decorated with the adapter.
1 parent 8df7732 commit 8ee821e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ public function getPasswordHasher($user): PasswordHasherInterface
6161
throw new \RuntimeException(sprintf('No password hasher has been configured for account "%s".', \is_object($user) ? get_debug_type($user) : $user));
6262
}
6363

64-
if (!$this->passwordHashers[$hasherKey] instanceof PasswordHasherInterface) {
65-
$this->passwordHashers[$hasherKey] = $this->passwordHashers[$hasherKey] instanceof PasswordEncoderInterface
66-
? new PasswordHasherAdapter($this->passwordHashers[$hasherKey])
67-
: $this->createHasher($this->passwordHashers[$hasherKey])
68-
;
69-
}
70-
71-
return $this->passwordHashers[$hasherKey];
64+
return $this->createHasherUsingAdapter($hasherKey);
7265
}
7366

7467
/**
@@ -111,6 +104,18 @@ private function createHasher(array $config, bool $isExtra = false): PasswordHas
111104
return new MigratingPasswordHasher($hasher, ...$extrapasswordHashers);
112105
}
113106

107+
private function createHasherUsingAdapter($hasherKey)
108+
{
109+
if (!$this->passwordHashers[$hasherKey] instanceof PasswordHasherInterface) {
110+
$this->passwordHashers[$hasherKey] = $this->passwordHashers[$hasherKey] instanceof PasswordEncoderInterface
111+
? new PasswordHasherAdapter($this->passwordHashers[$hasherKey])
112+
: $this->createHasher($this->passwordHashers[$hasherKey])
113+
;
114+
}
115+
116+
return $this->passwordHashers[$hasherKey];
117+
}
118+
114119
private function getHasherConfigFromAlgorithm(array $config): array
115120
{
116121
if ('auto' === $config['algorithm']) {
@@ -143,6 +148,8 @@ private function getHasherConfigFromAlgorithm(array $config): array
143148

144149
foreach ($frompasswordHashers as $name) {
145150
if ($hasher = $this->passwordHashers[$name] ?? false) {
151+
$hasher = $this->createHasherUsingAdapter($name);
152+
146153
$hasher = $hasher instanceof PasswordHasherInterface ? $hasher : $this->createHasher($hasher, true);
147154
} else {
148155
$hasher = $this->createHasher(['algorithm' => $name], true);

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ public function testMigrateFrom()
163163
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
164164
}
165165

166+
public function testMigrateFromLegacy()
167+
{
168+
if (!SodiumPasswordHasher::isSupported()) {
169+
$this->markTestSkipped('Sodium is not available');
170+
}
171+
172+
$factory = new PasswordHasherFactory([
173+
'plaintext_encoder' => $plaintext = new PlaintextPasswordEncoder(),
174+
SomeUser::class => ['algorithm' => 'sodium', 'migrate_from' => ['bcrypt', 'plaintext_encoder']],
175+
]);
176+
177+
$hasher = $factory->getPasswordHasher(SomeUser::class);
178+
$this->assertInstanceOf(MigratingPasswordHasher::class, $hasher);
179+
180+
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo', null), 'foo', null));
181+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo', null), 'foo', null));
182+
$this->assertTrue($hasher->verify($plaintext->encodePassword('foo', null), 'foo', null));
183+
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
184+
}
185+
166186
public function testDefaultMigratingHashers()
167187
{
168188
$this->assertInstanceOf(

0 commit comments

Comments
 (0)
0