UriSigner.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\HttpKernel;
  11. /**
  12. * Signs URIs.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class UriSigner
  17. {
  18. private $secret;
  19. private $parameter;
  20. /**
  21. * @param string $secret A secret
  22. * @param string $parameter Query string parameter to use
  23. */
  24. public function __construct(string $secret, string $parameter = '_hash')
  25. {
  26. $this->secret = $secret;
  27. $this->parameter = $parameter;
  28. }
  29. /**
  30. * Signs a URI.
  31. *
  32. * The given URI is signed by adding the query string parameter
  33. * which value depends on the URI and the secret.
  34. *
  35. * @param string $uri A URI to sign
  36. *
  37. * @return string The signed URI
  38. */
  39. public function sign($uri)
  40. {
  41. $url = parse_url($uri);
  42. if (isset($url['query'])) {
  43. parse_str($url['query'], $params);
  44. } else {
  45. $params = array();
  46. }
  47. $uri = $this->buildUrl($url, $params);
  48. return $uri.(false === strpos($uri, '?') ? '?' : '&').$this->parameter.'='.$this->computeHash($uri);
  49. }
  50. /**
  51. * Checks that a URI contains the correct hash.
  52. *
  53. * @param string $uri A signed URI
  54. *
  55. * @return bool True if the URI is signed correctly, false otherwise
  56. */
  57. public function check($uri)
  58. {
  59. $url = parse_url($uri);
  60. if (isset($url['query'])) {
  61. parse_str($url['query'], $params);
  62. } else {
  63. $params = array();
  64. }
  65. if (empty($params[$this->parameter])) {
  66. return false;
  67. }
  68. $hash = urlencode($params[$this->parameter]);
  69. unset($params[$this->parameter]);
  70. return $this->computeHash($this->buildUrl($url, $params)) === $hash;
  71. }
  72. private function computeHash($uri)
  73. {
  74. return urlencode(base64_encode(hash_hmac('sha256', $uri, $this->secret, true)));
  75. }
  76. private function buildUrl(array $url, array $params = array())
  77. {
  78. ksort($params, SORT_STRING);
  79. $url['query'] = http_build_query($params, '', '&');
  80. $scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
  81. $host = isset($url['host']) ? $url['host'] : '';
  82. $port = isset($url['port']) ? ':'.$url['port'] : '';
  83. $user = isset($url['user']) ? $url['user'] : '';
  84. $pass = isset($url['pass']) ? ':'.$url['pass'] : '';
  85. $pass = ($user || $pass) ? "$pass@" : '';
  86. $path = isset($url['path']) ? $url['path'] : '';
  87. $query = isset($url['query']) && $url['query'] ? '?'.$url['query'] : '';
  88. $fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
  89. return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
  90. }
  91. }