1 | <?php |
||
12 | class UrlSigner extends Component |
||
13 | { |
||
14 | /** |
||
15 | * @var string The name of the URL param for the HMAC |
||
16 | */ |
||
17 | public $hmacParam = 'hmac'; |
||
18 | |||
19 | /** |
||
20 | * @var string The name of the URL param for the parameters |
||
21 | */ |
||
22 | public $paramsParam = 'params'; |
||
23 | |||
24 | /** |
||
25 | * @var string The name of the URL param for the expiration date time |
||
26 | */ |
||
27 | public $expirationParam = 'expires'; |
||
28 | |||
29 | /** |
||
30 | * Note that expiration dates cannot be disabled. If you really need to you can set a longer duration for the links. |
||
31 | * @var \DateInterval The default interval for link validity (default: 1 week) |
||
32 | */ |
||
33 | private $_defaultExpirationInterval; |
||
34 | |||
35 | /** |
||
36 | * Stores the current timestamp, primarily used for testing. |
||
37 | * @var int |
||
38 | */ |
||
39 | private $_currentTimestamp; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | public $secret; |
||
45 | |||
46 | 26 | public function init(): void |
|
62 | |||
63 | 26 | public function setDefaultExpirationInterval(string $interval): void |
|
67 | |||
68 | 2 | public function setCurrentTimestamp(?int $time): void |
|
72 | |||
73 | /** |
||
74 | * Calculates the HMAC for a URL. |
||
75 | **/ |
||
76 | 18 | public function calculateHMAC( |
|
94 | |||
95 | /** |
||
96 | * This adds an HMAC to a list of query params. |
||
97 | * If |
||
98 | * @param array $queryParams List of query parameters |
||
99 | * @param bool $allowAddition Whether to allow extra parameters to be added. |
||
100 | * @throws \Exception |
||
101 | * @return void |
||
102 | */ |
||
103 | 20 | public function signParams( |
|
125 | |||
126 | /** |
||
127 | * Adds the expiration param if needed. |
||
128 | */ |
||
129 | 18 | private function addExpiration(array &$params, ?\DateTimeInterface $expiration = null): void |
|
138 | |||
139 | 16 | private function time(): int |
|
143 | |||
144 | 10 | private function checkExpiration(array $params): void |
|
153 | |||
154 | /** |
||
155 | * Adds the keys of all params to the param array so it is included for signing. |
||
156 | * @param array $params |
||
157 | */ |
||
158 | 12 | private function addParamKeys(array &$params): void |
|
166 | |||
167 | /** |
||
168 | * Extracts the signed params from an array of params. |
||
169 | * @param array $params |
||
170 | * @return array |
||
171 | */ |
||
172 | 14 | private function getSignedParams(array $params): array |
|
189 | |||
190 | /** |
||
191 | * Verifies the params for a specific route. |
||
192 | * Checks that the HMAC is present and valid. |
||
193 | * Checks that the HMAC is not expired. |
||
194 | * @param array $params |
||
195 | * @throws \Exception |
||
196 | * @return bool |
||
197 | */ |
||
198 | 16 | public function verify(array $params, string $route):void |
|
214 | |||
215 | 18 | private function urlEncode(string $bytes): string |
|
219 | } |
||
220 |