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

Ldap::invokeTest()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 75
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 45
nc 4
nop 0
dl 0
loc 75
rs 8.2666
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\TestSuite\AuthSource;
4
5
use \SimpleSAML_Configuration as ApplicationConfiguration;
6
use \SimpleSAML\Modules\Monitor\State as State;
7
use \SimpleSAML\Modules\Monitor\TestConfiguration as TestConfiguration;
8
use \SimpleSAML\Modules\Monitor\TestCase as TestCase;
9
use \SimpleSAML\Modules\Monitor\TestData as TestData;
10
use \SimpleSAML\Modules\Monitor\TestResult as TestResult;
11
12
final class Ldap extends \SimpleSAML\Modules\Monitor\TestSuiteFactory
13
{
14
    /**
15
     * @var ApplicationConfiguration
16
     */
17
    private $authSourceData;
18
19
    /**
20
     * @var array|null
21
     */
22
    private $authSourceSpecifics;
23
24
    /**
25
     * @var string[]
26
     */
27
    private $hosts;
28
29
    /**
30
     * @param integer|null;
31
     */
32
    private $certExpirationWarning = null;
33
34
    /**
35
     * @param TestConfiguration $configuration
36
     * @param TestData $testData
37
     */
38
    public function __construct($configuration, $testData)
39
    {
40
        $moduleConfig = $configuration->getModuleConfig();
41
        $authSourceData = $testData->getInputItem('authSourceData');
42
        $authSourceSpecifics = $testData->getInputItem('authSourceSpecifics');
43
44
        assert(is_array($authSourceData));
45
        assert(is_array($authSourceSpecifics) || is_null($authSourceSpecifics));
46
47
        $authSourceData = \SimpleSAML_Configuration::loadFromArray($authSourceData);
48
        $this->hosts = explode(' ', $authSourceData->getString('hostname'));
49
        $this->authSourceData = $authSourceData;
50
        $this->authSourceSpecifics = $authSourceSpecifics;
51
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
52
        $this->setCategory('LDAP authentication source');
53
54
        parent::__construct($configuration);
55
    }
56
57
    /**
58
     * @return void
59
     */
60
    public function invokeTest()
61
    {
62
        // Test LDAP configuration
63
        $confTest = new TestCase\AuthSource\Ldap\Configuration(
64
            new TestData(['authSourceData' => $this->authSourceData])
65
        );
66
        $confTestResult = $confTest->getTestResult();
67
        $this->addTestResult($confTestResult);
68
69
        if ($confTestResult->getState() === State::OK) {
70
            $connection = $confTestResult->getOutput('connection');
71
72
            // Test connection for each configured LDAP-server
73
            $failure = count($this->hosts);
74
            foreach ($this->hosts as $hostname) {
75
                $preparedTestData = $this->prepareConnection($hostname);
76
                $connTest = new TestCase\Network\ConnectUri(
77
                    new TestData($preparedTestData)
78
                );
79
                $connTestResult = $connTest->getTestResult();
80
                $this->addTestResult($connTestResult);
81
82
                if ($connTestResult->getState() === State::OK) {
83
                    $certData = $connTestResult->getOutput('certData');
84
85
                    // Test certificate when available
86
                    if ($certData !== null) {
87
                        $certTest = new TestCase\Cert(
88
                            new TestData([
89
                                'certData' => $certData,
90
                                'category' => 'LDAP Server Certificate',
91
                                'certExpirationWarning' => $this->certExpirationWarning,
92
                            ])
93
                        );
94
                        $certTestResult = $certTest->getTestResult();
95
                        $this->addTestResult($certTestResult);
96
                    }
97
                    $failure--;
98
                }
99
            }
100
101
            if ($failure === 0) {
102
                // Test bind
103
                $testData = new TestData([
104
                    'authSourceData' => $this->authSourceData,
105
                    'connection' => $connection
106
                ]);
107
                $bindTest = new TestCase\AuthSource\Ldap\Bind(
108
                    $testData
109
                );
110
                $bindTestResult = $bindTest->getTestResult();
111
                $this->addTestResult($bindTestResult);
112
113
                if ($bindTestResult->getState() === State::OK) {
114
                    // Test search
115
                    $testData = new TestData([
116
                        'authSourceData' => $this->authSourceData,
117
                        'connection' => $connection
118
                    ]);
119
120
                    $searchTest = new TestCase\AuthSource\Ldap\Search(
121
                        $testData
122
                    );
123
                    $searchTestResult = $searchTest->getTestResult();
124
                    $this->addTestResult($searchTestResult);
125
                }
126
            }
127
            unset($connection);
128
        }
129
130
        $state = $this->calculateState();
131
132
        $testResult = new TestResult('LDAP Authentication');
133
        $testResult->setState($state);
134
        $this->setTestResult($testResult);
135
    }
136
137
    /**
138
     * @param string $connectString
139
     *
140
     * @return array
141
     */
142
    private function prepareConnection($connectString)
143
    {
144
        $hostname = parse_url($connectString, PHP_URL_HOST);
145
        $authSourceData = $this->authSourceData;
146
        $authSourceSpecifics = $this->authSourceSpecifics;
147
148
        if (preg_match('/^(ldaps:\/\/(.*))$/', $connectString, $matches)) {
149
            // The default context
150
            $sslContext = ['capture_peer_cert' => true, 'verify_peer' => true];
151
152
            // The non-default context, if configured ...
153
            if (!is_null($authSourceSpecifics) && array_key_exists('ssl', $authSourceSpecifics)) {
154
                $sslContext = array_replace($sslContext, $authSourceSpecifics['ssl']);
155
            }
156
157
            $port = parse_url($connectString, PHP_URL_PORT);
158
            $port = $port ?: $authSourceData->getInteger('port', 636);
159
160
            $uri = 'ssl://' .  $hostname . ':' . $port;
161
            $context = stream_context_create(['ssl' => $sslContext]);
162
        } else {
163
            $port = $authSourceData->getInteger('port', 389);
164
            $uri = 'tcp://' . $hostname . ':' . $port;
165
            $context = stream_context_create();
166
        }
167
168
        $timeout = $authSourceData->getInteger('timeout', null);
169
        return ['uri' => $uri, 'context' => $context, 'timeout' => $timeout];
170
    }
171
}
172