1 | <?php |
||
2 | |||
3 | namespace Koded\Framework\Middleware; |
||
4 | |||
5 | use Koded\Http\Interfaces\HttpStatus; |
||
6 | use Koded\Http\ServerResponse; |
||
7 | use Koded\Stdlib\Configuration; |
||
8 | use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
||
9 | use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface}; |
||
10 | |||
11 | class HSTSMiddleware implements MiddlewareInterface |
||
12 | { |
||
13 | private int $maxAge = 0; |
||
14 | private string $includeSubDomains = ''; |
||
15 | |||
16 | public function __construct(Configuration $settings) |
||
17 | { |
||
18 | $this->maxAge = (int)$settings->get('hsts.maxAge', $this->maxAge); |
||
19 | if ($settings->get('hsts.includeSubdomains', $this->includeSubDomains)) { |
||
20 | $this->includeSubDomains = ';includeSubDomains'; |
||
21 | } |
||
22 | } |
||
23 | |||
24 | public function process( |
||
25 | ServerRequestInterface $request, |
||
26 | RequestHandlerInterface $handler): ResponseInterface |
||
27 | { |
||
28 | if ('https' !== $request->getUri()->getScheme()) { |
||
29 | return (new ServerResponse(null, HttpStatus::MOVED_PERMANENTLY)) |
||
30 | ->withHeader('Location', (string)$request->getUri()->withScheme('https')); |
||
31 | } |
||
32 | return $handler->handle($request)->withHeader( |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
33 | 'Strict-Transport-Security', 'max-age=' . $this->maxAge . $this->includeSubDomains |
||
34 | ); |
||
35 | } |
||
36 | } |
||
37 |