Passed
Branch monitor-2.5.x (8b654c)
by Tim
01:31
created

AuthSources   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A convertAuthSourceData() 0 19 5
B invokeTest() 0 56 7
A getAuthSourceSpecifics() 0 8 3
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\TestSuite;
4
5
use \SimpleSAML_Configuration as ApplicationConfiguration;
6
use \SimpleSAML\Modules\Monitor\TestConfiguration as TestConfiguration;
7
use \SimpleSAML\Modules\Monitor\TestResult as TestResult;
8
use \SimpleSAML\Modules\Monitor\TestData as TestData;
9
10
final class AuthSources extends \SimpleSAML\Modules\Monitor\TestSuiteFactory
11
{
12
    /**
13
     * @var ApplicationConfiguration
14
     */
15
    private $authSourceConfig;
16
17
    /**
18
     * @var array|bool
19
     */
20
    private $checkAuthSources;
21
22
    /**
23
     * @var array|null
24
     */
25
    private $authSourceSpecifics;
26
27
    /**
28
     * @param TestConfiguration $configuration
29
     */
30
    public function __construct($configuration)
31
    {
32
        $moduleConfig = $configuration->getModuleConfig();
33
        $this->authSourceConfig = $configuration->getAuthSourceConfig();
34
        $this->checkAuthSources = $moduleConfig->getValue('checkAuthSources', true);
35
        $this->authSourceSpecifics = $moduleConfig->getValue('authSourceSpecifics', null);
36
        $this->setCategory('Authentication sources');
37
38
        parent::__construct($configuration);
39
    }
40
41
    /**
42
     * @return void
43
     */
44
    public function invokeTest()
45
    {
46
        if ($this->checkAuthSources === true) {
47
            $authSources = $this->authSourceConfig->getOptions();
48
        } else if (is_array($this->checkAuthSources)) {
49
            $authSources = array_intersect($this->authSourceConfig->getOptions(), $this->checkAuthSources);
50
        } else { // false or invalid value
51
            return;
52
        }
53
54
        $configuration = $this->getConfiguration();
55
        $output = [];
56
57
        foreach ($authSources as $authSourceId) {
58
            $authSourceSpecifics = $this->getAuthSourceSpecifics($authSourceId);
59
            $authSourceData = $this->authSourceConfig->getValue($authSourceId);
60
            $input = [
61
                'authSourceId' => $authSourceId,
62
                'authSourceData' => $this->authSourceConfig->getValue($authSourceId),
63
                'authSourceSpecifics' => $authSourceSpecifics,
64
            ];
65
            $testData = new TestData($input);
66
67
            switch ($authSourceData[0]) {
68
                case 'ldap:LDAP':
69
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
70
                    $this->addTestResults($ldapTest->getTestResults());
71
                    $output[$authSourceId] = $ldapTest->getArrayizeTestResults();
72
                    break;
73
                case 'negotiate:Negotiate':
74
                    $negoTest = new AuthSource\Negotiate($configuration, $testData);
75
                    $this->addTestResults($negoTest->getTestResults());
76
77
                    // We need to do some convertions from Negotiate > LDAP
78
                    $this->convertAuthSourceData($authSourceData);
79
                    $testData->setInput($authSourceData, 'authSourceData');
80
81
                    $ldapTest = new AuthSource\Ldap($configuration, $testData);
82
                    $this->addTestResults($ldapTest->getTestResults());
83
84
                    $output[$authSourceId] = array_merge($negoTest->getArrayizeTestResults(), $ldapTest->getArrayizeTestResults());
85
                    break;
86
                case 'multiauth:MultiAuth':
87
                    // Relies on other authSources
88
                    continue 2;
89
                default:
90
                    // Not implemented
91
                    continue 2;
92
            }
93
        }
94
95
        $state = $this->calculateState();
96
        $testResult = new TestResult('Authentication sources');
97
        $testResult->setState($state);
98
        $testResult->setOutput($output);
99
        $this->setTestResult($testResult);
100
    }
101
102
    /**
103
     * @param string $authSourceId
104
     *
105
     * @return array|null
106
     */
107
    private function getAuthSourceSpecifics($authSourceId)
108
    {
109
        if (is_array($this->authSourceSpecifics)) {
110
            if (array_key_exists($authSourceId, $this->authSourceSpecifics)) {
111
                return $this->authSourceSpecifics[$authSourceId];
112
            }
113
        }
114
        return null;
115
    }
116
117
    /**
118
     * @param array $authSourceData
119
     *
120
     * @return void
121
     */
122
    private function convertAuthSourceData(&$authSourceData)
123
    {
124
        // LDAP and Negotiate authSources use different names for equal properties
125
        // Hopefully this function can go away in SSP 2.0
126
        if (isSet($authSourceData['debugLDAP'])) {
127
            $authSourceData['debug'] = $authSourceData['debugLDAP'];
128
            unset($authSourceData['debugLDAP']);
129
        }
130
        if (isSet($authSourceData['adminUser'])) {
131
            $authSourceData['search.username'] = $authSourceData['adminUser'];
132
            unset($authSourceData['adminUser']);
133
        }
134
        if (isSet($authSourceData['adminPassword'])) {
135
            $authSourceData['search.password'] = $authSourceData['adminPassword'];
136
            unset($authSourceData['adminPassword']);
137
        }
138
        if (isSet($authSourceData['base'])) {
139
            $authSourceData['search.base'] = $authSourceData['base'];
140
            unset($authSourceData['base']);
141
        }
142
    }
143
}
144