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)) { |
|
|
|
|
50
|
|
|
throw new LightSamlValidationException('Conditions.NotBefore must not be in the future'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (false == Helper::validateNotOnOrAfter($assertion->getConditions()->getNotOnOrAfterTimestamp(), $now, $allowedSecondsSkew)) { |
|
|
|
|
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)) { |
|
|
|
|
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()) { |
|
|
|
|
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)) { |
|
|
|
|
89
|
|
|
throw new LightSamlValidationException('SubjectConfirmationData.NotBefore must not be in the future'); |
90
|
|
|
} |
91
|
|
|
if (false == Helper::validateNotOnOrAfter($subjectConfirmation->getSubjectConfirmationData()->getNotOnOrAfterTimestamp(), $now, $allowedSecondsSkew)) { |
|
|
|
|
92
|
|
|
throw new LightSamlValidationException('SubjectConfirmationData.NotOnOrAfter must not be in the past'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.