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
22 changes: 22 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
CHANGELOG
=========

7.4
---

* Register alias for argument for password hasher when its key is not a class name:

With the following configuration:
```yaml
security:
password_hashers:
recovery_code: auto
```

It is possible to inject the `recovery_code` password hasher in a service:

```php
public function __construct(
#[Target('recovery_code')]
private readonly PasswordHasherInterface $passwordHasher,
) {
}
```

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Symfony\Component\PasswordHasher\Hasher\Pbkdf2PasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Component\Routing\Loader\ContainerLoader;
use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
use Symfony\Component\Security\Core\Authorization\Strategy\ConsensusStrategy;
Expand Down Expand Up @@ -706,6 +707,17 @@ private function createHashers(array $hashers, ContainerBuilder $container): voi
$hasherMap = [];
foreach ($hashers as $class => $hasher) {
$hasherMap[$class] = $this->createHasher($hasher);
// The key is not a class, so we register an alias for argument to
// ease getting the hasher
if (!class_exists($class) && !interface_exists($class)) {
$id = 'security.password_hasher.'.$class;
$container
->register($id, PasswordHasherInterface::class)
->setFactory([new Reference('security.password_hasher_factory'), 'getPasswordHasher'])
->setArgument(0, $class)
;
$container->registerAliasForArgument($id, PasswordHasherInterface::class, $class);
}
}

$container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\InMemoryUserChecker;
Expand Down Expand Up @@ -883,7 +884,7 @@ public function testCustomHasherWithMigrateFrom()
$container->loadFromExtension('security', [
'password_hashers' => [
'legacy' => 'md5',
'App\User' => [
TestUserChecker::class => [
'id' => 'App\Security\CustomHasher',
'migrate_from' => 'legacy',
],
Expand All @@ -895,11 +896,19 @@ public function testCustomHasherWithMigrateFrom()

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

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

$legacyAlias = \sprintf('%s $%s', PasswordHasherInterface::class, 'legacy');
$this->assertTrue($container->hasAlias($legacyAlias));
$definition = $container->getDefinition((string) $container->getAlias($legacyAlias));
$this->assertSame(PasswordHasherInterface::class, $definition->getClass());

$this->assertFalse($container->hasAlias(\sprintf('%s $%s', PasswordHasherInterface::class, 'symfonyBundleSecurityBundleTestsDependencyInjectionTestUserChecker')));
$this->assertFalse($container->hasAlias(\sprintf('.%s $%s', PasswordHasherInterface::class, TestUserChecker::class)));
}

public function testAuthenticatorsDecoration()
Expand Down