|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Suricate\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
class HttpBasicAuth extends \Suricate\Middleware |
|
8
|
|
|
{ |
|
9
|
|
|
const AUTHTYPE_ARRAY = 'array'; |
|
10
|
|
|
const AUTHTYPE_DB = 'database'; |
|
11
|
|
|
protected $options; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($options = null) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->options = [ |
|
16
|
|
|
'users' => [], |
|
17
|
|
|
'type' => self::AUTHTYPE_ARRAY, |
|
18
|
|
|
'path' => '/', |
|
19
|
|
|
'realm' => 'restricted area', |
|
20
|
|
|
'db' => [] |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
if ($options !== null) { |
|
24
|
|
|
$this->options = array_merge($this->options, (array) $options); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function shouldAuthenticate($request) |
|
29
|
|
|
{ |
|
30
|
|
|
$path = rtrim($this->options["path"], "/"); |
|
31
|
|
|
$regex = "#" . $path . "(/.*)?$#"; |
|
32
|
|
|
|
|
33
|
|
|
return preg_match($regex, $request->getRequestUri()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Authenticate against backend dispatcher |
|
38
|
|
|
* |
|
39
|
|
|
* @param ?string $user username |
|
40
|
|
|
* @param ?string $password password |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
private function authenticate(?string $user, ?string $password): bool |
|
44
|
|
|
{ |
|
45
|
|
|
switch ($this->options['type']) { |
|
46
|
|
|
case self::AUTHTYPE_ARRAY: |
|
47
|
|
|
return $this->authenticateAgainstArray($user, $password); |
|
48
|
|
|
case self::AUTHTYPE_DB: |
|
49
|
|
|
return $this->authenticateAgainstDatabase($user, $password); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Authenticate against array of usernames / passwords |
|
57
|
|
|
* |
|
58
|
|
|
* @param ?string $user username |
|
59
|
|
|
* @param ?string $password password |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
private function authenticateAgainstArray( |
|
63
|
|
|
?string $user, |
|
64
|
|
|
?string $password |
|
65
|
|
|
): bool { |
|
66
|
|
|
if ( |
|
67
|
|
|
isset($this->options['users'][$user]) && |
|
68
|
|
|
$this->options['users'][$user] == $password |
|
69
|
|
|
) { |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function authenticateAgainstDatabase( |
|
77
|
|
|
?string $user, |
|
|
|
|
|
|
78
|
|
|
?string $password |
|
|
|
|
|
|
79
|
|
|
): bool { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function call(&$request, &$response) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->shouldAuthenticate($response)) { |
|
86
|
|
|
$user = dataGet($_SERVER, 'PHP_AUTH_USER'); |
|
87
|
|
|
$password = dataGet($_SERVER, 'PHP_AUTH_PW'); |
|
88
|
|
|
|
|
89
|
|
|
if (!$this->authenticate($user, $password)) { |
|
90
|
|
|
app()->abort('401', 'not aut', [ |
|
91
|
|
|
"WWW-Authenticate" => sprintf( |
|
92
|
|
|
'Basic realm="%s"', |
|
93
|
|
|
$this->options["realm"] |
|
94
|
|
|
) |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.