|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* janitor (http://juliangut.com/janitor). |
|
5
|
|
|
* Effortless maintenance management. |
|
6
|
|
|
* |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @link https://github.com/juliangut/janitor |
|
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Janitor\Excluder; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Basic HTTP authorization maintenance excluder. |
|
18
|
|
|
*/ |
|
19
|
|
|
class BasicAuth implements ExcluderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* List of user/password to be excluded. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $users = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* BasicAuth constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string|array $users |
|
|
|
|
|
|
32
|
|
|
* @param mixed $password |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($users = null, $password = null) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($users !== null && !is_array($users)) { |
|
37
|
|
|
$users = [$users => $password]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (is_array($users)) { |
|
41
|
|
|
foreach ($users as $userName => $userPassword) { |
|
42
|
|
|
$this->addUser($userName, $userPassword); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add user. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $userName |
|
51
|
|
|
* @param mixed $password |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function addUser($userName, $password = null) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_string($userName) && trim($userName) !== '') { |
|
58
|
|
|
$this->users[trim($userName)] = $password; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
* |
|
67
|
|
|
* @throws \RuntimeException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isExcluded(ServerRequestInterface $request) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!count($this->users)) { |
|
72
|
|
|
throw new \RuntimeException('No users defined in basic authorization excluder'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$authData = $this->getAuth($request); |
|
76
|
|
|
|
|
77
|
|
|
foreach ($this->users as $username => $password) { |
|
78
|
|
|
if ($authData['username'] === $username && $authData['password'] === $password) { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Retrieve request authentication information. |
|
88
|
|
|
* |
|
89
|
|
|
* @param ServerRequestInterface $request |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
protected function getAuth(ServerRequestInterface $request) |
|
94
|
|
|
{ |
|
95
|
|
|
$authHeader = $request->getHeaderLine('Authorization'); |
|
96
|
|
|
if (preg_match('/^Basic /', $authHeader)) { |
|
97
|
|
|
$auth = explode(':', base64_decode(substr($authHeader, 6)), 2); |
|
98
|
|
|
|
|
99
|
|
|
return [ |
|
100
|
|
|
'username' => $auth[0], |
|
101
|
|
|
'password' => isset($auth[1]) ? $auth[1] : null, |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.