Passed
Pull Request — master (#280)
by Tim
02:41
created

AttributeAuthorityDescriptor::toXML()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 64
nop 1
dl 0
loc 31
rs 8.8333
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AttributeAuthorityDescriptor::toUnsignedXML() 0 25 6
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\Constants;
10
use SimpleSAML\SAML2\XML\saml\Attribute;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\TooManyElementsException;
14
use SimpleSAML\XML\Utils as XMLUtils;
15
use SimpleSAML\XMLSecurity\XML\ds\Signature;
16
17
use function preg_split;
18
19
/**
20
 * Class representing SAML 2 metadata AttributeAuthorityDescriptor.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class AttributeAuthorityDescriptor extends AbstractRoleDescriptor
25
{
26
    /**
27
     * List of AttributeService endpoints.
28
     *
29
     * Array with EndpointType objects.
30
     *
31
     * @var \SimpleSAML\SAML2\XML\md\AttributeService[]
32
     */
33
    protected array $AttributeServices = [];
34
35
    /**
36
     * List of AssertionIDRequestService endpoints.
37
     *
38
     * Array with EndpointType objects.
39
     *
40
     * @var \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
41
     */
42
    protected array $AssertionIDRequestServices = [];
43
44
    /**
45
     * List of supported NameID formats.
46
     *
47
     * Array of strings.
48
     *
49
     * @var \SimpleSAML\SAML2\XML\md\NameIDFormat[]
50
     */
51
    protected array $NameIDFormats = [];
52
53
    /**
54
     * List of supported attribute profiles.
55
     *
56
     * Array with strings.
57
     *
58
     * @var \SimpleSAML\SAML2\XML\md\AttributeProfile[]
59
     */
60
    protected array $AttributeProfiles = [];
61
62
    /**
63
     * List of supported attributes.
64
     *
65
     * Array with \SimpleSAML\SAML2\XML\saml\Attribute objects.
66
     *
67
     * @var Attribute[]
68
     */
69
    protected array $Attributes = [];
70
71
72
    /**
73
     * AttributeAuthorityDescriptor constructor.
74
     *
75
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeServices
76
     * @param string[] $protocolSupportEnumeration
77
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
78
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
79
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfiles
80
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attributes
81
     * @param string|null $ID
82
     * @param int|null $validUntil
83
     * @param string|null $cacheDuration
84
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
85
     * @param string|null $errorURL
86
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
87
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors
88
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contacts
89
     */
90
    public function __construct(
91
        array $attributeServices,
92
        array $protocolSupportEnumeration,
93
        array $assertionIDRequestService = [],
94
        array $nameIDFormats = [],
95
        array $attributeProfiles = [],
96
        array $attributes = [],
97
        ?string $ID = null,
98
        ?int $validUntil = null,
99
        ?string $cacheDuration = null,
100
        ?Extensions $extensions = null,
101
        ?string $errorURL = null,
102
        ?Organization $organization = null,
103
        array $keyDescriptors = [],
104
        array $contacts = []
105
    ) {
106
        parent::__construct(
107
            $protocolSupportEnumeration,
108
            $ID,
109
            $validUntil,
110
            $cacheDuration,
111
            $extensions,
112
            $errorURL,
113
            $keyDescriptors,
114
            $organization,
115
            $contacts
116
        );
117
118
        $this->setAttributeServices($attributeServices);
119
        $this->setAssertionIDRequestServices($assertionIDRequestService);
120
        $this->setNameIDFormats($nameIDFormats);
121
        $this->setAttributeProfiles($attributeProfiles);
122
        $this->setAttributes($attributes);
123
    }
124
125
126
    /**
127
     * Collect the value of the AttributeService-property
128
     *
129
     * @return \SimpleSAML\SAML2\XML\md\AttributeService[]
130
     */
131
    public function getAttributeServices(): array
132
    {
133
        return $this->AttributeServices;
134
    }
135
136
137
    /**
138
     * Set the value of the AttributeService-property
139
     *
140
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeServices
141
     */
142
    protected function setAttributeServices(array $attributeServices): void
143
    {
144
        Assert::minCount(
145
            $attributeServices,
146
            1,
147
            'AttributeAuthorityDescriptor must contain at least one AttributeService.',
148
            MissingElementException::class,
149
        );
150
        Assert::allIsInstanceOf(
151
            $attributeServices,
152
            AttributeService::class,
153
            'AttributeService is not an instance of EndpointType.',
154
            InvalidDOMElementException::class,
155
        );
156
        $this->AttributeServices = $attributeServices;
157
    }
158
159
160
    /**
161
     * Collect the value of the NameIDFormat-property
162
     *
163
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
164
     */
165
    public function getNameIDFormats(): array
166
    {
167
        return $this->NameIDFormats;
168
    }
169
170
171
    /**
172
     * Set the value of the NameIDFormat-property
173
     *
174
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
175
     */
176
    protected function setNameIDFormats(array $nameIDFormats): void
177
    {
178
        Assert::allIsInstanceOf($nameIDFormats, NameIDFormat::class);
179
        $this->NameIDFormats = $nameIDFormats;
180
    }
181
182
183
    /**
184
     * Collect the value of the AssertionIDRequestService-property
185
     *
186
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
187
     */
188
    public function getAssertionIDRequestServices(): array
189
    {
190
        return $this->AssertionIDRequestServices;
191
    }
192
193
194
    /**
195
     * Set the value of the AssertionIDRequestService-property
196
     *
197
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestServices
198
     */
199
    protected function setAssertionIDRequestServices(array $assertionIDRequestServices): void
200
    {
201
        Assert::allIsInstanceOf($assertionIDRequestServices, AssertionIDRequestService::class);
202
203
        $this->AssertionIDRequestServices = $assertionIDRequestServices;
204
    }
205
206
207
    /**
208
     * Collect the value of the AttributeProfile-property
209
     *
210
     * @return \SimpleSAML\SAML2\XML\md\AttributeProfile[]
211
     */
212
    public function getAttributeProfiles(): array
213
    {
214
        return $this->AttributeProfiles;
215
    }
216
217
218
    /**
219
     * Set the value of the AttributeProfile-property
220
     *
221
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfiles
222
     */
223
    protected function setAttributeProfiles(array $attributeProfiles): void
224
    {
225
        Assert::allIsInstanceOf($attributeProfiles, AttributeProfile::class);
226
        $this->AttributeProfiles = $attributeProfiles;
227
    }
228
229
230
    /**
231
     * Collect the value of the Attribute-property
232
     *
233
     * @return Attribute[]
234
     */
235
    public function getAttributes(): array
236
    {
237
        return $this->Attributes;
238
    }
239
240
241
    /**
242
     * Set the value of the Attribute-property
243
     *
244
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attributes
245
     */
246
    protected function setAttributes(?array $attributes): void
247
    {
248
        Assert::allIsInstanceOf($attributes, Attribute::class);
249
        $this->Attributes = $attributes;
250
    }
251
252
253
    /**
254
     * Initialize an IDPSSODescriptor.
255
     *
256
     * @param \DOMElement $xml The XML element we should load.
257
     * @return self
258
     *
259
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
260
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
261
     * @throws \SimpleSAML\XML\Exception\MissingElementException if one of the mandatory child-elements is missing
262
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
263
     */
264
    public static function fromXML(DOMElement $xml): object
265
    {
266
        Assert::same($xml->localName, 'AttributeAuthorityDescriptor', InvalidDOMElementException::class);
267
        Assert::same($xml->namespaceURI, AttributeAuthorityDescriptor::NS, InvalidDOMElementException::class);
268
269
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
270
        $validUntil = self::getAttribute($xml, 'validUntil', null);
271
272
        $attrServices = AttributeService::getChildrenOfClass($xml);
273
        Assert::notEmpty(
274
            $attrServices,
275
            'Must have at least one AttributeService in AttributeAuthorityDescriptor.',
276
            MissingElementException::class
277
        );
278
279
        $assertIDReqServices = AssertionIDRequestService::getChildrenOfClass($xml);
280
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
281
        $attrProfiles = AttributeProfile::getChildrenOfClass($xml);
282
        $attributes = Attribute::getChildrenOfClass($xml);
283
284
        $orgs = Organization::getChildrenOfClass($xml);
285
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
286
287
        $extensions = Extensions::getChildrenOfClass($xml);
288
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
289
290
        $signature = Signature::getChildrenOfClass($xml);
291
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
292
293
        $authority = new self(
294
            $attrServices,
295
            preg_split('/[\s]+/', trim($protocols)),
296
            $assertIDReqServices,
297
            $nameIDFormats,
298
            $attrProfiles,
299
            $attributes,
300
            self::getAttribute($xml, 'ID', null),
301
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
302
            self::getAttribute($xml, 'cacheDuration', null),
303
            !empty($extensions) ? $extensions[0] : null,
304
            self::getAttribute($xml, 'errorURL', null),
305
            !empty($orgs) ? $orgs[0] : null,
306
            KeyDescriptor::getChildrenOfClass($xml),
307
            ContactPerson::getChildrenOfClass($xml)
308
        );
309
310
        if (!empty($signature)) {
311
            $authority->setSignature($signature[0]);
312
        }
313
314
        $authority->setXML($xml);
315
316
        return $authority;
317
    }
318
319
320
    /**
321
     * Convert this descriptor to an unsigned XML document.
322
     * This method does not sign the resulting XML document.
323
     *
324
     * @param \DOMElement|null $parent
325
     * @return \DOMElement The root element of the DOM tree
326
     */
327
    protected function toUnsignedXML(DOMElement $parent = null): DOMElement
328
    {
329
        $e = parent::toUnsignedXML($parent);
330
331
        foreach ($this->AttributeServices as $ep) {
332
            $ep->toXML($e);
333
        }
334
335
        foreach ($this->AssertionIDRequestServices as $ep) {
336
            $ep->toXML($e);
337
        }
338
339
        foreach ($this->NameIDFormats as $nidFormat) {
340
            $nidFormat->toXML($e);
341
        }
342
343
        foreach ($this->AttributeProfiles as $ap) {
344
            $ap->toXML($e);
345
        }
346
347
        foreach ($this->Attributes as $a) {
348
            $a->toXML($e);
349
        }
350
351
        return $e;
352
    }
353
}
354