|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\md; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use SimpleSAML\Assert\Assert; |
|
9
|
|
|
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; |
|
10
|
|
|
use SimpleSAML\SAML2\Exception\ArrayValidationException; |
|
11
|
|
|
use SimpleSAML\XML\ArrayizableElementInterface; |
|
12
|
|
|
use SimpleSAML\XML\Attribute as XMLAttribute; |
|
13
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
|
14
|
|
|
use SimpleSAML\XML\ExtendableAttributesTrait; |
|
15
|
|
|
use SimpleSAML\XML\ExtendableElementTrait; |
|
16
|
|
|
use SimpleSAML\XML\SerializableElementInterface; |
|
17
|
|
|
use SimpleSAML\XML\XsNamespace as NS; |
|
18
|
|
|
|
|
19
|
|
|
use function array_change_key_case; |
|
20
|
|
|
use function array_key_exists; |
|
21
|
|
|
use function array_keys; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class representing SAML 2 EndpointType. |
|
25
|
|
|
* |
|
26
|
|
|
* This class can be used in two different ways: |
|
27
|
|
|
* |
|
28
|
|
|
* - You can extend the class without extending the constructor. Then you can use the methods available and the class |
|
29
|
|
|
* will generate an element with the same name as the extending class |
|
30
|
|
|
* (e.g. \SimpleSAML\SAML2\XML\md\AttributeService). |
|
31
|
|
|
* |
|
32
|
|
|
* - Alternatively, you may want to extend the type to add new attributes (e.g look at IndexedEndpointType). In that |
|
33
|
|
|
* case, you cannot use this class normally, as if you change the signature of the constructor, you cannot call |
|
34
|
|
|
* fromXML() in this class. In order to process an XML document, you can use the get*Attribute() static methods |
|
35
|
|
|
* from AbstractElement, and reimplement the fromXML() method with them to suit your new constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @package simplesamlphp/saml2 |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract class AbstractEndpointType extends AbstractMdElement implements ArrayizableElementInterface |
|
40
|
|
|
{ |
|
41
|
|
|
use ExtendableAttributesTrait; |
|
42
|
|
|
use ExtendableElementTrait; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** The namespace-attribute for the xs:any element */ |
|
46
|
|
|
public const XS_ANY_ELT_NAMESPACE = NS::OTHER; |
|
47
|
|
|
|
|
48
|
|
|
/** The namespace-attribute for the xs:anyAttribute element */ |
|
49
|
|
|
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* EndpointType constructor. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $binding |
|
56
|
|
|
* @param string $location |
|
57
|
|
|
* @param string|null $responseLocation |
|
58
|
|
|
* @param \SimpleSAML\XML\ElementInterface[] $children |
|
59
|
|
|
* @param array<\SimpleSAML\XML\Attribute> $attributes |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \SimpleSAML\Assert\AssertionFailedException |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct( |
|
64
|
|
|
protected string $binding, |
|
65
|
|
|
protected string $location, |
|
66
|
|
|
protected ?string $responseLocation = null, |
|
67
|
|
|
array $children = [], |
|
68
|
|
|
array $attributes = [], |
|
69
|
|
|
) { |
|
70
|
|
|
SAMLAssert::validURI($binding); |
|
71
|
|
|
SAMLAssert::validURI($location); |
|
72
|
|
|
SAMLAssert::nullOrValidURI($responseLocation); |
|
73
|
|
|
|
|
74
|
|
|
$this->setElements($children); |
|
75
|
|
|
$this->setAttributesNS($attributes); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Collect the value of the Binding property. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getBinding(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->binding; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Collect the value of the Location property. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getLocation(): string |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->location; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Collect the value of the ResponseLocation property. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string|null |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getResponseLocation(): ?string |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->responseLocation; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Initialize an EndpointType. |
|
114
|
|
|
* |
|
115
|
|
|
* Note: this method cannot be used when extending this class, if the constructor has a different signature. |
|
116
|
|
|
* |
|
117
|
|
|
* @param \DOMElement $xml The XML element we should load. |
|
118
|
|
|
* @return static |
|
119
|
|
|
* |
|
120
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
|
121
|
|
|
* if the qualified name of the supplied element is wrong |
|
122
|
|
|
* @throws \SimpleSAML\XML\Exception\MissingAttributeException |
|
123
|
|
|
* if the supplied element is missing any of the mandatory attributes |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function fromXML(DOMElement $xml): static |
|
126
|
|
|
{ |
|
127
|
|
|
$qualifiedName = static::getClassName(static::class); |
|
128
|
|
|
Assert::eq( |
|
129
|
|
|
$xml->localName, |
|
130
|
|
|
$qualifiedName, |
|
131
|
|
|
'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.', |
|
132
|
|
|
InvalidDOMElementException::class, |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
return new static( |
|
136
|
|
|
self::getAttribute($xml, 'Binding'), |
|
137
|
|
|
self::getAttribute($xml, 'Location'), |
|
138
|
|
|
self::getOptionalAttribute($xml, 'ResponseLocation', null), |
|
139
|
|
|
self::getChildElementsFromXML($xml), |
|
140
|
|
|
self::getAttributesNSFromXML($xml), |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Add this endpoint to an XML element. |
|
147
|
|
|
* |
|
148
|
|
|
* @param \DOMElement $parent The element we should append this endpoint to. |
|
149
|
|
|
* @return \DOMElement |
|
150
|
|
|
*/ |
|
151
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
|
152
|
|
|
{ |
|
153
|
|
|
$e = parent::instantiateParentElement($parent); |
|
154
|
|
|
|
|
155
|
|
|
$e->setAttribute('Binding', $this->getBinding()); |
|
156
|
|
|
$e->setAttribute('Location', $this->getLocation()); |
|
157
|
|
|
|
|
158
|
|
|
if ($this->getResponseLocation() !== null) { |
|
159
|
|
|
$e->setAttribute('ResponseLocation', $this->getResponseLocation()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
foreach ($this->getAttributesNS() as $attr) { |
|
163
|
|
|
$attr->toXML($e); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** @var \SimpleSAML\XML\SerializableElementInterface $child */ |
|
167
|
|
|
foreach ($this->getElements() as $child) { |
|
168
|
|
|
if (!$child->isEmptyElement()) { |
|
169
|
|
|
$child->toXML($e); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $e; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Create a class from an array |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $data |
|
181
|
|
|
* @return static |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function fromArray(array $data): static |
|
184
|
|
|
{ |
|
185
|
|
|
$data = self::processArrayContents($data); |
|
186
|
|
|
|
|
187
|
|
|
return new static( |
|
188
|
|
|
$data['Binding'], |
|
189
|
|
|
$data['Location'], |
|
190
|
|
|
$data['ResponseLocation'] ?? null, |
|
191
|
|
|
$data['children'] ?? [], |
|
192
|
|
|
$data['attributes'] ?? [], |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Validates an array representation of this object and returns the same array with |
|
199
|
|
|
* rationalized keys (casing) and parsed sub-elements. |
|
200
|
|
|
* |
|
201
|
|
|
* @param array $data |
|
202
|
|
|
* @return array $data |
|
203
|
|
|
*/ |
|
204
|
|
|
private static function processArrayContents(array $data): array |
|
205
|
|
|
{ |
|
206
|
|
|
$data = array_change_key_case($data, CASE_LOWER); |
|
207
|
|
|
|
|
208
|
|
|
// Make sure the array keys are known for this kind of object |
|
209
|
|
|
Assert::allOneOf( |
|
210
|
|
|
array_keys($data), |
|
211
|
|
|
['binding', 'location', 'responselocation', 'children', 'attributes'], |
|
212
|
|
|
ArrayValidationException::class, |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
// Make sure all the mandatory items exist |
|
216
|
|
|
Assert::keyExists($data, 'binding', ArrayValidationException::class); |
|
217
|
|
|
Assert::keyExists($data, 'location', ArrayValidationException::class); |
|
218
|
|
|
|
|
219
|
|
|
// Make sure the items have the correct data type |
|
220
|
|
|
Assert::string($data['binding'], ArrayValidationException::class); |
|
221
|
|
|
Assert::string($data['location'], ArrayValidationException::class); |
|
222
|
|
|
|
|
223
|
|
|
$retval = [ |
|
224
|
|
|
'Binding' => $data['binding'], |
|
225
|
|
|
'Location' => $data['location'], |
|
226
|
|
|
]; |
|
227
|
|
|
|
|
228
|
|
|
if (array_key_exists('responselocation', $data)) { |
|
229
|
|
|
Assert::string($data['responselocation'], ArrayValidationException::class); |
|
230
|
|
|
$retval['ResponseLocation'] = $data['responselocation']; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if (array_key_exists('children', $data)) { |
|
234
|
|
|
Assert::isArray($data['children'], ArrayValidationException::class); |
|
235
|
|
|
Assert::allIsInstanceOf( |
|
236
|
|
|
$data['children'], |
|
237
|
|
|
SerializableElementInterface::class, |
|
238
|
|
|
ArrayValidationException::class, |
|
239
|
|
|
); |
|
240
|
|
|
$retval['children'] = $data['children']; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if (array_key_exists('attributes', $data)) { |
|
244
|
|
|
Assert::isArray($data['attributes'], ArrayValidationException::class); |
|
245
|
|
|
Assert::allIsArray($data['attributes'], ArrayValidationException::class); |
|
246
|
|
|
foreach ($data['attributes'] as $i => $attr) { |
|
247
|
|
|
$retval['attributes'][] = XMLAttribute::fromArray($attr); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $retval; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Create an array from this class |
|
257
|
|
|
* |
|
258
|
|
|
* @return array |
|
259
|
|
|
*/ |
|
260
|
|
|
public function toArray(): array |
|
261
|
|
|
{ |
|
262
|
|
|
$data = [ |
|
263
|
|
|
'Binding' => $this->getBinding(), |
|
264
|
|
|
'Location' => $this->getLocation(), |
|
265
|
|
|
'ResponseLocation' => $this->getResponseLocation(), |
|
266
|
|
|
'children' => $this->getElements(), |
|
267
|
|
|
]; |
|
268
|
|
|
|
|
269
|
|
|
foreach ($this->getAttributesNS() as $a) { |
|
270
|
|
|
$data['attributes'][] = $a->toArray(); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return array_filter($data); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|