-
Notifications
You must be signed in to change notification settings - Fork 135
/
SubjectConfirmationData.php
251 lines (212 loc) · 7.35 KB
/
SubjectConfirmationData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace SimpleSAML\SAML2\XML\saml;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
use SimpleSAML\SAML2\Utils;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\Utils as XMLUtils;
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
use function filter_var;
use function gmdate;
use function is_null;
/**
* Class representing SAML 2 SubjectConfirmationData element.
*
* @package simplesamlphp/saml2
*/
final class SubjectConfirmationData extends AbstractSamlElement
{
use ExtendableAttributesTrait;
/**
* Initialize (and parse) a SubjectConfirmationData element.
*
* @param int|null $notBefore
* @param int|null $notOnOrAfter
* @param string|null $recipient
* @param string|null $inResponseTo
* @param string|null $address
* @param (\SimpleSAML\XMLSecurity\XML\ds\KeyInfo|\SimpleSAML\XML\Chunk)[] $info
* @param \DOMAttr[] $namespacedAttributes
*/
public function __construct(
protected ?int $notBefore = null,
protected ?int $notOnOrAfter = null,
protected ?string $recipient = null,
protected ?string $inResponseTo = null,
protected ?string $address = null,
protected array $info = [],
array $namespacedAttributes = [],
) {
Assert::nullOrNotWhitespaceOnly($recipient);
Assert::nullOrValidNCName($inResponseTo); // Covers the empty string
if (!is_null($address) && !filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
Utils::getContainer()->getLogger()->warning(
sprintf('Provided argument (%s) is not a valid IP address.', $address),
);
}
Assert::allIsInstanceOfAny($info, [Chunk::class, KeyInfo::class]);
$this->setAttributesNS($namespacedAttributes);
}
/**
* Collect the value of the NotBefore-property
*
* @return int|null
*/
public function getNotBefore(): ?int
{
return $this->notBefore;
}
/**
* Collect the value of the NotOnOrAfter-property
*
* @return int|null
*/
public function getNotOnOrAfter(): ?int
{
return $this->notOnOrAfter;
}
/**
* Collect the value of the Recipient-property
*
* @return string|null
*/
public function getRecipient(): ?string
{
return $this->recipient;
}
/**
* Collect the value of the InResponseTo-property
*
* @return string|null
*/
public function getInResponseTo(): ?string
{
return $this->inResponseTo;
}
/**
* Collect the value of the Address-property
*
* @return string|null
*/
public function getAddress(): ?string
{
return $this->address;
}
/**
* Collect the value of the info-property
*
* @return (\SimpleSAML\XMLSecurity\XML\ds\KeyInfo|\SimpleSAML\XML\Chunk)[]
*/
public function getInfo(): array
{
return $this->info;
}
/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
* @return bool
*/
public function isEmptyElement(): bool
{
return empty($this->notBefore)
&& empty($this->notOnOrAfter)
&& empty($this->recipient)
&& empty($this->inResponseTo)
&& empty($this->address)
&& empty($this->info)
&& empty($this->namespacedAttributes);
}
/**
* Convert XML into a SubjectConfirmationData
*
* @param \DOMElement $xml The XML element we should load
* @return self
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* if the qualified name of the supplied element is wrong
* @throws \SimpleSAML\XML\Exception\MissingAttributeException
* if the supplied element is missing any of the mandatory attributes
* @throws \SimpleSAML\Assert\AssertionFailedException
* if NotBefore or NotOnOrAfter contain an invalid date.
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'SubjectConfirmationData', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, SubjectConfirmationData::NS, InvalidDOMElementException::class);
$NotBefore = self::getAttribute($xml, 'NotBefore', null);
if ($NotBefore !== null) {
// Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
$NotBefore = preg_replace('/([.][0-9]+Z)$/', 'Z', $NotBefore, 1);
Assert::validDateTimeZulu($NotBefore, ProtocolViolationException::class);
$NotBefore = XMLUtils::xsDateTimeToTimestamp($NotBefore);
}
$NotOnOrAfter = self::getAttribute($xml, 'NotOnOrAfter', null);
if ($NotOnOrAfter !== null) {
// Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
$NotOnOrAfter = preg_replace('/([.][0-9]+Z)$/', 'Z', $NotOnOrAfter, 1);
Assert::validDateTimeZulu($NotOnOrAfter, ProtocolViolationException::class);
$NotOnOrAfter = XMLUtils::xsDateTimeToTimestamp($NotOnOrAfter);
}
$Recipient = self::getAttribute($xml, 'Recipient', null);
$InResponseTo = self::getAttribute($xml, 'InResponseTo', null);
$Address = self::getAttribute($xml, 'Address', null);
$info = [];
foreach ($xml->childNodes as $n) {
if (!($n instanceof DOMElement)) {
continue;
} elseif ($n->namespaceURI === C::NS_XDSIG && $n->localName === 'KeyInfo') {
$info[] = KeyInfo::fromXML($n);
continue;
} else {
$info[] = new Chunk($n);
continue;
}
}
return new static(
$NotBefore,
$NotOnOrAfter,
$Recipient,
$InResponseTo,
$Address,
$info,
self::getAttributesNSFromXML($xml),
);
}
/**
* Convert this element to XML.
*
* @param \DOMElement|null $parent The parent element we should append this element to.
* @return \DOMElement This element, as XML.
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
if ($this->getNotBefore() !== null) {
$e->setAttribute('NotBefore', gmdate('Y-m-d\TH:i:s\Z', $this->getNotBefore()));
}
if ($this->getNotOnOrAfter() !== null) {
$e->setAttribute('NotOnOrAfter', gmdate('Y-m-d\TH:i:s\Z', $this->getNotOnOrAfter()));
}
if ($this->getRecipient() !== null) {
$e->setAttribute('Recipient', $this->getRecipient());
}
if ($this->getInResponseTo() !== null) {
$e->setAttribute('InResponseTo', $this->getInResponseTo());
}
if ($this->getAddress() !== null) {
$e->setAttribute('Address', $this->getAddress());
}
foreach ($this->getAttributesNS() as $attr) {
$e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
}
foreach ($this->getInfo() as $n) {
$n->toXML($e);
}
return $e;
}
}