1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter\Event\BasicAuth; |
13
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author GeLo <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class BasicAuth implements BasicAuthInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $username; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $password; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|callable|null |
33
|
|
|
*/ |
34
|
|
|
private $matcher; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $username |
38
|
|
|
* @param string $password |
39
|
|
|
* @param string|callable|null $matcher |
40
|
|
|
*/ |
41
|
117 |
|
public function __construct($username, $password, $matcher = null) |
42
|
|
|
{ |
43
|
117 |
|
$this->setUsername($username); |
44
|
117 |
|
$this->setPassword($password); |
45
|
117 |
|
$this->setMatcher($matcher); |
46
|
117 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
18 |
|
public function getUsername() |
52
|
|
|
{ |
53
|
18 |
|
return $this->username; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
117 |
|
public function setUsername($username) |
60
|
|
|
{ |
61
|
117 |
|
$this->username = $username; |
62
|
117 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
18 |
|
public function getPassword() |
68
|
|
|
{ |
69
|
18 |
|
return $this->password; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
117 |
|
public function setPassword($password) |
76
|
|
|
{ |
77
|
117 |
|
$this->password = $password; |
78
|
117 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
72 |
|
public function hasMatcher() |
84
|
|
|
{ |
85
|
72 |
|
return $this->matcher !== null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
36 |
|
public function getMatcher() |
92
|
|
|
{ |
93
|
36 |
|
return $this->matcher; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
117 |
|
public function setMatcher($matcher) |
100
|
|
|
{ |
101
|
117 |
|
$this->matcher = $matcher; |
102
|
117 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
54 |
|
public function authenticate(InternalRequestInterface $internalRequest) |
108
|
|
|
{ |
109
|
54 |
|
if (!$this->match($internalRequest)) { |
110
|
18 |
|
return $internalRequest; |
111
|
|
|
} |
112
|
|
|
|
113
|
36 |
|
return $internalRequest->withHeader( |
114
|
36 |
|
'Authorization', |
115
|
36 |
|
'Basic '.base64_encode($this->username.':'.$this->password) |
116
|
28 |
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param InternalRequestInterface $request |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
54 |
|
private function match(InternalRequestInterface $request) |
125
|
|
|
{ |
126
|
54 |
|
return !$this->hasMatcher() |
127
|
50 |
|
|| (is_string($this->matcher) && preg_match($this->matcher, (string) $request->getUri())) |
128
|
54 |
|
|| (is_callable($this->matcher) && call_user_func($this->matcher, $request)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|