Completed
Branch feature-unit-tests (9d0273)
by Tim
01:44
created

Negotiate::invokeTest()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 8
nop 0
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSource;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
9
final class Negotiate extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /**
12
     * @var string|null
13
     */
14
    private $keytab = null;
15
16
    /**
17
     * @var string|null
18
     */
19
    private $authorization = null;
20
21
    /*
22
     * @param TestData $testData
23
     *
24
     * @return void
25
     */
26
    protected function initialize($testData)
27
    {
28
        $this->keytab = $testData->getInputItem('keytab');
29
30
        $authorization = $testData->getInputItem('authorization');
31
        $this->authorization = (is_null($authorization) || empty($authorization)) ? null : $authorization;
32
33
        parent::initialize($testData);
34
    }
35
36
    /*
37
     * @return void
38
     */
39
    public function invokeTest()
40
    {
41
        $testResult = new TestResult('Authentication', 'Kerberos token validation');
42
43
        $auth = new \KRB5NegotiateAuth($this->keytab);
44
45
        try {
46
            $reply = @$auth->doAuthentication();
47
        } catch (\Exception $error) {
48
            // Fallthru
49
        }
50
51
        if (isSet($error)) {
52
            $testResult->setState(State::WARNING);
53
            $testResult->setMessage($error->getMessage());
54
        } else if ($reply === true) {
55
            $testResult->setState(State::OK);
56
            $testResult->setMessage('Succesfully authenticated as '.$auth->getAuthenticatedUser());
57
        } else if (is_null($this->authorization)) {
58
            // Either misconfiguration of the browser, or user not authenticated at a KDC
59
            $testResult->setState(State::SKIPPED);
60
            $testResult->setMessage('Unable to authenticate; no token provided');
61
        } else { // $reply === false
62
            $testResult->setState(State::WARNING);
63
            $testResult->setMessage("Something went wrong and we couldn't tell why");
64
        }
65
66
        $this->setTestResult($testResult);
67
    }
68
}
69