|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* LDAP authentication source. |
|
5
|
|
|
* |
|
6
|
|
|
* See the ldap-entry in config-templates/authsources.php for information about |
|
7
|
|
|
* configuration of this authentication source. |
|
8
|
|
|
* |
|
9
|
|
|
* This class is based on www/auth/login.php. |
|
10
|
|
|
* |
|
11
|
|
|
* @package SimpleSAMLphp |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
declare(strict_types=1); |
|
15
|
|
|
|
|
16
|
|
|
namespace SimpleSAML\Module\ldap\Auth\Source; |
|
17
|
|
|
|
|
18
|
|
|
use SimpleSAML\Assert\Assert; |
|
19
|
|
|
use SimpleSAML\Auth; |
|
20
|
|
|
use SimpleSAML\Configuration; |
|
21
|
|
|
use SimpleSAML\Error; |
|
22
|
|
|
use SimpleSAML\Logger; |
|
23
|
|
|
use SimpleSAML\Module\core\Auth\UserPassOrgBase; |
|
24
|
|
|
|
|
25
|
|
|
use function array_key_exists; |
|
26
|
|
|
use function var_export; |
|
27
|
|
|
|
|
28
|
|
|
class LdapMulti extends UserPassOrgBase |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* An LDAP configuration object. |
|
32
|
|
|
*/ |
|
33
|
|
|
private Configuration $ldapConfig; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* An array with mappings for organization => authsource. |
|
37
|
|
|
*/ |
|
38
|
|
|
private array $mapping; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* An array with descriptions for organizations. |
|
42
|
|
|
*/ |
|
43
|
|
|
private array $orgs; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* An array of organization IDs to LDAP configuration objects. |
|
47
|
|
|
*/ |
|
48
|
|
|
private array $ldapOrgs; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Whether we should include the organization as part of the username. |
|
52
|
|
|
*/ |
|
53
|
|
|
private bool $includeOrgInUsername; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Constructor for this authentication source. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $info Information about this authentication source. |
|
60
|
|
|
* @param array $config Configuration. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(array $info, array $config) |
|
63
|
|
|
{ |
|
64
|
|
|
// Call the parent constructor first, as required by the interface |
|
65
|
|
|
parent::__construct($info, $config); |
|
66
|
|
|
|
|
67
|
|
|
$this->ldapConfig = Configuration::loadFromArray( |
|
68
|
|
|
$config, |
|
69
|
|
|
'authsources[' . var_export($this->authId, true) . ']' |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
$usernameOrgMethod = $this->ldapConfig->getValueValidate( |
|
73
|
|
|
'username_organization_method', |
|
74
|
|
|
['none', 'allow', 'force'] |
|
75
|
|
|
); |
|
76
|
|
|
$this->setUsernameOrgMethod($usernameOrgMethod); |
|
77
|
|
|
|
|
78
|
|
|
$this->includeOrgInUsername = $this->ldapConfig->getBoolean( |
|
79
|
|
|
'include_organization_in_username', |
|
80
|
|
|
false |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$this->mapping = $this->ldapConfig->getArray('mapping'); |
|
84
|
|
|
Assert::notEmpty($this->mapping); |
|
85
|
|
|
|
|
86
|
|
|
$organizations = array_keys($this->mapping); |
|
87
|
|
|
$authsources = Configuration::getConfig('authsources.php'); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($organizations as $organization) { |
|
90
|
|
|
Assert::keyExists($this->mapping[$organization], 'authsource'); |
|
91
|
|
|
$authsource = $this->mapping[$organization]['authsource']; |
|
92
|
|
|
Assert::notNull(Auth\Source::getById($authsource, Ldap::class)); |
|
93
|
|
|
|
|
94
|
|
|
if (array_key_exists('description', $this->mapping[$organization])) { |
|
95
|
|
|
$this->orgs[$organization] = $this->mapping[$organization]['description']; |
|
96
|
|
|
} else { |
|
97
|
|
|
$this->orgs[$organization] = $organization; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->ldapOrgs[$organization] = Configuration::loadFromArray( |
|
101
|
|
|
$authsources->getValue($authsource), |
|
|
|
|
|
|
102
|
|
|
'authsources[' . var_export($this->authId, true) . '][' . var_export($organization, true). ']' |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Attempt to log in using the given username and password. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $username The username the user wrote. |
|
112
|
|
|
* @param string $password The password the user wrote. |
|
113
|
|
|
* @return array Associative array with the users attributes. |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function login(string $username, string $password, string $organization): array |
|
116
|
|
|
{ |
|
117
|
|
|
if ($this->includeOrgInUsername) { |
|
118
|
|
|
$username = $username . '@' . $organization; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$authsource = $this->mapping[$organization]['authsource']; |
|
122
|
|
|
$sourceConfig = $this->ldapOrgs[$organization]; |
|
123
|
|
|
|
|
124
|
|
|
$ldap = new class (['AuthId' => $authsource], $sourceConfig->toArray()) extends Ldap { |
|
125
|
|
|
public function _login(string $username, string $password) { |
|
126
|
|
|
return $this->login($username, $password); |
|
127
|
|
|
} |
|
128
|
|
|
}; |
|
129
|
|
|
|
|
130
|
|
|
return $ldap->_login($username, $password); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Retrieve list of organizations. |
|
136
|
|
|
* |
|
137
|
|
|
* @return array Associative array with the organizations. |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getOrganizations(): array |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->orgs; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|