Passed
Pull Request — master (#28)
by Tim
01:55
created

LdapMulti.php$0 ➔ login()   A

Complexity

Conditions 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 26
rs 9.504

1 Method

Rating   Name   Duplication   Size   Complexity  
A LdapMulti.php$0 ➔ _login() 0 2 1
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
*/
103
        $this->orgs = array_keys($this->ldapConfig->getArray('mapping'));
104
        $this->mapping = $this->ldapConfig->getArray('mapping');
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
/**
118
        if (!array_key_exists($organization, $this->ldapOrgs)) {
119
            // The user has selected an organization which doesn't exist anymore.
120
            Logger::warning('Authentication source ' . var_export($this->authId, true) .
121
                ': Organization seems to have disappeared while the user logged in.' .
122
                ' Organization was ' . var_export($organization, true));
123
            throw new Error\Error('WRONGUSERPASS');
124
        }
125
*/
126
        if ($this->includeOrgInUsername) {
127
            $username = $username . '@' . $organization;
128
        }
129
130
        // To do: ensure the authsource exists
131
        $authsource = $this->mapping[$organization];
132
133
        // To do: ensure that the authsource is an Ldap-authsource
134
        $ldap = new class (['AuthId' => $authsource], $this->ldapOrgs[$organization]->toArray()) extends Ldap {
135
            public function _login(string $username, string $password) {
136
                return $this->login($username, $password);
137
            }
138
        };
139
140
        return $ldap->_login($username, $password);
141
    }
142
143
144
    /**
145
     * Retrieve list of organizations.
146
     *
147
     * @return array  Associative array with the organizations.
148
     */
149
    protected function getOrganizations(): array
150
    {
151
        return $this->orgs;
152
    }
153
}
154