Skip to content

Commit

Permalink
[PasswordHasher] Fix usage of PasswordHasherAdapter in PasswordHasher…
Browse files Browse the repository at this point in the history
…Factory
  • Loading branch information
peter17 authored and derrabus committed Jul 29, 2021
1 parent 61dd1e9 commit 8393c0d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
25 changes: 15 additions & 10 deletions Hasher/PasswordHasherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,7 @@ public function getPasswordHasher($user): PasswordHasherInterface
throw new \RuntimeException(sprintf('No password hasher has been configured for account "%s".', \is_object($user) ? get_debug_type($user) : $user));
}

if (!$this->passwordHashers[$hasherKey] instanceof PasswordHasherInterface) {
$this->passwordHashers[$hasherKey] = $this->passwordHashers[$hasherKey] instanceof PasswordEncoderInterface
? new PasswordHasherAdapter($this->passwordHashers[$hasherKey])
: $this->createHasher($this->passwordHashers[$hasherKey])
;
}

return $this->passwordHashers[$hasherKey];
return $this->createHasherUsingAdapter($hasherKey);
}

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

private function createHasherUsingAdapter(string $hasherKey): PasswordHasherInterface
{
if (!$this->passwordHashers[$hasherKey] instanceof PasswordHasherInterface) {
$this->passwordHashers[$hasherKey] = $this->passwordHashers[$hasherKey] instanceof PasswordEncoderInterface
? new PasswordHasherAdapter($this->passwordHashers[$hasherKey])
: $this->createHasher($this->passwordHashers[$hasherKey])
;
}

return $this->passwordHashers[$hasherKey];
}

private function getHasherConfigFromAlgorithm(array $config): array
{
if ('auto' === $config['algorithm']) {
Expand Down Expand Up @@ -142,8 +147,8 @@ private function getHasherConfigFromAlgorithm(array $config): array
$hasherChain = [$this->createHasher($config, true)];

foreach ($frompasswordHashers as $name) {
if ($hasher = $this->passwordHashers[$name] ?? false) {
$hasher = $hasher instanceof PasswordHasherInterface ? $hasher : $this->createHasher($hasher, true);
if (isset($this->passwordHashers[$name])) {
$hasher = $this->createHasherUsingAdapter($name);
} else {
$hasher = $this->createHasher(['algorithm' => $name], true);
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/Hasher/PasswordHasherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,29 @@ public function testMigrateFrom()
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
}

/**
* @group legacy
*/
public function testMigrateFromLegacy()
{
if (!SodiumPasswordHasher::isSupported()) {
$this->markTestSkipped('Sodium is not available');
}

$factory = new PasswordHasherFactory([
'plaintext_encoder' => $plaintext = new PlaintextPasswordEncoder(),
SomeUser::class => ['algorithm' => 'sodium', 'migrate_from' => ['bcrypt', 'plaintext_encoder']],
]);

$hasher = $factory->getPasswordHasher(SomeUser::class);
$this->assertInstanceOf(MigratingPasswordHasher::class, $hasher);

$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo', null), 'foo', null));
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo', null), 'foo', null));
$this->assertTrue($hasher->verify($plaintext->encodePassword('foo', null), 'foo', null));
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
}

public function testDefaultMigratingHashers()
{
$this->assertInstanceOf(
Expand Down

0 comments on commit 8393c0d

Please sign in to comment.