1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\WSSecurity\XML\wsse; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\WSSecurity\Constants as C; |
10
|
|
|
use SimpleSAML\XML\Attribute as XMLAttribute; |
11
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
12
|
|
|
use SimpleSAML\XML\Exception\MissingElementException; |
13
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
14
|
|
|
use SimpleSAML\XML\ExtendableAttributesTrait; |
15
|
|
|
use SimpleSAML\XML\ExtendableElementTrait; |
16
|
|
|
use SimpleSAML\XML\XsNamespace as NS; |
17
|
|
|
|
18
|
|
|
use function array_pop; |
19
|
|
|
use function array_unshift; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class defining the UsernameTokenType element |
23
|
|
|
* |
24
|
|
|
* @package simplesamlphp/ws-security |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractUsernameTokenType extends AbstractWsseElement |
27
|
|
|
{ |
28
|
|
|
use ExtendableAttributesTrait; |
29
|
|
|
use ExtendableElementTrait; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** The namespace-attribute for the xs:anyAttribute element */ |
32
|
|
|
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER; |
33
|
|
|
|
34
|
|
|
/** The exclusions for the xs:anyAttribute element */ |
35
|
|
|
public const XS_ANY_ATTR_EXCLUSIONS = [ |
36
|
|
|
['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Id'], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** The namespace-attribute for the xs:any element */ |
40
|
|
|
public const XS_ANY_ELT_NAMESPACE = NS::ANY; |
41
|
|
|
|
42
|
|
|
/** The exclusions for the xs:any element */ |
43
|
|
|
public const XS_ANY_ELT_EXCLUSIONS = [ |
44
|
|
|
['http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Username'], |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* AbstractUsernameTokenType constructor |
50
|
|
|
* |
51
|
|
|
* @param \SimpleSAML\WSSecurity\XML\wsse\Username $username |
52
|
|
|
* @param string|null $Id |
53
|
|
|
* @param \SimpleSAML\XML\SerializableElementInterface[] $children |
54
|
|
|
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes |
55
|
|
|
*/ |
56
|
|
|
final public function __construct( |
57
|
|
|
protected Username $username, |
58
|
|
|
protected ?string $Id = null, |
59
|
|
|
array $children = [], |
60
|
|
|
array $namespacedAttributes = [], |
61
|
|
|
) { |
62
|
|
|
Assert::nullOrValidNCName($Id); |
63
|
|
|
|
64
|
|
|
$this->setElements($children); |
65
|
|
|
$this->setAttributesNS($namespacedAttributes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
|
|
public function getId(): ?string |
73
|
|
|
{ |
74
|
|
|
return $this->Id; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \SimpleSAML\WSSecurity\XML\wsse\Username |
80
|
|
|
*/ |
81
|
|
|
public function getUsername(): Username |
82
|
|
|
{ |
83
|
|
|
return $this->username; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create an instance of this object from its XML representation. |
89
|
|
|
* |
90
|
|
|
* @param \DOMElement $xml |
91
|
|
|
* @return static |
92
|
|
|
* |
93
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
94
|
|
|
* if the qualified name of the supplied element is wrong |
95
|
|
|
*/ |
96
|
|
|
public static function fromXML(DOMElement $xml): static |
97
|
|
|
{ |
98
|
|
|
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
99
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
100
|
|
|
|
101
|
|
|
$username = Username::getChildrenOfClass($xml); |
102
|
|
|
Assert::minCount($username, 1, MissingElementException::class); |
103
|
|
|
Assert::maxCount($username, 1, TooManyElementsException::class); |
104
|
|
|
|
105
|
|
|
return new static( |
106
|
|
|
array_pop($username), |
107
|
|
|
$xml->hasAttributeNS(C::NS_SEC_UTIL, 'Id') ? $xml->getAttributeNS(C::NS_SEC_UTIL, 'Id') : null, |
108
|
|
|
self::getChildElementsFromXML($xml), |
109
|
|
|
self::getAttributesNSFromXML($xml), |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Add this username token to an XML element. |
116
|
|
|
* |
117
|
|
|
* @param \DOMElement $parent The element we should append this username token to. |
118
|
|
|
* @return \DOMElement |
119
|
|
|
*/ |
120
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
121
|
|
|
{ |
122
|
|
|
$e = parent::instantiateParentElement($parent); |
123
|
|
|
|
124
|
|
|
$attributes = $this->getAttributesNS(); |
125
|
|
|
if ($this->getId() !== null) { |
126
|
|
|
$idAttr = new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', $this->getId()); |
127
|
|
|
array_unshift($attributes, $idAttr); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
foreach ($attributes as $attr) { |
131
|
|
|
$attr->toXML($e); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->getUsername()->toXML($e); |
135
|
|
|
|
136
|
|
|
/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */ |
137
|
|
|
foreach ($this->getElements() as $child) { |
138
|
|
|
if (!$child->isEmptyElement()) { |
139
|
|
|
$child->toXML($e); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $e; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|