|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\ldap; |
|
6
|
|
|
|
|
7
|
|
|
use SimpleSAML\Assert\Assert; |
|
8
|
|
|
use SimpleSAML\Configuration; |
|
9
|
|
|
use SimpleSAML\Error; |
|
10
|
|
|
use SimpleSAML\Module\ldap\Connector; |
|
11
|
|
|
|
|
12
|
|
|
use function current; |
|
13
|
|
|
use function sprintf; |
|
14
|
|
|
|
|
15
|
|
|
class ConnectorFactory |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $authSource |
|
19
|
|
|
* @return \SimpleSAML\Module\ldap\ConnectorInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function fromAuthSource(string $authSource): ConnectorInterface |
|
22
|
|
|
{ |
|
23
|
|
|
// Get the authsources file, which should contain the config |
|
24
|
|
|
$authSources = Configuration::getConfig('authsources.php'); |
|
25
|
|
|
|
|
26
|
|
|
// Verify that the authsource config exists |
|
27
|
|
|
if (!$authSources->hasValue($authSource)) { |
|
28
|
|
|
throw new Error\Exception(sprintf( |
|
29
|
|
|
'Authsource [%s] not found in authsources.php', |
|
30
|
|
|
$authSource, |
|
31
|
|
|
)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Get just the specified authsource config values |
|
35
|
|
|
$ldapConfig = $authSources->getConfigItem($authSource); |
|
36
|
|
|
$type = $ldapConfig->toArray(); |
|
37
|
|
|
Assert::oneOf(current($type), ['ldap:Ldap']); |
|
38
|
|
|
|
|
39
|
|
|
$encryption = $ldapConfig->getOptionalString('encryption', 'ssl'); |
|
40
|
|
|
Assert::oneOf($encryption, ['none', 'ssl', 'tls']); |
|
41
|
|
|
|
|
42
|
|
|
$version = $ldapConfig->getOptionalInteger('version', 3); |
|
43
|
|
|
Assert::positiveInteger($version); |
|
44
|
|
|
|
|
45
|
|
|
$class = $ldapConfig->getOptionalString('connector', Connector\Ldap::class); |
|
46
|
|
|
Assert::classExists($class); |
|
47
|
|
|
Assert::implementsInterface($class, ConnectorInterface::class); |
|
48
|
|
|
|
|
49
|
|
|
return /** @psalm-var \SimpleSAML\Module\ldap\ConnectionInterface */ new $class( |
|
50
|
|
|
$ldapConfig->getString('connection_string'), |
|
51
|
|
|
$encryption, |
|
52
|
|
|
$version, |
|
53
|
|
|
$ldapConfig->getOptionalString('extension', 'ext_ldap'), |
|
54
|
|
|
$ldapConfig->getOptionalBoolean('debug', false), |
|
55
|
|
|
$ldapConfig->getOptionalArray('options', [ |
|
56
|
|
|
'network_timeout' => 3, |
|
57
|
|
|
'referrals' => false, |
|
58
|
|
|
]), |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|