vendor/symfony/security-bundle/DependencyInjection/SecurityExtension.php line 724

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\SecurityBundle\DependencyInjection;
  11. use Composer\InstalledVersions;
  12. use Symfony\Bridge\Twig\Extension\LogoutUrlExtension;
  13. use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
  14. use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\FirewallListenerFactoryInterface;
  15. use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
  16. use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
  17. use Symfony\Bundle\SecurityBundle\Security\LegacyLogoutHandlerListener;
  18. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  19. use Symfony\Component\Config\FileLocator;
  20. use Symfony\Component\Console\Application;
  21. use Symfony\Component\DependencyInjection\Alias;
  22. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  23. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  24. use Symfony\Component\DependencyInjection\ChildDefinition;
  25. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  26. use Symfony\Component\DependencyInjection\ContainerBuilder;
  27. use Symfony\Component\DependencyInjection\Definition;
  28. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  29. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  30. use Symfony\Component\DependencyInjection\Reference;
  31. use Symfony\Component\EventDispatcher\EventDispatcher;
  32. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  33. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  34. use Symfony\Component\HttpKernel\KernelEvents;
  35. use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
  36. use Symfony\Component\PasswordHasher\Hasher\Pbkdf2PasswordHasher;
  37. use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
  38. use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
  39. use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
  40. use Symfony\Component\Security\Core\Authorization\Strategy\ConsensusStrategy;
  41. use Symfony\Component\Security\Core\Authorization\Strategy\PriorityStrategy;
  42. use Symfony\Component\Security\Core\Authorization\Strategy\UnanimousStrategy;
  43. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  44. use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder;
  45. use Symfony\Component\Security\Core\Encoder\SodiumPasswordEncoder;
  46. use Symfony\Component\Security\Core\User\ChainUserProvider;
  47. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  48. use Symfony\Component\Security\Core\User\UserProviderInterface;
  49. use Symfony\Component\Security\Http\Authenticator\Debug\TraceableAuthenticatorManagerListener;
  50. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  51. /**
  52.  * SecurityExtension.
  53.  *
  54.  * @author Fabien Potencier <[email protected]>
  55.  * @author Johannes M. Schmitt <[email protected]>
  56.  */
  57. class SecurityExtension extends Extension implements PrependExtensionInterface
  58. {
  59.     private $requestMatchers = [];
  60.     private $expressions = [];
  61.     private $contextListeners = [];
  62.     /** @var list<array{int, AuthenticatorFactoryInterface|SecurityFactoryInterface}> */
  63.     private $factories = [];
  64.     /** @var list<AuthenticatorFactoryInterface|SecurityFactoryInterface> */
  65.     private $sortedFactories = [];
  66.     private $userProviderFactories = [];
  67.     private $statelessFirewallKeys = [];
  68.     private $authenticatorManagerEnabled = false;
  69.     public function prepend(ContainerBuilder $container)
  70.     {
  71.         foreach ($this->getSortedFactories() as $factory) {
  72.             if ($factory instanceof PrependExtensionInterface) {
  73.                 $factory->prepend($container);
  74.             }
  75.         }
  76.     }
  77.     public function load(array $configs, ContainerBuilder $container)
  78.     {
  79.         if (!class_exists(InstalledVersions::class)) {
  80.             trigger_deprecation('symfony/security-bundle', '5.4', 'Configuring Symfony without the Composer Runtime API is deprecated. Consider upgrading to Composer 2.1 or later.');
  81.         }
  82.         if (!array_filter($configs)) {
  83.             return;
  84.         }
  85.         $mainConfig = $this->getConfiguration($configs, $container);
  86.         $config = $this->processConfiguration($mainConfig, $configs);
  87.         // load services
  88.         $loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Resources/config'));
  89.         $loader->load('security.php');
  90.         $loader->load('password_hasher.php');
  91.         $loader->load('security_listeners.php');
  92.         $loader->load('security_rememberme.php');
  93.         if ($this->authenticatorManagerEnabled = $config['enable_authenticator_manager']) {
  94.             if ($config['always_authenticate_before_granting']) {
  95.                 throw new InvalidConfigurationException('The security option "always_authenticate_before_granting" cannot be used when "enable_authenticator_manager" is set to true. If you rely on this behavior, set it to false.');
  96.             }
  97.             $loader->load('security_authenticator.php');
  98.             // The authenticator system no longer has anonymous tokens. This makes sure AccessListener
  99.             // and AuthorizationChecker do not throw AuthenticationCredentialsNotFoundException when no
  100.             // token is available in the token storage.
  101.             $container->getDefinition('security.access_listener')->setArgument(3, false);
  102.             $container->getDefinition('security.authorization_checker')->setArgument(3, false);
  103.             $container->getDefinition('security.authorization_checker')->setArgument(4, false);
  104.         } else {
  105.             trigger_deprecation('symfony/security-bundle', '5.3', 'Not setting the "security.enable_authenticator_manager" config option to true is deprecated.');
  106.             if ($config['always_authenticate_before_granting']) {
  107.                 $authorizationChecker = $container->getDefinition('security.authorization_checker');
  108.                 $authorizationCheckerArgs = $authorizationChecker->getArguments();
  109.                 array_splice($authorizationCheckerArgs, 1, 0, [new Reference('security.authentication.manager')]);
  110.                 $authorizationChecker->setArguments($authorizationCheckerArgs);
  111.             }
  112.             $loader->load('security_legacy.php');
  113.         }
  114.         if ($container::willBeAvailable('symfony/twig-bridge', LogoutUrlExtension::class, ['symfony/security-bundle'], true)) {
  115.             $loader->load('templating_twig.php');
  116.         }
  117.         $loader->load('collectors.php');
  118.         $loader->load('guard.php');
  119.         $container->getDefinition('data_collector.security')->addArgument($this->authenticatorManagerEnabled);
  120.         if ($container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug')) {
  121.             $loader->load('security_debug.php');
  122.         }
  123.         if (!$container::willBeAvailable('symfony/expression-language', ExpressionLanguage::class, ['symfony/security-bundle'], true)) {
  124.             $container->removeDefinition('security.expression_language');
  125.             $container->removeDefinition('security.access.expression_voter');
  126.         }
  127.         // set some global scalars
  128.         $container->setParameter('security.access.denied_url', $config['access_denied_url']);
  129.         $container->setParameter('security.authentication.manager.erase_credentials', $config['erase_credentials']);
  130.         $container->setParameter('security.authentication.session_strategy.strategy', $config['session_fixation_strategy']);
  131.         if (isset($config['access_decision_manager']['service'])) {
  132.             $container->setAlias('security.access.decision_manager', $config['access_decision_manager']['service']);
  133.         } elseif (isset($config['access_decision_manager']['strategy_service'])) {
  134.             $container
  135.                 ->getDefinition('security.access.decision_manager')
  136.                 ->addArgument(new Reference($config['access_decision_manager']['strategy_service']));
  137.         } else {
  138.             $container
  139.                 ->getDefinition('security.access.decision_manager')
  140.                 ->addArgument($this->createStrategyDefinition(
  141.                     $config['access_decision_manager']['strategy'] ?? MainConfiguration::STRATEGY_AFFIRMATIVE,
  142.                     $config['access_decision_manager']['allow_if_all_abstain'],
  143.                     $config['access_decision_manager']['allow_if_equal_granted_denied']
  144.                 ));
  145.         }
  146.         $container->setParameter('security.access.always_authenticate_before_granting', $config['always_authenticate_before_granting']);
  147.         $container->setParameter('security.authentication.hide_user_not_found', $config['hide_user_not_found']);
  148.         if (class_exists(Application::class)) {
  149.             $loader->load('debug_console.php');
  150.             $debugCommand = $container->getDefinition('security.command.debug_firewall');
  151.             $debugCommand->replaceArgument(4, $this->authenticatorManagerEnabled);
  152.         }
  153.         $this->createFirewalls($config, $container);
  154.         $this->createAuthorization($config, $container);
  155.         $this->createRoleHierarchy($config, $container);
  156.         $container->getDefinition('security.authentication.guard_handler')
  157.             ->replaceArgument(2, $this->statelessFirewallKeys);
  158.         // @deprecated since Symfony 5.3
  159.         if ($config['encoders']) {
  160.             $this->createEncoders($config['encoders'], $container);
  161.         }
  162.         if ($config['password_hashers']) {
  163.             $this->createHashers($config['password_hashers'], $container);
  164.         }
  165.         if (class_exists(Application::class)) {
  166.             $loader->load('console.php');
  167.             // @deprecated since Symfony 5.3
  168.             $container->getDefinition('security.command.user_password_encoder')->replaceArgument(1, array_keys($config['encoders']));
  169.             $container->getDefinition('security.command.user_password_hash')->replaceArgument(1, array_keys($config['password_hashers']));
  170.         }
  171.         $container->registerForAutoconfiguration(VoterInterface::class)
  172.             ->addTag('security.voter');
  173.     }
  174.     /**
  175.      * @throws \InvalidArgumentException if the $strategy is invalid
  176.      */
  177.     private function createStrategyDefinition(string $strategy, bool $allowIfAllAbstainDecisions, bool $allowIfEqualGrantedDeniedDecisions): Definition
  178.     {
  179.         switch ($strategy) {
  180.             case MainConfiguration::STRATEGY_AFFIRMATIVE:
  181.                 return new Definition(AffirmativeStrategy::class, [$allowIfAllAbstainDecisions]);
  182.             case MainConfiguration::STRATEGY_CONSENSUS:
  183.                 return new Definition(ConsensusStrategy::class, [$allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions]);
  184.             case MainConfiguration::STRATEGY_UNANIMOUS:
  185.                 return new Definition(UnanimousStrategy::class, [$allowIfAllAbstainDecisions]);
  186.             case MainConfiguration::STRATEGY_PRIORITY:
  187.                 return new Definition(PriorityStrategy::class, [$allowIfAllAbstainDecisions]);
  188.         }
  189.         throw new \InvalidArgumentException(sprintf('The strategy "%s" is not supported.', $strategy));
  190.     }
  191.     private function createRoleHierarchy(array $config, ContainerBuilder $container)
  192.     {
  193.         if (!isset($config['role_hierarchy']) || 0 === \count($config['role_hierarchy'])) {
  194.             $container->removeDefinition('security.access.role_hierarchy_voter');
  195.             return;
  196.         }
  197.         $container->setParameter('security.role_hierarchy.roles', $config['role_hierarchy']);
  198.         $container->removeDefinition('security.access.simple_role_voter');
  199.     }
  200.     private function createAuthorization(array $config, ContainerBuilder $container)
  201.     {
  202.         foreach ($config['access_control'] as $access) {
  203.             $matcher = $this->createRequestMatcher(
  204.                 $container,
  205.                 $access['path'],
  206.                 $access['host'],
  207.                 $access['port'],
  208.                 $access['methods'],
  209.                 $access['ips']
  210.             );
  211.             $attributes = $access['roles'];
  212.             if ($access['allow_if']) {
  213.                 $attributes[] = $this->createExpression($container, $access['allow_if']);
  214.             }
  215.             $emptyAccess = 0 === \count(array_filter($access));
  216.             if ($emptyAccess) {
  217.                 throw new InvalidConfigurationException('One or more access control items are empty. Did you accidentally add lines only containing a "-" under "security.access_control"?');
  218.             }
  219.             $container->getDefinition('security.access_map')
  220.                       ->addMethodCall('add', [$matcher, $attributes, $access['requires_channel']]);
  221.         }
  222.         // allow cache warm-up for expressions
  223.         if (\count($this->expressions)) {
  224.             $container->getDefinition('security.cache_warmer.expression')
  225.                 ->replaceArgument(0, new IteratorArgument(array_values($this->expressions)));
  226.         } else {
  227.             $container->removeDefinition('security.cache_warmer.expression');
  228.         }
  229.     }
  230.     private function createFirewalls(array $config, ContainerBuilder $container)
  231.     {
  232.         if (!isset($config['firewalls'])) {
  233.             return;
  234.         }
  235.         $firewalls = $config['firewalls'];
  236.         $providerIds = $this->createUserProviders($config, $container);
  237.         $container->setParameter('security.firewalls', array_keys($firewalls));
  238.         // make the ContextListener aware of the configured user providers
  239.         $contextListenerDefinition = $container->getDefinition('security.context_listener');
  240.         $arguments = $contextListenerDefinition->getArguments();
  241.         $userProviders = [];
  242.         foreach ($providerIds as $userProviderId) {
  243.             $userProviders[] = new Reference($userProviderId);
  244.         }
  245.         $arguments[1] = $userProviderIteratorsArgument = new IteratorArgument($userProviders);
  246.         $contextListenerDefinition->setArguments($arguments);
  247.         $nbUserProviders = \count($userProviders);
  248.         if ($nbUserProviders > 1) {
  249.             $container->setDefinition('security.user_providers', new Definition(ChainUserProvider::class, [$userProviderIteratorsArgument]))
  250.                 ->setPublic(false);
  251.         } elseif (0 === $nbUserProviders) {
  252.             $container->removeDefinition('security.listener.user_provider');
  253.         } else {
  254.             $container->setAlias('security.user_providers', new Alias(current($providerIds)))->setPublic(false);
  255.         }
  256.         if (1 === \count($providerIds)) {
  257.             $container->setAlias(UserProviderInterface::class, current($providerIds));
  258.         }
  259.         $customUserChecker = false;
  260.         // load firewall map
  261.         $mapDef = $container->getDefinition('security.firewall.map');
  262.         $map = $authenticationProviders = $contextRefs = [];
  263.         foreach ($firewalls as $name => $firewall) {
  264.             if (isset($firewall['user_checker']) && 'security.user_checker' !== $firewall['user_checker']) {
  265.                 $customUserChecker = true;
  266.             }
  267.             $configId = 'security.firewall.map.config.'.$name;
  268.             [$matcher, $listeners, $exceptionListener, $logoutListener] = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds, $configId);
  269.             $contextId = 'security.firewall.map.context.'.$name;
  270.             $isLazy = !$firewall['stateless'] && (!empty($firewall['anonymous']['lazy']) || $firewall['lazy']);
  271.             $context = new ChildDefinition($isLazy ? 'security.firewall.lazy_context' : 'security.firewall.context');
  272.             $context = $container->setDefinition($contextId, $context);
  273.             $context
  274.                 ->replaceArgument(0, new IteratorArgument($listeners))
  275.                 ->replaceArgument(1, $exceptionListener)
  276.                 ->replaceArgument(2, $logoutListener)
  277.                 ->replaceArgument(3, new Reference($configId))
  278.             ;
  279.             $contextRefs[$contextId] = new Reference($contextId);
  280.             $map[$contextId] = $matcher;
  281.         }
  282.         $container->setAlias('security.firewall.context_locator', (string) ServiceLocatorTagPass::register($container, $contextRefs));
  283.         $mapDef->replaceArgument(0, new Reference('security.firewall.context_locator'));
  284.         $mapDef->replaceArgument(1, new IteratorArgument($map));
  285.         if (!$this->authenticatorManagerEnabled) {
  286.             // add authentication providers to authentication manager
  287.             $authenticationProviders = array_map(function ($id) {
  288.                 return new Reference($id);
  289.             }, array_values(array_unique($authenticationProviders)));
  290.             $container
  291.                 ->getDefinition('security.authentication.manager')
  292.                 ->replaceArgument(0, new IteratorArgument($authenticationProviders));
  293.         }
  294.         // register an autowire alias for the UserCheckerInterface if no custom user checker service is configured
  295.         if (!$customUserChecker) {
  296.             $container->setAlias('Symfony\Component\Security\Core\User\UserCheckerInterface', new Alias('security.user_checker', false));
  297.         }
  298.     }
  299.     private function createFirewall(ContainerBuilder $container, string $id, array $firewall, array &$authenticationProviders, array $providerIds, string $configId)
  300.     {
  301.         $config = $container->setDefinition($configId, new ChildDefinition('security.firewall.config'));
  302.         $config->replaceArgument(0, $id);
  303.         $config->replaceArgument(1, $firewall['user_checker']);
  304.         // Matcher
  305.         $matcher = null;
  306.         if (isset($firewall['request_matcher'])) {
  307.             $matcher = new Reference($firewall['request_matcher']);
  308.         } elseif (isset($firewall['pattern']) || isset($firewall['host'])) {
  309.             $pattern = $firewall['pattern'] ?? null;
  310.             $host = $firewall['host'] ?? null;
  311.             $methods = $firewall['methods'] ?? [];
  312.             $matcher = $this->createRequestMatcher($container, $pattern, $host, null, $methods);
  313.         }
  314.         $config->replaceArgument(2, $matcher ? (string) $matcher : null);
  315.         $config->replaceArgument(3, $firewall['security']);
  316.         // Security disabled?
  317.         if (false === $firewall['security']) {
  318.             return [$matcher, [], null, null];
  319.         }
  320.         $config->replaceArgument(4, $firewall['stateless']);
  321.         $firewallEventDispatcherId = 'security.event_dispatcher.'.$id;
  322.         // Provider id (must be configured explicitly per firewall/authenticator if more than one provider is set)
  323.         $defaultProvider = null;
  324.         if (isset($firewall['provider'])) {
  325.             if (!isset($providerIds[$normalizedName = str_replace('-', '_', $firewall['provider'])])) {
  326.                 throw new InvalidConfigurationException(sprintf('Invalid firewall "%s": user provider "%s" not found.', $id, $firewall['provider']));
  327.             }
  328.             $defaultProvider = $providerIds[$normalizedName];
  329.             if ($this->authenticatorManagerEnabled) {
  330.                 $container->setDefinition('security.listener.'.$id.'.user_provider', new ChildDefinition('security.listener.user_provider.abstract'))
  331.                     ->addTag('kernel.event_listener', ['dispatcher' => $firewallEventDispatcherId, 'event' => CheckPassportEvent::class, 'priority' => 2048, 'method' => 'checkPassport'])
  332.                     ->replaceArgument(0, new Reference($defaultProvider));
  333.             }
  334.         } elseif (1 === \count($providerIds)) {
  335.             $defaultProvider = reset($providerIds);
  336.         }
  337.         $config->replaceArgument(5, $defaultProvider);
  338.         // Register Firewall-specific event dispatcher
  339.         $container->register($firewallEventDispatcherId, EventDispatcher::class)
  340.             ->addTag('event_dispatcher.dispatcher', ['name' => $firewallEventDispatcherId]);
  341.         // Register listeners
  342.         $listeners = [];
  343.         $listenerKeys = [];
  344.         // Channel listener
  345.         $listeners[] = new Reference('security.channel_listener');
  346.         $contextKey = null;
  347.         $contextListenerId = null;
  348.         // Context serializer listener
  349.         if (false === $firewall['stateless']) {
  350.             $contextKey = $firewall['context'] ?? $id;
  351.             $listeners[] = new Reference($contextListenerId = $this->createContextListener($container, $contextKey, $this->authenticatorManagerEnabled ? $firewallEventDispatcherId : null));
  352.             $sessionStrategyId = 'security.authentication.session_strategy';
  353.             if ($this->authenticatorManagerEnabled) {
  354.                 $container
  355.                     ->setDefinition('security.listener.session.'.$id, new ChildDefinition('security.listener.session'))
  356.                     ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  357.             }
  358.         } else {
  359.             $this->statelessFirewallKeys[] = $id;
  360.             $sessionStrategyId = 'security.authentication.session_strategy_noop';
  361.         }
  362.         $container->setAlias(new Alias('security.authentication.session_strategy.'.$id, false), $sessionStrategyId);
  363.         $config->replaceArgument(6, $contextKey);
  364.         // Logout listener
  365.         $logoutListenerId = null;
  366.         if (isset($firewall['logout'])) {
  367.             $logoutListenerId = 'security.logout_listener.'.$id;
  368.             $logoutListener = $container->setDefinition($logoutListenerId, new ChildDefinition('security.logout_listener'));
  369.             $logoutListener->replaceArgument(2, new Reference($firewallEventDispatcherId));
  370.             $logoutListener->replaceArgument(3, [
  371.                 'csrf_parameter' => $firewall['logout']['csrf_parameter'],
  372.                 'csrf_token_id' => $firewall['logout']['csrf_token_id'],
  373.                 'logout_path' => $firewall['logout']['path'],
  374.             ]);
  375.             // add default logout listener
  376.             if (isset($firewall['logout']['success_handler'])) {
  377.                 // deprecated, to be removed in Symfony 6.0
  378.                 $logoutSuccessHandlerId = $firewall['logout']['success_handler'];
  379.                 $container->register('security.logout.listener.legacy_success_listener.'.$id, LegacyLogoutHandlerListener::class)
  380.                     ->setArguments([new Reference($logoutSuccessHandlerId)])
  381.                     ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  382.             } else {
  383.                 $logoutSuccessListenerId = 'security.logout.listener.default.'.$id;
  384.                 $container->setDefinition($logoutSuccessListenerId, new ChildDefinition('security.logout.listener.default'))
  385.                     ->replaceArgument(1, $firewall['logout']['target'])
  386.                     ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  387.             }
  388.             // add CSRF provider
  389.             if (isset($firewall['logout']['csrf_token_generator'])) {
  390.                 $logoutListener->addArgument(new Reference($firewall['logout']['csrf_token_generator']));
  391.             }
  392.             // add session logout listener
  393.             if (true === $firewall['logout']['invalidate_session'] && false === $firewall['stateless']) {
  394.                 $container->setDefinition('security.logout.listener.session.'.$id, new ChildDefinition('security.logout.listener.session'))
  395.                     ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  396.             }
  397.             // add cookie logout listener
  398.             if (\count($firewall['logout']['delete_cookies']) > 0) {
  399.                 $container->setDefinition('security.logout.listener.cookie_clearing.'.$id, new ChildDefinition('security.logout.listener.cookie_clearing'))
  400.                     ->addArgument($firewall['logout']['delete_cookies'])
  401.                     ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  402.             }
  403.             // add custom listeners (deprecated)
  404.             foreach ($firewall['logout']['handlers'] as $i => $handlerId) {
  405.                 $container->register('security.logout.listener.legacy_handler.'.$i, LegacyLogoutHandlerListener::class)
  406.                     ->addArgument(new Reference($handlerId))
  407.                     ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  408.             }
  409.             // register with LogoutUrlGenerator
  410.             $container
  411.                 ->getDefinition('security.logout_url_generator')
  412.                 ->addMethodCall('registerListener', [
  413.                     $id,
  414.                     $firewall['logout']['path'],
  415.                     $firewall['logout']['csrf_token_id'],
  416.                     $firewall['logout']['csrf_parameter'],
  417.                     isset($firewall['logout']['csrf_token_generator']) ? new Reference($firewall['logout']['csrf_token_generator']) : null,
  418.                     false === $firewall['stateless'] && isset($firewall['context']) ? $firewall['context'] : null,
  419.                 ])
  420.             ;
  421.         }
  422.         // Determine default entry point
  423.         $configuredEntryPoint = $firewall['entry_point'] ?? null;
  424.         // Authentication listeners
  425.         $firewallAuthenticationProviders = [];
  426.         [$authListeners, $defaultEntryPoint] = $this->createAuthenticationListeners($container, $id, $firewall, $firewallAuthenticationProviders, $defaultProvider, $providerIds, $configuredEntryPoint, $contextListenerId);
  427.         if (!$this->authenticatorManagerEnabled) {
  428.             $authenticationProviders = array_merge($authenticationProviders, $firewallAuthenticationProviders);
  429.         } else {
  430.             // $configuredEntryPoint is resolved into a service ID and stored in $defaultEntryPoint
  431.             $configuredEntryPoint = $defaultEntryPoint;
  432.             // authenticator manager
  433.             $authenticators = array_map(function ($id) {
  434.                 return new Reference($id);
  435.             }, $firewallAuthenticationProviders);
  436.             $container
  437.                 ->setDefinition($managerId = 'security.authenticator.manager.'.$id, new ChildDefinition('security.authenticator.manager'))
  438.                 ->replaceArgument(0, $authenticators)
  439.                 ->replaceArgument(2, new Reference($firewallEventDispatcherId))
  440.                 ->replaceArgument(3, $id)
  441.                 ->replaceArgument(7, $firewall['required_badges'] ?? [])
  442.                 ->addTag('monolog.logger', ['channel' => 'security'])
  443.             ;
  444.             $managerLocator = $container->getDefinition('security.authenticator.managers_locator');
  445.             $managerLocator->replaceArgument(0, array_merge($managerLocator->getArgument(0), [$id => new ServiceClosureArgument(new Reference($managerId))]));
  446.             // authenticator manager listener
  447.             $container
  448.                 ->setDefinition('security.firewall.authenticator.'.$id, new ChildDefinition('security.firewall.authenticator'))
  449.                 ->replaceArgument(0, new Reference($managerId))
  450.             ;
  451.             if ($container->hasDefinition('debug.security.firewall') && $this->authenticatorManagerEnabled) {
  452.                 $container
  453.                     ->register('debug.security.firewall.authenticator.'.$id, TraceableAuthenticatorManagerListener::class)
  454.                     ->setDecoratedService('security.firewall.authenticator.'.$id)
  455.                     ->setArguments([new Reference('debug.security.firewall.authenticator.'.$id.'.inner')])
  456.                     ->addTag('kernel.reset', ['method' => 'reset'])
  457.                 ;
  458.             }
  459.             // user checker listener
  460.             $container
  461.                 ->setDefinition('security.listener.user_checker.'.$id, new ChildDefinition('security.listener.user_checker'))
  462.                 ->replaceArgument(0, new Reference('security.user_checker.'.$id))
  463.                 ->addTag('kernel.event_subscriber', ['dispatcher' => $firewallEventDispatcherId]);
  464.             $listeners[] = new Reference('security.firewall.authenticator.'.$id);
  465.             // Add authenticators to the debug:firewall command
  466.             if ($container->hasDefinition('security.command.debug_firewall')) {
  467.                 $debugCommand = $container->getDefinition('security.command.debug_firewall');
  468.                 $debugCommand->replaceArgument(3, array_merge($debugCommand->getArgument(3), [$id => $authenticators]));
  469.             }
  470.         }
  471.         $config->replaceArgument(7, $configuredEntryPoint ?: $defaultEntryPoint);
  472.         $listeners = array_merge($listeners, $authListeners);
  473.         // Switch user listener
  474.         if (isset($firewall['switch_user'])) {
  475.             $listenerKeys[] = 'switch_user';
  476.             $listeners[] = new Reference($this->createSwitchUserListener($container, $id, $firewall['switch_user'], $defaultProvider, $firewall['stateless']));
  477.         }
  478.         // Access listener
  479.         $listeners[] = new Reference('security.access_listener');
  480.         // Exception listener
  481.         $exceptionListener = new Reference($this->createExceptionListener($container, $firewall, $id, $configuredEntryPoint ?: $defaultEntryPoint, $firewall['stateless']));
  482.         $config->replaceArgument(8, $firewall['access_denied_handler'] ?? null);
  483.         $config->replaceArgument(9, $firewall['access_denied_url'] ?? null);
  484.         $container->setAlias('security.user_checker.'.$id, new Alias($firewall['user_checker'], false));
  485.         $userCheckerLocator = $container->getDefinition('security.user_checker_locator');
  486.         $userCheckerLocator->replaceArgument(0, array_merge($userCheckerLocator->getArgument(0), [$id => new ServiceClosureArgument(new Reference('security.user_checker.'.$id))]));
  487.         foreach ($this->getSortedFactories() as $factory) {
  488.             $key = str_replace('-', '_', $factory->getKey());
  489.             if ('custom_authenticators' !== $key && \array_key_exists($key, $firewall)) {
  490.                 $listenerKeys[] = $key;
  491.             }
  492.         }
  493.         if ($firewall['custom_authenticators'] ?? false) {
  494.             foreach ($firewall['custom_authenticators'] as $customAuthenticatorId) {
  495.                 $listenerKeys[] = $customAuthenticatorId;
  496.             }
  497.         }
  498.         $config->replaceArgument(10, $listenerKeys);
  499.         $config->replaceArgument(11, $firewall['switch_user'] ?? null);
  500.         return [$matcher, $listeners, $exceptionListener, null !== $logoutListenerId ? new Reference($logoutListenerId) : null];
  501.     }
  502.     private function createContextListener(ContainerBuilder $container, string $contextKey, ?string $firewallEventDispatcherId)
  503.     {
  504.         if (isset($this->contextListeners[$contextKey])) {
  505.             return $this->contextListeners[$contextKey];
  506.         }
  507.         $listenerId = 'security.context_listener.'.\count($this->contextListeners);
  508.         $listener = $container->setDefinition($listenerId, new ChildDefinition('security.context_listener'));
  509.         $listener->replaceArgument(2, $contextKey);
  510.         if (null !== $firewallEventDispatcherId) {
  511.             $listener->replaceArgument(4, new Reference($firewallEventDispatcherId));
  512.             $listener->addTag('kernel.event_listener', ['event' => KernelEvents::RESPONSE, 'method' => 'onKernelResponse']);
  513.         }
  514.         return $this->contextListeners[$contextKey] = $listenerId;
  515.     }
  516.     private function createAuthenticationListeners(ContainerBuilder $container, string $id, array $firewall, array &$authenticationProviders, ?string $defaultProvider, array $providerIds, ?string $defaultEntryPoint, ?string $contextListenerId = null)
  517.     {
  518.         $listeners = [];
  519.         $hasListeners = false;
  520.         $entryPoints = [];
  521.         foreach ($this->getSortedFactories() as $factory) {
  522.             $key = str_replace('-', '_', $factory->getKey());
  523.             if (isset($firewall[$key])) {
  524.                 $userProvider = $this->getUserProvider($container, $id, $firewall, $key, $defaultProvider, $providerIds, $contextListenerId);
  525.                 if ($this->authenticatorManagerEnabled) {
  526.                     if (!$factory instanceof AuthenticatorFactoryInterface) {
  527.                         throw new InvalidConfigurationException(sprintf('Cannot configure AuthenticatorManager as "%s" authentication does not support it, set "security.enable_authenticator_manager" to `false`.', $key));
  528.                     }
  529.                     $authenticators = $factory->createAuthenticator($container, $id, $firewall[$key], $userProvider);
  530.                     if (\is_array($authenticators)) {
  531.                         foreach ($authenticators as $authenticator) {
  532.                             $authenticationProviders[] = $authenticator;
  533.                             $entryPoints[] = $authenticator;
  534.                         }
  535.                     } else {
  536.                         $authenticationProviders[] = $authenticators;
  537.                         $entryPoints[$key] = $authenticators;
  538.                     }
  539.                 } else {
  540.                     [$provider, $listenerId, $defaultEntryPoint] = $factory->create($container, $id, $firewall[$key], $userProvider, $defaultEntryPoint);
  541.                     $listeners[] = new Reference($listenerId);
  542.                     $authenticationProviders[] = $provider;
  543.                 }
  544.                 if ($factory instanceof FirewallListenerFactoryInterface) {
  545.                     $firewallListenerIds = $factory->createListeners($container, $id, $firewall[$key]);
  546.                     foreach ($firewallListenerIds as $firewallListenerId) {
  547.                         $listeners[] = new Reference($firewallListenerId);
  548.                     }
  549.                 }
  550.                 $hasListeners = true;
  551.             }
  552.         }
  553.         // the actual entry point is configured by the RegisterEntryPointPass
  554.         $container->setParameter('security.'.$id.'._indexed_authenticators', $entryPoints);
  555.         if (false === $hasListeners && !$this->authenticatorManagerEnabled) {
  556.             throw new InvalidConfigurationException(sprintf('No authentication listener registered for firewall "%s".', $id));
  557.         }
  558.         return [$listeners, $defaultEntryPoint];
  559.     }
  560.     private function getUserProvider(ContainerBuilder $container, string $id, array $firewall, string $factoryKey, ?string $defaultProvider, array $providerIds, ?string $contextListenerId): string
  561.     {
  562.         if (isset($firewall[$factoryKey]['provider'])) {
  563.             if (!isset($providerIds[$normalizedName = str_replace('-', '_', $firewall[$factoryKey]['provider'])])) {
  564.                 throw new InvalidConfigurationException(sprintf('Invalid firewall "%s": user provider "%s" not found.', $id, $firewall[$factoryKey]['provider']));
  565.             }
  566.             return $providerIds[$normalizedName];
  567.         }
  568.         if ('remember_me' === $factoryKey && $contextListenerId) {
  569.             $container->getDefinition($contextListenerId)->addTag('security.remember_me_aware', ['id' => $id, 'provider' => 'none']);
  570.         }
  571.         if ($defaultProvider) {
  572.             return $defaultProvider;
  573.         }
  574.         if (!$providerIds) {
  575.             $userProvider = sprintf('security.user.provider.missing.%s', $factoryKey);
  576.             $container->setDefinition(
  577.                 $userProvider,
  578.                 (new ChildDefinition('security.user.provider.missing'))->replaceArgument(0, $id)
  579.             );
  580.             return $userProvider;
  581.         }
  582.         if ('remember_me' === $factoryKey || 'anonymous' === $factoryKey || 'custom_authenticators' === $factoryKey) {
  583.             if ('custom_authenticators' === $factoryKey) {
  584.                 trigger_deprecation('symfony/security-bundle', '5.4', 'Not configuring explicitly the provider for the "%s" firewall is deprecated because it\'s ambiguous as there is more than one registered provider. Set the "provider" key to one of the configured providers, even if your custom authenticators don\'t use it.', $id);
  585.             }
  586.             return 'security.user_providers';
  587.         }
  588.         throw new InvalidConfigurationException(sprintf('Not configuring explicitly the provider for the "%s" %s on "%s" firewall is ambiguous as there is more than one registered provider.', $factoryKey, $this->authenticatorManagerEnabled ? 'authenticator' : 'listener', $id));
  589.     }
  590.     private function createEncoders(array $encoders, ContainerBuilder $container)
  591.     {
  592.         $encoderMap = [];
  593.         foreach ($encoders as $class => $encoder) {
  594.             if (class_exists($class) && !is_a($class, PasswordAuthenticatedUserInterface::class, true)) {
  595.                 trigger_deprecation('symfony/security-bundle', '5.3', 'Configuring an encoder for a user class that does not implement "%s" is deprecated, class "%s" should implement it.', PasswordAuthenticatedUserInterface::class, $class);
  596.             }
  597.             $encoderMap[$class] = $this->createEncoder($encoder);
  598.         }
  599.         $container
  600.             ->getDefinition('security.encoder_factory.generic')
  601.             ->setArguments([$encoderMap])
  602.         ;
  603.     }
  604.     private function createEncoder(array $config)
  605.     {
  606.         // a custom encoder service
  607.         if (isset($config['id'])) {
  608.             return new Reference($config['id']);
  609.         }
  610.         if ($config['migrate_from'] ?? false) {
  611.             return $config;
  612.         }
  613.         // plaintext encoder
  614.         if ('plaintext' === $config['algorithm']) {
  615.             $arguments = [$config['ignore_case']];
  616.             return [
  617.                 'class' => 'Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder',
  618.                 'arguments' => $arguments,
  619.             ];
  620.         }
  621.         // pbkdf2 encoder
  622.         if ('pbkdf2' === $config['algorithm']) {
  623.             return [
  624.                 'class' => 'Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder',
  625.                 'arguments' => [
  626.                     $config['hash_algorithm'],
  627.                     $config['encode_as_base64'],
  628.                     $config['iterations'],
  629.                     $config['key_length'],
  630.                 ],
  631.             ];
  632.         }
  633.         // bcrypt encoder
  634.         if ('bcrypt' === $config['algorithm']) {
  635.             $config['algorithm'] = 'native';
  636.             $config['native_algorithm'] = \PASSWORD_BCRYPT;
  637.             return $this->createEncoder($config);
  638.         }
  639.         // Argon2i encoder
  640.         if ('argon2i' === $config['algorithm']) {
  641.             if (SodiumPasswordHasher::isSupported() && !\defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13')) {
  642.                 $config['algorithm'] = 'sodium';
  643.             } elseif (\defined('PASSWORD_ARGON2I')) {
  644.                 $config['algorithm'] = 'native';
  645.                 $config['native_algorithm'] = \PASSWORD_ARGON2I;
  646.             } else {
  647.                 throw new InvalidConfigurationException(sprintf('Algorithm "argon2i" is not available. Use "%s" instead.', \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13') ? 'argon2id", "auto' : 'auto'));
  648.             }
  649.             return $this->createEncoder($config);
  650.         }
  651.         if ('argon2id' === $config['algorithm']) {
  652.             if (($hasSodium = SodiumPasswordHasher::isSupported()) && \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13')) {
  653.                 $config['algorithm'] = 'sodium';
  654.             } elseif (\defined('PASSWORD_ARGON2ID')) {
  655.                 $config['algorithm'] = 'native';
  656.                 $config['native_algorithm'] = \PASSWORD_ARGON2ID;
  657.             } else {
  658.                 throw new InvalidConfigurationException(sprintf('Algorithm "argon2id" is not available. Either use "%s", upgrade to PHP 7.3+ or use libsodium 1.0.15+ instead.', \defined('PASSWORD_ARGON2I') || $hasSodium ? 'argon2i", "auto' : 'auto'));
  659.             }
  660.             return $this->createEncoder($config);
  661.         }
  662.         if ('native' === $config['algorithm']) {
  663.             return [
  664.                 'class' => NativePasswordEncoder::class,
  665.                 'arguments' => [
  666.                         $config['time_cost'],
  667.                         (($config['memory_cost'] ?? 0) << 10) ?: null,
  668.                         $config['cost'],
  669.                     ] + (isset($config['native_algorithm']) ? [3 => $config['native_algorithm']] : []),
  670.             ];
  671.         }
  672.         if ('sodium' === $config['algorithm']) {
  673.             if (!SodiumPasswordHasher::isSupported()) {
  674.                 throw new InvalidConfigurationException('Libsodium is not available. Install the sodium extension or use "auto" instead.');
  675.             }
  676.             return [
  677.                 'class' => SodiumPasswordEncoder::class,
  678.                 'arguments' => [
  679.                     $config['time_cost'],
  680.                     (($config['memory_cost'] ?? 0) << 10) ?: null,
  681.                 ],
  682.             ];
  683.         }
  684.         // run-time configured encoder
  685.         return $config;
  686.     }
  687.     private function createHashers(array $hashers, ContainerBuilder $container)
  688.     {
  689.         $hasherMap = [];
  690.         foreach ($hashers as $class => $hasher) {
  691.             // @deprecated since Symfony 5.3, remove the check in 6.0
  692.             if (class_exists($class) && !is_a($class, PasswordAuthenticatedUserInterface::class, true)) {
  693.                 trigger_deprecation('symfony/security-bundle', '5.3', 'Configuring a password hasher for a user class that does not implement "%s" is deprecated, class "%s" should implement it.', PasswordAuthenticatedUserInterface::class, $class);
  694.             }
  695.             $hasherMap[$class] = $this->createHasher($hasher);
  696.         }
  697.         $container
  698.             ->getDefinition('security.password_hasher_factory')
  699.             ->setArguments([$hasherMap])
  700.         ;
  701.     }
  702.     private function createHasher(array $config)
  703.     {
  704.         // a custom hasher service
  705.         if (isset($config['id'])) {
  706.             return $config['migrate_from'] ?? false ? [
  707.                 'instance' => new Reference($config['id']),
  708.                 'migrate_from' => $config['migrate_from'],
  709.             ] : new Reference($config['id']);
  710.         }
  711.         if ($config['migrate_from'] ?? false) {
  712.             return $config;
  713.         }
  714.         // plaintext hasher
  715.         if ('plaintext' === $config['algorithm']) {
  716.             $arguments = [$config['ignore_case']];
  717.             return [
  718.                 'class' => PlaintextPasswordHasher::class,
  719.                 'arguments' => $arguments,
  720.             ];
  721.         }
  722.         // pbkdf2 hasher
  723.         if ('pbkdf2' === $config['algorithm']) {
  724.             return [
  725.                 'class' => Pbkdf2PasswordHasher::class,
  726.                 'arguments' => [
  727.                     $config['hash_algorithm'],
  728.                     $config['encode_as_base64'],
  729.                     $config['iterations'],
  730.                     $config['key_length'],
  731.                 ],
  732.             ];
  733.         }
  734.         // bcrypt hasher
  735.         if ('bcrypt' === $config['algorithm']) {
  736.             $config['algorithm'] = 'native';
  737.             $config['native_algorithm'] = \PASSWORD_BCRYPT;
  738.             return $this->createHasher($config);
  739.         }
  740.         // Argon2i hasher
  741.         if ('argon2i' === $config['algorithm']) {
  742.             if (SodiumPasswordHasher::isSupported() && !\defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13')) {
  743.                 $config['algorithm'] = 'sodium';
  744.             } elseif (\defined('PASSWORD_ARGON2I')) {
  745.                 $config['algorithm'] = 'native';
  746.                 $config['native_algorithm'] = \PASSWORD_ARGON2I;
  747.             } else {
  748.                 throw new InvalidConfigurationException(sprintf('Algorithm "argon2i" is not available. Either use "%s" or upgrade to PHP 7.2+ instead.', \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13') ? 'argon2id", "auto' : 'auto'));
  749.             }
  750.             return $this->createHasher($config);
  751.         }
  752.         if ('argon2id' === $config['algorithm']) {
  753.             if (($hasSodium = SodiumPasswordHasher::isSupported()) && \defined('SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13')) {
  754.                 $config['algorithm'] = 'sodium';
  755.             } elseif (\defined('PASSWORD_ARGON2ID')) {
  756.                 $config['algorithm'] = 'native';
  757.                 $config['native_algorithm'] = \PASSWORD_ARGON2ID;
  758.             } else {
  759.                 throw new InvalidConfigurationException(sprintf('Algorithm "argon2id" is not available. Either use "%s", upgrade to PHP 7.3+ or use libsodium 1.0.15+ instead.', \defined('PASSWORD_ARGON2I') || $hasSodium ? 'argon2i", "auto' : 'auto'));
  760.             }
  761.             return $this->createHasher($config);
  762.         }
  763.         if ('native' === $config['algorithm']) {
  764.             return [
  765.                 'class' => NativePasswordHasher::class,
  766.                 'arguments' => [
  767.                     $config['time_cost'],
  768.                     (($config['memory_cost'] ?? 0) << 10) ?: null,
  769.                     $config['cost'],
  770.                 ] + (isset($config['native_algorithm']) ? [3 => $config['native_algorithm']] : []),
  771.             ];
  772.         }
  773.         if ('sodium' === $config['algorithm']) {
  774.             if (!SodiumPasswordHasher::isSupported()) {
  775.                 throw new InvalidConfigurationException('Libsodium is not available. Install the sodium extension or use "auto" instead.');
  776.             }
  777.             return [
  778.                 'class' => SodiumPasswordHasher::class,
  779.                 'arguments' => [
  780.                     $config['time_cost'],
  781.                     (($config['memory_cost'] ?? 0) << 10) ?: null,
  782.                 ],
  783.             ];
  784.         }
  785.         // run-time configured hasher
  786.         return $config;
  787.     }
  788.     // Parses user providers and returns an array of their ids
  789.     private function createUserProviders(array $config, ContainerBuilder $container): array
  790.     {
  791.         $providerIds = [];
  792.         foreach ($config['providers'] as $name => $provider) {
  793.             $id = $this->createUserDaoProvider($name, $provider, $container);
  794.             $providerIds[str_replace('-', '_', $name)] = $id;
  795.         }
  796.         return $providerIds;
  797.     }
  798.     // Parses a <provider> tag and returns the id for the related user provider service
  799.     private function createUserDaoProvider(string $name, array $provider, ContainerBuilder $container): string
  800.     {
  801.         $name = $this->getUserProviderId($name);
  802.         // Doctrine Entity and In-memory DAO provider are managed by factories
  803.         foreach ($this->userProviderFactories as $factory) {
  804.             $key = str_replace('-', '_', $factory->getKey());
  805.             if (!empty($provider[$key])) {
  806.                 $factory->create($container, $name, $provider[$key]);
  807.                 return $name;
  808.             }
  809.         }
  810.         // Existing DAO service provider
  811.         if (isset($provider['id'])) {
  812.             $container->setAlias($name, new Alias($provider['id'], false));
  813.             return $provider['id'];
  814.         }
  815.         // Chain provider
  816.         if (isset($provider['chain'])) {
  817.             $providers = [];
  818.             foreach ($provider['chain']['providers'] as $providerName) {
  819.                 $providers[] = new Reference($this->getUserProviderId($providerName));
  820.             }
  821.             $container
  822.                 ->setDefinition($name, new ChildDefinition('security.user.provider.chain'))
  823.                 ->addArgument(new IteratorArgument($providers));
  824.             return $name;
  825.         }
  826.         throw new InvalidConfigurationException(sprintf('Unable to create definition for "%s" user provider.', $name));
  827.     }
  828.     private function getUserProviderId(string $name): string
  829.     {
  830.         return 'security.user.provider.concrete.'.strtolower($name);
  831.     }
  832.     private function createExceptionListener(ContainerBuilder $container, array $config, string $id, ?string $defaultEntryPoint, bool $stateless): string
  833.     {
  834.         $exceptionListenerId = 'security.exception_listener.'.$id;
  835.         $listener = $container->setDefinition($exceptionListenerId, new ChildDefinition('security.exception_listener'));
  836.         $listener->replaceArgument(3, $id);
  837.         $listener->replaceArgument(4, null === $defaultEntryPoint ? null : new Reference($defaultEntryPoint));
  838.         $listener->replaceArgument(8, $stateless);
  839.         // access denied handler setup
  840.         if (isset($config['access_denied_handler'])) {
  841.             $listener->replaceArgument(6, new Reference($config['access_denied_handler']));
  842.         } elseif (isset($config['access_denied_url'])) {
  843.             $listener->replaceArgument(5, $config['access_denied_url']);
  844.         }
  845.         return $exceptionListenerId;
  846.     }
  847.     private function createSwitchUserListener(ContainerBuilder $container, string $id, array $config, ?string $defaultProvider, bool $stateless): string
  848.     {
  849.         $userProvider = isset($config['provider']) ? $this->getUserProviderId($config['provider']) : $defaultProvider;
  850.         if (!$userProvider) {
  851.             throw new InvalidConfigurationException(sprintf('Not configuring explicitly the provider for the "switch_user" listener on "%s" firewall is ambiguous as there is more than one registered provider.', $id));
  852.         }
  853.         $switchUserListenerId = 'security.authentication.switchuser_listener.'.$id;
  854.         $listener = $container->setDefinition($switchUserListenerId, new ChildDefinition('security.authentication.switchuser_listener'));
  855.         $listener->replaceArgument(1, new Reference($userProvider));
  856.         $listener->replaceArgument(2, new Reference('security.user_checker.'.$id));
  857.         $listener->replaceArgument(3, $id);
  858.         $listener->replaceArgument(6, $config['parameter']);
  859.         $listener->replaceArgument(7, $config['role']);
  860.         $listener->replaceArgument(9, $stateless);
  861.         return $switchUserListenerId;
  862.     }
  863.     private function createExpression(ContainerBuilder $container, string $expression): Reference
  864.     {
  865.         if (isset($this->expressions[$id = '.security.expression.'.ContainerBuilder::hash($expression)])) {
  866.             return $this->expressions[$id];
  867.         }
  868.         if (!$container::willBeAvailable('symfony/expression-language', ExpressionLanguage::class, ['symfony/security-bundle'], true)) {
  869.             throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
  870.         }
  871.         $container
  872.             ->register($id, 'Symfony\Component\ExpressionLanguage\Expression')
  873.             ->setPublic(false)
  874.             ->addArgument($expression)
  875.         ;
  876.         return $this->expressions[$id] = new Reference($id);
  877.     }
  878.     private function createRequestMatcher(ContainerBuilder $container, ?string $path = null, ?string $host = null, ?int $port = null, array $methods = [], ?array $ips = null, array $attributes = []): Reference
  879.     {
  880.         if ($methods) {
  881.             $methods = array_map('strtoupper', $methods);
  882.         }
  883.         if (null !== $ips) {
  884.             foreach ($ips as $ip) {
  885.                 $container->resolveEnvPlaceholders($ip, null, $usedEnvs);
  886.                 if (!$usedEnvs && !$this->isValidIps($ip)) {
  887.                     throw new \LogicException(sprintf('The given value "%s" in the "security.access_control" config option is not a valid IP address.', $ip));
  888.                 }
  889.                 $usedEnvs = null;
  890.             }
  891.         }
  892.         $id = '.security.request_matcher.'.ContainerBuilder::hash([$path, $host, $port, $methods, $ips, $attributes]);
  893.         if (isset($this->requestMatchers[$id])) {
  894.             return $this->requestMatchers[$id];
  895.         }
  896.         // only add arguments that are necessary
  897.         $arguments = [$path, $host, $methods, $ips, $attributes, null, $port];
  898.         while (\count($arguments) > 0 && !end($arguments)) {
  899.             array_pop($arguments);
  900.         }
  901.         $container
  902.             ->register($id, 'Symfony\Component\HttpFoundation\RequestMatcher')
  903.             ->setPublic(false)
  904.             ->setArguments($arguments)
  905.         ;
  906.         return $this->requestMatchers[$id] = new Reference($id);
  907.     }
  908.     /**
  909.      * @deprecated since Symfony 5.4, use "addAuthenticatorFactory()" instead
  910.      */
  911.     public function addSecurityListenerFactory(SecurityFactoryInterface $factory)
  912.     {
  913.         trigger_deprecation('symfony/security-bundle', '5.4', 'Method "%s()" is deprecated, use "addAuthenticatorFactory()" instead.', __METHOD__);
  914.         $this->factories[] = [[
  915.             'pre_auth' => -10,
  916.             'form' => -30,
  917.             'http' => -40,
  918.             'remember_me' => -50,
  919.             'anonymous' => -60,
  920.         ][$factory->getPosition()], $factory];
  921.         $this->sortedFactories = [];
  922.     }
  923.     public function addAuthenticatorFactory(AuthenticatorFactoryInterface $factory)
  924.     {
  925.         $this->factories[] = [method_exists($factory, 'getPriority') ? $factory->getPriority() : 0, $factory];
  926.         $this->sortedFactories = [];
  927.     }
  928.     public function addUserProviderFactory(UserProviderFactoryInterface $factory)
  929.     {
  930.         $this->userProviderFactories[] = $factory;
  931.     }
  932.     /**
  933.      * {@inheritdoc}
  934.      */
  935.     public function getXsdValidationBasePath()
  936.     {
  937.         return __DIR__.'/../Resources/config/schema';
  938.     }
  939.     public function getNamespace()
  940.     {
  941.         return 'http://symfony.com/schema/dic/security';
  942.     }
  943.     public function getConfiguration(array $config, ContainerBuilder $container)
  944.     {
  945.         // first assemble the factories
  946.         return new MainConfiguration($this->getSortedFactories(), $this->userProviderFactories);
  947.     }
  948.     private function isValidIps($ips): bool
  949.     {
  950.         $ipsList = array_reduce((array) $ips, static function (array $ips, string $ip) {
  951.             return array_merge($ips, preg_split('/\s*,\s*/', $ip));
  952.         }, []);
  953.         if (!$ipsList) {
  954.             return false;
  955.         }
  956.         foreach ($ipsList as $cidr) {
  957.             if (!$this->isValidIp($cidr)) {
  958.                 return false;
  959.             }
  960.         }
  961.         return true;
  962.     }
  963.     private function isValidIp(string $cidr): bool
  964.     {
  965.         $cidrParts = explode('/', $cidr);
  966.         if (1 === \count($cidrParts)) {
  967.             return false !== filter_var($cidrParts[0], \FILTER_VALIDATE_IP);
  968.         }
  969.         $ip = $cidrParts[0];
  970.         $netmask = $cidrParts[1];
  971.         if (!ctype_digit($netmask)) {
  972.             return false;
  973.         }
  974.         if (filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
  975.             return $netmask <= 32;
  976.         }
  977.         if (filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
  978.             return $netmask <= 128;
  979.         }
  980.         return false;
  981.     }
  982.     /**
  983.      * @return array<int, SecurityFactoryInterface|AuthenticatorFactoryInterface>
  984.      */
  985.     private function getSortedFactories(): array
  986.     {
  987.         if (!$this->sortedFactories) {
  988.             $factories = [];
  989.             foreach ($this->factories as $i => $factory) {
  990.                 $factories[] = array_merge($factory, [$i]);
  991.             }
  992.             usort($factories, function ($a, $b) {
  993.                 return $b[0] <=> $a[0] ?: $a[2] <=> $b[2];
  994.             });
  995.             $this->sortedFactories = array_column($factories, 1);
  996.         }
  997.         return $this->sortedFactories;
  998.     }
  999. }