-
Notifications
You must be signed in to change notification settings - Fork 135
/
AbstractBaseID.php
162 lines (135 loc) · 4.84 KB
/
AbstractBaseID.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace SimpleSAML\SAML2\XML\saml;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML2\Compat\ContainerSingleton;
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\Utils;
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
use function count;
use function explode;
/**
* SAML BaseID data type.
*
* @package simplesamlphp/saml2
*/
abstract class AbstractBaseID extends AbstractBaseIDType implements
EncryptableElementInterface,
ExtensionPointInterface
{
use EncryptableElementTrait;
use ExtensionPointTrait;
/** @var string */
public const LOCALNAME = 'BaseID';
/**
* Initialize a saml:BaseID from scratch
*
* @param string $type
* @param string|null $NameQualifier
* @param string|null $SPNameQualifier
*/
protected function __construct(
protected string $type,
?string $NameQualifier = null,
?string $SPNameQualifier = null,
) {
parent::__construct($NameQualifier, $SPNameQualifier);
$this->dataType = C::XMLENC_ELEMENT;
}
/**
* @inheritDoc
*/
public function getXsiType(): string
{
return $this->type;
}
/**
* Convert XML into an BaseID
*
* @param \DOMElement $xml The XML element we should load
* @return \SimpleSAML\SAML2\XML\saml\BaseIdentifierInterface
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* if the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
Assert::true(
$xml->hasAttributeNS(C::NS_XSI, 'type'),
'Missing required xsi:type in <saml:BaseID> element.',
SchemaViolationException::class,
);
$type = $xml->getAttributeNS(C::NS_XSI, 'type');
Assert::validQName($type, SchemaViolationException::class);
// first, try to resolve the type to a full namespaced version
$qname = explode(':', $type, 2);
if (count($qname) === 2) {
list($prefix, $element) = $qname;
} else {
$prefix = null;
list($element) = $qname;
}
$ns = $xml->lookupNamespaceUri($prefix);
$type = ($ns === null) ? $element : implode(':', [$ns, $element]);
// now check if we have a handler registered for it
$handler = Utils::getContainer()->getExtensionHandler($type);
if ($handler === null) {
// we don't have a handler, proceed with unknown identifier
return new UnknownID(
new Chunk($xml),
$type,
self::getAttribute($xml, 'NameQualifier', null),
self::getAttribute($xml, 'SPNameQualifier', null),
);
}
Assert::subclassOf(
$handler,
AbstractBaseID::class,
'Elements implementing BaseID must extend \SimpleSAML\SAML2\XML\saml\AbstractBaseID.',
);
return $handler::fromXML($xml);
}
/**
* Convert this BaseID to XML.
*
* @param \DOMElement $parent The element we are converting to XML.
* @return \DOMElement The XML element after adding the data corresponding to this BaseID.
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::toXML($parent);
/** @psalm-var \DOMDocument $e->ownerDocument */
$xsiType = $e->ownerDocument->createAttributeNS(C::NS_XSI, 'xsi:type');
$xsiType->value = $this->getXsiType();
$e->setAttributeNodeNS($xsiType);
$e->setAttribute('xmlns:' . static::getXsiTypePrefix(), static::getXsiTypeNamespaceURI());
if ($this->getNameQualifier() !== null) {
$e->setAttribute('NameQualifier', $this->getNameQualifier());
}
if ($this->getSPNameQualifier() !== null) {
$e->setAttribute('SPNameQualifier', $this->getSPNameQualifier());
}
return $e;
}
public function getBlacklistedAlgorithms(): ?array
{
$container = ContainerSingleton::getInstance();
return $container->getBlacklistedEncryptionAlgorithms();
}
public function getEncryptionBackend(): ?EncryptionBackend
{
// return the encryption backend you want to use,
// or null if you are fine with the default
return null;
}
}