HttpDigestFactory.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * HttpDigestFactory creates services for HTTP digest authentication.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class HttpDigestFactory implements SecurityFactoryInterface
  21. {
  22. public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
  23. {
  24. $provider = 'security.authentication.provider.dao.'.$id;
  25. $container
  26. ->setDefinition($provider, new DefinitionDecorator('security.authentication.provider.dao'))
  27. ->replaceArgument(0, new Reference($userProvider))
  28. ->replaceArgument(1, new Reference('security.user_checker.'.$id))
  29. ->replaceArgument(2, $id)
  30. ;
  31. // entry point
  32. $entryPointId = $this->createEntryPoint($container, $id, $config, $defaultEntryPoint);
  33. // listener
  34. $listenerId = 'security.authentication.listener.digest.'.$id;
  35. $listener = $container->setDefinition($listenerId, new DefinitionDecorator('security.authentication.listener.digest'));
  36. $listener->replaceArgument(1, new Reference($userProvider));
  37. $listener->replaceArgument(2, $id);
  38. $listener->replaceArgument(3, new Reference($entryPointId));
  39. $listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
  40. return array($provider, $listenerId, $entryPointId);
  41. }
  42. public function getPosition()
  43. {
  44. return 'http';
  45. }
  46. public function getKey()
  47. {
  48. return 'http-digest';
  49. }
  50. public function addConfiguration(NodeDefinition $node)
  51. {
  52. $node
  53. ->beforeNormalization()
  54. ->ifTrue(function ($v) { return isset($v['key']); })
  55. ->then(function ($v) {
  56. if (isset($v['secret'])) {
  57. throw new \LogicException('Cannot set both key and secret options for http_digest, use only secret instead.');
  58. }
  59. @trigger_error('http_digest.key is deprecated since Symfony 2.8 and will be removed in 3.0. Use http_digest.secret instead.', E_USER_DEPRECATED);
  60. $v['secret'] = $v['key'];
  61. unset($v['key']);
  62. return $v;
  63. })
  64. ->end()
  65. ->children()
  66. ->scalarNode('provider')->end()
  67. ->scalarNode('realm')->defaultValue('Secured Area')->end()
  68. ->scalarNode('secret')->isRequired()->cannotBeEmpty()->end()
  69. ->end()
  70. ;
  71. }
  72. protected function createEntryPoint($container, $id, $config, $defaultEntryPoint)
  73. {
  74. if (null !== $defaultEntryPoint) {
  75. return $defaultEntryPoint;
  76. }
  77. $entryPointId = 'security.authentication.digest_entry_point.'.$id;
  78. $container
  79. ->setDefinition($entryPointId, new DefinitionDecorator('security.authentication.digest_entry_point'))
  80. ->addArgument($config['realm'])
  81. ->addArgument($config['secret'])
  82. ;
  83. return $entryPointId;
  84. }
  85. }