Skip to content

Commit decaf73

Browse files
committed
Refactor md:Extensions / samlp:Extensions to use the generic ExtendableElementTrait and filter saml-defined namespaces from it's content
1 parent c2ab36b commit decaf73

14 files changed

+63
-70
lines changed

phpstan-baseline.neon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,19 @@ parameters:
181181
path: src/XML/md/AbstractSignedMdElement.php
182182

183183
-
184-
message: '#^Call to an undefined method SimpleSAML\\XML\\AbstractElement\:\:getList\(\)\.$#'
184+
message: '#^Call to an undefined method SimpleSAML\\XML\\AbstractElement\:\:getElements\(\)\.$#'
185185
identifier: method.notFound
186186
count: 1
187187
path: src/XML/md/ContactPerson.php
188188

189189
-
190-
message: '#^Call to an undefined method SimpleSAML\\XML\\AbstractElement\:\:getList\(\)\.$#'
190+
message: '#^Call to an undefined method SimpleSAML\\XML\\AbstractElement\:\:getElements\(\)\.$#'
191191
identifier: method.notFound
192192
count: 1
193193
path: src/XML/md/EntitiesDescriptor.php
194194

195195
-
196-
message: '#^Call to an undefined method SimpleSAML\\XML\\AbstractElement\:\:getList\(\)\.$#'
196+
message: '#^Call to an undefined method SimpleSAML\\XML\\AbstractElement\:\:getElements\(\)\.$#'
197197
identifier: method.notFound
198198
count: 1
199199
path: src/XML/md/Organization.php

src/XML/ExtensionsTrait.php

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
namespace SimpleSAML\SAML2\XML;
66

77
use DOMElement;
8-
use SimpleSAML\SAML2\Assert\Assert;
9-
use SimpleSAML\SAML2\Constants as C;
10-
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11-
use SimpleSAML\XML\ElementInterface;
12-
13-
use function in_array;
8+
use SimpleSAML\XML\ExtendableElementTrait;
149

1510
/**
1611
* Trait grouping common functionality for elements implementing ExtensionType.
@@ -19,8 +14,7 @@
1914
*/
2015
trait ExtensionsTrait
2116
{
22-
/** @var \SimpleSAML\XML\SerializableElementInterface[] */
23-
protected array $extensions = [];
17+
use ExtendableElementTrait;
2418

2519

2620
/**
@@ -30,52 +24,19 @@ trait ExtensionsTrait
3024
*/
3125
public function __construct(array $extensions)
3226
{
33-
Assert::maxCount($extensions, C::UNBOUNDED_LIMIT);
34-
Assert::allIsInstanceOf($extensions, ElementInterface::class);
35-
36-
foreach ($extensions as $extension) {
37-
/** @var \SimpleSAML\XML\AbstractElement $extension */
38-
$namespace = $extension->getNamespaceURI();
39-
40-
Assert::notNull(
41-
$namespace,
42-
'Extensions MUST NOT include global (non-namespace-qualified) elements.',
43-
ProtocolViolationException::class,
44-
);
45-
Assert::true(
46-
!in_array($namespace, [C::NS_SAML, C::NS_SAMLP], true),
47-
'Extensions MUST NOT include any SAML-defined namespace elements.',
48-
ProtocolViolationException::class,
49-
);
50-
}
51-
52-
/**
53-
* Set an array with all extensions present.
54-
*/
55-
$this->extensions = $extensions;
56-
}
57-
58-
59-
/**
60-
* Get an array with all extensions present.
61-
*
62-
* @return \SimpleSAML\XML\SerializableElementInterface[]
63-
*/
64-
public function getList(): array
65-
{
66-
return $this->extensions;
27+
$this->setElements($extensions);
6728
}
6829

6930

7031
/**
7132
*/
7233
public function isEmptyElement(): bool
7334
{
74-
if (empty($this->getList())) {
35+
if (empty($this->getElements())) {
7536
return true;
7637
}
7738

78-
foreach ($this->getList() as $extension) {
39+
foreach ($this->getElements() as $extension) {
7940
if ($extension->isEmptyElement() === false) {
8041
return false;
8142
}
@@ -96,7 +57,7 @@ public function toXML(?DOMElement $parent = null): DOMElement
9657
$e = $this->instantiateParentElement($parent);
9758

9859
if (!$this->isEmptyElement()) {
99-
foreach ($this->getList() as $extension) {
60+
foreach ($this->getElements() as $extension) {
10061
if (!$extension->isEmptyElement()) {
10162
$extension->toXML($e);
10263
}

src/XML/md/AbstractMetadataDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(
4444
?Extensions $extensions = null,
4545
) {
4646
if ($extensions !== null) {
47-
$exts = $extensions->getList();
47+
$exts = $extensions->getElements();
4848

4949
/**
5050
* MDUI 2.1: this element MUST NOT appear more than once within a given <md:Extensions> element.

src/XML/md/ContactPerson.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public function toArray(): array
388388
'SurName' => $this->getSurName()?->getContent()->getValue(),
389389
'EmailAddress' => [],
390390
'TelephoneNumber' => [],
391-
'Extensions' => $this->Extensions?->getList(),
391+
'Extensions' => $this->Extensions?->getElements(),
392392
'attributes' => [],
393393
];
394394

src/XML/md/EntitiesDescriptor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ public function __construct(
7575
* manner, descendant <md:EntitiesDescriptor> and <md:EntityDescriptor> elements MUST
7676
* NOT contain a <mdrpi:RegistrationInfo> element in their <md:Extensions> element.
7777
*/
78-
$toplevel_regInfo = array_values(array_filter($extensions->getList(), function ($ext) {
78+
$toplevel_regInfo = array_values(array_filter($extensions->getElements(), function ($ext) {
7979
return $ext instanceof RegistrationInfo;
8080
}));
8181

8282
/**
8383
* The <mdrpi:PublicationInfo> element SHOULD only be used on the root element of a metadata document.
8484
*/
85-
$toplevel_pubInfo = array_values(array_filter($extensions->getList(), function ($ext) {
85+
$toplevel_pubInfo = array_values(array_filter($extensions->getElements(), function ($ext) {
8686
return $ext instanceof PublicationInfo;
8787
}));
8888

@@ -94,7 +94,7 @@ public function __construct(
9494
* When used in this manner, descendant <md:EntitiesDescriptor> and <md:EntityDescriptor>
9595
* elements MUST NOT contain a <mdrpi:PublicationPath> element in their <md:Extensions> element.
9696
*/
97-
$toplevel_pubPath = array_values(array_filter($extensions->getList(), function ($ext) {
97+
$toplevel_pubPath = array_values(array_filter($extensions->getElements(), function ($ext) {
9898
return $ext instanceof PublicationPath;
9999
}));
100100

@@ -156,7 +156,7 @@ private function getRecursiveExtensions(EntityDescriptor|EntitiesDescriptor $des
156156
{
157157
$extensions = [];
158158
if ($descriptor->getExtensions() !== null) {
159-
$extensions = $descriptor->getExtensions()->getList();
159+
$extensions = $descriptor->getExtensions()->getElements();
160160

161161
if ($descriptor instanceof EntitiesDescriptor) {
162162
$eds = array_merge($descriptor->getEntitiesDescriptors(), $descriptor->getEntityDescriptors());

src/XML/md/Extensions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use SimpleSAML\XML\SchemaValidatableElementInterface;
2828
use SimpleSAML\XML\SchemaValidatableElementTrait;
2929
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
30+
use SimpleSAML\XMLSchema\XML\Constants\NS;
3031

3132
use function array_key_exists;
3233

@@ -41,6 +42,21 @@ final class Extensions extends AbstractMdElement implements SchemaValidatableEle
4142
use SchemaValidatableElementTrait;
4243

4344

45+
/** The namespace-attribute for the xs:any element */
46+
public const string XS_ANY_ELT_NAMESPACE = NS::OTHER;
47+
48+
/**
49+
* The exclusions for the xs:any element
50+
*
51+
* @var array<int, array<int, string>>
52+
*/
53+
public const array XS_ANY_ELT_EXCLUSIONS = [
54+
['urn:oasis:names:tc:SAML:2.0:assertion', '*'],
55+
['urn:oasis:names:tc:SAML:2.0:metadata', '*'],
56+
['urn:oasis:names:tc:SAML:2.0:protocol', '*'],
57+
];
58+
59+
4460
/**
4561
* Create an Extensions object from its md:Extensions XML representation.
4662
*

src/XML/md/Organization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function toArray(): array
307307
'OrganizationName' => [],
308308
'OrganizationDisplayName' => [],
309309
'OrganizationURL' => [],
310-
'Extensions' => $this->getExtensions()?->getList(),
310+
'Extensions' => $this->getExtensions()?->getElements(),
311311
'attributes' => [],
312312
];
313313

src/XML/saml/AbstractSubjectConfirmationData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ abstract class AbstractSubjectConfirmationData extends AbstractAnyType
4545
*/
4646
public const array XS_ANY_ATTR_EXCLUSIONS = [
4747
['urn:oasis:names:tc:SAML:2.0:assertion', '*'],
48+
['urn:oasis:names:tc:SAML:2.0:metadata', '*'],
4849
['urn:oasis:names:tc:SAML:2.0:protocol', '*'],
4950
];
5051

src/XML/saml/Attribute.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Attribute extends AbstractSamlElement implements
4747
*/
4848
public const array XS_ANY_ATTR_EXCLUSIONS = [
4949
['urn:oasis:names:tc:SAML:2.0:assertion', '*'],
50+
['urn:oasis:names:tc:SAML:2.0:metadata', '*'],
5051
['urn:oasis:names:tc:SAML:2.0:protocol', '*'],
5152
];
5253

src/XML/samlp/Extensions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SimpleSAML\XML\SchemaValidatableElementInterface;
1313
use SimpleSAML\XML\SchemaValidatableElementTrait;
1414
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15+
use SimpleSAML\XMLSchema\XML\Constants\NS;
1516

1617
/**
1718
* Class for handling SAML2 extensions.
@@ -24,6 +25,21 @@ final class Extensions extends AbstractSamlpElement implements SchemaValidatable
2425
use SchemaValidatableElementTrait;
2526

2627

28+
/** The namespace-attribute for the xs:any element */
29+
public const string XS_ANY_ELT_NAMESPACE = NS::OTHER;
30+
31+
/**
32+
* The exclusions for the xs:any element
33+
*
34+
* @var array<int, array<int, string>>
35+
*/
36+
public const array XS_ANY_ELT_EXCLUSIONS = [
37+
['urn:oasis:names:tc:SAML:2.0:assertion', '*'],
38+
['urn:oasis:names:tc:SAML:2.0:metadata', '*'],
39+
['urn:oasis:names:tc:SAML:2.0:protocol', '*'],
40+
];
41+
42+
2743
/**
2844
* Create an Extensions object from its samlp:Extensions XML representation.
2945
*

0 commit comments

Comments
 (0)