X509Factory.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * X509Factory creates services for X509 certificate authentication.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @internal
  21. */
  22. class X509Factory implements SecurityFactoryInterface, AuthenticatorFactoryInterface
  23. {
  24. public function create(ContainerBuilder $container, string $id, array $config, string $userProvider, ?string $defaultEntryPoint)
  25. {
  26. $providerId = 'security.authentication.provider.pre_authenticated.'.$id;
  27. $container
  28. ->setDefinition($providerId, new ChildDefinition('security.authentication.provider.pre_authenticated'))
  29. ->replaceArgument(0, new Reference($userProvider))
  30. ->replaceArgument(1, new Reference('security.user_checker.'.$id))
  31. ->addArgument($id)
  32. ;
  33. // listener
  34. $listenerId = 'security.authentication.listener.x509.'.$id;
  35. $listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.x509'));
  36. $listener->replaceArgument(2, $id);
  37. $listener->replaceArgument(3, $config['user']);
  38. $listener->replaceArgument(4, $config['credentials']);
  39. $listener->addMethodCall('setSessionAuthenticationStrategy', [new Reference('security.authentication.session_strategy.'.$id)]);
  40. return [$providerId, $listenerId, $defaultEntryPoint];
  41. }
  42. public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId)
  43. {
  44. $authenticatorId = 'security.authenticator.x509.'.$firewallName;
  45. $container
  46. ->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.x509'))
  47. ->replaceArgument(0, new Reference($userProviderId))
  48. ->replaceArgument(2, $firewallName)
  49. ->replaceArgument(3, $config['user'])
  50. ->replaceArgument(4, $config['credentials'])
  51. ;
  52. return $authenticatorId;
  53. }
  54. public function getPosition()
  55. {
  56. return 'pre_auth';
  57. }
  58. public function getKey()
  59. {
  60. return 'x509';
  61. }
  62. public function addConfiguration(NodeDefinition $node)
  63. {
  64. $node
  65. ->children()
  66. ->scalarNode('provider')->end()
  67. ->scalarNode('user')->defaultValue('SSL_CLIENT_S_DN_Email')->end()
  68. ->scalarNode('credentials')->defaultValue('SSL_CLIENT_S_DN')->end()
  69. ->end()
  70. ;
  71. }
  72. }