From b923e4c4f6adde63f829d315214a23e8435351a7 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sun, 26 Jan 2020 21:36:07 +0100 Subject: [PATCH] Enabled remember me for the GuardManagerListener --- .../DependencyInjection/SecurityExtension.php | 21 +++++++++++++------ .../GuardAuthenticatorListenerTrait.php | 5 ----- .../Http/Firewall/GuardManagerListener.php | 7 +++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php index 54403cfa4a..f1bf246d8d 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php @@ -418,6 +418,19 @@ class SecurityExtension extends Extension implements PrependExtensionInterface // Determine default entry point $configuredEntryPoint = isset($firewall['entry_point']) ? $firewall['entry_point'] : null; + if ($this->guardAuthenticationManagerEnabled) { + // guard authentication manager listener (must be before calling createAuthenticationListeners() to inject remember me services) + $container + ->setDefinition('security.firewall.guard.'.$id, new ChildDefinition('security.firewall.guard')) + ->replaceArgument(2, new Reference('security.firewall.guard.'.$id.'.locator')) + ->replaceArgument(3, $id) + ->addTag('kernel.event_listener', ['event' => KernelEvents::REQUEST]) + ->addTag('security.remember_me_aware', ['id' => $id, 'provider' => 'none']) + ; + + $listeners[] = new Reference('security.firewall.guard.'.$id); + } + // Authentication listeners $firewallAuthenticationProviders = []; list($authListeners, $defaultEntryPoint) = $this->createAuthenticationListeners($container, $id, $firewall, $firewallAuthenticationProviders, $defaultProvider, $providerIds, $configuredEntryPoint, $contextListenerId); @@ -425,7 +438,7 @@ class SecurityExtension extends Extension implements PrependExtensionInterface $authenticationProviders = array_merge($authenticationProviders, $firewallAuthenticationProviders); if ($this->guardAuthenticationManagerEnabled) { - // guard authentication manager listener + // add authentication providers for this firewall to the GuardManagerListener (if guard is enabled) $container ->setDefinition('security.firewall.guard.'.$id.'.locator', new ChildDefinition('security.firewall.guard.locator')) ->setArguments([array_map(function ($id) { @@ -434,13 +447,9 @@ class SecurityExtension extends Extension implements PrependExtensionInterface ->addTag('container.service_locator') ; $container - ->setDefinition('security.firewall.guard.'.$id, new ChildDefinition('security.firewall.guard')) + ->getDefinition('security.firewall.guard.'.$id) ->replaceArgument(2, new Reference('security.firewall.guard.'.$id.'.locator')) - ->replaceArgument(3, $id) - ->addTag('kernel.event_listener', ['event' => KernelEvents::REQUEST]) ; - - $listeners[] = new Reference('security.firewall.guard.'.$id); } $config->replaceArgument(7, $configuredEntryPoint ?: $defaultEntryPoint); diff --git a/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticatorListenerTrait.php b/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticatorListenerTrait.php index ac1cb8200c..245f02c906 100644 --- a/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticatorListenerTrait.php +++ b/src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticatorListenerTrait.php @@ -150,11 +150,6 @@ trait GuardAuthenticatorListenerTrait throw new \UnexpectedValueException('Invalid guard authenticator passed to '.__METHOD__.'. Expected AuthenticatorInterface of either Security Core or Security Guard.'); } - // @todo implement remember me functionality - if (!isset($this->rememberMeServices)) { - return; - } - if (null === $this->rememberMeServices) { if (null !== $this->logger) { $this->logger->debug('Remember me skipped: it is not configured for the firewall.', ['authenticator' => \get_class($guardAuthenticator)]); diff --git a/src/Symfony/Component/Security/Http/Firewall/GuardManagerListener.php b/src/Symfony/Component/Security/Http/Firewall/GuardManagerListener.php index e2a80c9888..78681bd1e8 100644 --- a/src/Symfony/Component/Security/Http/Firewall/GuardManagerListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/GuardManagerListener.php @@ -17,6 +17,7 @@ use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterfac use Symfony\Component\Security\Core\Authentication\Authenticator\AuthenticatorInterface; use Symfony\Component\Security\Guard\Firewall\GuardAuthenticatorListenerTrait; use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; +use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; /** * @author Wouter de Jong @@ -34,6 +35,7 @@ class GuardManagerListener private $guardAuthenticators; protected $providerKey; protected $logger; + private $rememberMeServices; /** * @param AuthenticatorInterface[] $guardAuthenticators @@ -58,6 +60,11 @@ class GuardManagerListener $this->executeGuardAuthenticators($guardAuthenticators, $requestEvent); } + public function setRememberMeServices(RememberMeServicesInterface $rememberMeServices) + { + $this->rememberMeServices = $rememberMeServices; + } + protected function getGuardKey(string $key): string { // Guard authenticators in the GuardManagerListener are already indexed