1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\Configuration; |
|
|
|
|
8
|
|
|
use SimpleSAML\Error; |
9
|
|
|
use SimpleSAML\Module\ldap\ConnectorInterface; |
|
|
|
|
10
|
|
|
use SimpleSAML\Module\monitor\State; |
11
|
|
|
use SimpleSAML\Module\monitor\TestData; |
12
|
|
|
use SimpleSAML\Module\monitor\TestResult; |
13
|
|
|
|
14
|
|
|
use function intval; |
15
|
|
|
use function ldap_explode_dn; |
16
|
|
|
use function str_replace; |
17
|
|
|
use function stripos; |
18
|
|
|
use function strpos; |
19
|
|
|
use function substr; |
20
|
|
|
|
21
|
|
|
final class Search extends \SimpleSAML\Module\monitor\TestCaseFactory |
22
|
|
|
{ |
23
|
|
|
/** @var \SimpleSAML\Module\ldap\ConnectorInterface */ |
24
|
|
|
private ConnectorInterface $connection; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private string $base; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private string $username; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private string $password; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
private array $attributes = []; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \SimpleSAML\Module\monitor\TestData $testData |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
protected function initialize(TestData $testData): void |
45
|
|
|
{ |
46
|
|
|
$authSourceData = $testData->getInputItem('authSourceData'); |
47
|
|
|
|
48
|
|
|
// Just to be on the safe side, strip off any OU's and search to whole directory |
49
|
|
|
$base = $authSourceData->getArrayizeString('search.base'); |
50
|
|
|
$base = is_array($base) ? $base[0] : $base; |
51
|
|
|
if (($i = intval(stripos($base, 'DC='))) > 0) { |
52
|
|
|
$base = substr($base, $i); |
53
|
|
|
} |
54
|
|
|
$this->base = $base; |
55
|
|
|
|
56
|
|
|
$username = $authSourceData->getString('search.username'); |
57
|
|
|
$this->setSubject($username); |
58
|
|
|
if (strpos($username, 'DC=') > 0) { |
59
|
|
|
// We have been given a DN |
60
|
|
|
$username = ldap_explode_dn($username, 1); |
61
|
|
|
$this->username = $username[0]; |
62
|
|
|
$this->attributes = ['cn']; |
63
|
|
|
} else { |
64
|
|
|
// We have been given a sAMAccountName |
65
|
|
|
$this->username = $username; |
66
|
|
|
$this->attributes = ['sAMAccountName']; |
67
|
|
|
} |
68
|
|
|
$this->password = $authSourceData->getString('search.password'); |
69
|
|
|
$this->connection = $testData->getInputItem('connection'); |
70
|
|
|
|
71
|
|
|
parent::initialize($testData); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function invokeTest(): void |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
$this->connection->search([$this->base], sprintf('(|(%s=%s))', $this->attributes[0], $this->username), [], false); |
82
|
|
|
} catch (Error\Error $error) { |
83
|
|
|
// Fallthru |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$testResult = new TestResult('LDAP Search', $this->getSubject()); |
87
|
|
|
|
88
|
|
|
if (isset($error)) { |
89
|
|
|
// When you feed str_replace a string, outcome will be string too, but Psalm doesn't see it that way |
90
|
|
|
$msg = str_replace('Library - LDAP search(): ', '', $error->getMessage()); |
91
|
|
|
$testResult->setState(State::ERROR); |
92
|
|
|
$testResult->setMessage($msg); |
93
|
|
|
} else { |
94
|
|
|
$testResult->setState(State::OK); |
95
|
|
|
$testResult->setMessage('Search succesful'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->setTestResult($testResult); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: