Passed
Push — master ( c23af9...047c93 )
by Tim
04:50
created

LdapMulti   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 108
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 46 5
A getOrganizations() 0 3 1
A login() 0 15 3
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
namespace SimpleSAML\Module\ldap\Auth\Source;
15
16
use SimpleSAML\Configuration;
17
use SimpleSAML\Error;
18
use SimpleSAML\Logger;
19
use SimpleSAML\Module\ldap\ConfigHelper;
20
use Webmozart\Assert\Assert;
21
22
class LdapMulti extends \SimpleSAML\Module\core\Auth\UserPassOrgBase
23
{
24
    /**
25
     * An array with descriptions for organizations.
26
     */
27
    private $orgs;
28
29
    /**
30
     * An array of organization IDs to LDAP configuration objects.
31
     */
32
    private $ldapOrgs;
33
34
    /**
35
     * Whether we should include the organization as part of the username.
36
     */
37
    private $includeOrgInUsername;
38
39
40
    /**
41
     * Constructor for this authentication source.
42
     *
43
     * @param array $info  Information about this authentication source.
44
     * @param array $config  Configuration.
45
     */
46
    public function __construct(array $info, arrau $config)
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\ldap\Auth\Source\arrau was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
    {
48
        // Call the parent constructor first, as required by the interface
49
        parent::__construct($info, $config);
0 ignored issues
show
Bug introduced by
$config of type SimpleSAML\Module\ldap\Auth\Source\arrau is incompatible with the type array expected by parameter $config of SimpleSAML\Module\core\A...sOrgBase::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        parent::__construct($info, /** @scrutinizer ignore-type */ $config);
Loading history...
50
51
        $cfgHelper = Configuration::loadFromArray(
52
            $config,
53
            'Authentication source ' . var_export($this->authId, true)
54
        );
55
56
57
        $this->orgs = [];
58
        $this->ldapOrgs = [];
59
        foreach ($config as $name => $value) {
60
            if ($name === 'username_organization_method') {
61
                $usernameOrgMethod = $cfgHelper->getValueValidate(
62
                    'username_organization_method',
63
                    ['none', 'allow', 'force']
64
                );
65
                $this->setUsernameOrgMethod($usernameOrgMethod);
66
                continue;
67
            }
68
69
            if ($name === 'include_organization_in_username') {
70
                $this->includeOrgInUsername = $cfgHelper->getBoolean(
71
                    'include_organization_in_username',
72
                    false
73
                );
74
                continue;
75
            }
76
77
            $orgCfg = $cfgHelper->getArray($name);
78
            $orgId = $name;
79
80
            if (array_key_exists('description', $orgCfg)) {
81
                $this->orgs[$orgId] = $orgCfg['description'];
82
            } else {
83
                $this->orgs[$orgId] = $orgId;
84
            }
85
86
            $orgCfg = new ConfigHelper(
87
                $orgCfg,
88
                'Authentication source ' . var_export($this->authId, true) .
89
                    ', organization ' . var_export($orgId, true)
90
            );
91
            $this->ldapOrgs[$orgId] = $orgCfg;
92
        }
93
    }
94
95
96
    /**
97
     * Attempt to log in using the given username and password.
98
     *
99
     * @param string $username  The username the user wrote.
100
     * @param string $password  The password the user wrote.
101
     * @param string $org  The organization the user chose.
102
     * @return array  Associative array with the users attributes.
103
     */
104
    protected function login(string $username, string $password, string $org, array $sasl_args = null): array
105
    {
106
        if (!array_key_exists($org, $this->ldapOrgs)) {
107
            // The user has selected an organization which doesn't exist anymore.
108
            Logger::warning('Authentication source ' . var_export($this->authId, true) .
109
                ': Organization seems to have disappeared while the user logged in.' .
110
                ' Organization was ' . var_export($org, true));
111
            throw new Error\Error('WRONGUSERPASS');
112
        }
113
114
        if ($this->includeOrgInUsername) {
115
            $username = $username . '@' . $org;
116
        }
117
118
        return $this->ldapOrgs[$org]->login($username, $password, $sasl_args);
119
    }
120
121
122
    /**
123
     * Retrieve list of organizations.
124
     *
125
     * @return array  Associative array with the organizations.
126
     */
127
    protected function getOrganizations(): array
128
    {
129
        return $this->orgs;
130
    }
131
}
132