|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\mdrpi; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
|
8
|
|
|
use DOMElement; |
|
9
|
|
|
use SimpleSAML\Assert\Assert; |
|
10
|
|
|
use SimpleSAML\SAML2\Constants as C; |
|
11
|
|
|
use SimpleSAML\SAML2\Exception\ArrayValidationException; |
|
12
|
|
|
use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
|
13
|
|
|
use SimpleSAML\XML\ArrayizableElementInterface; |
|
14
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
|
15
|
|
|
|
|
16
|
|
|
use function preg_replace; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class for handling the mdrpi:Publication element. |
|
20
|
|
|
* |
|
21
|
|
|
* @link: http://docs.oasis-open.org/security/saml/Post2.0/saml-metadata-rpi/v1.0/saml-metadata-rpi-v1.0.pdf |
|
22
|
|
|
* @package simplesamlphp/saml2 |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Publication extends AbstractMdrpiElement implements ArrayizableElementInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Create/parse a mdrpi:PublicationInfo element. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $publisher |
|
30
|
|
|
* @param \DateTimeImmutable|null $creationInstant |
|
31
|
|
|
* @param string|null $publicationId |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
protected string $publisher, |
|
35
|
|
|
protected ?DateTimeImmutable $creationInstant = null, |
|
36
|
|
|
protected ?string $publicationId = null, |
|
37
|
|
|
) { |
|
38
|
|
|
Assert::nullOrSame($creationInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Collect the value of the publisher-property |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getPublisher(): string |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->publisher; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Collect the value of the creationInstant-property |
|
55
|
|
|
* |
|
56
|
|
|
* @return \DateTimeImmutable|null |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getCreationInstant(): ?DateTimeImmutable |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->creationInstant; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Collect the value of the publicationId-property |
|
66
|
|
|
* |
|
67
|
|
|
* @return string|null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getPublicationId(): ?string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->publicationId; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Convert XML into a Publication |
|
77
|
|
|
* |
|
78
|
|
|
* @param \DOMElement $xml The XML element we should load |
|
79
|
|
|
* @return static |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
|
82
|
|
|
* if the qualified name of the supplied element is wrong |
|
83
|
|
|
* @throws \SimpleSAML\XML\Exception\MissingAttributeException |
|
84
|
|
|
* if the supplied element is missing one of the mandatory attributes |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function fromXML(DOMElement $xml): static |
|
87
|
|
|
{ |
|
88
|
|
|
Assert::same($xml->localName, 'Publication', InvalidDOMElementException::class); |
|
89
|
|
|
Assert::same($xml->namespaceURI, Publication::NS, InvalidDOMElementException::class); |
|
90
|
|
|
|
|
91
|
|
|
$publisher = self::getAttribute($xml, 'publisher'); |
|
92
|
|
|
$creationInstant = self::getOptionalAttribute($xml, 'creationInstant', null); |
|
93
|
|
|
|
|
94
|
|
|
// 2.2.1: Time values MUST be expressed in the UTC timezone using the 'Z' timezone identifier |
|
95
|
|
|
if ($creationInstant !== null) { |
|
96
|
|
|
// Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications |
|
97
|
|
|
$creationInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $creationInstant, 1); |
|
98
|
|
|
|
|
99
|
|
|
Assert::validDateTimeZulu($creationInstant, ProtocolViolationException::class); |
|
100
|
|
|
$creationInstant = new DateTimeImmutable($creationInstant); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$publicationId = self::getOptionalAttribute($xml, 'publicationId', null); |
|
104
|
|
|
|
|
105
|
|
|
return new static($publisher, $creationInstant, $publicationId); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Convert this element to XML. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \DOMElement|null $parent The element we should append to. |
|
113
|
|
|
* @return \DOMElement |
|
114
|
|
|
*/ |
|
115
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
|
116
|
|
|
{ |
|
117
|
|
|
$e = $this->instantiateParentElement($parent); |
|
118
|
|
|
$e->setAttribute('publisher', $this->getPublisher()); |
|
119
|
|
|
|
|
120
|
|
|
if ($this->getCreationInstant() !== null) { |
|
121
|
|
|
$e->setAttribute('creationInstant', $this->getCreationInstant()->format(C::DATETIME_FORMAT)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($this->getPublicationId() !== null) { |
|
125
|
|
|
$e->setAttribute('publicationId', $this->getPublicationId()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $e; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Create a class from an array |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $data |
|
136
|
|
|
* @return static |
|
137
|
|
|
*/ |
|
138
|
|
|
public static function fromArray(array $data): static |
|
139
|
|
|
{ |
|
140
|
|
|
$data = self::processArrayContents($data); |
|
141
|
|
|
|
|
142
|
|
|
return new static( |
|
143
|
|
|
$data['publisher'], |
|
144
|
|
|
$data['creationInstant'] ?? null, |
|
145
|
|
|
$data['publicationId'] ?? null, |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Validates an array representation of this object and returns the same array with |
|
152
|
|
|
* rationalized keys (casing) and parsed sub-elements. |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $data |
|
155
|
|
|
* @return array $data |
|
156
|
|
|
*/ |
|
157
|
|
|
private static function processArrayContents(array $data): array |
|
158
|
|
|
{ |
|
159
|
|
|
$data = array_change_key_case($data, CASE_LOWER); |
|
160
|
|
|
|
|
161
|
|
|
Assert::allOneOf( |
|
162
|
|
|
array_keys($data), |
|
163
|
|
|
['publisher', 'creationinstant', 'publicationid'], |
|
164
|
|
|
ArrayValidationException::class, |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
Assert::keyExists($data, 'publisher', ArrayValidationException::class); |
|
168
|
|
|
Assert::string($data['publisher'], ArrayValidationException::class); |
|
169
|
|
|
$retval = ['publisher' => $data['publisher']]; |
|
170
|
|
|
|
|
171
|
|
|
if (array_key_exists('creationinstant', $data)) { |
|
172
|
|
|
Assert::string($data['creationinstant'], ArrayValidationException::class); |
|
173
|
|
|
Assert::validDateTimeZulu($data['creationinstant'], ArrayValidationException::class); |
|
174
|
|
|
$retval['creationInstant'] = new DateTimeImmutable($data['creationinstant']); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (array_key_exists('publicationid', $data)) { |
|
178
|
|
|
Assert::string($data['publicationid'], ArrayValidationException::class); |
|
179
|
|
|
$retval['publicationId'] = $data['publicationid']; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $retval; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Create an array from this class |
|
188
|
|
|
* |
|
189
|
|
|
* @return array |
|
190
|
|
|
*/ |
|
191
|
|
|
public function toArray(): array |
|
192
|
|
|
{ |
|
193
|
|
|
$data = []; |
|
194
|
|
|
$data['publisher'] = $this->getPublisher(); |
|
195
|
|
|
|
|
196
|
|
|
if ($this->getCreationInstant() !== null) { |
|
197
|
|
|
$data['creationInstant'] = $this->getCreationInstant()->format(C::DATETIME_FORMAT); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if ($this->getPublicationId() !== null) { |
|
201
|
|
|
$data['publicationId'] = $this->getPublicationId(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $data; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|