LogoutRequest::getNotOnOrAfter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants 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...
10
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooHighException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...VersionTooHighException 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...
11
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooLowException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...tVersionTooLowException 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...
12
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
13
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Type\SAMLDateTimeValue 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...
14
use SimpleSAML\SAML2\Type\SAMLStringValue;
15
use SimpleSAML\SAML2\XML\IdentifierTrait;
16
use SimpleSAML\SAML2\XML\saml\AbstractBaseID;
17
use SimpleSAML\SAML2\XML\saml\EncryptedID;
18
use SimpleSAML\SAML2\XML\saml\IdentifierInterface;
19
use SimpleSAML\SAML2\XML\saml\Issuer;
20
use SimpleSAML\SAML2\XML\saml\NameID;
21
use SimpleSAML\XML\SchemaValidatableElementInterface;
22
use SimpleSAML\XML\SchemaValidatableElementTrait;
23
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
24
use SimpleSAML\XMLSchema\Exception\MissingElementException;
25
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
26
use SimpleSAML\XMLSchema\Type\IDValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\IDValue 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...
27
use SimpleSAML\XMLSecurity\XML\ds\Signature;
28
29
use function array_pop;
30
31
/**
32
 * Class for SAML 2 logout request messages.
33
 *
34
 * @package simplesamlphp/saml2
35
 */
36
final class LogoutRequest extends AbstractRequest implements SchemaValidatableElementInterface
37
{
38
    use IdentifierTrait;
39
    use SchemaValidatableElementTrait;
40
41
42
    /**
43
     * Constructor for SAML 2 AttributeQuery.
44
     *
45
     * @param \SimpleSAML\XMLSchema\Type\IDValue $id
46
     * @param \SimpleSAML\SAML2\XML\saml\IdentifierInterface $identifier
47
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue $issueInstant
48
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notOnOrAfter
49
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $reason
50
     * @param \SimpleSAML\SAML2\XML\samlp\SessionIndex[] $sessionIndexes
51
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
52
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $destination
53
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $consent
54
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
55
     *
56
     * @throws \Exception
57
     */
58
    public function __construct(
59
        IDValue $id,
60
        IdentifierInterface $identifier,
61
        SAMLDateTimeValue $issueInstant,
62
        protected ?SAMLDateTimeValue $notOnOrAfter = null,
63
        protected ?SAMLStringValue $reason = null,
64
        protected array $sessionIndexes = [],
65
        ?Issuer $issuer = null,
66
        ?SAMLAnyURIValue $destination = null,
67
        ?SAMLAnyURIValue $consent = null,
68
        ?Extensions $extensions = null,
69
    ) {
70
        Assert::maxCount($sessionIndexes, C::UNBOUNDED_LIMIT);
71
        Assert::allIsInstanceOf($sessionIndexes, SessionIndex::class);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\SessionIndex 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...
72
73
        parent::__construct($id, $issuer, $issueInstant, $destination, $consent, $extensions);
74
75
        $this->setIdentifier($identifier);
76
    }
77
78
79
    /**
80
     * Retrieve the expiration time of this request.
81
     *
82
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null The expiration time of this request.
83
     */
84
    public function getNotOnOrAfter(): ?SAMLDateTimeValue
85
    {
86
        return $this->notOnOrAfter;
87
    }
88
89
90
    /**
91
     * Retrieve the reason for this request.
92
     *
93
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null The reason for this request.
94
     */
95
    public function getReason(): ?SAMLStringValue
96
    {
97
        return $this->reason;
98
    }
99
100
101
    /**
102
     * Retrieve the SessionIndexes of the sessions that should be terminated.
103
     *
104
     * @return \SimpleSAML\SAML2\XML\samlp\SessionIndex[]
105
     *   The SessionIndexes, or an empty array if all sessions should be terminated.
106
     */
107
    public function getSessionIndexes(): array
108
    {
109
        return $this->sessionIndexes;
110
    }
111
112
113
    /**
114
     * Convert XML into a LogoutRequest
115
     *
116
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
117
     *   if the qualified name of the supplied element is wrong
118
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
119
     *   if the supplied element is missing one of the mandatory attributes
120
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
121
     *   if one of the mandatory child-elements is missing
122
     * @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException
123
     *   if too many child-elements of a type are specified
124
     */
125
    public static function fromXML(DOMElement $xml): static
126
    {
127
        Assert::same($xml->localName, 'LogoutRequest', InvalidDOMElementException::class);
128
        Assert::same($xml->namespaceURI, LogoutRequest::NS, InvalidDOMElementException::class);
129
130
        $version = self::getAttribute($xml, 'Version', SAMLStringValue::class);
131
        Assert::true(version_compare('2.0', strval($version), '<='), RequestVersionTooLowException::class);
132
        Assert::true(version_compare('2.0', strval($version), '>='), RequestVersionTooHighException::class);
133
134
        $id = self::getAttribute($xml, 'ID', IDValue::Class);
135
        $issueInstant = self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class);
136
137
        $notOnOrAfter = self::getOptionalAttribute($xml, 'NotOnOrAfter', SAMLDateTimeValue::class, null);
138
139
        $issuer = Issuer::getChildrenOfClass($xml);
140
        Assert::countBetween($issuer, 0, 1);
141
142
        $extensions = Extensions::getChildrenOfClass($xml);
143
        Assert::maxCount(
144
            $extensions,
145
            1,
146
            'Only one saml:Extensions element is allowed.',
147
            TooManyElementsException::class,
148
        );
149
150
        $identifier = self::getIdentifierFromXML($xml);
151
        Assert::notNull(
152
            $identifier,
153
            'Missing <saml:NameID>, <saml:BaseID> or <saml:EncryptedID> in <samlp:LogoutRequest>.',
154
            MissingElementException::class,
155
        );
156
        Assert::isInstanceOfAny($identifier, [AbstractBaseID::class, NameID::class, EncryptedID::class]);
157
158
        $signature = Signature::getChildrenOfClass($xml);
159
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.');
160
161
        $sessionIndex = SessionIndex::getChildrenOfClass($xml);
162
163
        $request = new static(
164
            $id,
165
            $identifier,
0 ignored issues
show
Bug introduced by
It seems like $identifier can also be of type null; however, parameter $identifier of SimpleSAML\SAML2\XML\sam...tRequest::__construct() does only seem to accept SimpleSAML\SAML2\XML\saml\IdentifierInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
            /** @scrutinizer ignore-type */ $identifier,
Loading history...
166
            $issueInstant,
167
            $notOnOrAfter,
168
            self::getOptionalAttribute($xml, 'Reason', SAMLStringValue::class, null),
169
            $sessionIndex,
170
            array_pop($issuer),
171
            self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null),
172
            self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null),
173
            array_pop($extensions),
174
        );
175
176
        if (!empty($signature)) {
177
            $request->setSignature($signature[0]);
178
            $request->messageContainedSignatureUponConstruction = true;
179
            $request->setXML($xml);
180
        }
181
182
        return $request;
183
    }
184
185
186
    /**
187
     * Convert this message to an unsigned XML document.
188
     * This method does not sign the resulting XML document.
189
     */
190
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
191
    {
192
        $e = parent::toUnsignedXML($parent);
193
194
        if ($this->getNotOnOrAfter() !== null) {
195
            $e->setAttribute('NotOnOrAfter', $this->getNotOnOrAfter()->getValue());
196
        }
197
198
        if ($this->getReason() !== null) {
199
            $e->setAttribute('Reason', $this->getReason()->getValue());
200
        }
201
202
        /** @var \SimpleSAML\XML\SerializableElementInterface&\SimpleSAML\SAML2\XML\saml\IdentifierInterface $identifier */
203
        $identifier = $this->getIdentifier();
204
        $identifier->toXML($e);
205
206
        foreach ($this->getSessionIndexes() as $sessionIndex) {
207
            $sessionIndex->toXML($e);
208
        }
209
210
        return $e;
211
    }
212
}
213