Skip to content

Commit

Permalink
Rename providerKey to firewallName for more consistent naming
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Apr 20, 2020
1 parent 50224aa commit b1e040f
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 100 deletions.
Expand Up @@ -42,13 +42,13 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
return [$providerId, $listenerId, $defaultEntryPoint];
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId): string
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
if (null === $config['secret']) {
$config['secret'] = new Parameter('container.build_hash');
}

$authenticatorId = 'security.authenticator.anonymous.'.$id;
$authenticatorId = 'security.authenticator.anonymous.'.$firewallName;
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.anonymous'))
->replaceArgument(0, $config['secret']);
Expand Down
Expand Up @@ -25,5 +25,5 @@ interface AuthenticatorFactoryInterface
*
* @return string|string[] The authenticator service ID(s) to be used by the firewall
*/
public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId);
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId);
}
Expand Up @@ -49,7 +49,7 @@ public function addConfiguration(NodeDefinition $builder)
;
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId): array
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): array
{
return $config['services'];
}
Expand Down
Expand Up @@ -103,19 +103,19 @@ public function createEntryPoint(ContainerBuilder $container, string $id, array
return $entryPointId;
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId): string
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
if (isset($config['csrf_token_generator'])) {
throw new InvalidConfigurationException('The "csrf_token_generator" option of "form_login" is only available when "security.enable_authenticator_manager" is set to "false", use "enable_csrf" instead.');
}

$authenticatorId = 'security.authenticator.form_login.'.$id;
$authenticatorId = 'security.authenticator.form_login.'.$firewallName;
$options = array_intersect_key($config, $this->options);
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.form_login'))
->replaceArgument(1, new Reference($userProviderId))
->replaceArgument(2, new Reference($this->createAuthenticationSuccessHandler($container, $id, $config)))
->replaceArgument(3, new Reference($this->createAuthenticationFailureHandler($container, $id, $config)))
->replaceArgument(2, new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)))
->replaceArgument(3, new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)))
->replaceArgument(4, $options);

return $authenticatorId;
Expand Down
Expand Up @@ -46,9 +46,9 @@ public function create(ContainerBuilder $container, string $id, array $config, s
return [$provider, $listenerId, $entryPointId];
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId): string
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
$authenticatorId = 'security.authenticator.http_basic.'.$id;
$authenticatorId = 'security.authenticator.http_basic.'.$firewallName;
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.http_basic'))
->replaceArgument(0, $config['realm'])
Expand Down
Expand Up @@ -97,15 +97,15 @@ protected function createListener(ContainerBuilder $container, string $id, array
return $listenerId;
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId)
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId)
{
$authenticatorId = 'security.authenticator.json_login.'.$id;
$authenticatorId = 'security.authenticator.json_login.'.$firewallName;
$options = array_intersect_key($config, $this->options);
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.json_login'))
->replaceArgument(1, new Reference($userProviderId))
->replaceArgument(2, isset($config['success_handler']) ? new Reference($this->createAuthenticationSuccessHandler($container, $id, $config)) : null)
->replaceArgument(3, isset($config['failure_handler']) ? new Reference($this->createAuthenticationFailureHandler($container, $id, $config)) : null)
->replaceArgument(2, isset($config['success_handler']) ? new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)) : null)
->replaceArgument(3, isset($config['failure_handler']) ? new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)) : null)
->replaceArgument(4, $options);

return $authenticatorId;
Expand Down
Expand Up @@ -89,19 +89,19 @@ public function create(ContainerBuilder $container, string $id, array $config, ?
return [$authProviderId, $listenerId, $defaultEntryPoint];
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId): string
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
$templateId = $this->generateRememberMeServicesTemplateId($config, $id);
$rememberMeServicesId = $templateId.'.'.$id;
$templateId = $this->generateRememberMeServicesTemplateId($config, $firewallName);
$rememberMeServicesId = $templateId.'.'.$firewallName;

// create remember me services (which manage the remember me cookies)
$this->createRememberMeServices($container, $id, $templateId, [new Reference($userProviderId)], $config);
$this->createRememberMeServices($container, $firewallName, $templateId, [new Reference($userProviderId)], $config);

// create remember me listener (which executes the remember me services for other authenticators and logout)
$this->createRememberMeListener($container, $id, $rememberMeServicesId);
$this->createRememberMeListener($container, $firewallName, $rememberMeServicesId);

// create remember me authenticator (which re-authenticates the user based on the remember me cookie)
$authenticatorId = 'security.authenticator.remember_me.'.$id;
$authenticatorId = 'security.authenticator.remember_me.'.$firewallName;
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.remember_me'))
->replaceArgument(0, new Reference($rememberMeServicesId))
Expand Down
Expand Up @@ -43,9 +43,9 @@ public function create(ContainerBuilder $container, string $id, array $config, s
return [$providerId, $listenerId, $defaultEntryPoint];
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId)
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId)
{
$authenticatorId = 'security.authenticator.remote_user.'.$id;
$authenticatorId = 'security.authenticator.remote_user.'.$firewallName;
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.remote_user'))
->replaceArgument(0, new Reference($userProviderId))
Expand Down
Expand Up @@ -44,9 +44,9 @@ public function create(ContainerBuilder $container, string $id, array $config, s
return [$providerId, $listenerId, $defaultEntryPoint];
}

public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId)
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId)
{
$authenticatorId = 'security.authenticator.x509.'.$id;
$authenticatorId = 'security.authenticator.x509.'.$firewallName;
$container
->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.x509'))
->replaceArgument(0, new Reference($userProviderId))
Expand Down
Expand Up @@ -286,7 +286,7 @@ private function createFirewalls(array $config, ContainerBuilder $container)
// add authentication providers to authentication manager
$authenticationProviders = array_map(function ($id) {
return new Reference($id);
}, array_unique($authenticationProviders));
}, array_values(array_unique($authenticationProviders)));

$container
->getDefinition('security.authentication.manager')
Expand Down Expand Up @@ -439,9 +439,9 @@ private function createFirewall(ContainerBuilder $container, string $id, array $
$firewallAuthenticationProviders = [];
list($authListeners, $defaultEntryPoint) = $this->createAuthenticationListeners($container, $id, $firewall, $firewallAuthenticationProviders, $defaultProvider, $providerIds, $configuredEntryPoint, $contextListenerId);

$authenticationProviders = array_merge($authenticationProviders, $firewallAuthenticationProviders);

if ($this->authenticatorManagerEnabled) {
if (!$this->authenticatorManagerEnabled) {
$authenticationProviders = array_merge($authenticationProviders, $firewallAuthenticationProviders);
} else {
// authenticator manager
$authenticators = array_map(function ($id) {
return new Reference($id);
Expand Down Expand Up @@ -535,10 +535,10 @@ private function createAuthenticationListeners(ContainerBuilder $container, stri
$authenticators = $factory->createAuthenticator($container, $id, $firewall[$key], $userProvider);
if (\is_array($authenticators)) {
foreach ($authenticators as $i => $authenticator) {
$authenticationProviders[$id.'_'.$key.$i] = $authenticator;
$authenticationProviders[] = $authenticator;
}
} else {
$authenticationProviders[$id.'_'.$key] = $authenticators;
$authenticationProviders[] = $authenticators;
}

if ($factory instanceof EntryPointFactoryInterface) {
Expand All @@ -548,7 +548,7 @@ private function createAuthenticationListeners(ContainerBuilder $container, stri
list($provider, $listenerId, $defaultEntryPoint) = $factory->create($container, $id, $firewall[$key], $userProvider, $defaultEntryPoint);

$listeners[] = new Reference($listenerId);
$authenticationProviders[$id.'_'.$key] = $provider;
$authenticationProviders[] = $provider;
}
$hasListeners = true;
}
Expand Down
Expand Up @@ -47,17 +47,17 @@ class AuthenticatorManager implements AuthenticatorManagerInterface, UserAuthent
private $eventDispatcher;
private $eraseCredentials;
private $logger;
private $providerKey;
private $firewallName;

/**
* @param AuthenticatorInterface[] $authenticators The authenticators, with their unique providerKey as key
* @param AuthenticatorInterface[] $authenticators
*/
public function __construct(iterable $authenticators, TokenStorageInterface $tokenStorage, EventDispatcherInterface $eventDispatcher, string $providerKey, ?LoggerInterface $logger = null, bool $eraseCredentials = true)
public function __construct(iterable $authenticators, TokenStorageInterface $tokenStorage, EventDispatcherInterface $eventDispatcher, string $firewallName, ?LoggerInterface $logger = null, bool $eraseCredentials = true)
{
$this->authenticators = $authenticators;
$this->tokenStorage = $tokenStorage;
$this->eventDispatcher = $eventDispatcher;
$this->providerKey = $providerKey;
$this->firewallName = $firewallName;
$this->logger = $logger;
$this->eraseCredentials = $eraseCredentials;
}
Expand All @@ -68,7 +68,7 @@ public function __construct(iterable $authenticators, TokenStorageInterface $tok
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
{
// create an authenticated token for the User
$token = $authenticator->createAuthenticatedToken($passport = new SelfValidatingPassport($user, $badges), $this->providerKey);
$token = $authenticator->createAuthenticatedToken($passport = new SelfValidatingPassport($user, $badges), $this->firewallName);

// authenticate this in the system
return $this->handleAuthenticationSuccess($token, $passport, $request, $authenticator);
Expand All @@ -77,43 +77,43 @@ public function authenticateUser(UserInterface $user, AuthenticatorInterface $au
public function supports(Request $request): ?bool
{
if (null !== $this->logger) {
$context = ['firewall_key' => $this->providerKey];
$context = ['firewall_key' => $this->firewallName];

if ($this->authenticators instanceof \Countable || \is_array($this->authenticators)) {
$context['authenticators'] = \count($this->authenticators);
}

$this->logger->debug('Checking for guard authentication credentials.', $context);
$this->logger->debug('Checking for authenticator support.', $context);
}

$authenticators = [];
$lazy = true;
foreach ($this->authenticators as $key => $authenticator) {
foreach ($this->authenticators as $authenticator) {
if (null !== $this->logger) {
$this->logger->debug('Checking support on authenticator.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
$this->logger->debug('Checking support on authenticator.', ['firewall_key' => $this->firewallName, 'authenticator' => \get_class($authenticator)]);
}

if (false !== $supports = $authenticator->supports($request)) {
$authenticators[$key] = $authenticator;
$authenticators[] = $authenticator;
$lazy = $lazy && null === $supports;
} elseif (null !== $this->logger) {
$this->logger->debug('Authenticator does not support the request.', ['firewall_key' => $this->providerKey, 'authenticator' => \get_class($authenticator)]);
$this->logger->debug('Authenticator does not support the request.', ['firewall_key' => $this->firewallName, 'authenticator' => \get_class($authenticator)]);
}
}

if (!$authenticators) {
return false;
}

$request->attributes->set('_guard_authenticators', $authenticators);
$request->attributes->set('_security_authenticators', $authenticators);

return $lazy ? null : true;
}

public function authenticateRequest(Request $request): ?Response
{
$authenticators = $request->attributes->get('_guard_authenticators');
$request->attributes->remove('_guard_authenticators');
$authenticators = $request->attributes->get('_security_authenticators');
$request->attributes->remove('_security_authenticators');
if (!$authenticators) {
return null;
}
Expand All @@ -126,15 +126,16 @@ public function authenticateRequest(Request $request): ?Response
*/
private function executeAuthenticators(array $authenticators, Request $request): ?Response
{
foreach ($authenticators as $key => $authenticator) {
// recheck if the authenticator still supports the listener. support() is called
foreach ($authenticators as $authenticator) {
// recheck if the authenticator still supports the listener. supports() is called
// eagerly (before token storage is initialized), whereas authenticate() is called
// lazily (after initialization). This is important for e.g. the AnonymousAuthenticator
// as its support is relying on the (initialized) token in the TokenStorage.
if (false === $authenticator->supports($request)) {
if (null !== $this->logger) {
$this->logger->debug('Skipping the "{authenticator}" authenticator as it did not support the request.', ['authenticator' => \get_class($authenticator)]);
}

continue;
}

Expand Down Expand Up @@ -165,7 +166,7 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
$passport->checkIfCompletelyResolved();

// create the authenticated token
$authenticatedToken = $authenticator->createAuthenticatedToken($passport, $this->providerKey);
$authenticatedToken = $authenticator->createAuthenticatedToken($passport, $this->firewallName);
if (true === $this->eraseCredentials) {
$authenticatedToken->eraseCredentials();
}
Expand Down Expand Up @@ -204,7 +205,7 @@ private function handleAuthenticationSuccess(TokenInterface $authenticatedToken,
{
$this->tokenStorage->setToken($authenticatedToken);

$response = $authenticator->onAuthenticationSuccess($request, $authenticatedToken, $this->providerKey);
$response = $authenticator->onAuthenticationSuccess($request, $authenticatedToken, $this->firewallName);
if ($authenticator instanceof InteractiveAuthenticatorInterface && $authenticator->isInteractive()) {
$loginEvent = new InteractiveLoginEvent($request, $authenticatedToken);
$this->eventDispatcher->dispatch($loginEvent, SecurityEvents::INTERACTIVE_LOGIN);
Expand Down Expand Up @@ -233,7 +234,7 @@ private function handleAuthenticationFailure(AuthenticationException $authentica
$this->logger->debug('The "{authenticator}" authenticator set the failure response.', ['authenticator' => \get_class($authenticator)]);
}

$this->eventDispatcher->dispatch($loginFailureEvent = new LoginFailureEvent($authenticationException, $authenticator, $request, $response, $this->providerKey));
$this->eventDispatcher->dispatch($loginFailureEvent = new LoginFailureEvent($authenticationException, $authenticator, $request, $response, $this->firewallName));

// returning null is ok, it means they want the request to continue
return $loginFailureEvent->getResponse();
Expand Down

0 comments on commit b1e040f

Please sign in to comment.