Passed
Push — monitor-2.7.x ( 9e4b6d...32a54b )
by Tim
14:35 queued 04:20
created

Ldap   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 77
c 1
b 0
f 0
dl 0
loc 150
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B invokeTest() 0 72 7
A __construct() 0 17 2
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestSuite\AuthSource;
4
5
use SimpleSAML\Assert\Assert;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Assert\Assert 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...
6
use SimpleSAML\Configuration;
7
use SimpleSAML\Module\monitor\State;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\State 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...
8
use SimpleSAML\Module\monitor\TestConfiguration;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestConfiguration 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...
9
use SimpleSAML\Module\monitor\TestCase;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestCase 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...
10
use SimpleSAML\Module\monitor\TestData;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestData 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...
11
use SimpleSAML\Module\monitor\TestResult;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestResult 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...
12
13
use function array_key_exists;
14
use function array_replace;
15
use function explode;
16
use function is_null;
17
use function parse_url;
18
use function preg_match;
19
use function preg_split;
20
use function stream_context_create;
21
22
final class Ldap extends \SimpleSAML\Module\monitor\TestSuiteFactory
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestSuiteFactory 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...
23
{
24
    /** @var ApplicationConfiguration */
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monito...pplicationConfiguration 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...
25
    private $authSourceData;
26
27
    /** @var array|null */
28
    private $authSourceSpecifics;
29
30
    /** @var string[] */
31
    private $hosts;
32
33
    /** @var integer|null */
34
    private $certExpirationWarning = null;
35
36
37
    /**
38
     * @param TestConfiguration $configuration
39
     * @param TestData $testData
40
     */
41
    public function __construct(TestConfiguration $configuration, TestData $testData)
42
    {
43
        $moduleConfig = $configuration->getModuleConfig();
44
        $authSourceData = $testData->getInputItem('authSourceData');
45
        $authSourceSpecifics = $testData->getInputItem('authSourceSpecifics');
46
47
        assert(is_array($authSourceData));
48
        assert(is_array($authSourceSpecifics) || is_null($authSourceSpecifics));
49
50
        $authSourceData = Configuration::loadFromArray($authSourceData);
51
        $this->hosts = preg_split('/\s+/', $authSourceData->getString('hostname'), -1, PREG_SPLIT_NO_EMPTY);
52
        $this->authSourceData = $authSourceData;
0 ignored issues
show
Documentation Bug introduced by
It seems like $authSourceData of type SimpleSAML\Configuration is incompatible with the declared type SimpleSAML\Module\monito...pplicationConfiguration of property $authSourceData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
        $this->authSourceSpecifics = $authSourceSpecifics;
54
        $this->certExpirationWarning = $moduleConfig->getValue('certExpirationWarning', 28);
55
        $this->setCategory('LDAP authentication source');
56
57
        parent::__construct($configuration);
58
    }
59
60
61
    /**
62
     * @return void
63
     */
64
    public function invokeTest()
65
    {
66
        // Test LDAP configuration
67
        $confTest = new TestCase\AuthSource\Ldap\Configuration(
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monito...urce\Ldap\Configuration 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...
68
            new TestData(['authSourceData' => $this->authSourceData])
69
        );
70
        $confTestResult = $confTest->getTestResult();
71
        $this->addTestResult($confTestResult);
72
73
        if ($confTestResult->getState() === State::OK) {
74
            $connection = $confTestResult->getOutput('connection');
75
76
            // Test connection for each configured LDAP-server
77
            $failure = count($this->hosts);
78
            foreach ($this->hosts as $hostname) {
79
                $preparedTestData = $this->prepareConnection($hostname);
80
                $connTest = new TestCase\Network\ConnectUri(
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monito...Case\Network\ConnectUri 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...
81
                    new TestData($preparedTestData)
82
                );
83
                $connTestResult = $connTest->getTestResult();
84
                $this->addTestResult($connTestResult);
85
86
                if ($connTestResult->getState() === State::OK) {
87
                    $certData = $connTestResult->getOutput('certData');
88
89
                    // Test certificate when available
90
                    if ($certData !== null) {
91
                        $certTest = new TestCase\Cert(
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monitor\TestCase\Cert 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...
92
                            new TestData([
93
                                'certData' => $certData,
94
                                'category' => 'LDAP Server Certificate',
95
                                'certExpirationWarning' => $this->certExpirationWarning,
96
                            ])
97
                        );
98
                        $certTestResult = $certTest->getTestResult();
99
                        $this->addTestResult($certTestResult);
100
                    }
101
                    $failure--;
102
                }
103
            }
104
105
            if ($failure === 0) {
106
                // Test bind
107
                $bindTest = new TestCase\AuthSource\Ldap\Bind(
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monito...se\AuthSource\Ldap\Bind 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...
108
                    new TestData([
109
                        'authSourceData' => $this->authSourceData,
110
                        'connection' => $connection
111
                    ])
112
                );
113
                $bindTestResult = $bindTest->getTestResult();
114
                $this->addTestResult($bindTestResult);
115
116
                if ($bindTestResult->getState() === State::OK) {
117
                    // Test search
118
                    $searchTest = new TestCase\AuthSource\Ldap\Search(
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\monito...\AuthSource\Ldap\Search 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...
119
                        new TestData([
120
                            'authSourceData' => $this->authSourceData,
121
                            'connection' => $connection
122
                        ])
123
                    );
124
                    $searchTestResult = $searchTest->getTestResult();
125
                    $this->addTestResult($searchTestResult);
126
                }
127
            }
128
            unset($connection);
129
        }
130
131
        $state = $this->calculateState();
132
133
        $testResult = new TestResult('LDAP Authentication');
134
        $testResult->setState($state);
135
        $this->setTestResult($testResult);
136
    }
137
138
139
    /**
140
     * @param string $connectString
141
     *
142
     * @return array
143
     */
144
    private function prepareConnection($connectString)
145
    {
146
        $hostname = parse_url($connectString, PHP_URL_HOST);
147
        $authSourceData = $this->authSourceData;
148
        $authSourceSpecifics = $this->authSourceSpecifics;
149
150
        if (preg_match('/^(ldaps:\/\/(.*))$/', $connectString, $matches)) {
151
            // The default context
152
            $sslContext = ['capture_peer_cert' => true, 'verify_peer' => true];
153
154
            // The non-default context, if configured ...
155
            if (!is_null($authSourceSpecifics) && array_key_exists('ssl', $authSourceSpecifics)) {
156
                $sslContext = array_replace($sslContext, $authSourceSpecifics['ssl']);
157
            }
158
159
            $port = parse_url($connectString, PHP_URL_PORT);
160
            $port = $port ?: $authSourceData->getInteger('port', 636);
161
162
            $uri = 'ssl://' .  $hostname . ':' . $port;
163
            $context = stream_context_create(['ssl' => $sslContext]);
164
        } else {
165
            $port = $authSourceData->getInteger('port', 389);
166
            $uri = 'tcp://' . $hostname . ':' . $port;
167
            $context = stream_context_create();
168
        }
169
170
        $timeout = $authSourceData->getInteger('timeout', null);
171
        return ['uri' => $uri, 'context' => $context, 'timeout' => $timeout];
172
    }
173
}
174