From bc31e1add33accff770911d21f276ec761a99d6b Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 25 Jan 2024 18:17:38 +0100 Subject: [PATCH 1/3] Fix inheritance - No relation between BaseID and NameID --- src/SAML2/XML/saml/AbstractBaseID.php | 1 + src/SAML2/XML/saml/AbstractBaseIDType.php | 25 ++------------ src/SAML2/XML/saml/IDNameQualifiersTrait.php | 34 ++++++++++++++++++++ src/SAML2/XML/saml/NameIDType.php | 21 ++++++++---- 4 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 src/SAML2/XML/saml/IDNameQualifiersTrait.php diff --git a/src/SAML2/XML/saml/AbstractBaseID.php b/src/SAML2/XML/saml/AbstractBaseID.php index bf3811a47..84a69ea2e 100644 --- a/src/SAML2/XML/saml/AbstractBaseID.php +++ b/src/SAML2/XML/saml/AbstractBaseID.php @@ -146,6 +146,7 @@ public function toXML(DOMElement $parent = null): DOMElement return $e; } + public function getBlacklistedAlgorithms(): ?array { $container = ContainerSingleton::getInstance(); diff --git a/src/SAML2/XML/saml/AbstractBaseIDType.php b/src/SAML2/XML/saml/AbstractBaseIDType.php index c4b880ef5..0305291c4 100644 --- a/src/SAML2/XML/saml/AbstractBaseIDType.php +++ b/src/SAML2/XML/saml/AbstractBaseIDType.php @@ -14,6 +14,9 @@ */ abstract class AbstractBaseIDType extends AbstractSamlElement implements BaseIdentifierInterface { + use IDNameQualifiersTrait; + + /** * Initialize a saml:BaseIDAbstractType from scratch * @@ -33,28 +36,6 @@ protected function __construct( } - /** - * Collect the value of the NameQualifier-property - * - * @return string|null - */ - public function getNameQualifier(): ?string - { - return $this->nameQualifier; - } - - - /** - * Collect the value of the SPNameQualifier-property - * - * @return string|null - */ - public function getSPNameQualifier(): ?string - { - return $this->spNameQualifier; - } - - /** * Convert this BaseID to XML. * diff --git a/src/SAML2/XML/saml/IDNameQualifiersTrait.php b/src/SAML2/XML/saml/IDNameQualifiersTrait.php new file mode 100644 index 000000000..c573f7657 --- /dev/null +++ b/src/SAML2/XML/saml/IDNameQualifiersTrait.php @@ -0,0 +1,34 @@ +nameQualifier; + } + + + /** + * Collect the value of the SPNameQualifier-property + * + * @return string|null + */ + public function getSPNameQualifier(): ?string + { + return $this->spNameQualifier; + } +} diff --git a/src/SAML2/XML/saml/NameIDType.php b/src/SAML2/XML/saml/NameIDType.php index 87c6511c2..7f55061f6 100644 --- a/src/SAML2/XML/saml/NameIDType.php +++ b/src/SAML2/XML/saml/NameIDType.php @@ -14,8 +14,9 @@ * @package simplesamlphp/saml2 */ -abstract class NameIDType extends AbstractBaseIDType +abstract class NameIDType extends AbstractSamlElement implements IdentifierInterface { + use IDNameQualifiersTrait; use StringElementTrait; @@ -30,16 +31,16 @@ abstract class NameIDType extends AbstractBaseIDType */ protected function __construct( string $value, - ?string $nameQualifier = null, - ?string $spNameQualifier = null, + protected ?string $nameQualifier = null, + protected ?string $spNameQualifier = null, protected ?string $format = null, protected ?string $spProvidedID = null, ) { + Assert::nullOrNotWhitespaceOnly($nameQualifier); + Assert::nullOrNotWhitespaceOnly($spNameQualifier); Assert::nullOrValidURI($format); // Covers the empty string Assert::nullOrNotWhitespaceOnly($spProvidedID); - parent::__construct($nameQualifier, $spNameQualifier); - $this->setContent($value); } @@ -87,7 +88,15 @@ protected function validateContent(string $content): void */ public function toXML(DOMElement $parent = null): DOMElement { - $e = parent::toXML($parent); + $e = $this->instantiateParentElement($parent); + + if ($this->getNameQualifier() !== null) { + $e->setAttribute('NameQualifier', $this->getNameQualifier()); + } + + if ($this->getSPNameQualifier() !== null) { + $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier()); + } if ($this->getFormat() !== null) { $e->setAttribute('Format', $this->getFormat()); From 9d2b4d8c344d7adf28827175978aad8c5621a3d7 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 25 Jan 2024 18:30:56 +0100 Subject: [PATCH 2/3] Code de-dup --- src/SAML2/XML/saml/Issuer.php | 23 ----------------------- src/SAML2/XML/saml/NameID.php | 23 ----------------------- src/SAML2/XML/saml/NameIDType.php | 23 +++++++++++++++++++++++ 3 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/SAML2/XML/saml/Issuer.php b/src/SAML2/XML/saml/Issuer.php index 976193f8f..fafdbcd3d 100644 --- a/src/SAML2/XML/saml/Issuer.php +++ b/src/SAML2/XML/saml/Issuer.php @@ -64,27 +64,4 @@ public function __construct( parent::__construct($value, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID); } - - - /** - * Convert XML into an Issuer - * - * @param \DOMElement $xml The XML element we should load - * @return static - * - * @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, 'Issuer', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, Issuer::NS, InvalidDOMElementException::class); - - $Format = self::getOptionalAttribute($xml, 'Format', null); - $SPProvidedID = self::getOptionalAttribute($xml, 'SPProvidedID', null); - $NameQualifier = self::getOptionalAttribute($xml, 'NameQualifier', null); - $SPNameQualifier = self::getOptionalAttribute($xml, 'SPNameQualifier', null); - - return new static($xml->textContent, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID); - } } diff --git a/src/SAML2/XML/saml/NameID.php b/src/SAML2/XML/saml/NameID.php index f94ebb712..d13fa778f 100644 --- a/src/SAML2/XML/saml/NameID.php +++ b/src/SAML2/XML/saml/NameID.php @@ -47,29 +47,6 @@ public function __construct( } - /** - * Convert XML into an NameID - * - * @param \DOMElement $xml The XML element we should load - * @return static - * - * @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, 'NameID', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, NameID::NS, InvalidDOMElementException::class); - - $NameQualifier = self::getOptionalAttribute($xml, 'NameQualifier', null); - $SPNameQualifier = self::getOptionalAttribute($xml, 'SPNameQualifier', null); - $Format = self::getOptionalAttribute($xml, 'Format', null); - $SPProvidedID = self::getOptionalAttribute($xml, 'SPProvidedID', null); - - return new static($xml->textContent, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID); - } - - public function getBlacklistedAlgorithms(): ?array { $container = ContainerSingleton::getInstance(); diff --git a/src/SAML2/XML/saml/NameIDType.php b/src/SAML2/XML/saml/NameIDType.php index 7f55061f6..935b36d06 100644 --- a/src/SAML2/XML/saml/NameIDType.php +++ b/src/SAML2/XML/saml/NameIDType.php @@ -80,6 +80,29 @@ protected function validateContent(string $content): void } + /** + * Convert XML into an NameID + * + * @param \DOMElement $xml The XML element we should load + * @return static + * + * @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, static::getLocalName(), InvalidDOMElementException::class); + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); + + $NameQualifier = self::getOptionalAttribute($xml, 'NameQualifier', null); + $SPNameQualifier = self::getOptionalAttribute($xml, 'SPNameQualifier', null); + $Format = self::getOptionalAttribute($xml, 'Format', null); + $SPProvidedID = self::getOptionalAttribute($xml, 'SPProvidedID', null); + + return new static($xml->textContent, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID); + } + + /** * Convert this NameIDType to XML. * From 6cba163854a89bb62958738aa05b6056a7f0491d Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 25 Jan 2024 18:59:47 +0100 Subject: [PATCH 3/3] Fix codesniffer issues --- src/SAML2/XML/saml/Issuer.php | 2 -- src/SAML2/XML/saml/NameID.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/SAML2/XML/saml/Issuer.php b/src/SAML2/XML/saml/Issuer.php index fafdbcd3d..92512d3a6 100644 --- a/src/SAML2/XML/saml/Issuer.php +++ b/src/SAML2/XML/saml/Issuer.php @@ -4,10 +4,8 @@ namespace SimpleSAML\SAML2\XML\saml; -use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Constants as C; -use SimpleSAML\XML\Exception\InvalidDOMElementException; /** * Class representing the saml:Issuer element. diff --git a/src/SAML2/XML/saml/NameID.php b/src/SAML2/XML/saml/NameID.php index d13fa778f..1c9da4fe2 100644 --- a/src/SAML2/XML/saml/NameID.php +++ b/src/SAML2/XML/saml/NameID.php @@ -4,11 +4,9 @@ namespace SimpleSAML\SAML2\XML\saml; -use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Compat\ContainerSingleton; use SimpleSAML\SAML2\Exception\ArrayValidationException; -use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XMLSecurity\Backend\EncryptionBackend; use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface; use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;