Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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