|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Micro |
|
6
|
|
|
* |
|
7
|
|
|
* @author Raffael Sahli <[email protected]> |
|
8
|
|
|
* @copyright Copryright (c) 2012-2017 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPLv3 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Micro\Auth\Adapter\Basic; |
|
13
|
|
|
|
|
14
|
|
|
use \Micro\Auth\Exception; |
|
15
|
|
|
use \Psr\Log\LoggerInterface as Logger; |
|
16
|
|
|
use \Micro\Ldap as LdapServer; |
|
17
|
|
|
use \Micro\Auth\Adapter\AdapterInterface; |
|
18
|
|
|
use \Micro\Auth\Adapter\AbstractAdapter; |
|
19
|
|
|
|
|
20
|
|
|
class Ldap extends AbstractAdapter |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Ldap |
|
24
|
|
|
* |
|
25
|
|
|
* @var LdapServer |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $ldap; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* LDAP DN |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $ldap_dn; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* my account filter |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $account_filter = '(uid=%s)'; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Ldap connect |
|
48
|
|
|
* |
|
49
|
|
|
* @param Iterable $config |
|
50
|
|
|
* @param Logger $logger |
|
51
|
|
|
* @return void |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(?Iterable $config, Logger $logger) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
56
|
|
|
$this->setOptions($config); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set options |
|
62
|
|
|
* |
|
63
|
|
|
* @param Iterable $config |
|
64
|
|
|
* @return AdapterInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setOptions(?Iterable $config=null): AdapterInterface |
|
67
|
|
|
{ |
|
68
|
|
|
if ($config === null) { |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
foreach ($config as $option => $value) { |
|
73
|
|
|
switch ($option) { |
|
74
|
|
|
case 'ldap': |
|
|
|
|
|
|
75
|
|
|
$this->ldap = new LdapServer($value, $this->logger); |
|
76
|
|
|
break; |
|
77
|
|
|
|
|
78
|
|
|
case 'account_filter': |
|
79
|
|
|
$this->account_filter = $value; |
|
80
|
|
|
break; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if(!isset($config['ldap'])) { |
|
85
|
|
|
$this->ldap = new LdapServer(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return parent::setOptions($config); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Authenticate |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public function authenticate(): bool |
|
98
|
|
|
{ |
|
99
|
|
|
if (!isset($_SERVER['HTTP_AUTHORIZATION'])) { |
|
100
|
|
|
$this->logger->debug('skip auth adapter ['.get_class($this).'], no http authorization header found', [ |
|
101
|
|
|
'category' => get_class($this) |
|
102
|
|
|
]); |
|
103
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$header = $_SERVER['HTTP_AUTHORIZATION']; |
|
108
|
|
|
$parts = explode(' ', $header); |
|
109
|
|
|
|
|
110
|
|
|
if ($parts[0] == 'Basic') { |
|
111
|
|
|
$this->logger->debug('found http basic authorization header', [ |
|
112
|
|
|
'category' => get_class($this) |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
$username = $_SERVER['PHP_AUTH_USER']; |
|
116
|
|
|
$password = $_SERVER['PHP_AUTH_PW']; |
|
117
|
|
|
|
|
118
|
|
|
return $this->plainAuth($username, $password); |
|
119
|
|
|
} else { |
|
120
|
|
|
$this->logger->warning('http authorization header contains no basic string or invalid authentication string', [ |
|
121
|
|
|
'category' => get_class($this) |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
return false; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* LDAP Auth |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $username |
|
133
|
|
|
* @param string $password |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function plainAuth(string $username, string $password): bool |
|
137
|
|
|
{ |
|
138
|
|
|
$this->ldap->connect(); |
|
139
|
|
|
$resource = $this->ldap->getResource(); |
|
140
|
|
|
|
|
141
|
|
|
$esc_username = ldap_escape($username); |
|
142
|
|
|
$filter = htmlspecialchars_decode(sprintf($this->account_filter, $esc_username)); |
|
143
|
|
|
$result = ldap_search($resource, $this->ldap->getBase(), $filter, ['dn']); |
|
144
|
|
|
$entries = ldap_get_entries($resource, $result); |
|
145
|
|
|
|
|
146
|
|
|
if ($entries['count'] === 0) { |
|
147
|
|
|
$this->logger->warning("user not found with ldap filter [{$filter}]", [ |
|
148
|
|
|
'category' => get_class($this) |
|
149
|
|
|
]); |
|
150
|
|
|
|
|
151
|
|
|
return false; |
|
152
|
|
|
} elseif ($entries['count'] > 1) { |
|
153
|
|
|
$this->logger->warning("more than one user found with ldap filter [{$filter}]", [ |
|
154
|
|
|
'category' => get_class($this) |
|
155
|
|
|
]); |
|
156
|
|
|
|
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$dn = $entries[0]['dn']; |
|
161
|
|
|
$this->logger->info("found ldap user [{$dn}] with filter [{$filter}]", [ |
|
162
|
|
|
'category' => get_class($this) |
|
163
|
|
|
]); |
|
164
|
|
|
|
|
165
|
|
|
$result = ldap_bind($resource, $dn, $password); |
|
166
|
|
|
$this->logger->info("bind ldap user [{$dn}]", [ |
|
167
|
|
|
'category' => get_class($this), |
|
168
|
|
|
'result' => $result |
|
169
|
|
|
]); |
|
170
|
|
|
|
|
171
|
|
|
if ($result === false) { |
|
172
|
|
|
return false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$this->identifier = $username; |
|
176
|
|
|
$this->ldap_dn = $dn; |
|
177
|
|
|
|
|
178
|
|
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get attributes |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getAttributes(): array |
|
188
|
|
|
{ |
|
189
|
|
|
$search = []; |
|
190
|
|
|
foreach ($this->map as $attr => $value) { |
|
191
|
|
|
$search[] = $value['attr']; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$result = ldap_read($this->ldap->getResource(), $this->ldap_dn, '(objectClass=*)', $search); |
|
195
|
|
|
$entries = ldap_get_entries($this->ldap->getResource(), $result); |
|
196
|
|
|
$attributes = $entries[0]; |
|
197
|
|
|
|
|
198
|
|
|
$this->logger->info("get ldap user [{$this->ldap_dn}] attributes", [ |
|
199
|
|
|
'category' => get_class($this), |
|
200
|
|
|
'params' => $attributes, |
|
201
|
|
|
]); |
|
202
|
|
|
|
|
203
|
|
|
return $attributes; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.