-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAttributeAddUsersGroups.php
More file actions
385 lines (322 loc) · 13 KB
/
AttributeAddUsersGroups.php
File metadata and controls
385 lines (322 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Does a reverse membership lookup on the logged in user,
* looking for groups it is a member of and adds them to
* a defined attribute, in DN format.
*
* @package simplesamlphp/simplesamlphp-module-ldap
*/
declare(strict_types=1);
namespace SimpleSAML\Module\ldap\Auth\Process;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Error;
use SimpleSAML\Logger;
use SimpleSAML\Utils;
use Symfony\Component\Ldap\Adapter\ExtLdap\Query;
class AttributeAddUsersGroups extends BaseFilter
{
/** @var string|null */
protected ?string $searchUsername;
/** @var string|null */
protected ?string $searchPassword;
/** @var string|null */
protected ?string $product;
/**
* Initialize this filter.
*
* @param array<mixed> $config Configuration information about this filter.
* @param mixed $reserved For future use.
*/
public function __construct(array $config, $reserved)
{
parent::__construct($config, $reserved);
// Get filter specific config options
$this->searchUsername = $this->config->getOptionalString('search.username', null);
$this->searchPassword = $this->config->getOptionalString('search.password', null);
$this->product = $this->config->getOptionalString('ldap.product', null);
}
/**
* LDAP search filters to be added to the base filters for this authproc-filter.
* It's an array of key => value pairs that will be translated to (key=value) in the ldap query.
*
* @var array<mixed>
*/
protected array $additional_filters;
/**
* This is run when the filter is processed by SimpleSAML.
* It will attempt to find the current users groups using
* the best method possible for the LDAP product. The groups
* are then added to the request attributes.
*
* @throws \SimpleSAML\Error\Exception
* @param array<mixed> &$state
*/
public function process(array &$state): void
{
Assert::keyExists($state, 'Attributes');
// Log the process
Logger::debug(sprintf(
'%s : Attempting to get the users groups...',
$this->title,
));
$this->additional_filters = $this->config->getOptionalArray('additional_filters', []);
// Reference the attributes, just to make the names shorter
$attributes = &$state['Attributes'];
$map = &$this->attribute_map;
// Get the users groups from LDAP
$groups = $this->getGroups($attributes);
// If there are none, do not proceed
if (empty($groups)) {
return;
}
// Make the array if it is not set already
if (!isset($attributes[$map['groups']])) {
$attributes[$map['groups']] = [];
}
// Must be an array, else cannot merge groups
if (!is_array($attributes[$map['groups']])) {
throw new Error\Exception(sprintf(
'%s : The group attribute [%s] is not an array of group DNs. %s',
$this->title,
$map['groups'],
$this->varExport($attributes[$map['groups']]),
));
}
// Add the users group(s)
$group_attribute = &$attributes[$map['groups']];
$group_attribute = array_merge($group_attribute, $groups);
$group_attribute = array_unique($group_attribute);
// All done
Logger::debug(sprintf(
'%s : Added users groups to the group attribute[%s]: %s',
$this->title,
$map['groups'],
implode('; ', $groups),
));
}
/**
* Will perform a search using the required attribute values from the user to
* get their group membership, recursively.
*
* @throws \SimpleSAML\Error\Exception
* @param array<mixed> $attributes
* @return array<mixed>
*/
protected function getGroups(array $attributes): array
{
// Log the request
Logger::debug(sprintf(
'%s : Checking for groups based on the best method for the LDAP product.',
$this->title,
));
$this->connector->bind($this->searchUsername, $this->searchPassword);
$options = [
'scope' => $this->config->getOptionalString('search.scope', Query::SCOPE_SUB),
'timeout' => $this->config->getOptionalInteger('timeout', 3),
];
// Reference the map, just to make the name shorter
$map = &$this->attribute_map;
// All map-properties are guaranteed to exist and have a default value
$dn_attribute = $map['dn'];
$return_attribute = $map['return'];
// Based on the directory service, search LDAP for groups
// If any attributes are needed, prepare them before calling search method
switch ($this->product) {
case 'ActiveDirectory':
// Log the AD specific search
Logger::debug(sprintf(
'%s : Searching LDAP using ActiveDirectory specific method.',
$this->title,
));
// Make sure the defined DN attribute exists
if (!isset($attributes[$dn_attribute])) {
Logger::warning(sprintf(
"%s : The DN attribute [%s] is not defined in the user's Attributes: %s",
$this->title,
$dn_attribute,
implode(', ', array_keys($attributes)),
));
return [];
}
// Make sure the defined DN attribute has a value
if (!isset($attributes[$dn_attribute][0]) || !$attributes[$dn_attribute][0]) {
Logger::warning(sprintf(
'%s : The DN attribute [%s] does not have a [0] value defined. %s',
$this->title,
$dn_attribute,
$this->varExport($attributes[$dn_attribute]),
));
return [];
}
// Log the search
$arrayUtils = new Utils\Arrays();
Logger::debug(sprintf(
'%s : Searching ActiveDirectory group membership.'
. ' DN: %s DN Attribute: %s Member Attribute: %s Type Attribute: %s Type Value: %s Base: %s',
$this->title,
$attributes[$dn_attribute][0],
$dn_attribute,
$map['member'],
$map['type'],
$this->type_map['group'],
implode('; ', $arrayUtils->arrayize($this->searchBase)),
));
$filter = sprintf(
"(&(%s=%s)(%s=%s))",
$map['type'],
$this->type_map['group'],
$map['member'] . ':1.2.840.113556.1.4.1941:',
$this->connector->escapeFilterValue($attributes[$dn_attribute][0], true),
);
$entries = $this->connector->searchForMultiple(
$this->searchBase,
$filter,
$options,
true,
);
break;
case 'OpenLDAP':
// Log the OpenLDAP specific search
Logger::debug(sprintf(
'%s : Searching LDAP using OpenLDAP specific method.',
$this->title,
));
Logger::debug(sprintf(
'%s : Searching for groups in base [%s] with filter (%s=%s) and attributes %s',
$this->title,
implode(', ', $this->searchBase),
$map['memberOf'],
$attributes[$map['username']][0],
$map['member'],
));
$filter = sprintf(
'(&(%s=%s))',
$map['memberOf'],
$attributes[$map['username']][0],
);
$entries = $this->connector->searchForMultiple(
$this->searchBase,
$filter,
$options,
true,
);
break;
default:
// Log the generic search
Logger::debug(
sprintf('%s : Searching LDAP using the generic search method.', $this->title),
);
// Make sure the defined memberOf attribute exists
Assert::keyExists(
$attributes,
$map['memberOf'],
sprintf(
"%s : The memberOf attribute [%s] is not defined in the user's attributes: [%s]",
$this->title,
$map['memberOf'],
implode(', ', array_keys($attributes)),
),
Error\Exception::class,
);
// MemberOf must be an array of group DN's
Assert::isArray(
$attributes[$map['memberOf']],
sprintf(
'%s : The memberOf attribute [%s] is not an array of group DNs; %s',
$this->title,
$map['memberOf'],
$this->varExport($attributes[$map['memberOf']]),
),
Error\Exception::class,
);
Logger::debug(sprintf(
'%s : Checking DNs for groups. DNs: %s Attributes: %s, %s Group Type: %s',
$this->title,
implode('; ', $attributes[$map['memberOf']]),
$map['memberOf'],
$map['type'],
$this->type_map['group'],
));
// Search for the users group membership, recursively
$entries = $this->search($attributes[$map['memberOf']], $options);
}
$groups = [];
foreach ($entries as $entry) {
if ($entry->hasAttribute($return_attribute)) {
$values = $entry->getAttribute($return_attribute);
$groups[] = array_pop($values);
continue;
} elseif ($entry->hasAttribute(strtolower($return_attribute))) {
// Some backends return lowercase attributes
$values = $entry->getAttribute(strtolower($return_attribute));
$groups[] = array_pop($values);
continue;
}
// Could not find return attribute, log and continue
Logger::notice(sprintf(
'%s : The return attribute [%s] could not be found in entry `%s`.',
$this->title,
implode(', ', array_unique([$map['return'], strtolower($map['return'])])),
$entry->getDn(),
));
Logger::debug(sprintf('%s : Entry was: %s', $this->title, $this->varExport($entry)));
}
// All done
Logger::debug(sprintf(
'%s : User found to be a member of the following groups: %s',
$this->title,
empty($groups) ? 'none' : implode('; ', $groups),
));
return $groups;
}
/**
* Looks for groups from the list of DN's passed. Also
* recursively searches groups for further membership.
* Avoids loops by only searching a DN once. Returns
* the list of groups found.
*
* @param array<mixed> $memberOf
* @param array<mixed> $options
* @return array<mixed>
*/
protected function search(array $memberOf, array $options): array
{
// Shorten the variable name
$map = &$this->attribute_map;
// Used to determine what DN's have already been searched
static $searched = [];
// Init the groups variable
$entries = [];
// Set scope to 'base'
$options['scope'] = Query::SCOPE_BASE;
// Check each DN of the passed memberOf
foreach ($memberOf as $dn) {
// Avoid infinite loops, only need to check a DN once
if (isset($searched[$dn])) {
continue;
}
// Track all DN's that are searched
// Use DN for key as well, isset() is faster than in_array()
$searched[$dn] = $dn;
// Query LDAP for the attribute values for the DN
$entry = $this->connector->search(
[$dn],
sprintf("(%s=%s)", $map['type'], $this->type_map['group']),
$options,
true,
);
if ($entry === null) {
// Probably the DN does not exist within the given search base
continue;
}
// Add to found groups array
$entries[] = $entry;
// Recursively search "sub" groups
$subGroups = $entry->getAttribute($map['memberOf']);
if (!empty($subGroups)) {
$entries = array_merge($entries, $this->search($subGroups, $options));
}
}
return $entries;
}
}