Passed
Pull Request — master (#2)
by Tim
12:41 queued 10:02
created

SignableElementTrait::getCertificates()   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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML;
6
7
//use DOMElement;
8
//use DOMNode;
9
//use Exception;
10
use SimpleSAML\Assert\Assert;
11
//use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils;
12
use SimpleSAML\XMLSecurity\XML\ds\Signature;
13
use SimpleSAML\XMLSecurity\XMLSecurityKey;
14
use SimpleSAML\XML\Utils as XMLUtils;
15
16
/**
17
 * Helper trait for processing signed elements.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
trait SignableElementTrait
22
{
23
    /**
24
     * List of certificates that should be included in the message.
25
     *
26
     * @var string[]
27
     */
28
    protected array $certificates = [];
29
30
    /**
31
     * The private key we should use to sign an unsigned message.
32
     *
33
     * The private key can be null, in which case we can only validate an already signed message.
34
     *
35
     * @var \SimpleSAML\XMLSecurity\XMLSecurityKey|null
36
     */
37
    protected ?XMLSecurityKey $signingKey = null;
38
39
40
    /**
41
     * Retrieve the certificates that are included in the message.
42
     *
43
     * @return string[] An array of certificates
44
     */
45
    public function getCertificates(): array
46
    {
47
        return $this->certificates;
48
    }
49
50
51
    /**
52
     * Set the certificates that should be included in the element.
53
     * The certificates should be strings with the PEM encoded data.
54
     *
55
     * @param string[] $certificates An array of certificates.
56
     */
57
    public function setCertificates(array $certificates): void
58
    {
59
        Assert::allStringNotEmpty($certificates);
60
61
        $this->certificates = $certificates;
62
    }
63
64
65
    /**
66
     * Get the private key we should use to sign the message.
67
     *
68
     * If the key is null, the message will be sent unsigned.
69
     *
70
     * @return \SimpleSAML\XMLSecurity\XMLSecurityKey|null
71
     */
72
    public function getSigningKey(): ?XMLSecurityKey
73
    {
74
        return $this->signingKey;
75
    }
76
77
78
    /**
79
     * Set the private key we should use to sign the message.
80
     *
81
     * If the key is null, the message will be sent unsigned.
82
     *
83
     * @param \SimpleSAML\XMLSecurity\XMLSecurityKey|null $signingKey
84
     */
85
    public function setSigningKey(XMLSecurityKey $signingKey = null): void
86
    {
87
        $this->signingKey = $signingKey;
88
    }
89
90
91
    /**
92
     * Sign the given XML element.
93
     *
94
     * @param \DOMElement $root The element we should sign.
95
     * @return \DOMElement The signed element.
96
     * @throws \Exception If an error occurs while trying to sign.
97
    protected function signElement(DOMElement $root, DOMNode $insertBefore = null): DOMElement
98
    {
99
        if ($this->signingKey instanceof XMLSecurityKey) {
100
            if ($insertBefore !== null) {
101
                XMLSecurityUtils::insertSignature($this->signingKey, $this->certificates, $root, $insertBefore);
102
103
                $doc = clone $root->ownerDocument;
104
                $this->signature = Signature::fromXML(XMLUtils::xpQuery($doc->documentElement, './ds:Signature')[0]);
105
            } else {
106
                $this->signature = new Signature($this->signingKey->getAlgorithm(), $this->certificates, $this->signingKey);
107
                $this->signature->toXML($root);
108
            }
109
        }
110
        return $root;
111
    }
112
     */
113
}
114