Passed
Pull Request — master (#28)
by Tim
02:06
created

LdapMulti::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 46
rs 9.1448
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\Configuration;
20
use SimpleSAML\Error;
21
use SimpleSAML\Logger;
22
//use SimpleSAML\Module\ldap\ConfigHelper;
23
24
class LdapMulti extends \SimpleSAML\Module\core\Auth\UserPassOrgBase
25
{
26
    /**
27
     * An LDAP configuration object.
28
     */
29
    private Configuration $ldapConfig;
30
31
    /**
32
     * An array with mappings for organization => authsource.
33
     */
34
    private array $mapping;
35
36
    /**
37
     * An array with descriptions for organizations.
38
     */
39
    private array $orgs;
40
41
    /**
42
     * An array of organization IDs to LDAP configuration objects.
43
     */
44
    private array $ldapOrgs;
45
46
    /**
47
     * Whether we should include the organization as part of the username.
48
     */
49
    private bool $includeOrgInUsername;
50
51
52
    /**
53
     * Constructor for this authentication source.
54
     *
55
     * @param array $info  Information about this authentication source.
56
     * @param array $config  Configuration.
57
     */
58
    public function __construct(array $info, array $config)
59
    {
60
        // Call the parent constructor first, as required by the interface
61
        parent::__construct($info, $config);
62
63
        $this->ldapConfig = Configuration::loadFromArray(
64
            $config,
65
            'authsources[' . var_export($this->authId, true) . ']'
66
        );
67
68
        $this->orgs = [];
69
        $this->ldapOrgs = [];
70
        foreach ($config as $name => $value) {
71
            if ($name === 'username_organization_method') {
72
                $usernameOrgMethod = $this->ldapConfig->getValueValidate(
73
                    'username_organization_method',
74
                    ['none', 'allow', 'force']
75
                );
76
                $this->setUsernameOrgMethod($usernameOrgMethod);
77
                continue;
78
            }
79
80
            if ($name === 'include_organization_in_username') {
81
                $this->includeOrgInUsername = $this->ldapConfig->getBoolean(
82
                    'include_organization_in_username',
83
                    false
84
                );
85
                continue;
86
            }
87
88
            $orgCfg = $this->ldapConfig->getArray($name);
89
            $orgId = $name;
90
91
            if (array_key_exists('description', $orgCfg)) {
92
                $this->orgs[$orgId] = $orgCfg['description'];
93
            } else {
94
                $this->orgs[$orgId] = $orgId;
95
            }
96
97
            $this->ldapOrgs[$orgId] = Configuration::loadFromArray(
98
                $orgCfg,
99
                'authsources[' . var_export($this->authId, true) . '][' . var_export($orgId, true). ']'
100
            );
101
102
            $this->mapping = $this->ldapConfig->getArray('mapping');
103
        }
104
    }
105
106
107
    /**
108
     * Attempt to log in using the given username and password.
109
     *
110
     * @param string $username  The username the user wrote.
111
     * @param string $password  The password the user wrote.
112
     * @return array  Associative array with the users attributes.
113
     */
114
    protected function login(string $username, string $password, string $organization): array
115
    {
116
        if (!array_key_exists($organization, $this->ldapOrgs)) {
117
            // The user has selected an organization which doesn't exist anymore.
118
            Logger::warning('Authentication source ' . var_export($this->authId, true) .
119
                ': Organization seems to have disappeared while the user logged in.' .
120
                ' Organization was ' . var_export($organization, true));
121
            throw new Error\Error('WRONGUSERPASS');
122
        }
123
124
        if ($this->includeOrgInUsername) {
125
            $username = $username . '@' . $organization;
126
        }
127
128
        // To do: ensure the authsource exists
129
        $authsource = $this->mapping[$organization];
130
131
        // To do: ensure that the authsource is an Ldap-authsource
132
//        $ldap = new Ldap(['AuthId' => $authsource], $this->ldapOrgs[$organization]->toArray());
133
        $ldap = new class () extends Ldap {
134
            public function _authenticate(string $username, string $password) {
135
                return $this->authenticate($username, $password);
136
            }
137
        }
138
139
        return $ldap->_authenticate($username, $password);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_RETURN on line 139 at column 8
Loading history...
140
    }
141
142
143
    /**
144
     * Retrieve list of organizations.
145
     *
146
     * @return array  Associative array with the organizations.
147
     */
148
    protected function getOrganizations(): array
149
    {
150
        return $this->orgs;
151
    }
152
}
153