/
UriSigner.php
157 lines (129 loc) · 5.28 KB
/
UriSigner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
/**
* Signs URIs.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class UriSigner
{
private string $secret;
private string $parameter;
private string $expirationParameter;
/**
* @param string $secret A secret
* @param string $parameter Query string parameter to use
* @param string $expirationParameter Query string parameter to use for expiration
*/
public function __construct(#[\SensitiveParameter] string $secret, string $parameter = '_hash', string $expirationParameter = '_expiration')
{
$this->secret = $secret;
$this->parameter = $parameter;
$this->expirationParameter = $expirationParameter;
}
/**
* Signs a URI.
*
* The given URI is signed by adding the query string parameter
* which value depends on the URI and the secret.
*
* @param \DateTimeInterface|\DateInterval|int|null $expiration The expiration for the given URI.
* If $expiration is a \DateTimeInterface, it's expected to be the exact date + time.
* If $expiration is a \DateInterval, the interval is added to "now" to get the date + time.
* If $expiration is an int, it's expected to be a timestamp in seconds of the exact date + time.
* If $expiration is null, no expiration.
*
* The expiration is added as a query string parameter.
*/
public function sign(string $uri, \DateTimeInterface|\DateInterval|int $expiration = null): string
{
$url = parse_url($uri);
$params = [];
if (isset($url['query'])) {
parse_str($url['query'], $params);
}
if (isset($params[$this->expirationParameter])) {
throw new \LogicException(sprintf('URI query parameter conflict. Parameter name "%s" is reserved.', $this->expirationParameter));
}
if (null !== $expiration) {
$params[$this->expirationParameter] = $this->getExpirationTime($expiration);
}
$uri = $this->buildUrl($url, $params);
$params[$this->parameter] = $this->computeHash($uri);
return $this->buildUrl($url, $params);
}
/**
* Checks that a URI contains the correct hash.
* Also checks if the URI has not expired (If you used expiration during signing).
*/
public function check(string $uri): bool
{
$url = parse_url($uri);
$params = [];
if (isset($url['query'])) {
parse_str($url['query'], $params);
}
if (empty($params[$this->parameter])) {
return false;
}
$hash = $params[$this->parameter];
unset($params[$this->parameter]);
if (!hash_equals($this->computeHash($this->buildUrl($url, $params)), $hash)) {
return false;
}
if ($expiration = $params[$this->expirationParameter] ?? false) {
return !$this->isExpired((int) $expiration);
}
return true;
}
public function checkRequest(Request $request): bool
{
$qs = ($qs = $request->server->get('QUERY_STRING')) ? '?'.$qs : '';
// we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
return $this->check($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().$qs);
}
private function computeHash(string $uri): string
{
return base64_encode(hash_hmac('sha256', $uri, $this->secret, true));
}
private function buildUrl(array $url, array $params = []): string
{
ksort($params, \SORT_STRING);
$url['query'] = http_build_query($params, '', '&');
$scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
$host = $url['host'] ?? '';
$port = isset($url['port']) ? ':'.$url['port'] : '';
$user = $url['user'] ?? '';
$pass = isset($url['pass']) ? ':'.$url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = $url['path'] ?? '';
$query = $url['query'] ? '?'.$url['query'] : '';
$fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
}
private function getExpirationTime(\DateTimeInterface|\DateInterval|int $expiration): string
{
if ($expiration instanceof \DateTimeInterface) {
return $expiration->format('U');
}
if ($expiration instanceof \DateInterval) {
return (new \DateTimeImmutable())->add($expiration)->format('U');
}
return (string) $expiration;
}
private function isExpired(int $expiration): bool
{
return time() >= $expiration;
}
}
if (!class_exists(\Symfony\Component\HttpKernel\UriSigner::class, false)) {
class_alias(UriSigner::class, \Symfony\Component\HttpKernel\UriSigner::class);
}