Passed
Pull Request — master (#226)
by Jaime Pérez
03:17
created

ArtifactResolve   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 9 1
A setArtifact() 0 3 1
A getArtifact() 0 5 1
A __construct() 0 12 1
A fromXML() 0 35 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\samlp;
6
7
use DOMElement;
8
use SAML2\Constants;
9
use SAML2\Utils;
10
use SAML2\XML\ds\Signature;
11
use SAML2\XML\saml\Issuer;
12
use Webmozart\Assert\Assert;
13
14
/**
15
 * The Artifact is part of the SAML 2.0 IdP code, and it builds an artifact object.
16
 * I am using strings, because I find them easier to work with.
17
 * I want to use this, to be consistent with the other saml2_requests
18
 *
19
 * @author Danny Bollaert, UGent AS. <[email protected]>
20
 * @package SimpleSAMLphp
21
 */
22
class ArtifactResolve extends AbstractRequest
23
{
24
    /** @var string */
25
    protected $artifact;
26
27
28
    /**
29
     * Initialize an ArtifactResolve.
30
     *
31
     * @param string $artifact
32
     * @param \SAML2\XML\saml\Issuer|null $issuer
33
     * @param string|null $id
34
     * @param int|null $issueInstant
35
     * @param string|null $destination
36
     * @param string|null $consent
37
     * @param \SAML2\XML\samlp\Extensions $extensions
38
     *
39
     * @throws \Exception
40
     */
41
    public function __construct(
42
        string $artifact,
43
        ?Issuer $issuer = null,
44
        ?string $id = null,
45
        ?int $issueInstant = null,
46
        ?string $destination = null,
47
        ?string $consent = null,
48
        ?Extensions $extensions = null
49
    ) {
50
        parent::__construct($issuer, $id, $issueInstant, $destination, $consent, $extensions);
51
52
        $this->setArtifact($artifact);
53
    }
54
55
56
    /**
57
     * Retrieve the Artifact in this response.
58
     *
59
     * @return string artifact.
60
     *
61
     * @throws \InvalidArgumentException if assertions are false
62
     */
63
    public function getArtifact(): string
64
    {
65
        Assert::notEmpty($this->artifact, 'Artifact not set.');
66
67
        return $this->artifact;
68
    }
69
70
71
    /**
72
     * Set the artifact that should be included in this response.
73
     *
74
     * @param string $artifact
75
     * @return void
76
     */
77
    public function setArtifact(string $artifact): void
78
    {
79
        $this->artifact = $artifact;
80
    }
81
82
83
    /**
84
     * Create a class from XML
85
     *
86
     * @param \DOMElement $xml
87
     * @return self
88
     */
89
    public static function fromXML(DOMElement $xml): object
90
    {
91
        Assert::same($xml->localName, 'ArtifactResolve');
92
        Assert::same($xml->namespaceURI, ArtifactResolve::NS);
93
        Assert::same('2.0', self::getAttribute($xml, 'Version'));
94
95
        $issueInstant = Utils::xsDateTimeToTimestamp(self::getAttribute($xml, 'IssueInstant'));
96
97
        $issuer = Issuer::getChildrenOfClass($xml);
98
        Assert::maxCount($issuer, 1);
99
100
        $extensions = Extensions::getChildrenOfClass($xml);
101
        Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.');
102
103
        $signature = Signature::getChildrenOfClass($xml);
104
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.');
105
106
        $results = Utils::xpQuery($xml, './saml_protocol:Artifact');
107
        $artifact = $results[0]->textContent;
108
109
        $resolve = new self(
110
            $artifact,
111
            array_pop($issuer),
112
            self::getAttribute($xml, 'ID'),
113
            $issueInstant,
114
            self::getAttribute($xml, 'Destination', null),
115
            self::getAttribute($xml, 'Consent', null),
116
            array_pop($extensions)
117
        );
118
119
        if (!empty($signature)) {
120
            $resolve->setSignature($signature[0]);
121
        }
122
123
        return $resolve;
124
    }
125
126
127
    /**
128
     * Convert the response message to an XML element.
129
     *
130
     * @return \DOMElement This response.
131
     *
132
     * @throws \InvalidArgumentException if assertions are false
133
     */
134
    public function toXML(?DOMElement $parent = null): DOMElement
135
    {
136
        Assert::notEmpty($this->artifact, 'Cannot convert ArtifactResolve to XML without an Artifact set.');
137
138
        $e = parent::toXML($parent);
139
        $artifactelement = $e->ownerDocument->createElementNS(Constants::NS_SAMLP, 'Artifact', $this->artifact);
140
        $e->appendChild($artifactelement);
141
142
        return $this->signElement($e);
143
    }
144
}
145