1 | <?php |
||
8 | class HstsMiddleware |
||
9 | { |
||
10 | /** |
||
11 | * Next handler for the Guzzle middleware |
||
12 | * |
||
13 | * @var callable |
||
14 | */ |
||
15 | private $nextHandler; |
||
16 | |||
17 | /** |
||
18 | * Store instances cache |
||
19 | * |
||
20 | * @var StoreInterface[] |
||
21 | */ |
||
22 | private $storeInstances = []; |
||
23 | |||
24 | /** |
||
25 | * HstsMiddleware constructor |
||
26 | * |
||
27 | * @param callable $nextHandler Next handler to invoke. |
||
28 | */ |
||
29 | 17 | public function __construct(callable $nextHandler) |
|
33 | |||
34 | /** |
||
35 | * Invoke the guzzle middleware |
||
36 | * |
||
37 | * @param Request $request |
||
38 | * @param array $options |
||
39 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
40 | */ |
||
41 | 17 | public function __invoke(Request $request, array $options) |
|
54 | |||
55 | /** |
||
56 | * Rewrite the requested uri if the requested host is a known HSTS host |
||
57 | * |
||
58 | * @param Request $request |
||
59 | * @param StoreInterface $store |
||
60 | * @return Request |
||
61 | */ |
||
62 | 16 | private function handleHstsRewrite(Request $request, StoreInterface $store) |
|
78 | |||
79 | /** |
||
80 | * Register the host as a known HSTS host if the header is set properly |
||
81 | * |
||
82 | * @param Response $response |
||
83 | * @param Request $request |
||
84 | * @param StoreInterface $store |
||
85 | * @return Response |
||
86 | */ |
||
87 | 16 | private function handleHstsRegistering(Response $response, Request $request, StoreInterface $store) |
|
116 | |||
117 | /** |
||
118 | * Check if the given domain is a known HSTS host |
||
119 | * |
||
120 | * @param StoreInterface $store |
||
121 | * @param string $domainName |
||
122 | * @return bool |
||
123 | */ |
||
124 | 15 | private function isKnownHstsHosts(StoreInterface $store, $domainName) |
|
147 | |||
148 | /** |
||
149 | * Get the store instance, possibly cached |
||
150 | * |
||
151 | * @param array $options |
||
152 | * @return StoreInterface |
||
153 | * @throws InvalidArgumentException |
||
154 | */ |
||
155 | 17 | private function getStoreInstance(array $options) |
|
156 | { |
||
157 | // Get option or use the default store |
||
158 | 17 | $store = isset($options['hsts_store']) ? $options['hsts_store'] : ArrayStore::class; |
|
159 | |||
160 | // Just return the store if it is already an instance |
||
161 | 17 | if ($store instanceof StoreInterface) { |
|
162 | 1 | return $store; |
|
163 | } |
||
164 | |||
165 | // Instanciate new store or return already instanciated store |
||
166 | 16 | if (is_string($store) && class_exists($store) && class_implements($store, StoreInterface::class)) { |
|
167 | 15 | if (!isset($this->storeInstances[$store])) { |
|
168 | 15 | $this->storeInstances[$store] = new $store(); |
|
169 | } |
||
170 | |||
171 | 15 | return $this->storeInstances[$store]; |
|
172 | } |
||
173 | |||
174 | 1 | throw new InvalidArgumentException('hsts_store must be an ' . StoreInterface::class . |
|
175 | 1 | ' instance or the name of a class extending ' . StoreInterface::class); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Parse the HSTS header |
||
180 | * |
||
181 | * @param string $header |
||
182 | * @return array |
||
183 | */ |
||
184 | 12 | private function parseHeader($header) |
|
205 | |||
206 | /** |
||
207 | * Check if a host is an ip address |
||
208 | * |
||
209 | * @param string $host |
||
210 | * @return bool |
||
211 | */ |
||
212 | 16 | private function isIpAddress($host) |
|
216 | |||
217 | /** |
||
218 | * Handler for registering the middleware |
||
219 | * |
||
220 | * @return \Closure |
||
221 | */ |
||
222 | public static function handler() |
||
228 | } |
||
229 |