Passed
Branch monitor-2.5.x (8b654c)
by Tim
01:31
created

Bind::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor\TestCase\AuthSource\Ldap;
4
5
use \SimpleSAML\Modules\Monitor\State as State;
6
use \SimpleSAML\Modules\Monitor\TestData as TestData;
7
use \SimpleSAML\Modules\Monitor\TestResult as TestResult;
8
9
final class Bind extends \SimpleSAML\Modules\Monitor\TestCaseFactory
10
{
11
    /*
12
     * @var \SimpleSAML_Auth_LDAP
13
     */
14
    private $connection;
15
16
    /*
17
     * @var string
18
     */
19
    private $username;
20
21
    /*
22
     * @var string
23
     */
24
25
    private $password;
26
27
    /*
28
     * @param TestData $testData
29
     *
30
     * @return void
31
     */
32
    protected function initialize($testData)
33
    {
34
        $this->connection = $testData->getInputItem('connection');
35
        $authSourceData = $testData->getInputItem('authSourceData');
36
37
        $this->username = $authSourceData->getString('search.username', '<< unset >>');
38
        $this->password = $authSourceData->getString('search.password', '<< unset >>');
39
40
        parent::initialize($testData);
41
    }
42
   
43
    /*
44
     * @return void
45
     */
46
    public function invokeTest()
47
    {
48
        try {
49
            $bind = $this->connection->bind($this->username, $this->password);
50
        } catch (\Exception $error) {
51
            // Fallthru
52
        }
53
54
        $testResult = new TestResult('LDAP Bind', $this->username);
55
        if (isSet($error)) {
56
            $msg = str_replace('Library - LDAP bind(): ', '', $error->getMessage());
57
            $testResult->setState(State::FATAL);
58
        } elseif ($bind === true) {
59
            $msg = 'Bind succesful';
60
            $testResult->setState(State::OK);
61
        } else {
62
            $msg = 'Authentication failed';
63
            $testResult->setState(State::ERROR);
64
        }
65
66
        $testResult->setMessage($msg);
67
        $this->setTestResult($testResult);
68
    }
69
}
70