HttpBasicFactory.php 3.1 KB

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