UriSigner.php 3.2 KB

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