|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr7Middlewares\Utils; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Middleware to create basic http authentication. |
|
11
|
|
|
*/ |
|
12
|
|
|
class BasicAuthentication |
|
13
|
|
|
{ |
|
14
|
|
|
use Utils\AuthenticationTrait; |
|
15
|
|
|
use Utils\AttributeTrait; |
|
16
|
|
|
|
|
17
|
|
|
const KEY = 'USERNAME'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Returns the username. |
|
21
|
|
|
* |
|
22
|
|
|
* @param ServerRequestInterface $request |
|
23
|
|
|
* |
|
24
|
|
|
* @return string|null |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function getUsername(ServerRequestInterface $request) |
|
27
|
|
|
{ |
|
28
|
|
|
return self::getAttribute($request, self::KEY); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Execute the middleware. |
|
33
|
|
|
* |
|
34
|
|
|
* @param ServerRequestInterface $request |
|
35
|
|
|
* @param ResponseInterface $response |
|
36
|
|
|
* @param callable $next |
|
37
|
|
|
* |
|
38
|
|
|
* @return ResponseInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
41
|
|
|
{ |
|
42
|
|
|
$authorization = self::parseAuthorizationHeader($request->getHeaderLine('Authorization')); |
|
43
|
|
|
|
|
44
|
|
|
if ($authorization && $this->checkUserPassword($authorization['username'], $authorization['password'])) { |
|
45
|
|
|
return $next( |
|
46
|
|
|
self::setAttribute($request, self::KEY, $authorization['username']), |
|
47
|
|
|
$response |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $response |
|
52
|
|
|
->withStatus(401) |
|
53
|
|
|
->withHeader('WWW-Authenticate', 'Basic realm="'.$this->realm.'"'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validate the user and password. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $username |
|
60
|
|
|
* @param string $password |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function checkUserPassword($username, $password) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!isset($this->users[$username]) || $this->users[$username] !== $password) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Parses the authorization header for a basic authentication. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $header |
|
77
|
|
|
* |
|
78
|
|
|
* @return false|array |
|
79
|
|
|
*/ |
|
80
|
|
|
private static function parseAuthorizationHeader($header) |
|
81
|
|
|
{ |
|
82
|
|
|
if (strpos($header, 'Basic') !== 0) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$header = explode(':', base64_decode(substr($header, 6)), 2); |
|
87
|
|
|
|
|
88
|
|
|
return [ |
|
89
|
|
|
'username' => $header[0], |
|
90
|
|
|
'password' => isset($header[1]) ? $header[1] : null, |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|