JsonLoginFactory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\DependencyInjection\ChildDefinition;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * JsonLoginFactory creates services for JSON login authentication.
  16. *
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. *
  19. * @internal
  20. */
  21. class JsonLoginFactory extends AbstractFactory implements AuthenticatorFactoryInterface
  22. {
  23. public function __construct()
  24. {
  25. $this->addOption('username_path', 'username');
  26. $this->addOption('password_path', 'password');
  27. $this->defaultFailureHandlerOptions = [];
  28. $this->defaultSuccessHandlerOptions = [];
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getPosition()
  34. {
  35. return 'form';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getKey()
  41. {
  42. return 'json-login';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function createAuthProvider(ContainerBuilder $container, string $id, array $config, string $userProviderId)
  48. {
  49. $provider = 'security.authentication.provider.dao.'.$id;
  50. $container
  51. ->setDefinition($provider, new ChildDefinition('security.authentication.provider.dao'))
  52. ->replaceArgument(0, new Reference($userProviderId))
  53. ->replaceArgument(1, new Reference('security.user_checker.'.$id))
  54. ->replaceArgument(2, $id)
  55. ;
  56. return $provider;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function getListenerId()
  62. {
  63. return 'security.authentication.listener.json';
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function isRememberMeAware($config)
  69. {
  70. return false;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function createListener(ContainerBuilder $container, string $id, array $config, string $userProvider)
  76. {
  77. $listenerId = $this->getListenerId();
  78. $listener = new ChildDefinition($listenerId);
  79. $listener->replaceArgument(3, $id);
  80. $listener->replaceArgument(4, isset($config['success_handler']) ? new Reference($this->createAuthenticationSuccessHandler($container, $id, $config)) : null);
  81. $listener->replaceArgument(5, isset($config['failure_handler']) ? new Reference($this->createAuthenticationFailureHandler($container, $id, $config)) : null);
  82. $listener->replaceArgument(6, array_intersect_key($config, $this->options));
  83. $listener->addMethodCall('setSessionAuthenticationStrategy', [new Reference('security.authentication.session_strategy.'.$id)]);
  84. $listenerId .= '.'.$id;
  85. $container->setDefinition($listenerId, $listener);
  86. return $listenerId;
  87. }
  88. public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId)
  89. {
  90. $authenticatorId = 'security.authenticator.json_login.'.$firewallName;
  91. $options = array_intersect_key($config, $this->options);
  92. $container
  93. ->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.json_login'))
  94. ->replaceArgument(1, new Reference($userProviderId))
  95. ->replaceArgument(2, isset($config['success_handler']) ? new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)) : null)
  96. ->replaceArgument(3, isset($config['failure_handler']) ? new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)) : null)
  97. ->replaceArgument(4, $options);
  98. return $authenticatorId;
  99. }
  100. }