1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * Micro |
||
7 | * |
||
8 | * @copyright Copryright (c) 2015-2018 gyselroth GmbH (https://gyselroth.com) |
||
9 | * @license MIT https://opensource.org/licenses/MIT |
||
10 | */ |
||
11 | |||
12 | namespace Micro\Auth; |
||
13 | |||
14 | use Micro\Auth\Ldap\Exception; |
||
15 | use Psr\Log\LoggerInterface; |
||
16 | |||
17 | class Ldap |
||
18 | { |
||
19 | /** |
||
20 | * Connection resource. |
||
21 | * |
||
22 | * @var resource |
||
23 | */ |
||
24 | protected $connection; |
||
25 | |||
26 | /** |
||
27 | * Logger. |
||
28 | * |
||
29 | * @var LoggerInterface |
||
30 | */ |
||
31 | protected $logger; |
||
32 | |||
33 | /** |
||
34 | * URI. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $uri = 'ldap://127.0.0.1:389'; |
||
39 | |||
40 | /** |
||
41 | * Binddn. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $binddn; |
||
46 | |||
47 | /** |
||
48 | * Bindpw. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $bindpw; |
||
53 | |||
54 | /** |
||
55 | * Basedn. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $basedn = ''; |
||
60 | |||
61 | /** |
||
62 | * tls. |
||
63 | * |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $tls = false; |
||
67 | |||
68 | /** |
||
69 | * Options. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $options = []; |
||
74 | |||
75 | /** |
||
76 | * construct. |
||
77 | * |
||
78 | * @param iterable $config |
||
79 | * @param Logger $logger |
||
0 ignored issues
–
show
|
|||
80 | * |
||
81 | * @return resource |
||
82 | */ |
||
83 | public function __construct(LoggerInterface $logger, ?Iterable $config = null) |
||
84 | { |
||
85 | $this->setOptions($config); |
||
86 | $this->logger = $logger; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Connect. |
||
91 | * |
||
92 | * @return Ldap |
||
93 | */ |
||
94 | public function connect(): self |
||
95 | { |
||
96 | $this->logger->debug('connect to ldap server ['.$this->uri.']', [ |
||
97 | 'category' => get_class($this), |
||
98 | ]); |
||
99 | |||
100 | if (null === $this->binddn) { |
||
101 | $this->logger->warning('no binddn set for ldap connection, you should avoid anonymous bind', [ |
||
102 | 'category' => get_class($this), |
||
103 | ]); |
||
104 | } |
||
105 | |||
106 | if (false === $this->tls && 'ldaps' !== substr($this->uri, 0, 5)) { |
||
107 | $this->logger->warning('neither tls nor ldaps enabled for ldap connection, it is strongly reccommended to encrypt ldap connections', [ |
||
108 | 'category' => get_class($this), |
||
109 | ]); |
||
110 | } |
||
111 | |||
112 | $this->connection = ldap_connect($this->uri); |
||
113 | |||
114 | foreach ($this->options as $opt => $value) { |
||
115 | ldap_set_option($this->connection, constant($opt), $value); |
||
116 | } |
||
117 | |||
118 | if (true === $this->tls) { |
||
119 | ldap_start_tls($this->connection); |
||
120 | } |
||
121 | |||
122 | if ($this->connection) { |
||
123 | if (null !== $this->binddn) { |
||
124 | $bind = ldap_bind($this->connection, $this->binddn, $this->bindpw); |
||
125 | |||
126 | if ($bind) { |
||
127 | $this->logger->info('bind to ldap server ['.$this->uri.'] with binddn ['.$this->binddn.'] was succesful', [ |
||
128 | 'category' => get_class($this), |
||
129 | ]); |
||
130 | |||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | throw new Exception('failed bind to ldap server, error: '.ldap_error($this->connection)); |
||
135 | } |
||
136 | } else { |
||
137 | throw new Exception('failed connect to ldap server '.$this->uri); |
||
138 | } |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Close socket. |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function close(): bool |
||
149 | { |
||
150 | if (is_resource($this->connection)) { |
||
151 | return ldap_unbind($this->connection); |
||
152 | } |
||
153 | |||
154 | return true; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Set options. |
||
159 | * |
||
160 | * @param iterable $config |
||
161 | * |
||
162 | * @return Ldap |
||
163 | */ |
||
164 | public function setOptions(? Iterable $config = null): self |
||
165 | { |
||
166 | if (null === $config) { |
||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | foreach ($config as $option => $value) { |
||
171 | switch ($option) { |
||
172 | case 'options': |
||
173 | $this->options = $value; |
||
174 | |||
175 | break; |
||
176 | case 'uri': |
||
177 | case 'binddn': |
||
178 | case 'bindpw': |
||
179 | case 'basedn': |
||
180 | $this->{$option} = (string) $value; |
||
181 | |||
182 | break; |
||
183 | case 'tls': |
||
184 | $this->tls = (bool) $value; |
||
185 | |||
186 | break; |
||
187 | default: |
||
188 | throw new Exception('unknown option '.$option.' given'); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get base. |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getBase(): string |
||
201 | { |
||
202 | return $this->basedn; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get connection. |
||
207 | * |
||
208 | * @return resource |
||
209 | */ |
||
210 | public function getResource() |
||
211 | { |
||
212 | if (!is_resource($this->connection)) { |
||
213 | $this->connect(); |
||
214 | } |
||
215 | |||
216 | return $this->connection; |
||
217 | } |
||
218 | } |
||
219 |
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