SubjectConfirmationData   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

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

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