/
HttpBasicAuthentication.php
366 lines (316 loc) · 9.56 KB
/
HttpBasicAuthentication.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/*
* This file is part of Slim HTTP Basic Authentication middleware
*
* Copyright (c) 2013-2019 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/tuupola/slim-basic-auth
*
*/
namespace Slim\Middleware;
use Slim\Middleware\HttpBasicAuthentication\AuthenticatorInterface;
use Slim\Middleware\HttpBasicAuthentication\ArrayAuthenticator;
use Slim\Middleware\HttpBasicAuthentication\RequestMethodRule;
use Slim\Middleware\HttpBasicAuthentication\RequestPathRule;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class HttpBasicAuthentication
{
private $rules;
private $options = [
"secure" => true,
"relaxed" => ["localhost", "127.0.0.1"],
"users" => null,
"path" => null,
"passthrough" => null,
"realm" => "Protected",
"environment" => "HTTP_AUTHORIZATION",
"authenticator" => null,
"callback" => null,
"error" => null
];
public function __construct($options = [])
{
/* Setup stack for rules */
$this->rules = new \SplStack;
/* Store passed in options overwriting any defaults */
$this->hydrate($options);
/* If array of users was passed in options create an authenticator */
if (is_array($this->options["users"])) {
$this->options["authenticator"] = new ArrayAuthenticator([
"users" => $this->options["users"]
]);
}
/* If nothing was passed in options add default rules. */
if (!isset($options["rules"])) {
$this->addRule(new RequestMethodRule([
"passthrough" => ["OPTIONS"]
]));
}
/* If path was given in easy mode add rule for it. */
if (null !== $this->options["path"]) {
$this->addRule(new RequestPathRule([
"path" => $this->options["path"],
"passthrough" => $this->options["passthrough"]
]));
}
/* There must be an authenticator either passed via options */
/* or added because $this->options["users"] was an array. */
if (null === $this->options["authenticator"]) {
throw new \RuntimeException("Authenticator or users array must be given");
}
}
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
{
$host = $request->getUri()->getHost();
$scheme = $request->getUri()->getScheme();
$server_params = $request->getServerParams();
/* If rules say we should not authenticate call next and return. */
if (false === $this->shouldAuthenticate($request)) {
return $next($request, $response);
}
/* HTTP allowed only if secure is false or server is in relaxed array. */
if ("https" !== $scheme && true === $this->options["secure"]) {
if (!in_array($host, $this->options["relaxed"])) {
$message = sprintf(
"Insecure use of middleware over %s denied by configuration.",
strtoupper($scheme)
);
throw new \RuntimeException($message);
}
}
/* Just in case. */
$user = false;
$password = false;
/* If using PHP in CGI mode. */
if (isset($server_params[$this->options["environment"]])) {
if (preg_match("/Basic\s+(.*)$/i", $server_params[$this->options["environment"]], $matches)) {
list($user, $password) = explode(":", base64_decode($matches[1]), 2);
}
} else {
if (isset($server_params["PHP_AUTH_USER"])) {
$user = $server_params["PHP_AUTH_USER"];
}
if (isset($server_params["PHP_AUTH_PW"])) {
$password = $server_params["PHP_AUTH_PW"];
}
}
$params = ["user" => $user, "password" => $password];
/* Check if user authenticates. */
if (false === $this->options["authenticator"]($params)) {
/* Set response headers before giving it to error callback */
$response = $response
->withStatus(401)
->withHeader("WWW-Authenticate", sprintf('Basic realm="%s"', $this->options["realm"]));
return $this->error($request, $response, [
"message" => "Authentication failed",
"user" => $user,
]);
}
/* If callback returns false return with 401 Unauthorized. */
if (is_callable($this->options["callback"])) {
if (false === $this->options["callback"]($request, $response, $params)) {
/* Set response headers before giving it to error callback */
$response = $response
->withStatus(401)
->withHeader("WWW-Authenticate", sprintf('Basic realm="%s"', $this->options["realm"]));
return $this->error($request, $response, [
"message" => "Callback returned false",
"user" => $user
]);
}
}
/* Everything ok, call next middleware. */
return $next($request, $response);
}
private function hydrate($data = [])
{
foreach ($data as $key => $value) {
$method = "set" . ucfirst($key);
if (method_exists($this, $method)) {
call_user_func([$this, $method], $value);
}
}
}
private function shouldAuthenticate(RequestInterface $request)
{
/* If any of the rules in stack return false will not authenticate */
foreach ($this->rules as $callable) {
if (false === $callable($request)) {
return false;
}
}
return true;
}
/**
* Call the error handler if it exists
*
* @return void
*/
public function error(RequestInterface $request, ResponseInterface $response, $arguments)
{
if (is_callable($this->options["error"])) {
$handler_response = $this->options["error"]($request, $response, $arguments);
if (is_a($handler_response, "\Psr\Http\Message\ResponseInterface")) {
return $handler_response;
}
}
return $response;
}
public function getAuthenticator()
{
return $this->options["authenticator"];
}
public function setAuthenticator($authenticator)
{
$this->options["authenticator"] = $authenticator;
return $this;
}
public function getUsers()
{
return $this->options["users"];
}
/* Do not mess with users right now */
private function setUsers($users)
{
$this->options["users"] = $users;
return $this;
}
public function getPath()
{
return $this->options["path"];
}
/* Do not mess with path right now */
private function setPath($path)
{
$this->options["path"] = $path;
return $this;
}
public function getPassthrough()
{
return $this->options["passthrough"];
}
private function setPassthrough($passthrough)
{
$this->options["passthrough"] = $passthrough;
return $this;
}
public function getRealm()
{
return $this->options["realm"];
}
public function setRealm($realm)
{
$this->options["realm"] = $realm;
return $this;
}
public function getEnvironment()
{
return $this->options["environment"];
}
public function setEnvironment($environment)
{
$this->options["environment"] = $environment;
return $this;
}
/**
* Get the secure flag
*
* @return boolean
*/
public function getSecure()
{
return $this->options["secure"];
}
/**
* Set the secure flag
*
* @return self
*/
public function setSecure($secure)
{
$this->options["secure"] = !!$secure;
return $this;
}
/**
* Get hosts where secure rule is relaxed
*
* @return string
*/
public function getRelaxed()
{
return $this->options["relaxed"];
}
/**
* Set hosts where secure rule is relaxed
*
* @return self
*/
public function setRelaxed(array $relaxed)
{
$this->options["relaxed"] = $relaxed;
return $this;
}
/**
* Get the callback
*
* @return string
*/
public function getCallback()
{
return $this->options["callback"];
}
/**
* Set the callback
*
* @return self
*/
public function setCallback($callback)
{
$this->options["callback"] = $callback;
return $this;
}
/**
* Get the error handler
*
* @return string
*/
public function getError()
{
return $this->options["error"];
}
/**
* Set the error handler
*
* @return self
*/
public function setError($error)
{
$this->options["error"] = $error;
return $this;
}
public function getRules()
{
return $this->rules;
}
public function setRules(array $rules)
{
/* Clear the stack */
unset($this->rules);
$this->rules = new \SplStack;
/* Add the rules */
foreach ($rules as $callable) {
$this->addRule($callable);
}
return $this;
}
public function addRule($callable)
{
$this->rules->push($callable);
return $this;
}
}