-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHttps.php
188 lines (154 loc) · 5 KB
/
Https.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
declare(strict_types = 1);
namespace Middlewares;
use Middlewares\Utils\Factory;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class Https implements MiddlewareInterface
{
const HEADER = 'Strict-Transport-Security';
/**
* @var int One year by default
*/
private $maxAge = 31536000;
/**
* @var bool Whether add the preload directive or not
*/
private $preload = false;
/**
* @var bool Whether include subdomains
*/
private $includeSubdomains = false;
/**
* @var bool Whether check the headers "X-Forwarded-Proto: https" or "X-Forwarded-Port: 443"
*/
private $checkHttpsForward = false;
/**
* @var bool Whether to redirect on headers check
*/
private $redirect = true;
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;
public function __construct(ResponseFactoryInterface $responseFactory = null)
{
$this->responseFactory = $responseFactory ?: Factory::getResponseFactory();
}
/**
* Configure the max-age HSTS in seconds.
*/
public function maxAge(int $maxAge): self
{
$this->maxAge = $maxAge;
return $this;
}
/**
* Configure the includeSubDomains HSTS directive.
*/
public function includeSubdomains(bool $includeSubdomains = true): self
{
$this->includeSubdomains = $includeSubdomains;
return $this;
}
/**
* Configure the preload HSTS directive.
*/
public function preload(bool $preload = true): self
{
$this->preload = $preload;
return $this;
}
/**
* Configure whether check the following headers before redirect:
* X-Forwarded-Proto: https
* X-Forwarded-Port: 443.
*/
public function checkHttpsForward(bool $checkHttpsForward = true): self
{
$this->checkHttpsForward = $checkHttpsForward;
return $this;
}
/**
* Enabled or disable redirecting all together.
*/
public function redirect(bool $redirect = true): self
{
$this->redirect = $redirect;
return $this;
}
/**
* Process a request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$uri = $request->getUri();
if (strtolower($uri->getScheme()) !== 'https') {
if ($this->mustRedirect($request)) {
return $this->responseFactory->createResponse(301)
->withHeader('Location', (string) self::withHttps($uri));
}
$request = $request->withUri(self::withHttps($uri));
}
$response = $handler->handle($request);
if (!empty($this->maxAge)) {
$header = sprintf(
'max-age=%d%s%s',
$this->maxAge,
$this->includeSubdomains ? ';includeSubDomains' : '',
$this->preload ? ';preload' : ''
);
$response = $response
->withHeader(self::HEADER, $header);
}
if ($response->hasHeader('Location')) {
$location = parse_url($response->getHeaderLine('Location'));
if (!empty($location['host']) && $location['host'] === $uri->getHost()) {
$location['scheme'] = 'https';
unset($location['port']);
return $response->withHeader('Location', self::unParseUrl($location));
}
}
return $response;
}
/**
* Check whether the request must be redirected or not.
*/
private function mustRedirect(ServerRequestInterface $request): bool
{
if ($this->redirect === false) {
return false;
}
return !$this->checkHttpsForward || (
$request->getHeaderLine('X-Forwarded-Proto') !== 'https' &&
$request->getHeaderLine('X-Forwarded-Port') !== '443'
);
}
/**
* Stringify a url parsed with parse_url()
*/
private function unParseUrl(array $url): string
{
$scheme = isset($url['scheme']) ? $url['scheme'] . '://' : '';
$host = $url['host'] ?? '';
$port = isset($url['port']) ? ':' . $url['port'] : '';
$user = $url['user'] ?? '';
$pass = isset($url['pass']) ? ':' . $url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = $url['path'] ?? '';
$query = isset($url['query']) ? '?' . $url['query'] : '';
$fragment = isset($url['fragment']) ? '#' . $url['fragment'] : '';
return "{$scheme}{$user}{$pass}{$host}{$port}{$path}{$query}{$fragment}";
}
/**
* Converts a http uri to https.
*/
private static function withHttps(UriInterface $uri): UriInterface
{
return $uri->withScheme('https')->withPort(443);
}
}