UriSigner.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 string $secret;
  20. private string $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. public function sign(string $uri): string
  37. {
  38. $url = parse_url($uri);
  39. if (isset($url['query'])) {
  40. parse_str($url['query'], $params);
  41. } else {
  42. $params = [];
  43. }
  44. $uri = $this->buildUrl($url, $params);
  45. $params[$this->parameter] = $this->computeHash($uri);
  46. return $this->buildUrl($url, $params);
  47. }
  48. /**
  49. * Checks that a URI contains the correct hash.
  50. */
  51. public function check(string $uri): bool
  52. {
  53. $url = parse_url($uri);
  54. if (isset($url['query'])) {
  55. parse_str($url['query'], $params);
  56. } else {
  57. $params = [];
  58. }
  59. if (empty($params[$this->parameter])) {
  60. return false;
  61. }
  62. $hash = $params[$this->parameter];
  63. unset($params[$this->parameter]);
  64. return hash_equals($this->computeHash($this->buildUrl($url, $params)), $hash);
  65. }
  66. public function checkRequest(Request $request): bool
  67. {
  68. $qs = ($qs = $request->server->get('QUERY_STRING')) ? '?'.$qs : '';
  69. // we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
  70. return $this->check($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().$qs);
  71. }
  72. private function computeHash(string $uri): string
  73. {
  74. return base64_encode(hash_hmac('sha256', $uri, $this->secret, true));
  75. }
  76. private function buildUrl(array $url, array $params = []): string
  77. {
  78. ksort($params, \SORT_STRING);
  79. $url['query'] = http_build_query($params, '', '&');
  80. $scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
  81. $host = $url['host'] ?? '';
  82. $port = isset($url['port']) ? ':'.$url['port'] : '';
  83. $user = $url['user'] ?? '';
  84. $pass = isset($url['pass']) ? ':'.$url['pass'] : '';
  85. $pass = ($user || $pass) ? "$pass@" : '';
  86. $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. }