8000 Add provider key in PreAuthenticationGuardToken · symfony/symfony@5013258 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5013258

Browse files
committed
Add provider key in PreAuthenticationGuardToken
This is required to create the correct authenticated token in the GuardAuthenticationManager.
1 parent 526f756 commit 5013258

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,6 @@ private function createFirewalls(array $config, ContainerBuilder $container)
269269
if ($this->guardAuthenticationManagerEnabled) {
270270
$authenticationManagerId = 'security.authentication.manager.guard';
271271
$container->setAlias('security.authentication.manager', new Alias($authenticationManagerId));
272-
273-
// guard authentication manager listener
274-
$container
275-
->setDefinition('security.firewall.guard.'.$name.'locator', new ChildDefinition('security.firewall.guard.locator'))
276-
->setArguments([$authenticationProviders])
277-
->addTag('container.service_locator')
278-
;
279-
$container
280-
->setDefinition('security.firewall.guard.'.$name, new ChildDefinition('security.firewall.guard'))
281-
->replaceArgument(2, new Reference('security.firewall.guard.'.$name.'locator'))
282-
->replaceArgument(3, $name)
283-
->addTag('kernel.event_listener', ['event' => KernelEvents::REQUEST])
284-
;
285272
}
286273
$container
287274
->getDefinition($authenticationManagerId)
@@ -431,7 +418,29 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
431418
$configuredEntryPoint = isset($firewall['entry_point']) ? $firewall['entry_point'] : null;
432419

433 8000 420
// Authentication listeners
434-
list($authListeners, $defaultEntryPoint) = $this->createAuthenticationListeners($container, $id, $firewall, $authenticationProviders, $defaultProvider, $providerIds, $configuredEntryPoint, $contextListenerId);
421+
$firewallAuthenticationProviders = [];
422+
list($authListeners, $defaultEntryPoint) = $this->createAuthenticationListeners($container, $id, $firewall, $firewallAuthenticationProviders, $defaultProvider, $providerIds, $configuredEntryPoint, $contextListenerId);
423+
424+
$authenticationProviders = array_merge($authenticationProviders, $firewallAuthenticationProviders);
425+
426+
if ($this->guardAuthenticationManagerEnabled) {
427+
// guard authentication manager listener
428+
$container
429+
->setDefinition('security.firewall.guard.'.$id.'.locator', new ChildDefinition('security.firewall.guard.locator'))
430+
->setArguments([array_map(function ($id) {
431+
return new Reference($id);
432+
}, $firewallAuthenticationProviders)])
433+
->addTag('container.service_locator')
434+
;
435+
$container
436+
->setDefinition('security.firewall.guard.'.$id, new ChildDefinition('security.firewall.guard'))
437+
->replaceArgument(2, new Reference('security.firewall.guard.'.$id.'.locator'))
438+
->replaceArgument(3, $id)
439+
->addTag('kernel.event_listener', ['event' => KernelEvents::REQUEST])
440+
;
441+
442+
$listeners[] = new Reference('security.firewall.guard.'.$id);
443+
}
435444

436445
$config->replaceArgument(7, $configuredEntryPoint ?: $defaultEntryPoint);
437446

src/Symfony/Component/Security/Core/Authentication/GuardAuthenticationManager.php

Lines changed: 1 addition & 1 deletion

src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticatorListenerTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function authenticate(TokenInterface $token)
8181
}
8282

8383
try {
84-
$result = $this->authenticateViaGuard($guard, $token);
84+
$result = $this->authenticateViaGuard($guard, $token, $token->getProviderKey());
8585
} catch (AuthenticationException $exception) {
8686
$this->handleFailure($exception, $token);
8787
}
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private function executeGuardAuthenticator(string $uniqueGuardKey, Authenticator
7272
}
7373

7474
// create a token with the unique key, so that the provider knows which authenticator to use
75-
$token = new PreAuthenticationGuardToken($credentials, $uniqueGuardKey);
75+
$token = new PreAuthenticationGuardToken($credentials, $uniqueGuardKey, $this->providerKey);
7676

7777
if (null !== $this->logger) {
7878
$this->logger->debug('Passing guard token information to the GuardAuthenticationProvider', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($guardAuthenticator)]);

src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function authenticate(TokenInterface $token)
9393
throw new AuthenticationException(sprintf('Token with provider key "%s" did not originate from any of the guard authenticators of provider "%s".', $token->getGuardProviderKey(), $this->providerKey));
9494
}
9595

96-
return $this->authenticateViaGuard($guardAuthenticator, $token);
96+
return $this->authenticateViaGuard($guardAuthenticator, $token, $this->providerKey);
9797
}
9898

9999
public function supports(TokenInterface $token)

src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProviderTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
trait GuardAuthenticationProviderTrait
3030
{
31-
private function authenticateViaGuard(AuthenticatorInterface $guardAuthenticator, PreAuthenticationGuardToken $token): GuardTokenInterface
31+
private function authenticateViaGuard(AuthenticatorInterface $guardAuthenticator, PreAuthenticationGuardToken $token, string $providerKey): TokenInterface
3232
{
3333
// get the user from the GuardAuthenticator
3434
$user = $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
@@ -55,7 +55,7 @@ private function authenticateViaGuard(AuthenticatorInterface $guardAuthenticator
5555
$this->userChecker->checkPostAuth($user);
5656

5757
// turn the UserInterface into a TokenInterface
58-
$authenticatedToken = $guardAuthenticator->createAuthenticatedToken($user, $this->providerKey);
58+
$authenticatedToken = $guardAuthenticator->createAuthenticatedToken($user, $providerKey);
5959
if (!$authenticatedToken instanceof TokenInterface) {
6060
throw new \UnexpectedValueException(sprintf('The "%s::createAuthenticatedToken()" method must return a TokenInterface. You returned "%s".', get_debug_type($guardAuthenticator), get_debug_type($authenticatedToken)));
6161
}

src/Symfony/Component/Security/Guard/Token/PreAuthenticationGuardToken.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,30 @@ class PreAuthenticationGuardToken extends AbstractToken implements GuardTokenInt
2626
{
2727
private $credentials;
2828
private $guardProviderKey;
29+
private $providerKey;
2930

3031
/**
31-
* @param mixed $credentials
32-
* @param string $guardProviderKey Unique key that bind this token to a specific AuthenticatorInterface
32+
* @param mixed $credentials
33+
* @param string $guardProviderKey Unique key that bind this token to a specific AuthenticatorInterface
34+
* @param string|null $providerKey The general provider key (when using with HTTP, this is the firewall name)
3335
*/
34-
public function __construct($credentials, string $guardProviderKey)
36+
public function __construct($credentials, string $guardProviderKey, ?string $providerKey = null)
3537
{
3638
$this->credentials = $credentials;
3739
$this->guardProviderKey = $guardProviderKey;
40+
$this->providerKey = $providerKey;
3841

3942
parent::__construct([]);
4043

4144
// never authenticated
4245
parent::setAuthenticated(false);
4346
}
4447

48+
public function getProviderKey(): ?string
49+
{
50+
return $this->providerKey;
51+
}
52+
4553
public function getGuardProviderKey()
4654
{
4755
return $this->guardProviderKey;

src/Symfony/Component/Security/Http/Firewall/GuardManagerListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __invoke(RequestEvent $requestEvent)
5757

5858
protected function getGuardKey(string $key): string
5959
{
60-
// Guard authenticators in the GuardAuthenticationManager are already indexed
60+
// Guard authenticators in the GuardManagerListener are already indexed
6161
// by an unique key
6262
return $key;
6363
}

0 commit comments

Comments
 (0)
0