AbstractMessage::getIssuer()   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\Type\SAMLAnyURIValue;
9
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...
10
use SimpleSAML\SAML2\Utils\XPath;
11
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
12
use SimpleSAML\SAML2\XML\saml\Issuer;
13
use SimpleSAML\SAML2\XML\SignableElementTrait;
14
use SimpleSAML\SAML2\XML\SignedElementTrait;
15
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...
16
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
17
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
18
19
use function array_pop;
20
21
/**
22
 * Base class for all SAML 2 messages.
23
 *
24
 * Implements what is common between the samlp:RequestAbstractType and
25
 * samlp:StatusResponseType element types.
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
abstract class AbstractMessage extends AbstractSamlpElement implements SignableElementInterface, SignedElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\AbstractSamlpElement 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...
30
{
31
    use ExtendableElementTrait;
32
    use SignableElementTrait;
33
    use SignedElementTrait {
34
        SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait;
35
    }
36
37
38
    protected bool $messageContainedSignatureUponConstruction = false;
39
40
    /**
41
     * The original signed XML
42
     */
43
    protected DOMElement $xml;
44
45
46
    /**
47
     * Initialize a message.
48
     *
49
     * @param \SimpleSAML\XMLSchema\Type\IDValue $id
50
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
51
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $issueInstant
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
    protected function __construct(
59
        protected IDValue $id,
60
        protected ?Issuer $issuer = null,
61
        protected ?SAMLDateTimeValue $issueInstant = null,
62
        protected ?SAMLAnyURIValue $destination = null,
63
        protected ?SAMLAnyURIValue $consent = null,
64
        ?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...
65
    ) {
66
        $this->setExtensions($extensions);
67
    }
68
69
70
    /**
71
     * Retrieve the identifier of this message.
72
     *
73
     * @return \SimpleSAML\XMLSchema\Type\IDValue The identifier of this message
74
     */
75
    public function getId(): IDValue
76
    {
77
        return $this->id;
78
    }
79
80
81
    /**
82
     * Retrieve the issue timestamp of this message.
83
     *
84
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue The issue timestamp of this message, as an UNIX timestamp
85
     */
86
    public function getIssueInstant(): SAMLDateTimeValue
87
    {
88
        return $this->issueInstant;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->issueInstant could return the type null which is incompatible with the type-hinted return SimpleSAML\SAML2\Type\SAMLDateTimeValue. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
91
92
    /**
93
     * Retrieve the destination of this message.
94
     *
95
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null The destination of this message,
96
     *   or NULL if no destination is given
97
     */
98
    public function getDestination(): ?SAMLAnyURIValue
99
    {
100
        return $this->destination;
101
    }
102
103
104
    /**
105
     * Get the given consent for this message.
106
     * Most likely (though not required) a value of urn:oasis:names:tc:SAML:2.0:consent.
107
     *
108
     * @see \SimpleSAML\SAML2\Constants
109
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null Consent
110
     */
111
    public function getConsent(): ?SAMLAnyURIValue
112
    {
113
        return $this->consent;
114
    }
115
116
117
    /**
118
     * Retrieve the issuer if this message.
119
     *
120
     * @return \SimpleSAML\SAML2\XML\saml\Issuer|null The issuer of this message, or NULL if no issuer is given
121
     */
122
    public function getIssuer(): ?Issuer
123
    {
124
        return $this->issuer;
125
    }
126
127
128
    /**
129
     * Query whether or not the message contained a signature at the root level when the object was constructed.
130
     */
131
    public function isMessageConstructedWithSignature(): bool
132
    {
133
        return $this->messageContainedSignatureUponConstruction;
134
    }
135
136
137
    /**
138
     * Get the XML element.
139
     */
140
    public function getXML(): DOMElement
141
    {
142
        return $this->xml;
143
    }
144
145
146
    /**
147
     * Set the XML element.
148
     */
149
    protected function setXML(DOMElement $xml): void
150
    {
151
        $this->xml = $xml;
152
    }
153
154
155
    /**
156
     */
157
    protected function getOriginalXML(): DOMElement
158
    {
159
        return $this->xml ?? $this->toUnsignedXML();
160
    }
161
162
163
    /**
164
     * Convert this message to an unsigned XML document.
165
     * This method does not sign the resulting XML document.
166
     */
167
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
168
    {
169
        $root = $this->instantiateParentElement($parent);
170
171
        $root->setAttribute('Version', '2.0');
172
        $root->setAttribute('ID', $this->getId()->getValue());
173
        $root->setAttribute('IssueInstant', $this->getIssueInstant()->getValue());
174
175
        if ($this->getDestination() !== null) {
176
            $root->setAttribute('Destination', $this->getDestination()->getValue());
177
        }
178
179
        if ($this->getConsent() !== null) {
180
            $root->setAttribute('Consent', $this->getConsent()->getValue());
181
        }
182
183
        $this->getIssuer()?->toXML($root);
184
185
        $extensions = $this->getExtensions();
186
        if ($extensions !== null && !$extensions->isEmptyElement()) {
187
            $extensions->toXML($root);
188
        }
189
190
        return $root;
191
    }
192
193
194
    /**
195
     * Create XML from this class
196
     */
197
    public function toXML(?DOMElement $parent = null): DOMElement
198
    {
199
        if ($this->isSigned() === true && $this->signer === null) {
200
            // We already have a signed document and no signer was set to re-sign it
201
            if ($parent === null) {
202
                return $this->xml;
203
            }
204
205
            $node = $parent->ownerDocument?->importNode($this->getXML(), true);
206
            $parent->appendChild($node);
207
            return $parent;
208
        }
209
210
        $e = $this->toUnsignedXML($parent);
211
212
        if ($this->signer !== null) {
213
            $signedXML = $this->doSign($e);
214
215
            // Test for an Issuer
216
            $messageElements = XPath::xpQuery($signedXML, './saml_assertion:Issuer', XPath::getXPath($signedXML));
217
            $issuer = array_pop($messageElements);
218
219
            $signedXML->insertBefore($this->signature?->toXML($signedXML), $issuer->nextSibling);
220
            return $signedXML;
221
        }
222
223
        return $e;
224
    }
225
}
226