RemoteUserFactory.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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\Security\Factory;
  11. use Symfony\Component\Config\Definition\Builder\NodeDefinition;
  12. use Symfony\Component\DependencyInjection\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * RemoteUserFactory creates services for REMOTE_USER based authentication.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Maxime Douailin <maxime.douailin@gmail.com>
  20. *
  21. * @internal
  22. */
  23. class RemoteUserFactory implements SecurityFactoryInterface, AuthenticatorFactoryInterface
  24. {
  25. public function create(ContainerBuilder $container, string $id, array $config, string $userProvider, ?string $defaultEntryPoint)
  26. {
  27. $providerId = 'security.authentication.provider.pre_authenticated.'.$id;
  28. $container
  29. ->setDefinition($providerId, new ChildDefinition('security.authentication.provider.pre_authenticated'))
  30. ->replaceArgument(0, new Reference($userProvider))
  31. ->replaceArgument(1, new Reference('security.user_checker.'.$id))
  32. ->addArgument($id)
  33. ;
  34. $listenerId = 'security.authentication.listener.remote_user.'.$id;
  35. $listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.remote_user'));
  36. $listener->replaceArgument(2, $id);
  37. $listener->replaceArgument(3, $config['user']);
  38. $listener->addMethodCall('setSessionAuthenticationStrategy', [new Reference('security.authentication.session_strategy.'.$id)]);
  39. return [$providerId, $listenerId, $defaultEntryPoint];
  40. }
  41. public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId)
  42. {
  43. $authenticatorId = 'security.authenticator.remote_user.'.$firewallName;
  44. $container
  45. ->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.remote_user'))
  46. ->replaceArgument(0, new Reference($userProviderId))
  47. ->replaceArgument(2, $firewallName)
  48. ->replaceArgument(3, $config['user'])
  49. ;
  50. return $authenticatorId;
  51. }
  52. public function getPosition()
  53. {
  54. return 'pre_auth';
  55. }
  56. public function getKey()
  57. {
  58. return 'remote-user';
  59. }
  60. public function addConfiguration(NodeDefinition $node)
  61. {
  62. $node
  63. ->children()
  64. ->scalarNode('provider')->end()
  65. ->scalarNode('user')->defaultValue('REMOTE_USER')->end()
  66. ->end()
  67. ;
  68. }
  69. }