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\Model\Protocol; |
13
|
|
|
|
14
|
|
|
use LightSaml\Model\Context\DeserializationContext; |
15
|
|
|
use LightSaml\Model\Context\SerializationContext; |
16
|
|
|
use LightSaml\Model\Assertion\Assertion; |
17
|
|
|
use LightSaml\Model\Assertion\EncryptedElement; |
18
|
|
|
use LightSaml\SamlConstants; |
19
|
|
|
|
20
|
|
|
class Response extends StatusResponse |
21
|
|
|
{ |
22
|
|
|
/** @var Assertion[] */ |
23
|
|
|
protected $assertions = array(); |
24
|
|
|
|
25
|
|
|
/** @var EncryptedElement[] */ |
26
|
|
|
protected $encryptedAssertions = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return Assertion[] |
30
|
|
|
*/ |
31
|
21 |
|
public function getAllAssertions() |
32
|
|
|
{ |
33
|
21 |
|
return $this->assertions; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Assertion|null |
38
|
|
|
*/ |
39
|
6 |
|
public function getFirstAssertion() |
40
|
|
|
{ |
41
|
6 |
|
if (is_array($this->assertions) && isset($this->assertions[0])) { |
42
|
6 |
|
return $this->assertions[0]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return EncryptedElement[] |
50
|
|
|
*/ |
51
|
12 |
|
public function getAllEncryptedAssertions() |
52
|
|
|
{ |
53
|
12 |
|
return $this->encryptedAssertions; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return EncryptedElement|null |
58
|
|
|
*/ |
59
|
1 |
|
public function getFirstEncryptedAssertion() |
60
|
|
|
{ |
61
|
1 |
|
if (is_array($this->encryptedAssertions) && isset($this->encryptedAssertions[0])) { |
62
|
1 |
|
return $this->encryptedAssertions[0]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns assertions with <AuthnStatement> and <Subject> with at least one <SubjectConfirmation> |
70
|
|
|
* element containing a Method of urn:oasis:names:tc:SAML:2.0:cm:bearer. |
71
|
|
|
* |
72
|
|
|
* @return \LightSaml\Model\Assertion\Assertion[] |
73
|
|
|
*/ |
74
|
3 |
|
public function getBearerAssertions() |
75
|
|
|
{ |
76
|
3 |
|
$result = array(); |
77
|
3 |
|
if ($this->getAllAssertions()) { |
78
|
2 |
|
foreach ($this->getAllAssertions() as $assertion) { |
79
|
2 |
|
if ($assertion->hasBearerSubject()) { |
80
|
2 |
|
$result[] = $assertion; |
81
|
|
|
} |
82
|
|
|
} // foreach assertions |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Assertion $assertion |
90
|
|
|
* |
91
|
|
|
* @return Response |
92
|
|
|
*/ |
93
|
15 |
|
public function addAssertion(Assertion $assertion) |
94
|
|
|
{ |
95
|
15 |
|
$this->assertions[] = $assertion; |
96
|
|
|
|
97
|
15 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Assertion $removedAssertion |
102
|
|
|
* |
103
|
|
|
* @return Response |
104
|
|
|
*/ |
105
|
|
|
public function removeAssertion(Assertion $removedAssertion) |
106
|
|
|
{ |
107
|
|
|
$arr = array(); |
108
|
|
|
$hasThatAssertion = false; |
109
|
|
|
foreach ($this->getAllAssertions() as $assertion) { |
110
|
|
|
if ($assertion !== $removedAssertion) { |
111
|
|
|
$arr[] = $assertion; |
112
|
|
|
} else { |
113
|
|
|
$hasThatAssertion = true; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (false === $hasThatAssertion) { |
118
|
|
|
throw new \InvalidArgumentException('Response does not have assertion specified to be removed'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param EncryptedElement $encryptedAssertion |
126
|
|
|
* |
127
|
|
|
* @return Response |
128
|
|
|
*/ |
129
|
5 |
|
public function addEncryptedAssertion(EncryptedElement $encryptedAssertion) |
130
|
|
|
{ |
131
|
5 |
|
$this->encryptedAssertions[] = $encryptedAssertion; |
132
|
|
|
|
133
|
5 |
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \DOMNode $parent |
138
|
|
|
* @param SerializationContext $context |
139
|
|
|
*/ |
140
|
7 |
|
public function serialize(\DOMNode $parent, SerializationContext $context) |
141
|
|
|
{ |
142
|
7 |
|
$result = $this->createElement('samlp:Response', SamlConstants::NS_PROTOCOL, $parent, $context); |
143
|
|
|
|
144
|
7 |
|
parent::serialize($result, $context); |
145
|
|
|
|
146
|
7 |
|
$this->manyElementsToXml($this->getAllAssertions(), $result, $context, null); |
147
|
7 |
|
$this->manyElementsToXml($this->getAllEncryptedAssertions(), $result, $context, null); |
148
|
|
|
|
149
|
|
|
// must be done here at the end and not in a base class where declared in order to include signing of the elements added here |
150
|
7 |
|
$this->singleElementsToXml(array('Signature'), $result, $context); |
151
|
7 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param \DOMNode $node |
155
|
|
|
* @param DeserializationContext $context |
156
|
|
|
*/ |
157
|
7 |
View Code Duplication |
public function deserialize(\DOMNode $node, DeserializationContext $context) |
|
|
|
|
158
|
|
|
{ |
159
|
7 |
|
$this->checkXmlNodeName($node, 'Response', SamlConstants::NS_PROTOCOL); |
160
|
|
|
|
161
|
7 |
|
parent::deserialize($node, $context); |
162
|
|
|
|
163
|
7 |
|
$this->assertions = array(); |
164
|
7 |
|
$this->manyElementsFromXml( |
165
|
7 |
|
$node, |
|
|
|
|
166
|
7 |
|
$context, |
167
|
7 |
|
'Assertion', |
168
|
7 |
|
'saml', |
169
|
7 |
|
'LightSaml\Model\Assertion\Assertion', |
170
|
7 |
|
'addAssertion' |
171
|
|
|
); |
172
|
|
|
|
173
|
7 |
|
$this->encryptedAssertions = array(); |
174
|
7 |
|
$this->manyElementsFromXml( |
175
|
7 |
|
$node, |
|
|
|
|
176
|
7 |
|
$context, |
177
|
7 |
|
'EncryptedAssertion', |
178
|
7 |
|
'saml', |
179
|
7 |
|
'LightSaml\Model\Assertion\EncryptedAssertionReader', |
180
|
7 |
|
'addEncryptedAssertion' |
181
|
|
|
); |
182
|
7 |
|
} |
183
|
|
|
} |
184
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.