Passed
Pull Request — master (#374)
by Tim
02:35
created

AttributeAuthorityDescriptor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 95
dl 0
loc 246
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 50 1
A getAttributeProfile() 0 3 1
A getAttributeService() 0 3 1
A getAssertionIDRequestService() 0 3 1
A getAttributes() 0 3 1
A getNameIDFormat() 0 3 1
A fromXML() 0 64 4
A 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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Type\{AnyURIListValue, SAMLAnyURIValue, SAMLDateTimeValue};
10
use SimpleSAML\SAML2\XML\saml\Attribute;
11
use SimpleSAML\XML\Constants as C;
12
use SimpleSAML\XML\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
13
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
14
use SimpleSAML\XML\Type\{DurationValue, IDValue};
15
use SimpleSAML\XMLSecurity\XML\ds\Signature;
16
17
/**
18
 * Class representing SAML 2 metadata AttributeAuthorityDescriptor.
19
 *
20
 * @package simplesamlphp/saml2
21
 */
22
final class AttributeAuthorityDescriptor extends AbstractRoleDescriptorType implements
23
    SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
26
27
    /**
28
     * AttributeAuthorityDescriptor constructor.
29
     *
30
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeService
31
     * @param \SimpleSAML\SAML2\Type\AnyURIListValue $protocolSupportEnumeration
32
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
33
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormat
34
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfile
35
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attribute
36
     * @param \SimpleSAML\XML\Type\IDValue|null $ID
37
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil
38
     * @param \SimpleSAML\XML\Type\DurationValue|null $cacheDuration
39
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
40
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $errorURL
41
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
42
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
43
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contact
44
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
     */
46
    public function __construct(
47
        protected array $attributeService,
48
        AnyURIListValue $protocolSupportEnumeration,
49
        protected array $assertionIDRequestService = [],
50
        protected array $nameIDFormat = [],
51
        protected array $attributeProfile = [],
52
        protected array $attribute = [],
53
        ?IDValue $ID = null,
54
        ?SAMLDateTimeValue $validUntil = null,
55
        ?DurationValue $cacheDuration = null,
56
        ?Extensions $extensions = null,
57
        ?SAMLAnyURIValue $errorURL = null,
58
        ?Organization $organization = null,
59
        array $keyDescriptor = [],
60
        array $contact = [],
61
        array $namespacedAttributes = [],
62
    ) {
63
        Assert::maxCount($attributeService, C::UNBOUNDED_LIMIT);
64
        Assert::minCount(
65
            $attributeService,
66
            1,
67
            'AttributeAuthorityDescriptor must contain at least one AttributeService.',
68
            MissingElementException::class,
69
        );
70
        Assert::allIsInstanceOf(
71
            $attributeService,
72
            AttributeService::class,
73
            'AttributeService is not an instance of EndpointType.',
74
            InvalidDOMElementException::class,
75
        );
76
        Assert::maxCount($nameIDFormat, C::UNBOUNDED_LIMIT);
77
        Assert::allIsInstanceOf($nameIDFormat, NameIDFormat::class);
78
        Assert::maxCount($assertionIDRequestService, C::UNBOUNDED_LIMIT);
79
        Assert::allIsInstanceOf($assertionIDRequestService, AssertionIDRequestService::class);
80
        Assert::maxCount($attributeProfile, C::UNBOUNDED_LIMIT);
81
        Assert::allIsInstanceOf($attributeProfile, AttributeProfile::class);
82
        Assert::maxCount($attribute, C::UNBOUNDED_LIMIT);
83
        Assert::allIsInstanceOf($attribute, Attribute::class);
84
85
        parent::__construct(
86
            $protocolSupportEnumeration,
87
            $ID,
88
            $validUntil,
89
            $cacheDuration,
90
            $extensions,
91
            $errorURL,
92
            $keyDescriptor,
93
            $organization,
94
            $contact,
95
            $namespacedAttributes,
96
        );
97
    }
98
99
100
    /**
101
     * Collect the value of the AttributeService-property
102
     *
103
     * @return \SimpleSAML\SAML2\XML\md\AttributeService[]
104
     */
105
    public function getAttributeService(): array
106
    {
107
        return $this->attributeService;
108
    }
109
110
111
    /**
112
     * Collect the value of the NameIDFormat-property
113
     *
114
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
115
     */
116
    public function getNameIDFormat(): array
117
    {
118
        return $this->nameIDFormat;
119
    }
120
121
122
    /**
123
     * Collect the value of the AssertionIDRequestService-property
124
     *
125
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
126
     */
127
    public function getAssertionIDRequestService(): array
128
    {
129
        return $this->assertionIDRequestService;
130
    }
131
132
133
    /**
134
     * Collect the value of the AttributeProfile-property
135
     *
136
     * @return \SimpleSAML\SAML2\XML\md\AttributeProfile[]
137
     */
138
    public function getAttributeProfile(): array
139
    {
140
        return $this->attributeProfile;
141
    }
142
143
144
    /**
145
     * Collect the value of the Attribute-property
146
     *
147
     * @return \SimpleSAML\SAML2\XML\saml\Attribute[]
148
     */
149
    public function getAttributes(): array
150
    {
151
        return $this->attribute;
152
    }
153
154
155
    /**
156
     * Initialize an IDPSSODescriptor.
157
     *
158
     * @param \DOMElement $xml The XML element we should load.
159
     * @return static
160
     *
161
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
162
     *   if the qualified name of the supplied element is wrong
163
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
164
     *   if the supplied element is missing one of the mandatory attributes
165
     * @throws \SimpleSAML\XML\Exception\MissingElementException
166
     *   if one of the mandatory child-elements is missing
167
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
168
     *   if too many child-elements of a type are specified
169
     */
170
    public static function fromXML(DOMElement $xml): static
171
    {
172
        Assert::same($xml->localName, 'AttributeAuthorityDescriptor', InvalidDOMElementException::class);
173
        Assert::same($xml->namespaceURI, AttributeAuthorityDescriptor::NS, InvalidDOMElementException::class);
174
175
        $attrServices = AttributeService::getChildrenOfClass($xml);
176
        Assert::notEmpty(
177
            $attrServices,
178
            'Must have at least one AttributeService in AttributeAuthorityDescriptor.',
179
            MissingElementException::class,
180
        );
181
182
        $assertIDReqServices = AssertionIDRequestService::getChildrenOfClass($xml);
183
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
184
        $attrProfiles = AttributeProfile::getChildrenOfClass($xml);
185
        $attributes = Attribute::getChildrenOfClass($xml);
186
187
        $orgs = Organization::getChildrenOfClass($xml);
188
        Assert::maxCount(
189
            $orgs,
190
            1,
191
            'More than one Organization found in this descriptor',
192
            TooManyElementsException::class,
193
        );
194
195
        $extensions = Extensions::getChildrenOfClass($xml);
196
        Assert::maxCount(
197
            $extensions,
198
            1,
199
            'Only one md:Extensions element is allowed.',
200
            TooManyElementsException::class,
201
        );
202
203
        $signature = Signature::getChildrenOfClass($xml);
204
        Assert::maxCount(
205
            $signature,
206
            1,
207
            'Only one ds:Signature element is allowed.',
208
            TooManyElementsException::class,
209
        );
210
211
        $authority = new static(
212
            $attrServices,
213
            self::getAttribute($xml, 'protocolSupportEnumeration', AnyURIListValue::class),
214
            $assertIDReqServices,
215
            $nameIDFormats,
216
            $attrProfiles,
217
            $attributes,
218
            self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
219
            self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
220
            self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
221
            !empty($extensions) ? $extensions[0] : null,
222
            self::getOptionalAttribute($xml, 'errorURL', SAMLAnyURIValue::class, null),
223
            !empty($orgs) ? $orgs[0] : null,
224
            KeyDescriptor::getChildrenOfClass($xml),
225
            ContactPerson::getChildrenOfClass($xml),
226
            self::getAttributesNSFromXML($xml),
227
        );
228
229
        if (!empty($signature)) {
230
            $authority->setSignature($signature[0]);
231
            $authority->setXML($xml);
232
        }
233
        return $authority;
234
    }
235
236
237
    /**
238
     * Convert this assertion to an unsigned XML document.
239
     * This method does not sign the resulting XML document.
240
     *
241
     * @return \DOMElement The root element of the DOM tree
242
     */
243
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
244
    {
245
        $e = parent::toUnsignedXML($parent);
246
247
        foreach ($this->getAttributeService() as $ep) {
248
            $ep->toXML($e);
249
        }
250
251
        foreach ($this->getAssertionIDRequestService() as $ep) {
252
            $ep->toXML($e);
253
        }
254
255
        foreach ($this->getNameIDFormat() as $nidFormat) {
256
            $nidFormat->toXML($e);
257
        }
258
259
        foreach ($this->getAttributeProfile() as $ap) {
260
            $ap->toXML($e);
261
        }
262
263
        foreach ($this->getAttributes() as $a) {
264
            $a->toXML($e);
265
        }
266
267
        return $e;
268
    }
269
}
270