1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\saml; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\SAML2\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Constants as C; |
10
|
|
|
use SimpleSAML\SAML2\XML\EncryptableElementTrait; |
11
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
12
|
|
|
use SimpleSAML\XML\ExtendableAttributesTrait; |
13
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
14
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
15
|
|
|
use SimpleSAML\XML\XsNamespace as NS; |
16
|
|
|
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend; |
17
|
|
|
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class representing SAML 2 Attribute. |
21
|
|
|
* |
22
|
|
|
* @package simplesamlphp/saml2 |
23
|
|
|
*/ |
24
|
|
|
class Attribute extends AbstractSamlElement implements |
25
|
|
|
EncryptableElementInterface, |
26
|
|
|
SchemaValidatableElementInterface |
27
|
|
|
{ |
28
|
|
|
use EncryptableElementTrait; |
|
|
|
|
29
|
|
|
use ExtendableAttributesTrait; |
30
|
|
|
use SchemaValidatableElementTrait; |
31
|
|
|
|
32
|
|
|
/** The namespace-attribute for the xs:anyAttribute element */ |
33
|
|
|
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialize an Attribute. |
38
|
|
|
* |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string|null $nameFormat |
41
|
|
|
* @param string|null $friendlyName |
42
|
|
|
* @param \SimpleSAML\SAML2\XML\saml\AttributeValue[] $attributeValue |
43
|
|
|
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttribute |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
protected string $name, |
47
|
|
|
protected ?string $nameFormat = null, |
48
|
|
|
protected ?string $friendlyName = null, |
49
|
|
|
protected array $attributeValue = [], |
50
|
|
|
array $namespacedAttribute = [], |
51
|
|
|
) { |
52
|
|
|
Assert::notWhitespaceOnly($name, 'Cannot specify an empty name for an Attribute.'); |
53
|
|
|
Assert::nullOrValidURI($nameFormat); |
54
|
|
|
Assert::nullOrNotWhitespaceOnly($friendlyName, 'FriendlyName cannot be an empty string.'); |
55
|
|
|
Assert::maxCount($attributeValue, C::UNBOUNDED_LIMIT); |
56
|
|
|
Assert::allIsInstanceOf($attributeValue, AttributeValue::class, 'Invalid AttributeValue.'); |
57
|
|
|
|
58
|
|
|
if ($nameFormat === C::NAMEFORMAT_URI) { |
59
|
|
|
Assert::validURI( |
60
|
|
|
$name, |
61
|
|
|
sprintf("Attribute name `%s` does not match its declared format `%s`", $name, $nameFormat), |
62
|
|
|
); |
63
|
|
|
} elseif ($nameFormat === C::NAMEFORMAT_BASIC) { |
64
|
|
|
Assert::validNCName( |
65
|
|
|
$name, |
66
|
|
|
sprintf("Attribute name `%s` does not match its declared format `%s`", $name, $nameFormat), |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->setAttributesNS($namespacedAttribute); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Collect the value of the Name-property |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getName(): string |
80
|
|
|
{ |
81
|
|
|
return $this->name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Collect the value of the NameFormat-property |
87
|
|
|
* |
88
|
|
|
* @return string|null |
89
|
|
|
*/ |
90
|
|
|
public function getNameFormat(): ?string |
91
|
|
|
{ |
92
|
|
|
return $this->nameFormat; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Collect the value of the FriendlyName-property |
98
|
|
|
* |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
|
|
public function getFriendlyName(): ?string |
102
|
|
|
{ |
103
|
|
|
return $this->friendlyName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Collect the value of the attributeValues-property |
109
|
|
|
* |
110
|
|
|
* @return \SimpleSAML\SAML2\XML\saml\AttributeValue[] |
111
|
|
|
*/ |
112
|
|
|
public function getAttributeValues(): array |
113
|
|
|
{ |
114
|
|
|
return $this->attributeValue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
public function getEncryptionBackend(): ?EncryptionBackend |
119
|
|
|
{ |
120
|
|
|
// return the encryption backend you want to use, |
121
|
|
|
// or null if you are fine with the default |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Convert XML into a Attribute |
128
|
|
|
* |
129
|
|
|
* @param \DOMElement $xml The XML element we should load |
130
|
|
|
* @return static |
131
|
|
|
* |
132
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
133
|
|
|
* if the qualified name of the supplied element is wrong |
134
|
|
|
* @throws \SimpleSAML\XML\Exception\MissingAttributeException |
135
|
|
|
* if the supplied element is missing one of the mandatory attributes |
136
|
|
|
*/ |
137
|
|
|
public static function fromXML(DOMElement $xml): static |
138
|
|
|
{ |
139
|
|
|
Assert::same($xml->localName, 'Attribute', InvalidDOMElementException::class); |
140
|
|
|
Assert::same($xml->namespaceURI, Attribute::NS, InvalidDOMElementException::class); |
141
|
|
|
|
142
|
|
|
return new static( |
143
|
|
|
self::getAttribute($xml, 'Name'), |
144
|
|
|
self::getOptionalAttribute($xml, 'NameFormat', null), |
145
|
|
|
self::getOptionalAttribute($xml, 'FriendlyName', null), |
146
|
|
|
AttributeValue::getChildrenOfClass($xml), |
147
|
|
|
self::getAttributesNSFromXML($xml), |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Convert this Attribute to XML. |
154
|
|
|
* |
155
|
|
|
* @param \DOMElement|null $parent The element we should append this Attribute to. |
156
|
|
|
* @return \DOMElement |
157
|
|
|
*/ |
158
|
|
|
public function toXML(?DOMElement $parent = null): DOMElement |
159
|
|
|
{ |
160
|
|
|
$e = $this->instantiateParentElement($parent); |
161
|
|
|
$e->setAttribute('Name', $this->getName()); |
162
|
|
|
|
163
|
|
|
if ($this->getNameFormat() !== null) { |
164
|
|
|
$e->setAttribute('NameFormat', $this->getNameFormat()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($this->getFriendlyName() !== null) { |
168
|
|
|
$e->setAttribute('FriendlyName', $this->getFriendlyName()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
foreach ($this->getAttributesNS() as $attr) { |
172
|
|
|
$attr->toXML($e); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
foreach ($this->getAttributeValues() as $av) { |
176
|
|
|
$av->toXML($e); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $e; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|