Completed
Push — master ( da4b66...3ae217 )
by Tim
19s queued 15s
created

isMessageConstructedWithSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML11\Constants as C;
11
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
12
use SimpleSAML\SAML11\Utils;
13
use SimpleSAML\SAML11\XML\SignableElementTrait;
14
use SimpleSAML\SAML11\XML\SignedElementTrait;
15
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
16
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
17
18
use function strval;
19
20
/**
21
 * Base class for all SAML 1.1 messages.
22
 *
23
 * Implements what is common between the samlp:RequestAbstractType and
24
 * samlp:ResponseAbstractType element types.
25
 *
26
 * @package simplesamlphp/saml11
27
 */
28
abstract class AbstractMessage extends AbstractSamlpElement implements SignableElementInterface, SignedElementInterface
29
{
30
    use SignableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\SAML11\XML\SignableElementTrait requires some properties which are not provided by SimpleSAML\SAML11\XML\samlp\AbstractMessage: $ownerDocument, $documentElement
Loading history...
31
    use SignedElementTrait {
1 ignored issue
show
introduced by
The trait SimpleSAML\SAML11\XML\SignedElementTrait requires some properties which are not provided by SimpleSAML\SAML11\XML\samlp\AbstractMessage: $ownerDocument, $documentElement
Loading history...
32
        SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait;
33
    }
34
35
36
    /** @var bool */
37
    protected bool $messageContainedSignatureUponConstruction = false;
38
39
    /**
40
     * The original signed XML
41
     *
42
     * @var \DOMElement
43
     */
44
    protected DOMElement $xml;
45
46
47
    /**
48
     * Initialize a message.
49
     *
50
     * @param int $majorVersion
51
     * @param int $minorVersion
52
     * @param \DateTimeImmutable $issueInstant
53
     *
54
     * @throws \Exception
55
     */
56
    protected function __construct(
57
        protected int $majorVersion,
58
        protected int $minorVersion,
59
        protected ?DateTimeImmutable $issueInstant,
60
    ) {
61
        Assert::nullOrSame($issueInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class);
62
    }
63
64
65
    /**
66
     * Retrieve the major version of this message.
67
     *
68
     * @return int The major version of this message
69
     */
70
    public function getMajorVersion(): int
71
    {
72
        return $this->majorVersion;
73
    }
74
75
76
    /**
77
     * Retrieve the minor version of this message.
78
     *
79
     * @return int The minor version of this message
80
     */
81
    public function getMinorVersion(): int
82
    {
83
        return $this->minorVersion;
84
    }
85
86
87
    /**
88
     * Retrieve the issue timestamp of this message.
89
     *
90
     * @return \DateTimeImmutable The issue timestamp of this message, as an UNIX timestamp
91
     */
92
    public function getIssueInstant(): DateTimeImmutable
93
    {
94
        if ($this->issueInstant === null) {
95
            return Utils::getContainer()->getClock()->now();
96
        }
97
98
        return $this->issueInstant;
99
    }
100
101
102
    /**
103
     * Query whether or not the message contained a signature at the root level when the object was constructed.
104
     *
105
     * @return bool
106
     */
107
    public function isMessageConstructedWithSignature(): bool
108
    {
109
        return $this->messageContainedSignatureUponConstruction;
110
    }
111
112
113
    /**
114
     * Get the XML element.
115
     *
116
     * @return \DOMElement
117
     */
118
    public function getXML(): DOMElement
119
    {
120
        return $this->xml;
121
    }
122
123
124
    /**
125
     * Set the XML element.
126
     *
127
     * @param \DOMElement $xml
128
     */
129
    protected function setXML(DOMElement $xml): void
130
    {
131
        $this->xml = $xml;
132
    }
133
134
135
    /**
136
     * @return \DOMElement
137
     */
138
    protected function getOriginalXML(): DOMElement
139
    {
140
        return $this->xml ?? $this->toUnsignedXML();
141
    }
142
143
144
    /**
145
     * Convert this message to an unsigned XML document.
146
     * This method does not sign the resulting XML document.
147
     *
148
     * @return \DOMElement The root element of the DOM tree
149
     */
150
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
151
    {
152
        $e = $this->instantiateParentElement($parent);
153
154
        /* Ugly hack to add another namespace declaration to the root element. */
155
        $e->setAttributeNS(C::NS_SAML, 'saml:tmp', 'tmp');
156
        $e->removeAttributeNS(C::NS_SAML, 'tmp');
157
158
        $e->setAttribute('MajorVersion', strval($this->getMajorVersion()));
159
        $e->setAttribute('MinorVersion', strval($this->getMinorVersion()));
160
        $e->setAttribute('IssueInstant', $this->getIssueInstant()->format(C::DATETIME_FORMAT));
161
162
        return $e;
163
    }
164
}
165