SubjectConfirmationData   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 65
dl 0
loc 209
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 3
A fromXML() 0 35 3
A getInResponseTo() 0 3 1
A getRecipient() 0 3 1
A getNotOnOrAfter() 0 3 1
B toXML() 0 29 8
B isEmptyElement() 0 9 7
A getNotBefore() 0 3 1
A getAddress() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\SAML2\Assert\Assert;
10
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
11
use SimpleSAML\SAML2\Constants as C;
12
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
13
use SimpleSAML\SAML2\Utils;
14
use SimpleSAML\XML\Exception\InvalidDOMElementException;
15
use SimpleSAML\XML\ExtendableAttributesTrait;
16
use SimpleSAML\XML\ExtendableElementTrait;
17
use SimpleSAML\XML\SchemaValidatableElementInterface;
18
use SimpleSAML\XML\SchemaValidatableElementTrait;
19
use SimpleSAML\XML\XsNamespace as NS;
20
21
use function filter_var;
22
use function is_null;
23
24
/**
25
 * Class representing SAML 2 SubjectConfirmationData element.
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
final class SubjectConfirmationData extends AbstractSamlElement implements SchemaValidatableElementInterface
30
{
31
    use ExtendableAttributesTrait;
32
    use ExtendableElementTrait;
33
    use SchemaValidatableElementTrait;
34
35
36
    /** The namespace-attribute for the xs:any element */
37
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
38
39
    /** The namespace-attribute for the xs:anyAttribute element */
40
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
41
42
43
    /**
44
     * Initialize (and parse) a SubjectConfirmationData element.
45
     *
46
     * @param \DateTimeImmutable|null $notBefore
47
     * @param \DateTimeImmutable|null $notOnOrAfter
48
     * @param string|null $recipient
49
     * @param string|null $inResponseTo
50
     * @param string|null $address
51
     * @param \SimpleSAML\XML\SerializableElementInterface[] $children
52
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\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...
53
     */
54
    public function __construct(
55
        protected ?DateTimeImmutable $notBefore = null,
56
        protected ?DateTimeImmutable $notOnOrAfter = null,
57
        protected ?string $recipient = null,
58
        protected ?string $inResponseTo = null,
59
        protected ?string $address = null,
60
        array $children = [],
61
        array $namespacedAttributes = [],
62
    ) {
63
        Assert::nullOrSame($notBefore?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class);
64
        Assert::nullOrSame($notOnOrAfter?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class);
65
        Assert::nullOrNotWhitespaceOnly($recipient);
66
        Assert::nullOrValidNCName($inResponseTo); // Covers the empty string
67
68
        if (!is_null($address) && !filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
69
            Utils::getContainer()->getLogger()->warning(
70
                sprintf('Provided argument (%s) is not a valid IP address.', $address),
71
            );
72
        }
73
74
        $this->setElements($children);
75
        $this->setAttributesNS($namespacedAttributes);
76
    }
77
78
79
    /**
80
     * Collect the value of the NotBefore-property
81
     *
82
     * @return \DateTimeImmutable|null
83
     */
84
    public function getNotBefore(): ?DateTimeImmutable
85
    {
86
        return $this->notBefore;
87
    }
88
89
90
    /**
91
     * Collect the value of the NotOnOrAfter-property
92
     *
93
     * @return \DateTimeImmutable|null
94
     */
95
    public function getNotOnOrAfter(): ?DateTimeImmutable
96
    {
97
        return $this->notOnOrAfter;
98
    }
99
100
101
    /**
102
     * Collect the value of the Recipient-property
103
     *
104
     * @return string|null
105
     */
106
    public function getRecipient(): ?string
107
    {
108
        return $this->recipient;
109
    }
110
111
112
    /**
113
     * Collect the value of the InResponseTo-property
114
     *
115
     * @return string|null
116
     */
117
    public function getInResponseTo(): ?string
118
    {
119
        return $this->inResponseTo;
120
    }
121
122
123
    /**
124
     * Collect the value of the Address-property
125
     *
126
     * @return string|null
127
     */
128
    public function getAddress(): ?string
129
    {
130
        return $this->address;
131
    }
132
133
134
    /**
135
     * Test if an object, at the state it's in, would produce an empty XML-element
136
     *
137
     * @return bool
138
     */
139
    public function isEmptyElement(): bool
140
    {
141
        return empty($this->notBefore)
142
            && empty($this->notOnOrAfter)
143
            && empty($this->recipient)
144
            && empty($this->inResponseTo)
145
            && empty($this->address)
146
            && empty($this->elements)
147
            && empty($this->namespacedAttributes);
148
    }
149
150
151
    /**
152
     * Convert XML into a SubjectConfirmationData
153
     *
154
     * @param \DOMElement $xml The XML element we should load
155
     * @return static
156
     *
157
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
158
     *   if the qualified name of the supplied element is wrong
159
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
160
     *   if the supplied element is missing any of the mandatory attributes
161
     * @throws \SimpleSAML\Assert\AssertionFailedException
162
     *   if NotBefore or NotOnOrAfter contain an invalid date.
163
     */
164
    public static function fromXML(DOMElement $xml): static
165
    {
166
        Assert::same($xml->localName, 'SubjectConfirmationData', InvalidDOMElementException::class);
167
        Assert::same($xml->namespaceURI, SubjectConfirmationData::NS, InvalidDOMElementException::class);
168
169
        $NotBefore = self::getOptionalAttribute($xml, 'NotBefore', null);
170
        if ($NotBefore !== null) {
171
            // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
172
            $NotBefore = preg_replace('/([.][0-9]+Z)$/', 'Z', $NotBefore, 1);
173
174
            SAMLAssert::validDateTime($NotBefore, ProtocolViolationException::class);
175
            $NotBefore = new DateTimeImmutable($NotBefore);
176
        }
177
178
        $NotOnOrAfter = self::getOptionalAttribute($xml, 'NotOnOrAfter', null);
179
        if ($NotOnOrAfter !== null) {
180
            // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
181
            $NotOnOrAfter = preg_replace('/([.][0-9]+Z)$/', 'Z', $NotOnOrAfter, 1);
182
183
            SAMLAssert::validDateTime($NotOnOrAfter, ProtocolViolationException::class);
184
            $NotOnOrAfter = new DateTimeImmutable($NotOnOrAfter);
185
        }
186
187
        $Recipient = self::getOptionalAttribute($xml, 'Recipient', null);
188
        $InResponseTo = self::getOptionalAttribute($xml, 'InResponseTo', null);
189
        $Address = self::getOptionalAttribute($xml, 'Address', null);
190
191
        return new static(
192
            $NotBefore,
193
            $NotOnOrAfter,
194
            $Recipient,
195
            $InResponseTo,
196
            $Address,
197
            self::getChildElementsFromXML($xml),
198
            self::getAttributesNSFromXML($xml),
199
        );
200
    }
201
202
203
    /**
204
     * Convert this element to XML.
205
     *
206
     * @param  \DOMElement|null $parent The parent element we should append this element to.
207
     * @return \DOMElement This element, as XML.
208
     */
209
    public function toXML(?DOMElement $parent = null): DOMElement
210
    {
211
        $e = $this->instantiateParentElement($parent);
212
213
        if ($this->getNotBefore() !== null) {
214
            $e->setAttribute('NotBefore', $this->getNotBefore()->format(C::DATETIME_FORMAT));
215
        }
216
        if ($this->getNotOnOrAfter() !== null) {
217
            $e->setAttribute('NotOnOrAfter', $this->getNotOnOrAfter()->format(C::DATETIME_FORMAT));
218
        }
219
        if ($this->getRecipient() !== null) {
220
            $e->setAttribute('Recipient', $this->getRecipient());
221
        }
222
        if ($this->getInResponseTo() !== null) {
223
            $e->setAttribute('InResponseTo', $this->getInResponseTo());
224
        }
225
        if ($this->getAddress() !== null) {
226
            $e->setAttribute('Address', $this->getAddress());
227
        }
228
229
        foreach ($this->getAttributesNS() as $attr) {
230
            $attr->toXML($e);
231
        }
232
233
        foreach ($this->getElements() as $n) {
234
            $n->toXML($e);
235
        }
236
237
        return $e;
238
    }
239
}
240