1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\monitor\TestSuite\AuthSource; |
4
|
|
|
|
5
|
|
|
use \SimpleSAML\Module\monitor\State as State; |
6
|
|
|
use \SimpleSAML\Module\monitor\TestConfiguration as TestConfiguration; |
7
|
|
|
use \SimpleSAML\Module\monitor\TestCase as TestCase; |
8
|
|
|
use \SimpleSAML\Module\monitor\TestData as TestData; |
9
|
|
|
|
10
|
|
|
final class Ldap extends \SimpleSAML\Module\monitor\TestSuiteFactory |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $authSourceData = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $hosts = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param TestConfiguration $configuration |
24
|
|
|
* @param TestData $testData |
25
|
|
|
*/ |
26
|
|
|
public function __construct($configuration, $testData) |
27
|
|
|
{ |
28
|
|
|
$authSourceData = $testData->getInput('authSourceData'); |
29
|
|
|
assert(is_array($authSourceData)); |
30
|
|
|
|
31
|
|
|
$this->authSourceData = $authSourceData; |
32
|
|
|
$this->hosts = explode(' ', $authSourceData['hostname']); |
33
|
|
|
$this->setCategory('LDAP authentication source'); |
34
|
|
|
|
35
|
|
|
parent::__construct($configuration); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function invokeTest() |
42
|
|
|
{ |
43
|
|
|
// Test LDAP configuration |
44
|
|
|
$confTest = new TestCase\AuthSource\Ldap\Configuration( |
45
|
|
|
$this, |
46
|
|
|
new TestData(['authSourceData' => $this->authSourceData]) |
47
|
|
|
); |
48
|
|
|
$confTestResult = $confTest->getTestResult(); |
49
|
|
|
$this->addTestResult($confTestResult); |
50
|
|
|
|
51
|
|
|
if ($confTestResult->getState() === State::OK) { |
52
|
|
|
$connection = $confTestResult->getOutput('connection'); |
53
|
|
|
|
54
|
|
|
// Test connection for each configured LDAP-server |
55
|
|
|
foreach ($this->hosts as $hostname) { |
56
|
|
|
$preparedTestData = $this->prepareConnection($hostname, $this->authSourceData); |
57
|
|
|
$connTest = new TestCase\Network\ConnectUri( |
58
|
|
|
$this, |
59
|
|
|
new TestData($preparedTestData) |
60
|
|
|
); |
61
|
|
|
$connTestResult = $connTest->getTestResult(); |
62
|
|
|
$this->addTestResult($connTestResult); |
63
|
|
|
|
64
|
|
|
if ($connTestResult->getState() === State::OK) { |
65
|
|
|
$certData = $connTestResult->getOutput('certData'); |
66
|
|
|
|
67
|
|
|
// Test certificate when available |
68
|
|
|
if ($certData !== null) { |
69
|
|
|
$certTest = new TestCase\Cert( |
70
|
|
|
$this, |
71
|
|
|
new TestData(['certData' => $certData, 'category' => 'LDAP Server Certificate']) |
72
|
|
|
); |
73
|
|
|
$certTestResult = $certTest->getTestResult(); |
74
|
|
|
$this->addTestResult($certTestResult); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Test bind |
80
|
|
|
$testData = new TestData([ |
81
|
|
|
'authSourceData' => $this->authSourceData, |
82
|
|
|
'connection' => $connection |
83
|
|
|
]); |
84
|
|
|
$bindTest = new TestCase\AuthSource\Ldap\Bind( |
85
|
|
|
$this, |
86
|
|
|
$testData |
87
|
|
|
); |
88
|
|
|
$bindTestResult = $bindTest->getTestResult(); |
89
|
|
|
$this->addTestResult($bindTestResult); |
90
|
|
|
|
91
|
|
|
if ($bindTestResult->getState() === State::OK) { |
92
|
|
|
// Test search |
93
|
|
|
$testData = new TestData([ |
94
|
|
|
'authSourceData' => $this->authSourceData, |
95
|
|
|
'connection' => $connection |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$searchTest = new TestCase\AuthSource\Ldap\Search( |
99
|
|
|
$this, |
100
|
|
|
$testData |
101
|
|
|
); |
102
|
|
|
$searchTestResult = $searchTest->getTestResult(); |
103
|
|
|
$this->addTestResult($searchTestResult); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $connectString |
110
|
|
|
* @param array $authSourceData |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
private function prepareConnection($connectString, $authSourceData) |
115
|
|
|
{ |
116
|
|
|
$hostname = parse_url($connectString, PHP_URL_HOST); |
117
|
|
|
|
118
|
|
|
if (preg_match('/^(ldaps:\/\/(.*))$/', $connectString, $matches)) { |
119
|
|
|
$port = parse_url($connectString, PHP_URL_PORT); |
120
|
|
|
$uri = 'ssl://' . $hostname . ($port === null) ? '' : (':' . $port); |
121
|
|
|
$context = stream_context_create(array("ssl" => array("capture_peer_cert" => true, "verify_peer" => true))); |
122
|
|
|
} else { |
123
|
|
|
$port = $authSourceData['port']; |
124
|
|
|
$uri = 'tcp://' . $hostname . ':' . $port; |
125
|
|
|
$context = stream_context_create(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$timeout = isSet($authSourceData['timeout']) ? $authSourceData['timeout'] : null; |
129
|
|
|
return ['uri' => $uri, 'context' => $context, 'timeout' => $timeout]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|