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 | * @var string |
||
37 | */ |
||
38 | public $secret; |
||
39 | |||
40 | 16 | public function init(): void |
|
54 | |||
55 | 16 | public function setDefaultExpirationInterval(string $interval): void |
|
60 | /** |
||
61 | * Calculates the HMAC for a URL. |
||
62 | **/ |
||
63 | 10 | public function calculateHMAC( |
|
81 | |||
82 | /** |
||
83 | * This adds an HMAC to a list of query params. |
||
84 | * If |
||
85 | * @param array $queryParams List of query parameters |
||
86 | * @param bool $allowAddition Whether to allow extra parameters to be added. |
||
87 | * @throws \Exception |
||
88 | * @return void |
||
89 | */ |
||
90 | 12 | public function signParams( |
|
112 | |||
113 | /** |
||
114 | * Adds the expiration param if needed. |
||
115 | */ |
||
116 | 10 | private function addExpiration(array &$params, ?\DateTimeInterface $expiration = null): void |
|
125 | |||
126 | 6 | private function checkExpiration(array $params): bool |
|
131 | |||
132 | /** |
||
133 | * Adds the keys of all params to the param array so it is included for signing. |
||
134 | * @param array $params |
||
135 | */ |
||
136 | 4 | private function addParamKeys(array &$params): void |
|
144 | |||
145 | /** |
||
146 | * Extracts the signed params from an array of params. |
||
147 | * @param array $params |
||
148 | * @return array |
||
149 | */ |
||
150 | 8 | private function getSignedParams(array $params): array |
|
167 | |||
168 | /** |
||
169 | * Verifies the params for a specific route. |
||
170 | * Checks that the HMAC is present and valid. |
||
171 | * Checks that the HMAC is not expired. |
||
172 | * @param array $params |
||
173 | * @throws \Exception |
||
174 | * @return bool |
||
175 | */ |
||
176 | 8 | public function verify(array $params, string $route):bool |
|
192 | |||
193 | 10 | private function urlEncode(string $bytes): string |
|
197 | } |
||
198 |