Passed
Branch master (c86cc6)
by Tim
03:54
created

CustomSignable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\AbstractElement;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
13
use SimpleSAML\XMLSecurity\XML\ds\Signature;
14
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
15
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
16
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
17
use SimpleSAML\XMLSecurity\XML\SignableElementTrait;
18
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
19
use SimpleSAML\XMLSecurity\XML\SignedElementTrait;
20
21
/**
22
 * This is an example class demonstrating an object that can be signed and encrypted.
23
 *
24
 * @package simplesamlphp/xml-security
25
 */
26
class CustomSignable extends AbstractElement implements
27
    SignableElementInterface,
28
    SignedElementInterface,
29
    EncryptableElementInterface
30
{
31
    use SignableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XMLSecurity\XML\SignableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\Test\XML\CustomSignable: $ownerDocument, $documentElement
Loading history...
32
    use SignedElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XMLSecurity\XML\SignedElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\Test\XML\CustomSignable: $ownerDocument, $documentElement
Loading history...
33
    use EncryptableElementTrait;
34
35
    /** @var string */
36
    public const NS = 'urn:x-simplesamlphp:namespace';
37
38
    /** @var string */
39
    public const NS_PREFIX = 'ssp';
40
41
    /** @var bool */
42
    protected bool $formatOutput = false;
43
44
    /** @var \SimpleSAML\XMLSecurity\XML\ds\Signature|null */
45
    protected ?Signature $signature = null;
46
47
    /** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null */
48
    private ?EncryptionBackend $backend = null;
49
50
    /** @var string[] */
51
    private array $blacklistedAlgs = [];
52
53
    /**
54
     * Constructor
55
     *
56
     * @param \DOMElement $xml
57
     */
58
    public function __construct(
59
        protected DOMElement $xml,
60
        protected ?string $id
61
    ) {
62
    }
63
64
65
    /**
66
     * Get the namespace for the element.
67
     *
68
     * @return string
69
     */
70
    public static function getNamespaceURI(): string
71
    {
72
        return static::NS;
73
    }
74
75
76
    /**
77
     * Get the namespace-prefix for the element.
78
     *
79
     * @return string
80
     */
81
    public static function getNamespacePrefix(): string
82
    {
83
        return static::NS_PREFIX;
84
    }
85
86
87
    /**
88
     * Get the XML element.
89
     *
90
     * @return \DOMElement
91
     */
92
    public function getXML(): DOMElement
93
    {
94
        return $this->xml;
95
    }
96
97
98
    /**
99
     * @return string|null
100
     */
101
    public function getId(): ?string
102
    {
103
        return $this->id;
104
    }
105
106
107
    /**
108
     * @inheritDoc
109
     */
110
    protected function getOriginalXML(): DOMElement
111
    {
112
        return $this->xml;
113
    }
114
115
116
    /**
117
     * Implement a method like this if your encrypted object needs to instantiate a new decryptor, for example, to
118
     * decrypt a session key. This method is required by \SimpleSAML\XMLSecurity\XML\EncryptedElementTrait.
119
     *
120
     * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null The encryption backend to use, or null if we want
121
     * to use the default.
122
     */
123
    public function getEncryptionBackend(): ?EncryptionBackend
124
    {
125
        return $this->backend;
126
    }
127
128
129
    /**
130
     * Implement a method like this if your encrypted object needs to instantiate a new decryptor, for example, to
131
     * decrypt a session key. This method is required by \SimpleSAML\XMLSecurity\XML\EncryptedElementTrait.
132
     *
133
     * @param \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null $backend The encryption backend we want to use, or
134
     * null if we want to use the defaults.
135
     */
136
    public function setEncryptionBackend(?EncryptionBackend $backend): void
137
    {
138
        $this->backend = $backend;
139
    }
140
141
142
    /**
143
     * Implement a method like this if your encrypted object needs to instantiate a new decryptor, for example, to
144
     * decrypt a session key. This method is required by \SimpleSAML\XMLSecurity\XML\EncryptedElementTrait.
145
     *
146
     * @return string[]|null An array with all algorithm identifiers that we want to blacklist, or null if we want to
147
     * use the defaults.
148
     */
149
    public function getBlacklistedAlgorithms(): ?array
150
    {
151
        return $this->blacklistedAlgs;
152
    }
153
154
155
    /**
156
     * Implement a method like this if your encrypted object needs to instantiate a new decryptor, for example, to
157
     * decrypt a session key. This method is required by \SimpleSAML\XMLSecurity\XML\EncryptedElementTrait.
158
     *
159
     * @param string[]|null $algIds An array with the identifiers of the algorithms we want to blacklist, or null if we
160
     * want to use the defaults.
161
     */
162
    public function setBlacklistedAlgorithms(?array $algIds): void
163
    {
164
        $this->blacklistedAlgs = $algIds;
165
    }
166
167
168
    /**
169
     * Convert XML into a CustomSignable
170
     *
171
     * @param \DOMElement $xml The XML element we should load
172
     * @return static
173
     *
174
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
175
     *   if the qualified name of the supplied element is wrong
176
     */
177
    public static function fromXML(DOMElement $xml): static
178
    {
179
        Assert::same($xml->localName, 'CustomSignable', InvalidDOMElementException::class);
180
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
181
182
        $id = self::getOptionalAttribute($xml, 'id', null);
183
        $signature = Signature::getChildrenOfClass($xml);
184
        Assert::maxCount($signature, 1, TooManyElementsException::class);
185
186
        $customSignable = new static($xml, $id);
187
        if (!empty($signature)) {
188
            $customSignable->signature = $signature[0];
189
        }
190
        return $customSignable;
191
    }
192
193
194
    /**
195
     * Convert this CustomSignable to XML.
196
     *
197
     * @param \DOMElement|null $parent The parent element to append this CustomSignable to.
198
     * @return \DOMElement The XML element after adding the data corresponding to this CustomSignable.
199
     * @throws \Exception
200
     */
201
    public function toXML(DOMElement $parent = null): DOMElement
202
    {
203
        if ($this->signer !== null) {
204
            $signedXML = $this->doSign($this->xml);
205
            $signedXML->insertBefore($this->signature->toXML($signedXML), $signedXML->firstChild);
206
            return $signedXML;
207
        }
208
209
        return $this->xml;
210
    }
211
}
212