ArtifactResponse   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 3 1
A __construct() 0 20 1
B fromXML() 0 62 6
A toUnsignedXML() 0 7 1
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\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...
10
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...
11
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
12
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...
13
use SimpleSAML\SAML2\Type\SAMLStringValue;
14
use SimpleSAML\SAML2\Utils\XPath;
15
use SimpleSAML\SAML2\XML\saml\Issuer;
16
use SimpleSAML\XML\SchemaValidatableElementInterface;
17
use SimpleSAML\XML\SchemaValidatableElementTrait;
18
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
19
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
20
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...
21
use SimpleSAML\XMLSchema\Type\NCNameValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\NCNameValue 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...
22
use SimpleSAML\XMLSecurity\XML\ds\Signature;
23
24
use function array_pop;
25
use function version_compare;
26
27
/**
28
 * The \SimpleSAML\SAML2\XML\samlp\ArtifactResponse,
29
 *  is the response to the \SimpleSAML\SAML2\XML\samlp\ArtifactResolve.
30
 *
31
 * @package simplesamlphp/saml2
32
 */
33
class ArtifactResponse extends AbstractStatusResponse implements SchemaValidatableElementInterface
34
{
35
    use SchemaValidatableElementTrait;
36
37
38
    /**
39
     * Constructor for SAML 2 ArtifactResponse.
40
     *
41
     * @param \SimpleSAML\XMLSchema\Type\IDValue $id
42
     * @param \SimpleSAML\SAML2\XML\samlp\Status $status
43
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue $issueInstant
44
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
45
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo
46
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $destination
47
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $consent
48
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions|null $extensions
49
     * @param \SimpleSAML\SAML2\XML\samlp\AbstractMessage|null $message
50
     */
51
    final public function __construct(
52
        IDValue $id,
53
        Status $status,
54
        SAMLDateTimeValue $issueInstant,
55
        ?Issuer $issuer = null,
56
        ?NCNameValue $inResponseTo = null,
57
        ?SAMLAnyURIValue $destination = null,
58
        ?SAMLAnyURIValue $consent = null,
59
        ?Extensions $extensions = null,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\Extensions 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...
60
        protected ?AbstractMessage $message = null,
61
    ) {
62
        parent::__construct(
63
            $id,
64
            $status,
65
            $issueInstant,
66
            $issuer,
67
            $inResponseTo,
68
            $destination,
69
            $consent,
70
            $extensions,
71
        );
72
    }
73
74
75
    /**
76
     * Collect the value of the any-property
77
     *
78
     * @return \SimpleSAML\SAML2\XML\samlp\AbstractMessage|null
79
     */
80
    public function getMessage(): ?AbstractMessage
81
    {
82
        return $this->message;
83
    }
84
85
86
    /**
87
     * Convert XML into an ArtifactResponse
88
     *
89
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
90
     *   if the qualified name of the supplied element is wrong
91
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
92
     *   if the supplied element is missing one of the mandatory attributes
93
     */
94
    public static function fromXML(DOMElement $xml): static
95
    {
96
        Assert::same($xml->localName, 'ArtifactResponse', InvalidDOMElementException::class);
97
        Assert::same($xml->namespaceURI, ArtifactResponse::NS, InvalidDOMElementException::class);
98
99
        $version = self::getAttribute($xml, 'Version', SAMLStringValue::class);
100
        Assert::true(version_compare('2.0', $version->getValue(), '<='), RequestVersionTooLowException::class);
101
        Assert::true(version_compare('2.0', $version->getValue(), '>='), RequestVersionTooHighException::class);
102
103
        $issuer = Issuer::getChildrenOfClass($xml);
104
        Assert::countBetween($issuer, 0, 1);
105
106
        // find message; it should come last, after the Status-element
107
        $status = XPath::xpQuery($xml, './saml_protocol:Status', XPath::getXPath($xml));
108
        $status = $status[0];
109
        $message = null;
110
111
        for ($child = $status->nextSibling; $child !== null; $child = $child->nextSibling) {
112
            if ($child instanceof DOMElement) {
113
                $message = MessageFactory::fromXML($child);
114
                break;
115
            }
116
            /* Ignore comments and text nodes. */
117
        }
118
119
        $status = Status::getChildrenOfClass($xml);
120
        Assert::count($status, 1);
121
122
        $extensions = Extensions::getChildrenOfClass($xml);
123
        Assert::maxCount(
124
            $extensions,
125
            1,
126
            'Only one saml:Extensions element is allowed.',
127
            TooManyElementsException::class,
128
        );
129
130
        $signature = Signature::getChildrenOfClass($xml);
131
        Assert::maxCount(
132
            $signature,
133
            1,
134
            'Only one ds:Signature element is allowed.',
135
            TooManyElementsException::class,
136
        );
137
138
        $response = new static(
139
            self::getAttribute($xml, 'ID', IDValue::class),
140
            array_pop($status),
141
            self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class),
142
            empty($issuer) ? null : array_pop($issuer),
143
            self::getOptionalAttribute($xml, 'InResponseTo', NCNameValue::class, null),
144
            self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null),
145
            self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null),
146
            empty($extensions) ? null : array_pop($extensions),
147
            $message,
148
        );
149
150
        if (!empty($signature)) {
151
            $response->setSignature($signature[0]);
152
            $response->setXML($xml);
153
        }
154
155
        return $response;
156
    }
157
158
159
    /**
160
     * Convert this message to an unsigned XML document.
161
     * This method does not sign the resulting XML document.
162
     */
163
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
164
    {
165
        $e = parent::toUnsignedXML($parent);
166
167
        $this->getMessage()?->toXML($e);
168
169
        return $e;
170
    }
171
}
172