1 | <?php |
||
12 | abstract class BaseUrlSigner implements UrlSigner |
||
13 | { |
||
14 | /** |
||
15 | * The key that is used to generate secure signatures. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $signatureKey; |
||
20 | |||
21 | /** |
||
22 | * The URL's query parameter name for the expiration. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $expiresParameter; |
||
27 | |||
28 | /** |
||
29 | * The URL's query parameter name for the signature. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $signatureParameter; |
||
34 | |||
35 | /** |
||
36 | * @param string $signatureKey |
||
37 | * @param string $expiresParameter |
||
38 | * @param string $signatureParameter |
||
39 | * |
||
40 | * @throws InvalidSignatureKey |
||
41 | */ |
||
42 | public function __construct($signatureKey, $expiresParameter = 'expires', $signatureParameter = 'signature') |
||
52 | |||
53 | /** |
||
54 | * Get a secure URL to a controller action. |
||
55 | * |
||
56 | * @param string $url |
||
57 | * @param \DateTime|int $expiration |
||
58 | * |
||
59 | * @throws InvalidExpiration |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function sign($url, $expiration) |
||
72 | |||
73 | /** |
||
74 | * Add expiration and signature query parameters to an url. |
||
75 | * |
||
76 | * @param UriInterface $url |
||
77 | * @param string $expiration |
||
78 | * @param string $signature |
||
79 | * |
||
80 | * @return \League\Url\UrlImmutable |
||
81 | */ |
||
82 | protected function signUrl(UriInterface $url, $expiration, $signature) |
||
91 | |||
92 | /** |
||
93 | * Validate a signed url. |
||
94 | * |
||
95 | * @param string $url |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function validate($url) |
||
121 | |||
122 | /** |
||
123 | * Check if a query is missing a necessary parameter. |
||
124 | * |
||
125 | * @param array $query |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | protected function isMissingAQueryParameter(array $query) |
||
141 | |||
142 | /** |
||
143 | * Check if a timestamp is in the future. |
||
144 | * |
||
145 | * @param int $timestamp |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | protected function isFuture($timestamp) |
||
153 | |||
154 | /** |
||
155 | * Retrieve the intended URL by stripping off the UrlSigner specific parameters. |
||
156 | * |
||
157 | * @param UriInterface $url |
||
158 | * |
||
159 | * @return UriInterface |
||
160 | */ |
||
161 | protected function getIntendedUrl(UriInterface $url) |
||
170 | |||
171 | /** |
||
172 | * Retrieve the expiration timestamp for a link based on an absolute DateTime or a relative number of days. |
||
173 | * |
||
174 | * @param \DateTime|int $expiration The expiration date of this link. |
||
175 | * - DateTime: The value will be used as expiration date |
||
176 | * - int: The expiration time will be set to X days from now |
||
177 | * |
||
178 | * @throws \Spatie\UrlSigner\Exceptions\InvalidExpiration |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function getExpirationTimestamp($expiration) |
||
198 | |||
199 | /** |
||
200 | * Determine if the url has a forged signature. |
||
201 | * |
||
202 | * @param UriInterface $url |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | protected function hasValidSignature(UriInterface $url) |
||
219 | |||
220 | /** |
||
221 | * Turn a key => value associate array into a query string. |
||
222 | * |
||
223 | * @param array $query |
||
224 | * |
||
225 | * @return string|null |
||
226 | */ |
||
227 | protected function buildQueryStringFromArray(array $query) |
||
236 | } |
||
237 |