Passed
Pull Request — master (#6)
by Tim
01:53
created

X509Digest::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
11
/**
12
 * Class representing a ds:X509Digest element.
13
 *
14
 * @package simplesaml/xml-security
15
 */
16
final class X509Digest extends AbstractDsElement
17
{
18
    /**
19
     * The digest.
20
     *
21
     * @var string
22
     */
23
    protected string $digest;
24
25
26
    /**
27
     * The digest algorithm.
28
     *
29
     * @var string
30
     */
31
    protected string $algorithm;
32
33
34
    /**
35
     * Initialize a X509Digest element.
36
     *
37
     * @param string $digest
38
     * @param string $algorithm
39
     */
40
    public function __construct(string $digest, string $algorithm)
41
    {
42
        $this->setDigest($digest);
43
        $this->setAlgorithm($algorithm);
44
    }
45
46
47
    /**
48
     * Collect the value of the digest-property
49
     *
50
     * @return string
51
     */
52
    public function getDigest(): string
53
    {
54
        return $this->digest;
55
    }
56
57
58
    /**
59
     * Set the value of the digest-property
60
     *
61
     * @param string $digest
62
     */
63
    private function setDigest(string $digest): void
64
    {
65
        Assert::notEmpty($digest, 'X509Digest cannot be empty');
66
        $this->digest = $digest;
67
    }
68
69
70
    /**
71
     * Collect the value of the algorithm-property
72
     *
73
     * @return string
74
     */
75
    public function getAlgorithm(): string
76
    {
77
        return $this->algorithm;
78
    }
79
80
81
    /**
82
     * Set the value of the algorithm-property
83
     *
84
     * @param string $algorithm
85
     */
86
    private function setAlgorithm(string $algorithm): void
87
    {
88
        Assert::notEmpty($algorithm, 'Algorithm cannot be empty');
89
        $this->algorithm = $algorithm;
90
    }
91
92
93
    /**
94
     * Convert XML into a X509Digest
95
     *
96
     * @param \DOMElement $xml The XML element we should load
97
     * @return self
98
     *
99
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
100
     *   If the qualified name of the supplied element is wrong
101
     */
102
    public static function fromXML(DOMElement $xml): object
103
    {
104
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
105
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
106
107
        $digest = $xml->textContent;
108
        $algorithm = self::getAttribute($xml, 'Algorithm');
109
110
        Assert::stringNotEmpty($xml->textContent, 'Missing value digest.');
111
        /**
112
         * Note: This test is not watertight but prevents a string containing illegal characters
113
         * from being passed and ensures the string roughly follows the correct format for a Base64 encoded string
114
         */
115
        Assert::string(
116
            filter_var(
117
                $digest,
118
                FILTER_VALIDATE_REGEXP,
119
                [
120
                    'options' => [
121
                        'regexp' => '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/'
122
                    ]
123
                ]
124
            ),
125
            'Digest is not a valid Base64 encoded string'
126
        );
127
        return new self($digest, $algorithm);
128
    }
129
130
131
    /**
132
     * Convert this X509Digest element to XML.
133
     *
134
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
135
     * @return \DOMElement
136
     */
137
    public function toXML(DOMElement $parent = null): DOMElement
138
    {
139
        $e = $this->instantiateParentElement($parent);
140
141
        $e->textContent = $this->digest;
142
        $e->setAttribute('Algorithm', $this->algorithm);
143
144
        return $e;
145
    }
146
}
147