UriSigner.php 2.9 KB

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 = [];
  46. }
  47. $uri = $this->buildUrl($url, $params);
  48. $params[$this->parameter] = $this->computeHash($uri);
  49. return $this->buildUrl($url, $params);
  50. }
  51. /**
  52. * Checks that a URI contains the correct hash.
  53. *
  54. * @param string $uri A signed URI
  55. *
  56. * @return bool True if the URI is signed correctly, false otherwise
  57. */
  58. public function check($uri)
  59. {
  60. $url = parse_url($uri);
  61. if (isset($url['query'])) {
  62. parse_str($url['query'], $params);
  63. } else {
  64. $params = [];
  65. }
  66. if (empty($params[$this->parameter])) {
  67. return false;
  68. }
  69. $hash = $params[$this->parameter];
  70. unset($params[$this->parameter]);
  71. return $this->computeHash($this->buildUrl($url, $params)) === $hash;
  72. }
  73. private function computeHash($uri)
  74. {
  75. return base64_encode(hash_hmac('sha256', $uri, $this->secret, true));
  76. }
  77. private function buildUrl(array $url, array $params = [])
  78. {
  79. ksort($params, SORT_STRING);
  80. $url['query'] = http_build_query($params, '', '&');
  81. $scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
  82. $host = isset($url['host']) ? $url['host'] : '';
  83. $port = isset($url['port']) ? ':'.$url['port'] : '';
  84. $user = isset($url['user']) ? $url['user'] : '';
  85. $pass = isset($url['pass']) ? ':'.$url['pass'] : '';
  86. $pass = ($user || $pass) ? "$pass@" : '';
  87. $path = isset($url['path']) ? $url['path'] : '';
  88. $query = isset($url['query']) && $url['query'] ? '?'.$url['query'] : '';
  89. $fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
  90. return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
  91. }
  92. }