Cancelled
Branch master (244451)
by Tim
10:31
created

AuthSources   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 100
rs 10
wmc 13

3 Methods

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