LoginLinkFactory.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\NodeBuilder;
  12. use Symfony\Component\Config\Definition\Builder\NodeDefinition;
  13. use Symfony\Component\Config\FileLocator;
  14. use Symfony\Component\DependencyInjection\ChildDefinition;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  20. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandler;
  21. /**
  22. * @internal
  23. * @experimental in 5.2
  24. */
  25. class LoginLinkFactory extends AbstractFactory implements AuthenticatorFactoryInterface
  26. {
  27. public function addConfiguration(NodeDefinition $node)
  28. {
  29. /** @var NodeBuilder $builder */
  30. $builder = $node->fixXmlConfig('signature_property', 'signature_properties')->children();
  31. $builder
  32. ->scalarNode('check_route')
  33. ->isRequired()
  34. ->info('Route that will validate the login link - e.g. "app_login_link_verify".')
  35. ->end()
  36. ->scalarNode('check_post_only')
  37. ->defaultFalse()
  38. ->info('If true, only HTTP POST requests to "check_route" will be handled by the authenticator.')
  39. ->end()
  40. ->arrayNode('signature_properties')
  41. ->isRequired()
  42. ->prototype('scalar')->end()
  43. ->requiresAtLeastOneElement()
  44. ->info('An array of properties on your User that are used to sign the link. If any of these change, all existing links will become invalid.')
  45. ->example(['email', 'password'])
  46. ->end()
  47. ->integerNode('lifetime')
  48. ->defaultValue(600)
  49. ->info('The lifetime of the login link in seconds.')
  50. ->end()
  51. ->integerNode('max_uses')
  52. ->defaultNull()
  53. ->info('Max number of times a login link can be used - null means unlimited within lifetime.')
  54. ->end()
  55. ->scalarNode('used_link_cache')
  56. ->info('Cache service id used to expired links of max_uses is set.')
  57. ->end()
  58. ->scalarNode('success_handler')
  59. ->info(sprintf('A service id that implements %s.', AuthenticationSuccessHandlerInterface::class))
  60. ->end()
  61. ->scalarNode('failure_handler')
  62. ->info(sprintf('A service id that implements %s.', AuthenticationFailureHandlerInterface::class))
  63. ->end()
  64. ->scalarNode('provider')
  65. ->info('The user provider to load users from.')
  66. ->end()
  67. ;
  68. foreach (array_merge($this->defaultSuccessHandlerOptions, $this->defaultFailureHandlerOptions) as $name => $default) {
  69. if (\is_bool($default)) {
  70. $builder->booleanNode($name)->defaultValue($default);
  71. } else {
  72. $builder->scalarNode($name)->defaultValue($default);
  73. }
  74. }
  75. }
  76. public function getKey()
  77. {
  78. return 'login-link';
  79. }
  80. public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
  81. {
  82. if (!class_exists(LoginLinkHandler::class)) {
  83. throw new \LogicException('Login login link requires symfony/security-http:^5.2.');
  84. }
  85. if (!$container->hasDefinition('security.authenticator.login_link')) {
  86. $loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/../../Resources/config'));
  87. $loader->load('security_authenticator_login_link.php');
  88. }
  89. if (null !== $config['max_uses'] && !isset($config['used_link_cache'])) {
  90. $config['used_link_cache'] = 'security.authenticator.cache.expired_links';
  91. $defaultCacheDefinition = $container->getDefinition($config['used_link_cache']);
  92. if (!$defaultCacheDefinition->hasTag('cache.pool')) {
  93. $defaultCacheDefinition->addTag('cache.pool');
  94. }
  95. }
  96. $expiredStorageId = null;
  97. if (isset($config['used_link_cache'])) {
  98. $expiredStorageId = 'security.authenticator.expired_login_link_storage.'.$firewallName;
  99. $container
  100. ->setDefinition($expiredStorageId, new ChildDefinition('security.authenticator.expired_login_link_storage'))
  101. ->replaceArgument(0, new Reference($config['used_link_cache']))
  102. ->replaceArgument(1, $config['lifetime']);
  103. }
  104. $linkerId = 'security.authenticator.login_link_handler.'.$firewallName;
  105. $linkerOptions = [
  106. 'route_name' => $config['check_route'],
  107. 'lifetime' => $config['lifetime'],
  108. 'max_uses' => $config['max_uses'] ?? null,
  109. ];
  110. $container
  111. ->setDefinition($linkerId, new ChildDefinition('security.authenticator.abstract_login_link_handler'))
  112. ->replaceArgument(1, new Reference($userProviderId))
  113. ->replaceArgument(3, $config['signature_properties'])
  114. ->replaceArgument(5, $linkerOptions)
  115. ->replaceArgument(6, $expiredStorageId ? new Reference($expiredStorageId) : null)
  116. ->addTag('security.authenticator.login_linker', ['firewall' => $firewallName])
  117. ;
  118. $authenticatorId = 'security.authenticator.login_link.'.$firewallName;
  119. $container
  120. ->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.login_link'))
  121. ->replaceArgument(0, new Reference($linkerId))
  122. ->replaceArgument(2, new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)))
  123. ->replaceArgument(3, new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)))
  124. ->replaceArgument(4, [
  125. 'check_route' => $config['check_route'],
  126. 'check_post_only' => $config['check_post_only'],
  127. ]);
  128. return $authenticatorId;
  129. }
  130. public function getPosition()
  131. {
  132. return 'form';
  133. }
  134. protected function createAuthProvider(ContainerBuilder $container, string $id, array $config, string $userProviderId)
  135. {
  136. throw new \Exception('The old authentication system is not supported with login_link.');
  137. }
  138. protected function getListenerId()
  139. {
  140. throw new \Exception('The old authentication system is not supported with login_link.');
  141. }
  142. protected function createListener(ContainerBuilder $container, string $id, array $config, string $userProvider)
  143. {
  144. throw new \Exception('The old authentication system is not supported with login_link.');
  145. }
  146. protected function createEntryPoint(ContainerBuilder $container, string $id, array $config, ?string $defaultEntryPointId)
  147. {
  148. throw new \Exception('The old authentication system is not supported with login_link.');
  149. }
  150. }