Passed
Pull Request — master (#280)
by Tim
02:30
created

ArtifactResolve::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
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\Assert\Assert;
9
use SimpleSAML\SAML2\Constants;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\Utils\XPath;
12
use SimpleSAML\SAML2\XML\saml\Issuer;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
use SimpleSAML\XML\Utils as XMLUtils;
16
use SimpleSAML\XMLSecurity\XML\ds\Signature;
17
18
use function array_pop;
19
20
/**
21
 * The Artifact is part of the SAML 2.0 IdP code, and it builds an artifact object.
22
 * I am using strings, because I find them easier to work with.
23
 * I want to use this, to be consistent with the other saml2_requests
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
class ArtifactResolve extends AbstractRequest
28
{
29
    /** @var string */
30
    protected string $artifact;
31
32
33
    /**
34
     * Initialize an ArtifactResolve.
35
     *
36
     * @param string $artifact
37
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
38
     * @param string|null $id
39
     * @param int|null $issueInstant
40
     * @param string|null $destination
41
     * @param string|null $consent
42
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
43
     *
44
     * @throws \Exception
45
     */
46
    public function __construct(
47
        string $artifact,
48
        ?Issuer $issuer = null,
49
        ?string $id = null,
50
        ?int $issueInstant = null,
51
        ?string $destination = null,
52
        ?string $consent = null,
53
        ?Extensions $extensions = null
54
    ) {
55
        parent::__construct($issuer, $id, $issueInstant, $destination, $consent, $extensions);
56
57
        $this->setArtifact($artifact);
58
    }
59
60
61
    /**
62
     * Retrieve the Artifact in this response.
63
     *
64
     * @return string artifact.
65
     *
66
     * @throws \SimpleSAML\Assert\AssertionFailedException if assertions are false
67
     */
68
    public function getArtifact(): string
69
    {
70
        Assert::notEmpty($this->artifact, 'Artifact not set.');
71
72
        return $this->artifact;
73
    }
74
75
76
    /**
77
     * Set the artifact that should be included in this response.
78
     *
79
     * @param string $artifact
80
     */
81
    public function setArtifact(string $artifact): void
82
    {
83
        $this->artifact = $artifact;
84
    }
85
86
87
    /**
88
     * Create a class from XML
89
     *
90
     * @param \DOMElement $xml
91
     * @return self
92
     *
93
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
94
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
95
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
96
     */
97
    public static function fromXML(DOMElement $xml): object
98
    {
99
        Assert::same($xml->localName, 'ArtifactResolve', InvalidDOMElementException::class);
100
        Assert::same($xml->namespaceURI, ArtifactResolve::NS, InvalidDOMElementException::class);
101
        Assert::same('2.0', self::getAttribute($xml, 'Version'));
102
103
        $issueInstant = self::getAttribute($xml, 'IssueInstant');
104
        Assert::validDateTimeZulu($issueInstant, ProtocolViolationException::class);
105
        $issueInstant = XMLUtils::xsDateTimeToTimestamp($issueInstant);
106
107
        $issuer = Issuer::getChildrenOfClass($xml);
108
        Assert::maxCount($issuer, 1);
109
110
        $extensions = Extensions::getChildrenOfClass($xml);
111
        Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.', TooManyElementsException::class);
112
113
        $signature = Signature::getChildrenOfClass($xml);
114
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
115
116
        $results = XPath::xpQuery($xml, './saml_protocol:Artifact', XPath::getXPath($xml));
117
        $artifact = $results[0]->textContent;
118
119
        $resolve = new self(
120
            $artifact,
121
            array_pop($issuer),
122
            self::getAttribute($xml, 'ID'),
123
            $issueInstant,
124
            self::getAttribute($xml, 'Destination', null),
125
            self::getAttribute($xml, 'Consent', null),
126
            array_pop($extensions)
127
        );
128
129
        if (!empty($signature)) {
130
            $resolve->setSignature($signature[0]);
131
        }
132
133
        $resolve->setXML($xml);
134
        return $resolve;
135
    }
136
137
138
    /**
139
     * Convert this message to an unsigned XML document.
140
     * This method does not sign the resulting XML document.
141
     *
142
     * @return \DOMElement The root element of the DOM tree
143
     */
144
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
145
    {
146
        Assert::notEmpty($this->artifact, 'Cannot convert ArtifactResolve to XML without an Artifact set.');
147
148
        /** @psalm-var \DOMDocument $e->ownerDocument */
149
        $e = parent::toUnsignedXML($parent);
150
        $artifactelement = $e->ownerDocument->createElementNS(Constants::NS_SAMLP, 'Artifact', $this->artifact);
0 ignored issues
show
Bug introduced by
The method createElementNS() does not exist on null. ( Ignorable by Annotation )

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

150
        /** @scrutinizer ignore-call */ 
151
        $artifactelement = $e->ownerDocument->createElementNS(Constants::NS_SAMLP, 'Artifact', $this->artifact);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
        $e->appendChild($artifactelement);
152
153
        return $e;
154
    }
155
}
156