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

Commit 576f604

Browse filesBrowse files
committed
[SecurityBundle] register alias for argument for password hasher
1 parent 5955b14 commit 576f604
Copy full SHA for 576f604

File tree

3 files changed

+32
-4
lines changed
Filter options

3 files changed

+32
-4
lines changed

‎src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Register alias for argument for password hasher when the key is not a class name
8+
49
7.3
510
---
611

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
3333
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
3434
use Symfony\Component\DependencyInjection\Reference;
35+
use Symfony\Component\DependencyInjection\Tests\Compiler\D;
3536
use Symfony\Component\EventDispatcher\EventDispatcher;
3637
use Symfony\Component\ExpressionLanguage\Expression;
3738
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
@@ -49,6 +50,7 @@
4950
use Symfony\Component\PasswordHasher\Hasher\Pbkdf2PasswordHasher;
5051
use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
5152
use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
53+
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
5254
use Symfony\Component\Routing\Loader\ContainerLoader;
5355
use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
5456
use Symfony\Component\Security\Core\Authorization\Strategy\ConsensusStrategy;
@@ -706,6 +708,17 @@ private function createHashers(array $hashers, ContainerBuilder $container): voi
706708
$hasherMap = [];
707709
foreach ($hashers as $class => $hasher) {
708710
$hasherMap[$class] = $this->createHasher($hasher);
711+
// The key is not a class, so we register an alias for argument to
712+
// ease getting the hasher
713+
if (!class_exists($class) && !interface_exists($class)) {
714+
$id = 'security.password_hasher.' .ContainerBuilder::hash($class);
715+
$container
716+
->register($id, PasswordHasherInterface::class)
717+
->setFactory([new Reference('security.password_hasher_factory'), 'getPasswordHasher'])
718+
->setArgument(0, $class)
719+
;
720+
$container->registerAliasForArgument($id, PasswordHasherInterface::class, $class);
721+
}
709722
}
710723

711724
$container

‎src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
1516
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
1617
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FirewallListenerFactoryInterface;
17-
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
1818
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1919
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\DummyProvider;
2020
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
@@ -29,6 +29,7 @@
2929
use Symfony\Component\HttpFoundation\Request;
3030
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
3131
use Symfony\Component\HttpFoundation\Response;
32+
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
3233
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
3334
use Symfony\Component\Security\Core\Exception\AuthenticationException;
3435
use Symfony\Component\Security\Core\User\InMemoryUserChecker;
@@ -883,7 +884,7 @@ public function testCustomHasherWithMigrateFrom()
883884
$container->loadFromExtension('security', [
884885
'password_hashers' => [
885886
'legacy' => 'md5',
886-
'App\User' => [
887+
TestUserChecker::class => [
887888
'id' => 'App\Security\CustomHasher',
888889
'migrate_from' => 'legacy',
889890
],
@@ -893,13 +894,22 @@ public function testCustomHasherWithMigrateFrom()
893894

894895
$container->compile();
895896

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

898-
$this->assertArrayHasKey('App\User', $hashersMap);
899-
$this->assertEquals($hashersMap['App\User'], [
900+
$this->assertArrayHasKey(TestUserChecker::class, $hashersMap);
901+
$this->assertEquals($hashersMap[TestUserChecker::class], [
900902
'instance' => new Reference('App\Security\CustomHasher'),
901903
'migrate_from' => ['legacy'],
902904
]);
905+
906+
$legacyAlias = \sprintf('%s $%s', PasswordHasherInterface::class, 'legacy');
907+
$this->assertTrue($container->hasAlias($legacyAlias));
908+
$definition = $container->getDefinition((string) $container->getAlias($legacyAlias));
909+
$this->assertSame(PasswordHasherInterface::class, $definition->getClass());
910+
911+
$this->assertFalse($container->hasAlias(\sprintf('%s $%s', PasswordHasherInterface::class, 'symfonyBundleSecurityBundleTestsDependencyInjectionTestUserChecker')));
912+
$this->assertFalse($container->hasAlias(\sprintf('.%s $%s', PasswordHasherInterface::class, TestUserChecker::class)));
903913
}
904914

905915
public function testAuthenticatorsDecoration()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.