Passed
Push — master ( eb6d7f...017f7a )
by Tim
02:40
created

setAttributeServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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