1 | <?php |
||
10 | final class Search extends \SimpleSAML\Module\monitor\TestCaseFactory |
||
11 | { |
||
12 | /* |
||
13 | * @var \SimpleSAML_Auth_LDAP|null |
||
14 | */ |
||
15 | private $connection = null; |
||
16 | |||
17 | /* |
||
18 | * @var string|null |
||
19 | */ |
||
20 | private $base = null; |
||
21 | |||
22 | /* |
||
23 | * @var string|null |
||
24 | */ |
||
25 | private $username = null; |
||
26 | |||
27 | /* |
||
28 | * @var string|null |
||
29 | */ |
||
30 | private $password = null; |
||
31 | |||
32 | /* |
||
33 | * @var array |
||
34 | */ |
||
35 | private $attributes = array(); |
||
36 | |||
37 | /* |
||
38 | * @param TestData $testData |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | protected function initialize($testData) |
||
43 | { |
||
44 | $authSourceData = $testData->getInput('authSourceData'); |
||
45 | |||
46 | $base = $authSourceData['search.base']; |
||
47 | $base = is_array($base) ? $base[0] : $base; |
||
48 | if (($i = stripos($base, 'DC=')) > 0) { |
||
49 | $base = substr($base, $i); |
||
50 | } |
||
51 | $this->base = $base; |
||
52 | |||
53 | $username = $authSourceData['search.username']; |
||
54 | $this->setSubject($username); |
||
55 | if (strpos($username, 'DC=') > 0) { |
||
56 | // We have been given a DN |
||
57 | $username = ldap_explode_dn($username, 1); |
||
58 | $this->username = $username[0]; |
||
59 | $this->attributes = array('cn'); |
||
60 | } else { |
||
61 | // We have been given a sAMAccountName |
||
62 | $this->username = $username; |
||
63 | $this->attributes = array('sAMAccountName'); |
||
64 | } |
||
65 | $this->password = $authSourceData['search.password']; |
||
66 | $this->connection = $testData->getInput('connection'); |
||
67 | |||
68 | parent::initialize($testData); |
||
69 | } |
||
70 | |||
71 | /* |
||
72 | * @return void |
||
73 | */ |
||
74 | protected function invokeTest() |
||
75 | { |
||
76 | $connection = $this->connection; |
||
77 | $testResult = new TestResult('LDAP Search', $this->getSubject()); |
||
78 | |||
79 | try { |
||
80 | $distinguishedName = $connection->searchfordn($this->base, $this->attributes, $this->username); |
||
81 | } catch (\Exception $e) { |
||
82 | $distinguishedName = false; |
||
83 | } |
||
84 | |||
85 | if (is_string($distinguishedName)) { |
||
86 | $msg = 'Search succesful'; |
||
87 | $state = State::OK; |
||
88 | } elseif ($distinguishedName === false) { |
||
89 | $msg = str_replace('Library - LDAP searchfordn(): ', '', $e->getMessage()); |
||
90 | $state = State::ERROR; |
||
91 | } else ($distinguishedName === null) { |
||
92 | // Search for configured search.username returned no results; Shouldn't happen!! |
||
93 | $msg = 'Invalid search result'; |
||
|
|||
94 | $state = State::WARNING; |
||
95 | } |
||
96 | |||
97 | $testResult->setState($state); |
||
98 | $testResult->setMessage($msg); |
||
99 | $this->setTestResult($testResult); |
||
100 | } |
||
101 | } |
||
102 |