1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
12
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
declare(strict_types=1); |
18
|
|
|
|
19
|
|
|
namespace Phauthentic\Authentication\Identifier\Ldap; |
20
|
|
|
|
21
|
|
|
use ErrorException; |
22
|
|
|
use RuntimeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Provides a very thin OOP wrapper around the ldap_* functions. |
26
|
|
|
* |
27
|
|
|
* We don't need and want a huge LDAP lib for our purpose. |
28
|
|
|
* |
29
|
|
|
* But this makes it easier to unit test code that is using LDAP because we can |
30
|
|
|
* mock it very easy. It also provides some convenience. |
31
|
|
|
*/ |
32
|
|
|
class ExtensionAdapter implements AdapterInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* LDAP Object |
36
|
|
|
* |
37
|
|
|
* @var \LDAP\Connection|null |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
protected $connection; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @throws \RuntimeException |
45
|
|
|
*/ |
46
|
|
|
public function __construct() |
47
|
|
|
{ |
48
|
|
|
if (!extension_loaded('ldap')) { |
49
|
|
|
throw new RuntimeException('You must enable the ldap extension to use the LDAP identifier.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) { |
53
|
|
|
define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Bind to LDAP directory |
59
|
|
|
* |
60
|
|
|
* @param string $bind Bind rdn |
61
|
|
|
* @param string $password Bind password |
62
|
|
|
* @return bool |
63
|
|
|
* @throws \ErrorException |
64
|
|
|
*/ |
65
|
|
|
public function bind(string $bind, string $password): bool |
66
|
|
|
{ |
67
|
|
|
$this->setErrorHandler(); |
68
|
|
|
$result = ldap_bind($this->getConnection(), $bind, $password); |
|
|
|
|
69
|
|
|
$this->unsetErrorHandler(); |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the LDAP connection |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
* @throws \RuntimeException If the connection is empty |
79
|
|
|
*/ |
80
|
|
|
public function getConnection() |
81
|
|
|
{ |
82
|
|
|
if (empty($this->connection)) { |
83
|
|
|
throw new RuntimeException('You are not connected to a LDAP server.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->connection; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Connect to an LDAP server |
91
|
|
|
* |
92
|
|
|
* @param string $host Hostname |
93
|
|
|
* @param int $port Port |
94
|
|
|
* @param array<int, int|bool|string> $options Additional LDAP options |
95
|
|
|
* @return void |
96
|
|
|
* @throws \ErrorException |
97
|
|
|
*/ |
98
|
|
|
public function connect(string $host, int $port, array $options): void |
99
|
|
|
{ |
100
|
|
|
$this->setErrorHandler(); |
101
|
|
|
$this->connection = ldap_connect($host, $port) ?: null; |
|
|
|
|
102
|
|
|
$this->unsetErrorHandler(); |
103
|
|
|
|
104
|
|
|
if (is_array($options)) { |
|
|
|
|
105
|
|
|
foreach ($options as $option => $value) { |
106
|
|
|
$this->setOption($option, $value); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the value of the given option |
113
|
|
|
* |
114
|
|
|
* @param int $option Option to set |
115
|
|
|
* @param int|bool|string $value The new value for the specified option |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function setOption(int $option, $value) |
119
|
|
|
{ |
120
|
|
|
$this->setErrorHandler(); |
121
|
|
|
ldap_set_option($this->getConnection(), $option, $value); |
|
|
|
|
122
|
|
|
$this->unsetErrorHandler(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the current value for given option |
127
|
|
|
* |
128
|
|
|
* @param int $option Option to get |
129
|
|
|
* @return mixed This will be set to the option value. |
130
|
|
|
*/ |
131
|
|
|
public function getOption($option) |
132
|
|
|
{ |
133
|
|
|
$returnValue = null; |
134
|
|
|
$this->setErrorHandler(); |
135
|
|
|
ldap_get_option($this->getConnection(), $option, $returnValue); |
|
|
|
|
136
|
|
|
$this->unsetErrorHandler(); |
137
|
|
|
|
138
|
|
|
return $returnValue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the diagnostic message |
143
|
|
|
* |
144
|
|
|
* @return string|null |
145
|
|
|
*/ |
146
|
|
|
public function getDiagnosticMessage() |
147
|
|
|
{ |
148
|
|
|
return $this->getOption(LDAP_OPT_DIAGNOSTIC_MESSAGE); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Unbind from LDAP directory |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function unbind() |
157
|
|
|
{ |
158
|
|
|
$this->setErrorHandler(); |
159
|
|
|
if ($this->connection) { |
160
|
|
|
ldap_unbind($this->connection); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
$this->unsetErrorHandler(); |
163
|
|
|
|
164
|
|
|
$this->connection = null; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set an error handler to turn LDAP errors into exceptions |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
* @throws \ErrorException |
172
|
|
|
*/ |
173
|
|
|
protected function setErrorHandler() |
174
|
|
|
{ |
175
|
|
|
set_error_handler( |
176
|
|
|
function ($errorNumber, $errorText) { |
177
|
|
|
throw new ErrorException($errorText); |
178
|
|
|
}, |
179
|
|
|
E_ALL |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Restore the error handler |
185
|
|
|
* |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
protected function unsetErrorHandler() |
189
|
|
|
{ |
190
|
|
|
restore_error_handler(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths