|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleSAML\Module\Monitor\TestCase\AuthSource\Ldap; |
|
4
|
|
|
|
|
5
|
|
|
use SimpleSAML\Module\Monitor\State; |
|
6
|
|
|
use SimpleSAML\Module\Monitor\TestData; |
|
7
|
|
|
use SimpleSAML\Module\Monitor\TestResult; |
|
8
|
|
|
|
|
9
|
|
|
final class Search extends \SimpleSAML\Module\Monitor\TestCaseFactory |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var \SimpleSAML\Module\ldap\Auth\Ldap */ |
|
12
|
|
|
private $connection; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $base; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $username; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $password; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
private $attributes = []; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param \SimpleSAML\Module\Monitor\TestData $testData |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function initialize(TestData $testData): void |
|
33
|
|
|
{ |
|
34
|
|
|
$authSourceData = $testData->getInputItem('authSourceData'); |
|
35
|
|
|
|
|
36
|
|
|
// Just to be on the safe side, strip off any OU's and search to whole directory |
|
37
|
|
|
$base = $authSourceData->getArrayizeString('search.base', '<< unset >>'); |
|
38
|
|
|
$base = is_array($base) ? $base[0] : $base; |
|
39
|
|
|
if (($i = intval(stripos($base, 'DC='))) > 0) { |
|
40
|
|
|
$base = substr($base, $i); |
|
41
|
|
|
} |
|
42
|
|
|
$this->base = $base; |
|
43
|
|
|
|
|
44
|
|
|
$username = $authSourceData->getString('search.username', '<< unset >>'); |
|
45
|
|
|
$this->setSubject($username); |
|
46
|
|
|
if (strpos($username, 'DC=') > 0) { |
|
47
|
|
|
// We have been given a DN |
|
48
|
|
|
$username = ldap_explode_dn($username, 1); |
|
49
|
|
|
$this->username = $username[0]; |
|
50
|
|
|
$this->attributes = ['cn']; |
|
51
|
|
|
} else { |
|
52
|
|
|
// We have been given a sAMAccountName |
|
53
|
|
|
$this->username = $username; |
|
54
|
|
|
$this->attributes = ['sAMAccountName']; |
|
55
|
|
|
} |
|
56
|
|
|
$this->password = $authSourceData->getString('search.password', '<< unset >>'); |
|
57
|
|
|
$this->connection = $testData->getInputItem('connection'); |
|
58
|
|
|
|
|
59
|
|
|
parent::initialize($testData); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function invokeTest(): void |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$this->connection->searchfordn($this->base, $this->attributes, $this->username); |
|
70
|
|
|
} catch (\SimpleSAML\Error\Error $error) { |
|
71
|
|
|
// Fallthru |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$testResult = new TestResult('LDAP Search', $this->getSubject()); |
|
75
|
|
|
|
|
76
|
|
|
if (isset($error)) { |
|
77
|
|
|
// When you feed str_replace a string, outcome will be string too, but Psalm doesn't see it that way |
|
78
|
|
|
|
|
79
|
|
|
/** @var string $msg */ |
|
80
|
|
|
$msg = str_replace('Library - LDAP searchfordn(): ', '', $error->getMessage()); |
|
81
|
|
|
$testResult->setState(State::ERROR); |
|
82
|
|
|
$testResult->setMessage($msg); |
|
83
|
|
|
} else { |
|
84
|
|
|
$testResult->setState(State::OK); |
|
85
|
|
|
$testResult->setMessage('Search succesful'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->setTestResult($testResult); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|