AssertionTimeValidator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 25
c 1
b 0
f 0
dl 0
loc 75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateAuthnStatements() 0 9 4
A validateTimeRestrictions() 0 9 2
A validateSubject() 0 13 6
A validateConditions() 0 12 4
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Validator\Model\Assertion;
13
14
use LightSaml\Error\LightSamlValidationException;
15
use LightSaml\Helper;
16
use LightSaml\Model\Assertion\Assertion;
17
18
class AssertionTimeValidator implements AssertionTimeValidatorInterface
19
{
20
    /**
21
     * @param int $now
22
     * @param int $allowedSecondsSkew
23
     *
24
     * @throws \LightSaml\Error\LightSamlValidationException
25
     *
26
     * @return void
27
     */
28
    public function validateTimeRestrictions(Assertion $assertion, $now, $allowedSecondsSkew)
29
    {
30
        if ($allowedSecondsSkew < 0) {
31
            $allowedSecondsSkew = -1 * $allowedSecondsSkew;
32
        }
33
34
        $this->validateConditions($assertion, $now, $allowedSecondsSkew);
35
        $this->validateAuthnStatements($assertion, $now, $allowedSecondsSkew);
36
        $this->validateSubject($assertion, $now, $allowedSecondsSkew);
37
    }
38
39
    /**
40
     * @param int $now
41
     * @param int $allowedSecondsSkew
42
     */
43
    protected function validateConditions(Assertion $assertion, $now, $allowedSecondsSkew)
44
    {
45
        if (false == $assertion->getConditions()) {
46
            return;
47
        }
48
49
        if (false == Helper::validateNotBefore($assertion->getConditions()->getNotBeforeTimestamp(), $now, $allowedSecondsSkew)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
50
            throw new LightSamlValidationException('Conditions.NotBefore must not be in the future');
51
        }
52
53
        if (false == Helper::validateNotOnOrAfter($assertion->getConditions()->getNotOnOrAfterTimestamp(), $now, $allowedSecondsSkew)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
54
            throw new LightSamlValidationException('Conditions.NotOnOrAfter must not be in the past');
55
        }
56
    }
57
58
    /**
59
     * @param int $now
60
     * @param int $allowedSecondsSkew
61
     */
62
    protected function validateAuthnStatements(Assertion $assertion, $now, $allowedSecondsSkew)
63
    {
64
        if (false == $assertion->getAllAuthnStatements()) {
65
            return;
66
        }
67
68
        foreach ($assertion->getAllAuthnStatements() as $authnStatement) {
69
            if (false == Helper::validateNotOnOrAfter($authnStatement->getSessionNotOnOrAfterTimestamp(), $now, $allowedSecondsSkew)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
70
                throw new LightSamlValidationException('AuthnStatement attribute SessionNotOnOrAfter MUST be in the future');
71
            }
72
            // TODO: Consider validating that authnStatement.AuthnInstant is in the past
73
        }
74
    }
75
76
    /**
77
     * @param int $now
78
     * @param int $allowedSecondsSkew
79
     */
80
    protected function validateSubject(Assertion $assertion, $now, $allowedSecondsSkew)
81
    {
82
        if (false == $assertion->getSubject()) {
0 ignored issues
show
introduced by
The condition false == $assertion->getSubject() is always false.
Loading history...
83
            return;
84
        }
85
86
        foreach ($assertion->getSubject()->getAllSubjectConfirmations() as $subjectConfirmation) {
87
            if ($subjectConfirmation->getSubjectConfirmationData()) {
88
                if (false == Helper::validateNotBefore($subjectConfirmation->getSubjectConfirmationData()->getNotBeforeTimestamp(), $now, $allowedSecondsSkew)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
89
                    throw new LightSamlValidationException('SubjectConfirmationData.NotBefore must not be in the future');
90
                }
91
                if (false == Helper::validateNotOnOrAfter($subjectConfirmation->getSubjectConfirmationData()->getNotOnOrAfterTimestamp(), $now, $allowedSecondsSkew)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
92
                    throw new LightSamlValidationException('SubjectConfirmationData.NotOnOrAfter must not be in the past');
93
                }
94
            }
95
        }
96
    }
97
}
98