AuthSources   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 108
rs 10
wmc 11

3 Methods

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