Passed
Pull Request — master (#374)
by Tim
03:49 queued 01:20
created

AbstractRoleDescriptor::toUnsignedXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Type\{SAMLAnyURIValue, SAMLDateTimeValue, SAMLStringValue};
11
use SimpleSAML\SAML2\Utils;
12
use SimpleSAML\SAML2\XML\{ExtensionPointInterface, ExtensionPointTrait};
13
use SimpleSAML\XML\Chunk;
14
use SimpleSAML\XML\Exception\{InvalidDOMElementException, SchemaViolationException, TooManyElementsException};
15
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
16
use SimpleSAML\XML\Type\{DurationValue, IDValue, QNameValue, StringValue};
17
18
use function array_pop;
19
20
/**
21
 * Class representing a SAML2 RoleDescriptor element.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
abstract class AbstractRoleDescriptor extends AbstractRoleDescriptorType implements
26
    ExtensionPointInterface,
27
    SchemaValidatableElementInterface
28
{
29
    use ExtensionPointTrait;
30
    use SchemaValidatableElementTrait;
31
32
    /** @var string */
33
    public const LOCALNAME = 'RoleDescriptor';
34
35
36
    /**
37
     * Initialize a md:RoleDescriptor from scratch.
38
     *
39
     * @param \SimpleSAML\XML\Type\QNameValue $type
40
     * @param string[] $protocolSupportEnumeration A set of URI specifying the protocols supported.
41
     * @param \SimpleSAML\XML\Type\IDValue|null $ID The ID for this document. Defaults to null.
42
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil Unix time of validity for this document.
43
     *   Defaults to null.
44
     * @param \SimpleSAML\XML\Type\DurationValue|null $cacheDuration Maximum time this document can be cached.
45
     *   Defaults to null.
46
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An Extensions object. Defaults to null.
47
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $errorURL An URI where to redirect users for support.
48
     *   Defaults to null.
49
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
50
     *   An array of KeyDescriptor elements. Defaults to an empty array.
51
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
52
     *   The organization running this entity. Defaults to null.
53
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contactPerson
54
     *   An array of contacts for this entity. Defaults to an empty array.
55
     * @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...
56
     */
57
    public function __construct(
58
        protected QNameValue $type,
59
        array $protocolSupportEnumeration,
60
        ?IDValue $ID = null,
61
        ?SAMLDateTimeValue $validUntil = null,
62
        ?DurationValue $cacheDuration = null,
63
        ?Extensions $extensions = null,
64
        ?SAMLAnyURIValue $errorURL = null,
65
        array $keyDescriptor = [],
66
        ?Organization $organization = null,
67
        array $contactPerson = [],
68
        array $namespacedAttributes = [],
69
    ) {
70
        parent::__construct(
71
            $protocolSupportEnumeration,
72
            $ID,
73
            $validUntil,
74
            $cacheDuration,
75
            $extensions,
76
            $errorURL,
77
            $keyDescriptor,
78
            $organization,
79
            $contactPerson,
80
            $namespacedAttributes,
81
        );
82
    }
83
84
85
    /**
86
     * Return the xsi:type value corresponding this element.
87
     *
88
     * @return \SimpleSAML\XML\Type\QNameValue
89
     */
90
    public function getXsiType(): QNameValue
91
    {
92
        return $this->type;
93
    }
94
95
96
    /**
97
     * Convert XML into an RoleDescriptor
98
     * @param \DOMElement $xml The XML element we should load
99
     * @return static
100
     *
101
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
102
     *   if the qualified name of the supplied element is wrong
103
     */
104
    public static function fromXML(DOMElement $xml): static
105
    {
106
        Assert::same($xml->localName, 'RoleDescriptor', InvalidDOMElementException::class);
107
        Assert::same($xml->namespaceURI, C::NS_MD, InvalidDOMElementException::class);
108
        Assert::true(
109
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
110
            'Missing required xsi:type in <md:RoleDescriptor> element.',
111
            SchemaViolationException::class,
112
        );
113
114
        $type = QNameValue::fromDocument($xml->getAttributeNS(C::NS_XSI, 'type'), $xml);
115
116
        // now check if we have a handler registered for it
117
        $handler = Utils::getContainer()->getExtensionHandler($type);
118
        if ($handler === null) {
119
            // we don't have a handler, proceed with unknown RoleDescriptor
120
            $protocols = self::getAttribute($xml, 'protocolSupportEnumeration', SAMLStringValue::class);
121
122
            $orgs = Organization::getChildrenOfClass($xml);
123
            Assert::maxCount(
124
                $orgs,
125
                1,
126
                'More than one Organization found in this descriptor',
127
                TooManyElementsException::class,
128
            );
129
130
            $extensions = Extensions::getChildrenOfClass($xml);
131
            Assert::maxCount(
132
                $extensions,
133
                1,
134
                'Only one md:Extensions element is allowed.',
135
                TooManyElementsException::class,
136
            );
137
138
            return new UnknownRoleDescriptor(
139
                new Chunk($xml),
140
                $type,
141
                preg_split('/[\s]+/', trim($protocols->getValue())),
142
                self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
143
                self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
144
                self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
145
                array_pop($extensions),
146
                self::getOptionalAttribute($xml, 'errorURL', SAMLAnyURIValue::class, null),
147
                KeyDescriptor::getChildrenOfClass($xml),
148
                array_pop($orgs),
149
                ContactPerson::getChildrenOfClass($xml),
150
                self::getAttributesNSFromXML($xml),
151
            );
152
        }
153
154
        Assert::subclassOf(
155
            $handler,
156
            AbstractRoleDescriptor::class,
157
            'Elements implementing RoleDescriptor must extend \SimpleSAML\SAML2\XML\saml\AbstractRoleDescriptor.',
158
        );
159
160
        return $handler::fromXML($xml);
161
    }
162
}
163