Skip to content

Commit

Permalink
[SecurityBundle] Do not replace authenticators service by their trace…
Browse files Browse the repository at this point in the history
…able version
  • Loading branch information
MatTheCat authored and fabpot committed Dec 30, 2024
1 parent 4bed202 commit e7b04b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
10 changes: 6 additions & 4 deletions DependencyInjection/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,13 @@ private function createAuthenticationListeners(ContainerBuilder $container, stri
}

if ($container->hasDefinition('debug.security.firewall')) {
foreach ($authenticationProviders as $authenticatorId) {
$container->register('debug.'.$authenticatorId, TraceableAuthenticator::class)
->setDecoratedService($authenticatorId)
->setArguments([new Reference('debug.'.$authenticatorId.'.inner')])
foreach ($authenticationProviders as &$authenticatorId) {
$traceableId = 'debug.'.$authenticatorId;
$container
->register($traceableId, TraceableAuthenticator::class)
->setArguments([new Reference($authenticatorId)])
;
$authenticatorId = $traceableId;
}
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/DependencyInjection/SecurityExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
Expand Down Expand Up @@ -900,6 +902,30 @@ public function testCustomHasherWithMigrateFrom()
]);
}

public function testAuthenticatorsDecoration()
{
$container = $this->getRawContainer();
$container->setParameter('kernel.debug', true);
$container->getCompilerPassConfig()->setOptimizationPasses([
new ResolveChildDefinitionsPass(),
new DecoratorServicePass(),
new ResolveReferencesToAliasesPass(),
]);

$container->register(TestAuthenticator::class);
$container->loadFromExtension('security', [
'firewalls' => ['main' => ['custom_authenticator' => TestAuthenticator::class]],
]);
$container->compile();

/** @var Reference[] $managerAuthenticators */
$managerAuthenticators = $container->getDefinition('security.authenticator.manager.main')->getArgument(0);
$this->assertCount(1, $managerAuthenticators);
$this->assertSame('debug.'.TestAuthenticator::class, (string) reset($managerAuthenticators), 'AuthenticatorManager must be injected traceable authenticators in debug mode.');

$this->assertTrue($container->hasDefinition(TestAuthenticator::class), 'Original authenticator must still exist in the container so it can be used outside of the AuthenticatorManager’s context.');
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit e7b04b5

Please sign in to comment.