DigestAuthenticationEntryPoint.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Component\Security\Http\EntryPoint;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Exception\NonceExpiredException;
  16. /**
  17. * DigestAuthenticationEntryPoint starts an HTTP Digest authentication.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class DigestAuthenticationEntryPoint implements AuthenticationEntryPointInterface
  22. {
  23. private $secret;
  24. private $realmName;
  25. private $nonceValiditySeconds;
  26. private $logger;
  27. public function __construct($realmName, $secret, $nonceValiditySeconds = 300, LoggerInterface $logger = null)
  28. {
  29. $this->realmName = $realmName;
  30. $this->secret = $secret;
  31. $this->nonceValiditySeconds = $nonceValiditySeconds;
  32. $this->logger = $logger;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function start(Request $request, AuthenticationException $authException = null)
  38. {
  39. $expiryTime = microtime(true) + $this->nonceValiditySeconds * 1000;
  40. $signatureValue = md5($expiryTime.':'.$this->secret);
  41. $nonceValue = $expiryTime.':'.$signatureValue;
  42. $nonceValueBase64 = base64_encode($nonceValue);
  43. $authenticateHeader = sprintf('Digest realm="%s", qop="auth", nonce="%s"', $this->realmName, $nonceValueBase64);
  44. if ($authException instanceof NonceExpiredException) {
  45. $authenticateHeader .= ', stale="true"';
  46. }
  47. if (null !== $this->logger) {
  48. $this->logger->debug('WWW-Authenticate header sent.', array('header' => $authenticateHeader));
  49. }
  50. $response = new Response();
  51. $response->headers->set('WWW-Authenticate', $authenticateHeader);
  52. $response->setStatusCode(401);
  53. return $response;
  54. }
  55. /**
  56. * @deprecated Since version 2.8, to be removed in 3.0. Use getSecret() instead.
  57. */
  58. public function getKey()
  59. {
  60. @trigger_error(__METHOD__.'() is deprecated since Symfony 2.8 and will be removed in 3.0. Use getSecret() instead.', E_USER_DEPRECATED);
  61. return $this->getSecret();
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getSecret()
  67. {
  68. return $this->secret;
  69. }
  70. /**
  71. * @return string
  72. */
  73. public function getRealmName()
  74. {
  75. return $this->realmName;
  76. }
  77. }