1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* php version 7.1.0 |
11
|
|
|
* |
12
|
|
|
* @category Web-security |
13
|
|
|
* @package Shieldon |
14
|
|
|
* @author Terry Lin <[email protected]> |
15
|
|
|
* @copyright 2019 terrylinooo |
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
18
|
|
|
* @see https://shieldon.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall\Middleware; |
24
|
|
|
|
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
27
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
28
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
29
|
|
|
use Shieldon\Psr7\Response; |
30
|
|
|
use InvalidArgumentException; |
31
|
|
|
use function array_column; |
32
|
|
|
use function count; |
33
|
|
|
use function password_verify; |
34
|
|
|
use function strpos; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* A PSR-15 middleware that provides WWW-Authenticate protection. |
38
|
|
|
*/ |
39
|
|
|
class HttpAuthentication implements MiddlewareInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* 401 - Unauthorized. |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
const HTTP_STATUS_CODE = 401; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The URL list that you want to protect. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $list = [ |
54
|
|
|
[ |
55
|
|
|
// Begin-with URL |
56
|
|
|
'url' => '/wp-amdin', |
57
|
|
|
// Username |
58
|
|
|
'user' => 'wp_shieldon_user', |
59
|
|
|
// Password encrypted by `password_hash()` function. |
60
|
|
|
// In this case, the uncrypted string is `wp_shieldon_pass`. |
61
|
|
|
'pass' => '$2y$10$eA/S6rH3JDkYV9nrrUvuMOTh8Q/ts33DdCerbNAUpdwtSl3Xq9cQq', |
62
|
|
|
], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The text displays on prompted window. |
67
|
|
|
* Most modern browsers won't show this anymore. You can ignore that. |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $realm; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor. |
75
|
|
|
* |
76
|
|
|
* @param array $list The list that want to be protected. |
77
|
|
|
* @param string $realm The welcome message. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function __construct(array $list = [], string $realm = 'Welcome to area 51.') |
82
|
|
|
{ |
83
|
87 |
|
$this->set($list); |
84
|
|
|
$this->realm = $realm; |
85
|
87 |
|
} |
86
|
87 |
|
|
87
|
|
|
/** |
88
|
|
|
* Invoker. |
89
|
|
|
* |
90
|
|
|
* @param ServerRequestInterface $request The PSR-7 server request. |
91
|
|
|
* @param RequestHandlerInterface $handler The PSR-15 request handler. |
92
|
|
|
* |
93
|
|
|
* @return ResponseInterface |
94
|
|
|
*/ |
95
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
96
|
|
|
{ |
97
|
12 |
|
$currentUrl = $request->getUri()->getPath(); |
98
|
|
|
$serverParams = $request->getServerParams(); |
99
|
12 |
|
|
100
|
12 |
|
foreach ($this->list as $urlInfo) { |
101
|
|
|
// If we have set the protection for current URL. |
102
|
12 |
|
if (0 === strpos($currentUrl, $urlInfo['url'])) { |
103
|
|
|
// Prompt a window to ask for username and password. |
104
|
|
|
if (!isset($serverParams['PHP_AUTH_USER']) || |
105
|
12 |
|
!isset($serverParams['PHP_AUTH_PW']) |
106
|
|
|
) { |
107
|
|
|
$authenticate = 'Basic realm="' . $this->realm . '"'; |
108
|
|
|
return (new Response) |
109
|
2 |
|
->withStatus(self::HTTP_STATUS_CODE) |
110
|
2 |
|
->withHeader('WWW-Authenticate', $authenticate); |
111
|
|
|
} |
112
|
1 |
|
|
113
|
1 |
|
// Identify the username and password for current URL. |
114
|
1 |
|
if ($urlInfo['user'] !== $serverParams['PHP_AUTH_USER'] || |
115
|
1 |
|
!password_verify($serverParams['PHP_AUTH_PW'], $urlInfo['pass']) |
116
|
|
|
) { |
117
|
|
|
unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
118
|
|
|
return (new Response)->withStatus(self::HTTP_STATUS_CODE); |
119
|
|
|
} |
120
|
1 |
|
} |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
return $handler->handle($request); |
124
|
11 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set up the URL list that you want to protect. |
128
|
|
|
* |
129
|
10 |
|
* @param array $list The URL list want to be protected. |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
public function set(array $list = []): void |
134
|
|
|
{ |
135
|
|
|
if (!empty($list)) { |
136
|
|
|
$count = count($list); |
137
|
|
|
$urlCount = count(array_column($list, 'url')); |
138
|
|
|
$userCount = count(array_column($list, 'user')); |
139
|
87 |
|
$passCount = count(array_column($list, 'pass')); |
140
|
|
|
|
141
|
87 |
|
if ($count !== $urlCount || |
142
|
85 |
|
$count !== $userCount || |
143
|
85 |
|
$count !== $passCount |
144
|
85 |
|
) { |
145
|
85 |
|
throw new InvalidArgumentException( |
146
|
|
|
'The columns in the array should be fit the specification. ' |
147
|
|
|
); |
148
|
85 |
|
} |
149
|
84 |
|
|
150
|
85 |
|
$this->list = $list; |
151
|
|
|
} |
152
|
1 |
|
} |
153
|
|
|
} |
154
|
|
|
|