Signature   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 49
c 3
b 0
f 0
dl 0
loc 160
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 17 3
A fromXML() 0 49 2
A getKeyInfo() 0 3 1
A __construct() 0 9 1
A getObjects() 0 3 1
A getSignedInfo() 0 3 1
A getId() 0 3 1
A getSignatureValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\SchemaValidatableElementInterface;
9
use SimpleSAML\XML\SchemaValidatableElementTrait;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSchema\Exception\MissingElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\IDValue;
14
use SimpleSAML\XMLSecurity\Assert\Assert;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
17
use function array_pop;
18
use function strval;
19
20
/**
21
 * Class representing a ds:Signature element.
22
 *
23
 * @package simplesamlphp/xml-security
24
 */
25
final class Signature extends AbstractDsElement implements SchemaValidatableElementInterface
26
{
27
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\Signature: $message, $line
Loading history...
28
29
30
    /**
31
     * Signature constructor.
32
     *
33
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo
34
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue
35
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
36
     * @param \SimpleSAML\XMLSecurity\XML\ds\DsObject[] $objects
37
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id
38
     */
39
    public function __construct(
40
        protected SignedInfo $signedInfo,
41
        protected SignatureValue $signatureValue,
42
        protected ?KeyInfo $keyInfo = null,
43
        protected array $objects = [],
44
        protected ?IDValue $Id = null,
45
    ) {
46
        Assert::maxCount($objects, C::UNBOUNDED_LIMIT);
47
        Assert::allIsInstanceOf($objects, DsObject::class);
48
    }
49
50
51
    /**
52
     * Get the Id used for this signature.
53
     *
54
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
55
     */
56
    public function getId(): ?IDValue
57
    {
58
        return $this->Id;
59
    }
60
61
62
    /**
63
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo
64
     */
65
    public function getSignedInfo(): SignedInfo
66
    {
67
        return $this->signedInfo;
68
    }
69
70
71
    /**
72
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
73
     */
74
    public function getSignatureValue(): SignatureValue
75
    {
76
        return $this->signatureValue;
77
    }
78
79
80
    /**
81
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
82
     */
83
    public function getKeyInfo(): ?KeyInfo
84
    {
85
        return $this->keyInfo;
86
    }
87
88
89
    /**
90
     * Get the array of ds:Object elements attached to this signature.
91
     *
92
     * @return \SimpleSAML\XMLSecurity\XML\ds\DsObject[]
93
     */
94
    public function getObjects(): array
95
    {
96
        return $this->objects;
97
    }
98
99
100
    /**
101
     * Convert XML into a Signature element
102
     *
103
     * @param \DOMElement $xml
104
     * @return static
105
     *
106
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
107
     *   If the qualified name of the supplied element is wrong
108
     */
109
    public static function fromXML(DOMElement $xml): static
110
    {
111
        Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class);
112
        Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class);
113
114
        $signedInfo = SignedInfo::getChildrenOfClass($xml);
115
        Assert::minCount(
116
            $signedInfo,
117
            1,
118
            'ds:Signature needs exactly one ds:SignedInfo element.',
119
            MissingElementException::class,
120
        );
121
        Assert::maxCount(
122
            $signedInfo,
123
            1,
124
            'ds:Signature needs exactly one ds:SignedInfo element.',
125
            TooManyElementsException::class,
126
        );
127
128
        $signatureValue = SignatureValue::getChildrenOfClass($xml);
129
        Assert::minCount(
130
            $signatureValue,
131
            1,
132
            'ds:Signature needs exactly one ds:SignatureValue element.',
133
            MissingElementException::class,
134
        );
135
        Assert::maxCount(
136
            $signatureValue,
137
            1,
138
            'ds:Signature needs exactly one ds:SignatureValue element.',
139
            TooManyElementsException::class,
140
        );
141
142
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
143
        Assert::maxCount(
144
            $keyInfo,
145
            1,
146
            'ds:Signature can hold a maximum of one ds:KeyInfo element.',
147
            TooManyElementsException::class,
148
        );
149
150
        $objects = DsObject::getChildrenOfClass($xml);
151
152
        return new static(
153
            array_pop($signedInfo),
154
            array_pop($signatureValue),
155
            empty($keyInfo) ? null : array_pop($keyInfo),
156
            $objects,
157
            self::getOptionalAttribute($xml, 'Id', IDValue::class, null),
158
        );
159
    }
160
161
162
    /**
163
     * Convert this Signature element to XML.
164
     *
165
     * @param \DOMElement|null $parent The element we should append this Signature element to.
166
     * @return \DOMElement
167
     */
168
    public function toXML(?DOMElement $parent = null): DOMElement
169
    {
170
        $e = $this->instantiateParentElement($parent);
171
172
        if ($this->getId() !== null) {
173
            $e->setAttribute('Id', strval($this->getId()));
174
        }
175
176
        $this->getSignedInfo()->toXML($e);
177
        $this->getSignatureValue()->toXML($e);
178
        $this->getKeyInfo()?->toXML($e);
179
180
        foreach ($this->getObjects() as $o) {
181
            $o->toXML($e);
182
        }
183
184
        return $e;
185
    }
186
}
187