Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[PasswordHasher][SecurityBundle] Fix password migration with custom hasher service with security bundle config #51686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -848,7 +848,10 @@ private function createHasher(array $config)
{
// a custom hasher service
if (isset($config['id'])) {
return new Reference($config['id']);
return $config['migrate_from'] ?? false ? [
'instance' => new Reference($config['id']),
'migrate_from' => $config['migrate_from'],
] : new Reference($config['id']);
}

if ($config['migrate_from'] ?? false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,33 @@ public function testLegacyAuthorizationManagerSignature()
$this->assertEquals('%security.access.always_authenticate_before_granting%', (string) $args[3]);
}

public function testCustomHasherWithMigrateFrom()
{
$container = $this->getRawContainer();

$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'password_hashers' => [
'legacy' => 'md5',
'App\User' => [
'id' => 'App\Security\CustomHasher',
'migrate_from' => 'legacy',
],
],
'firewalls' => ['main' => ['http_basic' => true]],
]);

$container->compile();

$hashersMap = $container->getDefinition('security.password_hasher_factory')->getArgument(0);

$this->assertArrayHasKey('App\User', $hashersMap);
$this->assertEquals($hashersMap['App\User'], [
'instance' => new Reference('App\Security\CustomHasher'),
'migrate_from' => ['legacy'],
]);
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function getPasswordHasher($user): PasswordHasherInterface
*/
private function createHasher(array $config, bool $isExtra = false): PasswordHasherInterface
{
if (isset($config['instance'])) {
if (!isset($config['migrate_from'])) {
return $config['instance'];
}

$config = $this->getMigratingPasswordConfig($config);
}

if (isset($config['algorithm'])) {
$rawConfig = $config;
$config = $this->getHasherConfigFromAlgorithm($config);
Expand Down Expand Up @@ -142,24 +150,8 @@ private function getHasherConfigFromAlgorithm(array $config): array
];
}

if ($frompasswordHashers = ($config['migrate_from'] ?? false)) {
unset($config['migrate_from']);
$hasherChain = [$this->createHasher($config, true)];

foreach ($frompasswordHashers as $name) {
if (isset($this->passwordHashers[$name])) {
$hasher = $this->createHasherUsingAdapter($name);
} else {
$hasher = $this->createHasher(['algorithm' => $name], true);
}

$hasherChain[] = $hasher;
}

return [
'class' => MigratingPasswordHasher::class,
'arguments' => $hasherChain,
];
if ($config['migrate_from'] ?? false) {
return $this->getMigratingPasswordConfig($config);
}

switch ($config['algorithm']) {
Expand Down Expand Up @@ -239,4 +231,26 @@ private function getHasherConfigFromAlgorithm(array $config): array
],
];
}

private function getMigratingPasswordConfig(array $config): array
{
$frompasswordHashers = $config['migrate_from'];
unset($config['migrate_from']);
$hasherChain = [$this->createHasher($config, true)];

foreach ($frompasswordHashers as $name) {
if ($this->passwordHashers[$name] ?? false) {
$hasher = $this->createHasherUsingAdapter($name);
} else {
$hasher = $this->createHasher(['algorithm' => $name], true);
}

$hasherChain[] = $hasher;
}

return [
'class' => MigratingPasswordHasher::class,
'arguments' => $hasherChain,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public function testGetHasherWithService()
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));
}

public function testGetHasherWithInstance()
{
$factory = new PasswordHasherFactory([
PasswordAuthenticatedUserInterface::class => ['instance' => new MessageDigestPasswordHasher('sha1')],
]);

$hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
$expectedHasher = new MessageDigestPasswordHasher('sha1');
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));
}

public function testGetHasherWithClassName()
{
$factory = new PasswordHasherFactory([
Expand Down Expand Up @@ -163,6 +174,28 @@ public function testMigrateFrom()
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
}

public function testMigrateFromWithCustomInstance()
{
if (!SodiumPasswordHasher::isSupported()) {
$this->markTestSkipped('Sodium is not available');
}

$sodium = new SodiumPasswordHasher();

$factory = new PasswordHasherFactory([
'digest_hasher' => $digest = new MessageDigestPasswordHasher('sha256'),
SomeUser::class => ['instance' => $sodium, 'migrate_from' => ['bcrypt', 'digest_hasher']],
]);

$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($digest->hash('foo', null), 'foo', null));
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
}

/**
* @group legacy
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.