Passed
Push — master ( e61fad...cc1ca4 )
by Tim
02:24
created

BaseID::getEncryptionBackend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Compat\ContainerSingleton;
10
use SimpleSAML\SAML2\Constants;
11
use SimpleSAML\SAML2\XML\IDNameQualifiersTrait;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\XMLStringElementTrait;
14
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
15
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
16
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
17
18
use function trim;
19
20
/**
21
 * SAML BaseID data type.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
class BaseID extends AbstractSamlElement implements BaseIdentifierInterface, EncryptableElementInterface
26
{
27
    use EncryptableElementTrait;
28
    use IDNameQualifiersTrait;
29
    use XMLStringElementTrait;
30
31
    /** @var string */
32
    public const LOCALNAME = 'BaseID';
33
34
    /** @var string */
35
    protected string $type;
36
37
38
    /**
39
     * Initialize a saml:BaseID from scratch
40
     *
41
     * @param string $type
42
     * @param string $value
43
     * @param string|null $NameQualifier
44
     * @param string|null $SPNameQualifier
45
     */
46
    protected function __construct(
47
        string $type,
48
        string $value,
49
        ?string $NameQualifier = null,
50
        ?string $SPNameQualifier = null
51
    ) {
52
        $this->setType($type);
53
        $this->setContent($value);
54
        $this->setNameQualifier($NameQualifier);
55
        $this->setSPNameQualifier($SPNameQualifier);
56
    }
57
58
59
    /**
60
     * Get the type of this BaseID (expressed in the xsi:type attribute).
61
     *
62
     * @return string
63
     */
64
    public function getType(): string
65
    {
66
        return $this->type;
67
    }
68
69
70
    /**
71
     * Set the type of this BaseID (in the xsi:type attribute)
72
     *
73
     * @param string $type
74
     */
75
    protected function setType(string $type): void
76
    {
77
        Assert::notWhitespaceOnly($type, 'The "xsi:type" attribute of an identifier cannot be empty.');
78
79
        $this->type = $type;
80
    }
81
82
83
    /**
84
     * Validate the content of the element.
85
     *
86
     * @param string $content  The value to go in the XML textContent
87
     * @throws \Exception on failure
88
     * @return void
89
     */
90
    protected function validateContent(string $content): void
91
    {
92
        Assert::notWhitespaceOnly($content);
93
    }
94
95
96
    public function getBlacklistedAlgorithms(): ?array
97
    {
98
        $container = ContainerSingleton::getInstance();
99
        return $container->getBlacklistedEncryptionAlgorithms();
100
    }
101
102
103
    public function getEncryptionBackend(): ?EncryptionBackend
104
    {
105
        // return the encryption backend you want to use,
106
        // or null if you are fine with the default
107
        return null;
108
    }
109
110
111
    /**
112
     * Convert XML into an BaseID
113
     *
114
     * @param \DOMElement $xml The XML element we should load
115
     *
116
     * @return \SimpleSAML\SAML2\XML\saml\BaseID
117
     *
118
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
119
     */
120
    public static function fromXML(DOMElement $xml): object
121
    {
122
        Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
123
        Assert::notNull($xml->namespaceURI, InvalidDOMElementException::class);
124
        Assert::same($xml->namespaceURI, BaseID::NS, InvalidDOMElementException::class);
125
        Assert::true(
126
            $xml->hasAttributeNS(Constants::NS_XSI, 'type'),
127
            'Missing required xsi:type in <saml:BaseID> element.',
128
            InvalidDOMElementException::class
129
        );
130
131
        $type = $xml->getAttributeNS(Constants::NS_XSI, 'type');
132
133
        return new self(
134
            $type,
135
            trim($xml->textContent),
136
            self::getAttribute($xml, 'NameQualifier', null),
137
            self::getAttribute($xml, 'SPNameQualifier', null)
138
        );
139
    }
140
141
142
    /**
143
     * Convert this BaseID to XML.
144
     *
145
     * @param \DOMElement $parent The element we are converting to XML.
146
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
147
     */
148
    public function toXML(DOMElement $parent = null): DOMElement
149
    {
150
        $element = $this->instantiateParentElement($parent);
151
152
        if ($this->NameQualifier !== null) {
153
            $element->setAttribute('NameQualifier', $this->NameQualifier);
154
        }
155
156
        if ($this->SPNameQualifier !== null) {
157
            $element->setAttribute('SPNameQualifier', $this->SPNameQualifier);
158
        }
159
160
        $element->textContent = $this->content;
161
162
        $element->setAttributeNS(Constants::NS_XSI, 'xsi:type', $this->type);
163
164
        return $element;
165
    }
166
}
167