|
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\Statement; |
|
13
|
|
|
|
|
14
|
|
|
use LightSaml\Error\LightSamlValidationException; |
|
15
|
|
|
use LightSaml\Helper; |
|
16
|
|
|
use LightSaml\Model\Assertion\AbstractStatement; |
|
17
|
|
|
use LightSaml\Model\Assertion\Attribute; |
|
18
|
|
|
use LightSaml\Model\Assertion\AttributeStatement; |
|
19
|
|
|
use LightSaml\Model\Assertion\AuthnContext; |
|
20
|
|
|
use LightSaml\Model\Assertion\AuthnStatement; |
|
21
|
|
|
|
|
22
|
|
|
class StatementValidator implements StatementValidatorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @param AbstractStatement $statement |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \LightSaml\Error\LightSamlValidationException |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
15 |
|
public function validateStatement(AbstractStatement $statement) |
|
32
|
|
|
{ |
|
33
|
15 |
|
if ($statement instanceof AuthnStatement) { |
|
34
|
11 |
|
$this->validateAuthnStatement($statement); |
|
35
|
5 |
|
} elseif ($statement instanceof AttributeStatement) { |
|
36
|
4 |
|
$this->validateAttributeStatement($statement); |
|
37
|
|
|
} else { |
|
38
|
1 |
|
throw new LightSamlValidationException(sprintf("Unsupported Statement type '%s'", get_class($statement))); |
|
39
|
|
|
} |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
11 |
|
private function validateAuthnStatement(AuthnStatement $statement) |
|
43
|
|
|
{ |
|
44
|
11 |
|
if (false == $statement->getAuthnInstantTimestamp()) { |
|
45
|
1 |
|
throw new LightSamlValidationException('AuthnStatement MUST have an AuthnInstant attribute'); |
|
46
|
|
|
} |
|
47
|
10 |
|
if (false == Helper::validateOptionalString($statement->getSessionIndex())) { |
|
|
|
|
|
|
48
|
1 |
|
throw new LightSamlValidationException('SessionIndex attribute of AuthnStatement must contain at least one non-whitespace character'); |
|
49
|
|
|
} |
|
50
|
9 |
|
if ($statement->getSubjectLocality()) { |
|
51
|
3 |
|
if (false == Helper::validateOptionalString($statement->getSubjectLocality()->getAddress())) { |
|
|
|
|
|
|
52
|
1 |
|
throw new LightSamlValidationException('Address attribute of SubjectLocality must contain at least one non-whitespace character'); |
|
53
|
|
|
} |
|
54
|
2 |
|
if (false == Helper::validateOptionalString($statement->getSubjectLocality()->getDnsName())) { |
|
|
|
|
|
|
55
|
1 |
|
throw new LightSamlValidationException('DNSName attribute of SubjectLocality must contain at least one non-whitespace character'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
7 |
|
if (false == $statement->getAuthnContext()) { |
|
59
|
1 |
|
throw new LightSamlValidationException('AuthnStatement MUST have an AuthnContext element'); |
|
60
|
|
|
} |
|
61
|
6 |
|
$this->validateAuthnContext($statement->getAuthnContext()); |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
6 |
|
private function validateAuthnContext(AuthnContext $authnContext) |
|
65
|
|
|
{ |
|
66
|
6 |
|
if (false == $authnContext->getAuthnContextClassRef() && |
|
67
|
6 |
|
false == $authnContext->getAuthnContextDecl() && |
|
68
|
6 |
|
false == $authnContext->getAuthnContextDeclRef() |
|
69
|
|
|
) { |
|
70
|
1 |
|
throw new LightSamlValidationException('AuthnContext element MUST contain at least one AuthnContextClassRef, AuthnContextDecl or AuthnContextDeclRef element'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
if ($authnContext->getAuthnContextClassRef() && |
|
74
|
5 |
|
$authnContext->getAuthnContextDecl() && |
|
75
|
5 |
|
$authnContext->getAuthnContextDeclRef() |
|
|
|
|
|
|
76
|
|
|
) { |
|
77
|
1 |
|
throw new LightSamlValidationException('AuthnContext MUST NOT contain more than two elements.'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
if ($authnContext->getAuthnContextClassRef()) { |
|
81
|
3 |
|
if (false == Helper::validateWellFormedUriString($authnContext->getAuthnContextClassRef())) { |
|
|
|
|
|
|
82
|
1 |
|
throw new LightSamlValidationException('AuthnContextClassRef has a value which is not a wellformed absolute uri'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
3 |
|
if ($authnContext->getAuthnContextDeclRef()) { |
|
|
|
|
|
|
86
|
1 |
|
if (false === Helper::validateWellFormedUriString($authnContext->getAuthnContextDeclRef())) { |
|
87
|
1 |
|
throw new LightSamlValidationException('AuthnContextDeclRef has a value which is not a wellformed absolute uri'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
private function validateAttributeStatement(AttributeStatement $statement) |
|
93
|
|
|
{ |
|
94
|
4 |
|
if (false == $statement->getAllAttributes()) { |
|
95
|
1 |
|
throw new LightSamlValidationException('AttributeStatement MUST contain at least one Attribute or EncryptedAttribute'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
foreach ($statement->getAllAttributes() as $attribute) { |
|
99
|
3 |
|
$this->validateAttribute($attribute); |
|
100
|
|
|
} |
|
101
|
2 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param Attribute $attribute |
|
105
|
|
|
* |
|
106
|
|
|
* @throws LightSamlValidationException |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
3 |
|
private function validateAttribute(Attribute $attribute) |
|
111
|
|
|
{ |
|
112
|
3 |
|
if (false == Helper::validateRequiredString($attribute->getName())) { |
|
|
|
|
|
|
113
|
1 |
|
throw new LightSamlValidationException('Name attribute of Attribute element MUST contain at least one non-whitespace character'); |
|
114
|
|
|
} |
|
115
|
2 |
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.