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

IDPSSODescriptor   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 262
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeProfile() 0 3 1
A getSupportedAttribute() 0 3 1
B fromXML() 0 58 5
A getSingleSignOnService() 0 3 1
A __construct() 0 58 1
A getNameIDMappingService() 0 3 1
B toUnsignedXML() 0 29 8
A wantAuthnRequestsSigned() 0 3 1
A getAssertionIDRequestService() 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\TooManyElementsException;
12
use SimpleSAML\XML\Utils as XMLUtils;
13
use SimpleSAML\XMLSecurity\XML\ds\Signature;
14
15
use function preg_split;
16
17
/**
18
 * Class representing SAML 2 IDPSSODescriptor.
19
 *
20
 * @package simplesamlphp/saml2
21
 */
22
final class IDPSSODescriptor extends AbstractSSODescriptor
23
{
24
    /**
25
     * IDPSSODescriptor constructor.
26
     *
27
     * @param \SimpleSAML\SAML2\XML\md\SingleSignOnService[] $singleSignOnService
28
     * @param string[] $protocolSupportEnumeration
29
     * @param bool|null $wantAuthnRequestsSigned
30
     * @param \SimpleSAML\SAML2\XML\md\NameIDMappingService[] $nameIDMappingService
31
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
32
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfile
33
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attribute
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\KeyDescriptor[] $keyDescriptor
40
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
41
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contact
42
     * @param \SimpleSAML\SAML2\XML\md\ArtifactResolutionService[] $artifactResolutionService
43
     * @param \SimpleSAML\SAML2\XML\md\SingleLogoutService[] $singleLogoutService
44
     * @param \SimpleSAML\SAML2\XML\md\ManageNameIDService[] $manageNameIDService
45
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormat
46
     */
47
    public function __construct(
48
        protected array $singleSignOnService,
49
        array $protocolSupportEnumeration,
50
        protected ?bool $wantAuthnRequestsSigned = null,
51
        protected array $nameIDMappingService = [],
52
        protected array $assertionIDRequestService = [],
53
        protected array $attributeProfile = [],
54
        protected array $attribute = [],
55
        ?string $ID = null,
56
        ?int $validUntil = null,
57
        ?string $cacheDuration = null,
58
        ?Extensions $extensions = null,
59
        ?string $errorURL = null,
60
        array $keyDescriptor = [],
61
        ?Organization $organization = null,
62
        array $contact = [],
63
        array $artifactResolutionService = [],
64
        array $singleLogoutService = [],
65
        array $manageNameIDService = [],
66
        array $nameIDFormat = [],
67
    ) {
68
        Assert::minCount($singleSignOnService, 1, 'At least one SingleSignOnService must be specified.');
69
        Assert::allIsInstanceOf(
70
            $singleSignOnService,
71
            SingleSignOnService::class,
72
            'All md:SingleSignOnService endpoints must be an instance of SingleSignOnService.',
73
        );
74
        Assert::allIsInstanceOf(
75
            $nameIDMappingService,
76
            NameIDMappingService::class,
77
            'All md:NameIDMappingService endpoints must be an instance of NameIDMappingService.',
78
        );
79
        Assert::allIsInstanceOf(
80
            $assertionIDRequestService,
81
            AssertionIDRequestService::class,
82
            'All md:AssertionIDRequestService endpoints must be an instance of AssertionIDRequestService.',
83
        );
84
        Assert::allIsInstanceOf($attributeProfile, AttributeProfile::class);
85
        Assert::allIsInstanceOf(
86
            $attribute,
87
            Attribute::class,
88
            'All md:Attribute elements must be an instance of Attribute.',
89
        );
90
91
        parent::__construct(
92
            $protocolSupportEnumeration,
93
            $ID,
94
            $validUntil,
95
            $cacheDuration,
96
            $extensions,
97
            $errorURL,
98
            $keyDescriptor,
99
            $organization,
100
            $contact,
101
            $artifactResolutionService,
102
            $singleLogoutService,
103
            $manageNameIDService,
104
            $nameIDFormat,
105
        );
106
    }
107
108
109
    /**
110
     * Collect the value of the WantAuthnRequestsSigned-property
111
     *
112
     * @return bool|null
113
     */
114
    public function wantAuthnRequestsSigned(): ?bool
115
    {
116
        return $this->wantAuthnRequestsSigned;
117
    }
118
119
120
    /**
121
     * Get the SingleSignOnService endpoints
122
     *
123
     * @return \SimpleSAML\SAML2\XML\md\SingleSignOnService[]
124
     */
125
    public function getSingleSignOnService(): array
126
    {
127
        return $this->singleSignOnService;
128
    }
129
130
131
    /**
132
     * Get the NameIDMappingService endpoints
133
     *
134
     * @return \SimpleSAML\SAML2\XML\md\NameIDMappingService[]
135
     */
136
    public function getNameIDMappingService(): array
137
    {
138
        return $this->nameIDMappingService;
139
    }
140
141
142
    /**
143
     * Collect the AssertionIDRequestService endpoints
144
     *
145
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
146
     */
147
    public function getAssertionIDRequestService(): array
148
    {
149
        return $this->assertionIDRequestService;
150
    }
151
152
153
    /**
154
     * Get the attribute profiles supported
155
     *
156
     * @return \SimpleSAML\SAML2\XML\md\AttributeProfile[]
157
     */
158
    public function getAttributeProfile(): array
159
    {
160
        return $this->attributeProfile;
161
    }
162
163
164
    /**
165
     * Get the attributes supported by this IdP
166
     *
167
     * @return \SimpleSAML\SAML2\XML\saml\Attribute[]
168
     */
169
    public function getSupportedAttribute(): array
170
    {
171
        return $this->attribute;
172
    }
173
174
175
    /**
176
     * Initialize an IDPSSODescriptor.
177
     *
178
     * @param \DOMElement $xml The XML element we should load.
179
     * @return \SimpleSAML\SAML2\XML\md\IDPSSODescriptor
180
     *
181
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
182
     *   if the qualified name of the supplied element is wrong
183
     * @throws \SimpleSAML\XML\Exception\MissingElementException
184
     *   if one of the mandatory child-elements is missing
185
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
186
     *   if too many child-elements of a type are specified
187
     */
188
    public static function fromXML(DOMElement $xml): static
189
    {
190
        Assert::same($xml->localName, 'IDPSSODescriptor', InvalidDOMElementException::class);
191
        Assert::same($xml->namespaceURI, IDPSSODescriptor::NS, InvalidDOMElementException::class);
192
193
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
194
        $validUntil = self::getAttribute($xml, 'validUntil', null);
195
        $orgs = Organization::getChildrenOfClass($xml);
196
        Assert::maxCount(
197
            $orgs,
198
            1,
199
            'More than one Organization found in this descriptor',
200
            TooManyElementsException::class,
201
        );
202
203
        $extensions = Extensions::getChildrenOfClass($xml);
204
        Assert::maxCount(
205
            $extensions,
206
            1,
207
            'Only one md:Extensions element is allowed.',
208
            TooManyElementsException::class,
209
        );
210
211
        $signature = Signature::getChildrenOfClass($xml);
212
        Assert::maxCount(
213
            $signature,
214
            1,
215
            'Only one ds:Signature element is allowed.',
216
            TooManyElementsException::class,
217
        );
218
219
        $idpssod = new static(
220
            SingleSignOnService::getChildrenOfClass($xml),
221
            preg_split('/[\s]+/', trim($protocols)),
222
            self::getBooleanAttribute($xml, 'WantAuthnRequestsSigned', null),
223
            NameIDMappingService::getChildrenOfClass($xml),
224
            AssertionIDRequestService::getChildrenOfClass($xml),
225
            AttributeProfile::getChildrenOfClass($xml),
226
            Attribute::getChildrenOfClass($xml),
227
            self::getAttribute($xml, 'ID', null),
228
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
229
            self::getAttribute($xml, 'cacheDuration', null),
230
            !empty($extensions) ? $extensions[0] : null,
231
            self::getAttribute($xml, 'errorURL', null),
232
            KeyDescriptor::getChildrenOfClass($xml),
233
            !empty($orgs) ? $orgs[0] : null,
234
            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...
235
            ArtifactResolutionService::getChildrenOfClass($xml),
236
            SingleLogoutService::getChildrenOfClass($xml),
237
            ManageNameIDService::getChildrenOfClass($xml),
238
            NameIDFormat::getChildrenOfClass($xml),
239
        );
240
241
        if (!empty($signature)) {
242
            $idpssod->setSignature($signature[0]);
243
            $idpssod->setXML($xml);
244
        }
245
        return $idpssod;
246
    }
247
248
249
    /**
250
     * Convert this assertion to an unsigned XML document.
251
     * This method does not sign the resulting XML document.
252
     *
253
     * @return \DOMElement The root element of the DOM tree
254
     */
255
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
256
    {
257
        $e = parent::toUnsignedXML($parent);
258
259
        if (is_bool($this->wantAuthnRequestsSigned)) {
260
            $e->setAttribute('WantAuthnRequestsSigned', $this->wantAuthnRequestsSigned ? 'true' : 'false');
261
        }
262
263
        foreach ($this->getSingleSignOnService() as $ep) {
264
            $ep->toXML($e);
265
        }
266
267
        foreach ($this->getNameIDMappingService() as $ep) {
268
            $ep->toXML($e);
269
        }
270
271
        foreach ($this->getAssertionIDRequestService() as $ep) {
272
            $ep->toXML($e);
273
        }
274
275
        foreach ($this->getAttributeProfile() as $ap) {
276
            $ap->toXML($e);
277
        }
278
279
        foreach ($this->getSupportedAttribute() as $a) {
280
            $a->toXML($e);
281
        }
282
283
        return $e;
284
    }
285
}
286