SignedInfo::getReferences()   A
last analyzed

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\ds;
6
7
use DOMElement;
8
use SimpleSAML\XML\Constants as C;
9
use SimpleSAML\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
use SimpleSAML\XMLSecurity\Assert\Assert;
16
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
17
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface;
18
use SimpleSAML\XMLSecurity\XML\CanonicalizableElementTrait;
19
20
use function array_pop;
21
use function strval;
22
23
/**
24
 * Class representing a ds:SignedInfo element.
25
 *
26
 * @package simplesamlphp/xml-security
27
 */
28
final class SignedInfo extends AbstractDsElement implements
29
    CanonicalizableElementInterface,
30
    SchemaValidatableElementInterface
31
{
32
    use CanonicalizableElementTrait;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\XMLSecurity\X...nicalizableElementTrait requires the property $ownerDocument which is not provided by SimpleSAML\XMLSecurity\XML\ds\SignedInfo.
Loading history...
33
    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\SignedInfo: $message, $line
Loading history...
34
35
36
    /*
37
     * @var DOMElement
38
     */
39
    protected ?DOMElement $xml = null;
40
41
42
    /**
43
     * Initialize a SignedInfo.
44
     *
45
     * @param \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod $canonicalizationMethod
46
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod $signatureMethod
47
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
48
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $Id
49
     */
50
    public function __construct(
51
        protected CanonicalizationMethod $canonicalizationMethod,
52
        protected SignatureMethod $signatureMethod,
53
        protected array $references,
54
        protected ?IDValue $Id = null,
55
    ) {
56
        Assert::maxCount($references, C::UNBOUNDED_LIMIT);
57
        Assert::allIsInstanceOf($references, Reference::class, InvalidArgumentException::class);
58
    }
59
60
61
    /**
62
     * Collect the value of the canonicalizationMethod-property
63
     *
64
     * @return \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod
65
     */
66
    public function getCanonicalizationMethod(): CanonicalizationMethod
67
    {
68
        return $this->canonicalizationMethod;
69
    }
70
71
72
    /**
73
     * Collect the value of the signatureMethod-property
74
     *
75
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod
76
     */
77
    public function getSignatureMethod(): SignatureMethod
78
    {
79
        return $this->signatureMethod;
80
    }
81
82
83
    /**
84
     * Collect the value of the references-property
85
     *
86
     * @return \SimpleSAML\XMLSecurity\XML\ds\Reference[]
87
     */
88
    public function getReferences(): array
89
    {
90
        return $this->references;
91
    }
92
93
94
    /**
95
     * Collect the value of the Id-property
96
     *
97
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
98
     */
99
    public function getId(): ?IDValue
100
    {
101
        return $this->Id;
102
    }
103
104
105
    /**
106
     * @inheritDoc
107
     */
108
    protected function getOriginalXML(): DOMElement
109
    {
110
        if ($this->xml !== null) {
111
            return $this->xml;
112
        }
113
114
        return $this->toXML();
115
    }
116
117
118
    /**
119
     * Convert XML into a SignedInfo instance
120
     *
121
     * @param \DOMElement $xml The XML element we should load
122
     * @return static
123
     *
124
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
125
     *   If the qualified name of the supplied element is wrong
126
     */
127
    public static function fromXML(DOMElement $xml): static
128
    {
129
        Assert::same($xml->localName, 'SignedInfo', InvalidDOMElementException::class);
130
        Assert::same($xml->namespaceURI, SignedInfo::NS, InvalidDOMElementException::class);
131
132
        $canonicalizationMethod = CanonicalizationMethod::getChildrenOfClass($xml);
133
        Assert::minCount(
134
            $canonicalizationMethod,
135
            1,
136
            'A ds:SignedInfo element must contain exactly one ds:CanonicalizationMethod',
137
            MissingElementException::class,
138
        );
139
        Assert::maxCount(
140
            $canonicalizationMethod,
141
            1,
142
            'A ds:SignedInfo element must contain exactly one ds:CanonicalizationMethod',
143
            TooManyElementsException::class,
144
        );
145
146
        $signatureMethod = SignatureMethod::getChildrenOfClass($xml);
147
        Assert::minCount(
148
            $signatureMethod,
149
            1,
150
            'A ds:SignedInfo element must contain exactly one ds:SignatureMethod',
151
            MissingElementException::class,
152
        );
153
        Assert::maxCount(
154
            $signatureMethod,
155
            1,
156
            'A ds:SignedInfo element must contain exactly one ds:SignatureMethod',
157
            TooManyElementsException::class,
158
        );
159
160
        $references = Reference::getChildrenOfClass($xml);
161
        Assert::minCount(
162
            $references,
163
            1,
164
            'A ds:SignedInfo element must contain at least one ds:Reference',
165
            MissingElementException::class,
166
        );
167
168
        $signedInfo = new static(
169
            array_pop($canonicalizationMethod),
170
            array_pop($signatureMethod),
171
            $references,
172
            self::getOptionalAttribute($xml, 'Id', IDValue::class, null),
173
        );
174
175
        $signedInfo->xml = $xml;
176
        return $signedInfo;
177
    }
178
179
180
    /**
181
     * Convert this SignedInfo element to XML.
182
     *
183
     * @param \DOMElement|null $parent The element we should append this SignedInfo element to.
184
     * @return \DOMElement
185
     */
186
    public function toXML(?DOMElement $parent = null): DOMElement
187
    {
188
        $e = $this->instantiateParentElement($parent);
189
190
        if ($this->getId() !== null) {
191
            $e->setAttribute('Id', strval($this->getId()));
192
        }
193
194
        $this->getCanonicalizationMethod()->toXML($e);
195
        $this->getSignatureMethod()->toXML($e);
196
197
        foreach ($this->getReferences() as $ref) {
198
            $ref->toXML($e);
199
        }
200
201
        return $e;
202
    }
203
}
204