Passed
Push — master ( 7a371e...4a8d98 )
by Tim
02:17
created

AttributeAuthorityDescriptor   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 87
dl 0
loc 237
rs 10
c 2
b 0
f 0
wmc 17

8 Methods

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