From b88f13d068cde8223fa6911f524c5f6098027db5 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Fri, 17 Jan 2025 12:48:04 +0100 Subject: [PATCH] Migrate to value types --- composer.json | 2 +- src/Exception/ProtocolViolationException.php | 29 +++++++++++ src/XML/ds/AbstractDSAKeyValueType.php | 3 +- src/XML/ds/AbstractKeyInfoType.php | 12 +++-- src/XML/ds/AbstractPGPDataType.php | 3 +- src/XML/ds/AbstractSPKIDataType.php | 3 +- src/XML/ds/CanonicalizationMethod.php | 19 +++---- src/XML/ds/DigestMethod.php | 17 +++--- src/XML/ds/DigestValue.php | 18 +++---- src/XML/ds/DsObject.php | 54 ++++++++++---------- src/XML/ds/Exponent.php | 15 ++---- src/XML/ds/G.php | 15 ++---- src/XML/ds/HMACOutputLength.php | 15 ++---- src/XML/ds/J.php | 15 ++---- src/XML/ds/KeyInfo.php | 6 +-- src/XML/ds/KeyName.php | 18 +++---- src/XML/ds/KeyValue.php | 3 +- src/XML/ds/Manifest.php | 15 +++--- src/XML/ds/MgmtData.php | 18 +++---- src/XML/ds/Modulus.php | 15 ++---- src/XML/ds/P.php | 15 ++---- src/XML/ds/PGPKeyID.php | 15 ++---- src/XML/ds/PGPKeyPacket.php | 15 ++---- src/XML/ds/PgenCounter.php | 15 ++---- src/XML/ds/Q.php | 15 ++---- src/XML/ds/RSAKeyValue.php | 3 +- src/XML/ds/Reference.php | 46 ++++++++--------- src/XML/ds/RetrievalMethod.php | 32 ++++++------ src/XML/ds/SPKISexp.php | 15 ++---- src/XML/ds/Seed.php | 15 ++---- src/XML/ds/Signature.php | 20 ++++---- src/XML/ds/SignatureMethod.php | 18 +++---- src/XML/ds/SignatureProperties.php | 21 ++++---- src/XML/ds/SignatureProperty.php | 36 ++++++------- src/XML/ds/SignatureValue.php | 40 +++++++++------ src/XML/ds/SignedInfo.php | 29 ++++++----- src/XML/ds/Transform.php | 31 +++++------ src/XML/ds/Transforms.php | 3 +- src/XML/ds/X509CRL.php | 15 ++---- src/XML/ds/X509Certificate.php | 15 ++---- src/XML/ds/X509Data.php | 49 ++++++++++++------ src/XML/ds/X509IssuerName.php | 15 ++---- src/XML/ds/X509SKI.php | 15 ++---- src/XML/ds/X509SerialNumber.php | 15 ++---- src/XML/ds/X509SubjectName.php | 15 ++---- src/XML/ds/XPath.php | 15 +++--- src/XML/ds/Y.php | 15 ++---- src/XML/dsig11/X509Digest.php | 42 +++++++++------ src/XML/xenc/CarriedKeyName.php | 15 ++---- src/XML/xenc/CipherValue.php | 15 ++---- src/XML/xenc/Generator.php | 15 ++---- src/XML/xenc/KANonce.php | 17 +++--- src/XML/xenc/OAEPparams.php | 15 ++---- src/XML/xenc/P.php | 15 ++---- src/XML/xenc/PgenCounter.php | 17 +++--- src/XML/xenc/Q.php | 15 ++---- src/XML/xenc/Seed.php | 17 +++--- src/XML/xenc/XencPublic.php | 17 +++--- src/XML/xenc11/DerivedKeyName.php | 15 ++---- src/XML/xenc11/MasterKeyName.php | 15 ++---- src/XML/xenc11/Specified.php | 15 ++---- 61 files changed, 483 insertions(+), 610 deletions(-) create mode 100644 src/Exception/ProtocolViolationException.php diff --git a/composer.json b/composer.json index b22d3c10..b271ce32 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "ext-spl": "*", "simplesamlphp/assert": "~1.8.0", - "simplesamlphp/xml-common": "~1.24.0" + "simplesamlphp/xml-common": "dev-feature/xsd-types" }, "require-dev": { "simplesamlphp/simplesamlphp-test-framework": "~1.8.0" diff --git a/src/Exception/ProtocolViolationException.php b/src/Exception/ProtocolViolationException.php new file mode 100644 index 00000000..b1574626 --- /dev/null +++ b/src/Exception/ProtocolViolationException.php @@ -0,0 +1,29 @@ +Id; } @@ -116,7 +118,7 @@ public function toXML(?DOMElement $parent = null): DOMElement $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } foreach ($this->getInfo() as $elt) { diff --git a/src/XML/ds/AbstractPGPDataType.php b/src/XML/ds/AbstractPGPDataType.php index 644c0fc9..04d587ad 100644 --- a/src/XML/ds/AbstractPGPDataType.php +++ b/src/XML/ds/AbstractPGPDataType.php @@ -10,8 +10,7 @@ use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\Exception\TooManyElementsException; use SimpleSAML\XML\ExtendableElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; use SimpleSAML\XML\XsNamespace as NS; use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; diff --git a/src/XML/ds/AbstractSPKIDataType.php b/src/XML/ds/AbstractSPKIDataType.php index 89621d9f..eabada97 100644 --- a/src/XML/ds/AbstractSPKIDataType.php +++ b/src/XML/ds/AbstractSPKIDataType.php @@ -11,8 +11,7 @@ use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\Registry\ElementRegistry; use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\SerializableElementInterface; +use SimpleSAML\XML\{SchemaValidatableElementTrait, SerializableElementInterface}; use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; /** diff --git a/src/XML/ds/CanonicalizationMethod.php b/src/XML/ds/CanonicalizationMethod.php index bf5c66a8..69abbd4a 100644 --- a/src/XML/ds/CanonicalizationMethod.php +++ b/src/XML/ds/CanonicalizationMethod.php @@ -8,11 +8,13 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\AnyURIValue; use SimpleSAML\XMLSecurity\Constants as C; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; +use function strval; + /** * Class representing a ds:CanonicalizationMethod element. * @@ -25,12 +27,11 @@ final class CanonicalizationMethod extends AbstractDsElement implements SchemaVa /** * Initialize a CanonicalizationMethod element. * - * @param string $Algorithm + * @param \SimpleSAML\XML\Type\AnyURIValue $Algorithm */ public function __construct( - protected string $Algorithm, + protected AnyURIValue $Algorithm, ) { - Assert::validURI($Algorithm, SchemaViolationException::class); Assert::oneOf( $Algorithm, [ @@ -48,9 +49,9 @@ public function __construct( /** * Collect the value of the Algorithm-property * - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getAlgorithm(): string + public function getAlgorithm(): AnyURIValue { return $this->Algorithm; } @@ -70,7 +71,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'CanonicalizationMethod', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, CanonicalizationMethod::NS, InvalidDOMElementException::class); - $Algorithm = CanonicalizationMethod::getAttribute($xml, 'Algorithm'); + $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class); return new static($Algorithm); } @@ -85,7 +86,7 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->setAttribute('Algorithm', $this->getAlgorithm()); + $e->setAttribute('Algorithm', strval($this->getAlgorithm())); return $e; } diff --git a/src/XML/ds/DigestMethod.php b/src/XML/ds/DigestMethod.php index 0dc2f333..cb8f8030 100644 --- a/src/XML/ds/DigestMethod.php +++ b/src/XML/ds/DigestMethod.php @@ -9,8 +9,8 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\ExtendableElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\AnyURIType; use SimpleSAML\XML\XsNamespace as NS; use SimpleSAML\XMLSecurity\Constants as C; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; @@ -30,14 +30,13 @@ final class DigestMethod extends AbstractDsElement implements SchemaValidatableE /** * Initialize a DigestMethod element. * - * @param string $Algorithm + * @param \SimpleSAML\XML\Type\AnyURIValuestring $Algorithm * @param list<\SimpleSAML\XML\SerializableElementInterface> $elements */ public function __construct( - protected string $Algorithm, + protected AnyURIValue $Algorithm, array $elements = [], ) { - Assert::validURI($Algorithm, SchemaViolationException::class); Assert::oneOf( $Algorithm, array_keys(C::$DIGEST_ALGORITHMS), @@ -52,9 +51,9 @@ public function __construct( /** * Collect the value of the Algorithm-property * - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getAlgorithm(): string + public function getAlgorithm(): AnyURIValue { return $this->Algorithm; } @@ -74,7 +73,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'DigestMethod', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class); - $Algorithm = DigestMethod::getAttribute($xml, 'Algorithm'); + $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class); $elements = self::getChildElementsFromXML($xml); return new static($Algorithm, $elements); @@ -90,7 +89,7 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->setAttribute('Algorithm', $this->getAlgorithm()); + $e->setAttribute('Algorithm', strval($this->getAlgorithm())); foreach ($this->elements as $elt) { if (!$elt->isEmptyElement()) { diff --git a/src/XML/ds/DigestValue.php b/src/XML/ds/DigestValue.php index 9747af30..23282d9c 100644 --- a/src/XML/ds/DigestValue.php +++ b/src/XML/ds/DigestValue.php @@ -4,9 +4,9 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; /** * Class representing a ds:DigestValue element. @@ -15,15 +15,9 @@ */ final class DigestValue extends AbstractDsElement implements SchemaValidatableElementInterface { - use Base64ElementTrait; use SchemaValidatableElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/DsObject.php b/src/XML/ds/DsObject.php index 592c11ec..b5434bdd 100644 --- a/src/XML/ds/DsObject.php +++ b/src/XML/ds/DsObject.php @@ -7,11 +7,14 @@ use DOMElement; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\ExtendableElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\{AnyURIValue, IDValue, StringValue}; use SimpleSAML\XML\XsNamespace as NS; use SimpleSAML\XMLSecurity\Assert\Assert; +use function is_null; +use function strval; + /** * Class representing a ds:Object element. * @@ -32,20 +35,17 @@ final class DsObject extends AbstractDsElement implements SchemaValidatableEleme /** * Initialize a ds:Object element. * - * @param string|null $Id - * @param string|null $MimeType - * @param string|null $Encoding + * @param \SimpleSAML\XML\Type\IDValue|null $Id + * @param \SimpleSAML\XML\Type\StringValue|null $MimeType + * @param \SimpleSAML\XML\Type\AnyURIValue|null $Encoding * @param \SimpleSAML\XML\SerializableElementInterface[] $elements */ public function __construct( - protected ?string $Id = null, - protected ?string $MimeType = null, - protected ?string $Encoding = null, + protected ?IDValue $Id = null, + protected ?StringValue $MimeType = null, + protected ?AnyURIValue $Encoding = null, array $elements = [], ) { - Assert::nullOrValidNCName($Id); - Assert::nullOrValidURI($Encoding); - $this->setElements($elements); } @@ -53,9 +53,9 @@ public function __construct( /** * Collect the value of the Id-property * - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } @@ -64,9 +64,9 @@ public function getId(): ?string /** * Collect the value of the MimeType-property * - * @return string|null + * @return \SimpleSAML\XML\Type\StringValue|null */ - public function getMimeType(): ?string + public function getMimeType(): ?StringValue { return $this->MimeType; } @@ -75,9 +75,9 @@ public function getMimeType(): ?string /** * Collect the value of the Encoding-property * - * @return string|null + * @return \SimpleSAML\XML\Type\AnyURIValue|null */ - public function getEncoding(): ?string + public function getEncoding(): ?AnyURIValue { return $this->Encoding; } @@ -90,10 +90,10 @@ public function getEncoding(): ?string */ public function isEmptyElement(): bool { - return empty($this->elements) - && empty($this->Id) - && empty($this->MimeType) - && empty($this->Encoding); + return empty($this->getElements()) + && is_null($this->getId()) + && is_null($this->getMimeType()) + && is_null($this->getEncoding()); } @@ -111,9 +111,9 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'Object', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, DsObject::NS, InvalidDOMElementException::class); - $Id = DsObject::getOptionalAttribute($xml, 'Id', null); - $MimeType = DsObject::getOptionalAttribute($xml, 'MimeType', null); - $Encoding = DsObject::getOptionalAttribute($xml, 'Encoding', null); + $Id = self::getOptionalAttribute($xml, 'Id', IDValue::class, null); + $MimeType = self::getOptionalAttribute($xml, 'MimeType', StringValue::class, null); + $Encoding = self::getOptionalAttribute($xml, 'Encoding', AnyURIValue::class, null); $elements = self::getChildElementsFromXML($xml); return new static($Id, $MimeType, $Encoding, $elements); @@ -131,15 +131,15 @@ public function toXML(?DOMElement $parent = null): DOMElement $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } if ($this->getMimeType() !== null) { - $e->setAttribute('MimeType', $this->getMimeType()); + $e->setAttribute('MimeType', strval($this->getMimeType())); } if ($this->getEncoding() !== null) { - $e->setAttribute('Encoding', $this->getEncoding()); + $e->setAttribute('Encoding', strval($this->getEncoding())); } foreach ($this->getElements() as $elt) { diff --git a/src/XML/ds/Exponent.php b/src/XML/ds/Exponent.php index 325f82fa..81c596d7 100644 --- a/src/XML/ds/Exponent.php +++ b/src/XML/ds/Exponent.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:Exponent element. @@ -13,14 +14,8 @@ */ final class Exponent extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/G.php b/src/XML/ds/G.php index 0e879816..fda4b66a 100644 --- a/src/XML/ds/G.php +++ b/src/XML/ds/G.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:G element. @@ -13,14 +14,8 @@ */ final class G extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/HMACOutputLength.php b/src/XML/ds/HMACOutputLength.php index e8a6228c..2187440c 100644 --- a/src/XML/ds/HMACOutputLength.php +++ b/src/XML/ds/HMACOutputLength.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\IntegerElementTrait; +use SimpleSAML\XML\Type\IntegerValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:HMACOutputLength element. @@ -13,14 +14,8 @@ */ final class HMACOutputLength extends AbstractDsElement { - use IntegerElementTrait; + use TypedTextContentTrait; - - /** - * @param string $length - */ - public function __construct(string $length) - { - $this->setContent($length); - } + /** @var string */ + public const TEXTCONTENT_TYPE = IntegerValue::class; } diff --git a/src/XML/ds/J.php b/src/XML/ds/J.php index 22e724c3..649127cb 100644 --- a/src/XML/ds/J.php +++ b/src/XML/ds/J.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:J element. @@ -13,14 +14,8 @@ */ final class J extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/KeyInfo.php b/src/XML/ds/KeyInfo.php index 01cb7c13..bc97e06b 100644 --- a/src/XML/ds/KeyInfo.php +++ b/src/XML/ds/KeyInfo.php @@ -7,8 +7,8 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\IDValue; use function array_merge; @@ -35,7 +35,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'KeyInfo', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, KeyInfo::NS, InvalidDOMElementException::class); - $Id = self::getOptionalAttribute($xml, 'Id', null); + $Id = self::getOptionalAttribute($xml, 'Id', IDValue::class, null); $keyName = KeyName::getChildrenOfClass($xml); $keyValue = KeyValue::getChildrenOfClass($xml); diff --git a/src/XML/ds/KeyName.php b/src/XML/ds/KeyName.php index b16bd14c..00527acf 100644 --- a/src/XML/ds/KeyName.php +++ b/src/XML/ds/KeyName.php @@ -4,9 +4,9 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:KeyName element. @@ -16,14 +16,8 @@ final class KeyName extends AbstractDsElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/ds/KeyValue.php b/src/XML/ds/KeyValue.php index 7fba8cc6..4b522219 100644 --- a/src/XML/ds/KeyValue.php +++ b/src/XML/ds/KeyValue.php @@ -11,8 +11,7 @@ use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\Exception\TooManyElementsException; use SimpleSAML\XML\ExtendableElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; use SimpleSAML\XML\XsNamespace as NS; /** diff --git a/src/XML/ds/Manifest.php b/src/XML/ds/Manifest.php index 1c7adac0..54f9be80 100644 --- a/src/XML/ds/Manifest.php +++ b/src/XML/ds/Manifest.php @@ -8,8 +8,8 @@ use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingElementException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\IDValue; use SimpleSAML\XMLSecurity\Assert\Assert; /** @@ -25,15 +25,14 @@ final class Manifest extends AbstractDsElement implements SchemaValidatableEleme * Initialize a ds:Manifest * * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references - * @param string|null $Id + * @param \SimpleSAML\XML\Type\IDValue|null $Id */ public function __construct( protected array $references, - protected ?string $Id = null, + protected ?IDValue $Id = null, ) { Assert::maxCount($references, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOf($references, Reference::class); - Assert::nullOrValidNCName($Id); } @@ -47,7 +46,7 @@ public function getReferences(): array /** - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ public function getId(): ?string { @@ -69,7 +68,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'Manifest', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, Manifest::NS, InvalidDOMElementException::class); - $Id = self::getOptionalAttribute($xml, 'Id', null); + $Id = self::getOptionalAttribute($xml, 'Id', IDValue::class, null); $references = Reference::getChildrenOfClass($xml); Assert::minCount( @@ -97,7 +96,7 @@ public function toXML(?DOMElement $parent = null): DOMElement $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } foreach ($this->getReferences() as $reference) { diff --git a/src/XML/ds/MgmtData.php b/src/XML/ds/MgmtData.php index b51eb4cc..97fe2853 100644 --- a/src/XML/ds/MgmtData.php +++ b/src/XML/ds/MgmtData.php @@ -4,9 +4,9 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:MgmtData element. @@ -16,14 +16,8 @@ final class MgmtData extends AbstractDsElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/ds/Modulus.php b/src/XML/ds/Modulus.php index ba48cac2..52947a92 100644 --- a/src/XML/ds/Modulus.php +++ b/src/XML/ds/Modulus.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:Modulus element. @@ -13,14 +14,8 @@ */ final class Modulus extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/P.php b/src/XML/ds/P.php index a5173b4c..3532b2fa 100644 --- a/src/XML/ds/P.php +++ b/src/XML/ds/P.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:P element. @@ -13,14 +14,8 @@ */ final class P extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/PGPKeyID.php b/src/XML/ds/PGPKeyID.php index dea62bc9..aae5f449 100644 --- a/src/XML/ds/PGPKeyID.php +++ b/src/XML/ds/PGPKeyID.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:PGPKeyID element. @@ -13,14 +14,8 @@ */ final class PGPKeyID extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/PGPKeyPacket.php b/src/XML/ds/PGPKeyPacket.php index 614bf86d..c78324aa 100644 --- a/src/XML/ds/PGPKeyPacket.php +++ b/src/XML/ds/PGPKeyPacket.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:PGPKeyPacket element. @@ -13,14 +14,8 @@ */ final class PGPKeyPacket extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/PgenCounter.php b/src/XML/ds/PgenCounter.php index 9210d1b8..9d23d2df 100644 --- a/src/XML/ds/PgenCounter.php +++ b/src/XML/ds/PgenCounter.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:PgenCounter element. @@ -13,14 +14,8 @@ */ final class PgenCounter extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/Q.php b/src/XML/ds/Q.php index 3089f707..7e886f2e 100644 --- a/src/XML/ds/Q.php +++ b/src/XML/ds/Q.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:Q element. @@ -13,14 +14,8 @@ */ final class Q extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/RSAKeyValue.php b/src/XML/ds/RSAKeyValue.php index 4bfef458..b6134258 100644 --- a/src/XML/ds/RSAKeyValue.php +++ b/src/XML/ds/RSAKeyValue.php @@ -9,8 +9,7 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingElementException; use SimpleSAML\XML\Exception\TooManyElementsException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; /** * Class representing a ds:RSAKeyValue element. diff --git a/src/XML/ds/Reference.php b/src/XML/ds/Reference.php index 91db0085..c81483c8 100644 --- a/src/XML/ds/Reference.php +++ b/src/XML/ds/Reference.php @@ -8,11 +8,12 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingElementException; use SimpleSAML\XML\Exception\TooManyElementsException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\{AnyURIValue, IDValue}; use SimpleSAML\XMLSecurity\Assert\Assert; use function array_pop; +use function strval; /** * Class representing a ds:Reference element. @@ -29,21 +30,18 @@ final class Reference extends AbstractDsElement implements SchemaValidatableElem * @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod * @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms - * @param string|null $Id - * @param string|null $Type - * @param string|null $URI + * @param \SimpleSAML\XML\Type\IDValue|null $Id + * @param \SimpleSAML\XML\Type\AnyURIValue|null $Type + * @param \SimplesAML\XML\Type\AnyURIValue|null $URI */ public function __construct( protected DigestMethod $digestMethod, protected DigestValue $digestValue, protected ?Transforms $transforms = null, - protected ?string $Id = null, - protected ?string $Type = null, - protected ?string $URI = null, + protected ?IDValue $Id = null, + protected ?AnyURIValue $Type = null, + protected ?AnyURIValue $URI = null, ) { - Assert::nullOrValidNCName($Id); - Assert::nullOrValidURI($Type); - Assert::nullOrValidURI($URI); } @@ -75,27 +73,27 @@ public function getDigestValue(): DigestValue /** - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } /** - * @return string|null + * @return \SimpleSAML\XML\Type\AnyURIValue|null */ - public function getType(): ?string + public function getType(): ?AnyURIValue { return $this->Type; } /** - * @return string|null + * @return \SimpleSAML\XML\Type\AnyURIValue|null */ - public function getURI(): ?string + public function getURI(): ?AnyURIValue { return $this->URI; } @@ -108,7 +106,7 @@ public function getURI(): ?string */ public function isXPointer(): bool { - return !empty($this->URI) && str_starts_with($this->URI, '#xpointer'); + return !is_null($this->getURI()) && str_starts_with(strval($this->URI()), '#xpointer'); } @@ -126,9 +124,9 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'Reference', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, Reference::NS, InvalidDOMElementException::class); - $Id = self::getOptionalAttribute($xml, 'Id', null); - $Type = self::getOptionalAttribute($xml, 'Type', null); - $URI = self::getOptionalAttribute($xml, 'URI', null); + $Id = self::getOptionalAttribute($xml, 'Id', IDValue::class, null); + $Type = self::getOptionalAttribute($xml, 'Type', AnyURIValue::class, null); + $URI = self::getOptionalAttribute($xml, 'URI', AnyURI::class, null); $transforms = Transforms::getChildrenOfClass($xml); Assert::maxCount( @@ -175,13 +173,13 @@ public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } if ($this->getType() !== null) { - $e->setAttribute('Type', $this->getType()); + $e->setAttribute('Type', strval($this->getType())); } if ($this->getURI() !== null) { - $e->setAttribute('URI', $this->getURI()); + $e->setAttribute('URI', strval($this->getURI())); } $this->getTransforms()?->toXML($e); diff --git a/src/XML/ds/RetrievalMethod.php b/src/XML/ds/RetrievalMethod.php index 01419508..180fe4fa 100644 --- a/src/XML/ds/RetrievalMethod.php +++ b/src/XML/ds/RetrievalMethod.php @@ -9,8 +9,10 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\Exception\TooManyElementsException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\AnyURIValue; + +use function strval; /** * Class representing a ds:RetrievalMethod element. @@ -25,16 +27,14 @@ final class RetrievalMethod extends AbstractDsElement implements SchemaValidatab * Initialize a ds:RetrievalMethod * * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms - * @param string $URI - * @param string|null $Type + * @param \SimpleSAML\XML\Type\AnyURIValue $URI + * @param \SimpleSAML\XML\Type\AnyURIValue|null $Type */ final public function __construct( protected ?Transforms $transforms, - protected string $URI, - protected ?string $Type = null, + protected AnyURIValue $URI, + protected ?AnyURIValue $Type = null, ) { - Assert::validURI($URI, SchemaViolationException::class); // Covers the empty string - Assert::nullOrValidURI($Type, SchemaViolationException::class); // Covers the empty string } @@ -48,18 +48,18 @@ public function getTransforms(): ?Transforms /** - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getURI(): string + public function getURI(): AnyURIValue { return $this->URI; } /** - * @return string|null + * @return \SimpleSAML\XML\Type\AnyURIValue|null */ - public function getType(): ?string + public function getType(): ?AnyURIValue { return $this->Type; } @@ -79,8 +79,8 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'RetrievalMethod', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, RetrievalMethod::NS, InvalidDOMElementException::class); - $URI = self::getAttribute($xml, 'URI'); - $Type = self::getOptionalAttribute($xml, 'Type', null); + $URI = self::getAttribute($xml, 'URI', AnyURIValue::class); + $Type = self::getOptionalAttribute($xml, 'Type', AnyURIValue::class, null); $transforms = Transforms::getChildrenOfClass($xml); Assert::maxCount( @@ -107,10 +107,10 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->setAttribute('URI', $this->getURI()); + $e->setAttribute('URI', strval($this->getURI())); if ($this->getType() !== null) { - $e->setAttribute('Type', $this->getType()); + $e->setAttribute('Type', strval($this->getType())); } $this->getTransforms()?->toXML($e); diff --git a/src/XML/ds/SPKISexp.php b/src/XML/ds/SPKISexp.php index 1ca50c4a..45ed2957 100644 --- a/src/XML/ds/SPKISexp.php +++ b/src/XML/ds/SPKISexp.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:SPKISexp element. @@ -13,14 +14,8 @@ */ final class SPKISexp extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/Seed.php b/src/XML/ds/Seed.php index cf0713c9..45e3d12b 100644 --- a/src/XML/ds/Seed.php +++ b/src/XML/ds/Seed.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:seed element. @@ -13,14 +14,8 @@ */ final class Seed extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/Signature.php b/src/XML/ds/Signature.php index e5da1f3d..cddb0942 100644 --- a/src/XML/ds/Signature.php +++ b/src/XML/ds/Signature.php @@ -8,12 +8,13 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingElementException; use SimpleSAML\XML\Exception\TooManyElementsException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\IDValue; use SimpleSAML\XMLSecurity\Assert\Assert; use SimpleSAML\XMLSecurity\Constants as C; use function array_pop; +use function strval; /** * Class representing a ds:Signature element. @@ -31,27 +32,26 @@ final class Signature extends AbstractDsElement implements SchemaValidatableElem * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo * @param \SimpleSAML\XMLSecurity\XML\ds\DsObject[] $objects - * @param string|null $Id + * @param \SimpleSAML\XML\Type\IDValue|null $Id */ public function __construct( protected SignedInfo $signedInfo, protected SignatureValue $signatureValue, protected ?KeyInfo $keyInfo, protected array $objects = [], - protected ?string $Id = null, + protected ?IDValue $Id = null, ) { Assert::maxCount($objects, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOf($objects, DsObject::class); - Assert::nullOrValidNCName($Id); } /** * Get the Id used for this signature. * - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } @@ -109,8 +109,6 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class); - $Id = self::getOptionalAttribute($xml, 'Id', null); - $signedInfo = SignedInfo::getChildrenOfClass($xml); Assert::minCount( $signedInfo, @@ -154,7 +152,7 @@ public static function fromXML(DOMElement $xml): static array_pop($signatureValue), empty($keyInfo) ? null : array_pop($keyInfo), $objects, - $Id, + self::getOptionalAttribute($xml, 'Id', IDValue::class, null), ); } @@ -170,7 +168,7 @@ public function toXML(?DOMElement $parent = null): DOMElement $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } $this->getSignedInfo()->toXML($e); diff --git a/src/XML/ds/SignatureMethod.php b/src/XML/ds/SignatureMethod.php index eeb04d8a..4d17e471 100644 --- a/src/XML/ds/SignatureMethod.php +++ b/src/XML/ds/SignatureMethod.php @@ -10,8 +10,8 @@ use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\Exception\TooManyElementsException; use SimpleSAML\XML\ExtendableElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\AnyURIValue; use SimpleSAML\XML\XsNamespace as NS; use SimpleSAML\XMLSecurity\Constants as C; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; @@ -19,6 +19,7 @@ use function array_keys; use function array_merge; use function array_pop; +use function strval; /** * Class representing a ds:SignatureMethod element. @@ -37,16 +38,15 @@ final class SignatureMethod extends AbstractDsElement implements SchemaValidatab /** * Initialize a SignatureMethod element. * - * @param string $Algorithm + * @param \SimpleSAML\XML\Type\AnyURIValue $Algorithm * @param \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null $hmacOutputLength * @param array<\SimpleSAML\XML\SerializableElementInterface> $children */ public function __construct( - protected string $Algorithm, + protected AnyURIValue $Algorithm, protected ?HMACOutputLength $hmacOutputLength = null, array $children = [], ) { - Assert::validURI($Algorithm, SchemaViolationException::class); Assert::oneOf( $Algorithm, array_merge( @@ -64,9 +64,9 @@ public function __construct( /** * Collect the value of the Algorithm-property * - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getAlgorithm(): string + public function getAlgorithm(): AnyURIValue { return $this->Algorithm; } @@ -97,7 +97,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class); - $Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm'); + $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class); $hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml); Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class); @@ -115,7 +115,7 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->setAttribute('Algorithm', $this->getAlgorithm()); + $e->setAttribute('Algorithm', strval($this->getAlgorithm())); $this->getHMACOutputLength()?->toXML($e); diff --git a/src/XML/ds/SignatureProperties.php b/src/XML/ds/SignatureProperties.php index c0aeb7c8..d4b9d02e 100644 --- a/src/XML/ds/SignatureProperties.php +++ b/src/XML/ds/SignatureProperties.php @@ -9,10 +9,12 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingElementException; use SimpleSAML\XML\Exception\SchemaViolationException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\IDValue; use SimpleSAML\XMLSecurity\Assert\Assert; +use function strval; + /** * Class representing a ds:SignatureProperties element. * @@ -26,15 +28,14 @@ final class SignatureProperties extends AbstractDsElement implements SchemaValid * Initialize a ds:SignatureProperties * * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureProperty[] $signatureProperty - * @param string|null $Id + * @param \SimpleSAML\XML\Type\IDValue|null $Id */ public function __construct( protected array $signatureProperty, - protected ?string $Id = null, + protected ?IDValue $Id = null, ) { Assert::maxCount($signatureProperty, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOf($signatureProperty, SignatureProperty::class, SchemaViolationException::class); - Assert::nullOrValidNCName($Id); } @@ -48,9 +49,9 @@ public function getSignatureProperty(): array /** - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } @@ -71,8 +72,6 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->namespaceURI, SignatureProperties::NS, InvalidDOMElementException::class); $signatureProperty = SignatureProperty::getChildrenOfClass($xml); - $Id = self::getOptionalAttribute($xml, 'Id', null); - Assert::minCount( $signatureProperty, 1, @@ -82,7 +81,7 @@ public static function fromXML(DOMElement $xml): static return new static( $signatureProperty, - $Id, + self::getOptionalAttribute($xml, 'Id', IDValue::class, null), ); } @@ -98,7 +97,7 @@ public function toXML(?DOMElement $parent = null): DOMElement $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } foreach ($this->getSignatureProperty() as $signatureProperty) { diff --git a/src/XML/ds/SignatureProperty.php b/src/XML/ds/SignatureProperty.php index 3f07cedd..a07ee0cd 100644 --- a/src/XML/ds/SignatureProperty.php +++ b/src/XML/ds/SignatureProperty.php @@ -9,11 +9,13 @@ use SimpleSAML\XML\Exception\MissingElementException; use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\ExtendableElementTrait; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\{AnyURIValue, IDValue}; use SimpleSAML\XML\XsNamespace as NS; use SimpleSAML\XMLSecurity\Assert\Assert; +use function strval; + /** * Class representing a ds:SignatureProperty element. * @@ -32,34 +34,31 @@ final class SignatureProperty extends AbstractDsElement implements SchemaValidat * Initialize a ds:SignatureProperty * * @param \SimpleSAML\XML\SerializableElementInterface[] $elements - * @param string $Target - * @param string|null $Id + * @param \SimpleSAML\XML\Type\AnyURIValue $Target + * @param \SimpleSAML\XML\Type\IDValue|null $Id */ public function __construct( array $elements, - protected string $Target, - protected ?string $Id = null, + protected AnyURIValue $Target, + protected ?IDValue $Id = null, ) { - Assert::validURI($Target, SchemaViolationException::class); // Covers the empty string - Assert::nullOrValidNCName($Id); - $this->setElements($elements); } /** - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getTarget(): string + public function getTarget(): AnyURIValue { return $this->Target; } /** - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } @@ -79,9 +78,6 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'SignatureProperty', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, SignatureProperty::NS, InvalidDOMElementException::class); - $Target = self::getAttribute($xml, 'Target'); - $Id = self::getOptionalAttribute($xml, 'Id', null); - $children = self::getChildElementsFromXML($xml); Assert::minCount( $children, @@ -92,8 +88,8 @@ public static function fromXML(DOMElement $xml): static return new static( $children, - $Target, - $Id, + self::getAttribute($xml, 'Target', AnyURIValue::class), + self::getOptionalAttribute($xml, 'Id', IDValue::class, null), ); } @@ -107,10 +103,10 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->setAttribute('Target', $this->getTarget()); + $e->setAttribute('Target', strval($this->getTarget())); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } foreach ($this->getElements() as $element) { diff --git a/src/XML/ds/SignatureValue.php b/src/XML/ds/SignatureValue.php index cb51b850..3dcc2f1d 100644 --- a/src/XML/ds/SignatureValue.php +++ b/src/XML/ds/SignatureValue.php @@ -5,12 +5,13 @@ namespace SimpleSAML\XMLSecurity\XML\ds; use DOMElement; -use SimpleSAML\XML\Base64ElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\{Base64BinaryValue, IDValue}; use SimpleSAML\XMLSecurity\Assert\Assert; +use function strval; + /** * Class representing a ds:SignatureValue element. * @@ -18,35 +19,42 @@ */ final class SignatureValue extends AbstractDsElement implements SchemaValidatableElementInterface { - use Base64ElementTrait; use SchemaValidatableElementTrait; /** - * @param string $content + * @param string $value * @param string|null $Id */ public function __construct( - string $content, - protected ?string $Id = null, + protected Base64BinaryValue $value, + protected ?IDValue $Id = null, ) { - Assert::nullOrValidNCName($Id); - - $this->setContent($content); } /** * Get the Id used for this signature value. * - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } + /** + * Get the content for this signature value. + * + * @return \SimpleSAML\XML\Type\Base64BinaryValue + */ + public function getValue(): ?Base64Binary + { + return $this->value; + } + + /** * Convert XML into a SignatureValue element * @@ -61,9 +69,9 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'SignatureValue', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, SignatureValue::NS, InvalidDOMElementException::class); - $Id = self::getOptionalAttribute($xml, 'Id', null); + $Id = self::getOptionalAttribute($xml, 'Id', IDValue::class, null); - return new static($xml->textContent, $Id); + return new static(Base64Binary::fromString($xml->textContent), $Id); } @@ -76,10 +84,10 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->textContent = $this->getContent(); + $e->textContent = strval($this->getContent()); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } return $e; diff --git a/src/XML/ds/SignedInfo.php b/src/XML/ds/SignedInfo.php index 7fb621ae..5d265696 100644 --- a/src/XML/ds/SignedInfo.php +++ b/src/XML/ds/SignedInfo.php @@ -9,14 +9,14 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingElementException; use SimpleSAML\XML\Exception\TooManyElementsException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\IDValue; use SimpleSAML\XMLSecurity\Assert\Assert; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; -use SimpleSAML\XMLSecurity\XML\CanonicalizableElementInterface; -use SimpleSAML\XMLSecurity\XML\CanonicalizableElementTrait; +use SimpleSAML\XMLSecurity\XML\{CanonicalizableElementInterface, CanonicalizableElementTrait}; use function array_pop; +use function strval; /** * Class representing a ds:SignedInfo element. @@ -42,17 +42,16 @@ final class SignedInfo extends AbstractDsElement implements * @param \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod $canonicalizationMethod * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod $signatureMethod * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references - * @param string|null $Id + * @param \SimpleSAML\XML\Type\IDValue|null $Id */ public function __construct( protected CanonicalizationMethod $canonicalizationMethod, protected SignatureMethod $signatureMethod, protected array $references, - protected ?string $Id = null, + protected ?IDValue $Id = null, ) { Assert::maxCount($references, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOf($references, Reference::class, InvalidArgumentException::class); - Assert::nullOrValidNCName($Id); } @@ -92,9 +91,9 @@ public function getReferences(): array /** * Collect the value of the Id-property * - * @return string|null + * @return \SimpleSAML\XML\Type\IDValue|null */ - public function getId(): ?string + public function getId(): ?IDValue { return $this->Id; } @@ -127,8 +126,6 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'SignedInfo', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, SignedInfo::NS, InvalidDOMElementException::class); - $Id = self::getOptionalAttribute($xml, 'Id', null); - $canonicalizationMethod = CanonicalizationMethod::getChildrenOfClass($xml); Assert::minCount( $canonicalizationMethod, @@ -165,7 +162,13 @@ public static function fromXML(DOMElement $xml): static MissingElementException::class, ); - $signedInfo = new static(array_pop($canonicalizationMethod), array_pop($signatureMethod), $references, $Id); + $signedInfo = new static( + array_pop($canonicalizationMethod), + array_pop($signatureMethod), + $references, + self::getOptionalAttribute($xml, 'Id', IDValue::class, null), + ); + $signedInfo->xml = $xml; return $signedInfo; } @@ -182,7 +185,7 @@ public function toXML(?DOMElement $parent = null): DOMElement $e = $this->instantiateParentElement($parent); if ($this->getId() !== null) { - $e->setAttribute('Id', $this->getId()); + $e->setAttribute('Id', strval($this->getId())); } $this->getCanonicalizationMethod()->toXML($e); diff --git a/src/XML/ds/Transform.php b/src/XML/ds/Transform.php index 1f4f7133..9711bebe 100644 --- a/src/XML/ds/Transform.php +++ b/src/XML/ds/Transform.php @@ -9,8 +9,8 @@ use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\Exception\TooManyElementsException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\AnyURIValue; use SimpleSAML\XMLSecurity\Constants as C; use SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces; @@ -28,20 +28,18 @@ class Transform extends AbstractDsElement implements SchemaValidatableElementInt /** * Initialize the Transform element. * - * @param string $algorithm + * @param \SimpleSAML\XML\Type\AnyURIValue $algorithm * @param \SimpleSAML\XMLSecurity\XML\ds\XPath|null $xpath * @param \SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces|null $inclusiveNamespaces */ final public function __construct( - protected string $algorithm, + protected AnyURIValue $algorithm, protected ?XPath $xpath = null, protected ?InclusiveNamespaces $inclusiveNamespaces = null, ) { - Assert::validURI($algorithm, SchemaViolationException::class); - if ($xpath !== null) { Assert::nullOrEq( - $this->algorithm, + $algorithm, C::XPATH10_URI, sprintf('Transform algorithm "%s" required if XPath provided.', C::XPATH10_URI), ); @@ -49,7 +47,7 @@ final public function __construct( if ($inclusiveNamespaces !== null) { Assert::oneOf( - $this->algorithm, + $algorithm, [ C::C14N_INCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS, @@ -67,9 +65,9 @@ final public function __construct( /** * Get the algorithm associated with this transform. * - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getAlgorithm(): string + public function getAlgorithm(): AnyURIValue { return $this->algorithm; } @@ -108,8 +106,6 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'Transform', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, Transform::NS, InvalidDOMElementException::class); - $alg = self::getAttribute($xml, 'Algorithm'); - $xpath = XPath::getChildrenOfClass($xml); Assert::maxCount($xpath, 1, 'Only one XPath element supported per Transform.', TooManyElementsException::class); @@ -121,7 +117,11 @@ public static function fromXML(DOMElement $xml): static TooManyElementsException::class, ); - return new static($alg, array_pop($xpath), array_pop($prefixes)); + return new static( + self::getAttribute($xml, 'Algorithm', AnyURIValue::class), + array_pop($xpath), + array_pop($prefixes), + ); } @@ -134,7 +134,7 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->setAttribute('Algorithm', $this->getAlgorithm()); + $e->setAttribute('Algorithm', strval($this->getAlgorithm())); switch ($this->getAlgorithm()) { case C::XPATH10_URI: @@ -146,9 +146,6 @@ public function toXML(?DOMElement $parent = null): DOMElement break; } -//$doc = \SimpleSAML\XML\DOMDocumentFactory::create(); -//$doc->append($doc->importNode($e, true)); -//return $doc->documentElement; return $e; } } diff --git a/src/XML/ds/Transforms.php b/src/XML/ds/Transforms.php index ad068a8d..14b95448 100644 --- a/src/XML/ds/Transforms.php +++ b/src/XML/ds/Transforms.php @@ -8,8 +8,7 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; /** diff --git a/src/XML/ds/X509CRL.php b/src/XML/ds/X509CRL.php index e40c3c77..61ded867 100644 --- a/src/XML/ds/X509CRL.php +++ b/src/XML/ds/X509CRL.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:X509CRL element. @@ -13,14 +14,8 @@ */ final class X509CRL extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/X509Certificate.php b/src/XML/ds/X509Certificate.php index 9454dfb4..7d06f8d3 100644 --- a/src/XML/ds/X509Certificate.php +++ b/src/XML/ds/X509Certificate.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:X509Certificate element. @@ -13,14 +14,8 @@ */ final class X509Certificate extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/X509Data.php b/src/XML/ds/X509Data.php index ae2a4845..294444da 100644 --- a/src/XML/ds/X509Data.php +++ b/src/XML/ds/X509Data.php @@ -7,9 +7,10 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\XML\Chunk; -use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\ExtendableElementInterface; +use SimpleSAML\XML\Exception\{InvalidDOMElementException, ProtocolViolationException}; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\SerializableElementInterface; use SimpleSAML\XMLSecurity\Constants as C; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest; @@ -21,36 +22,48 @@ */ final class X509Data extends AbstractDsElement implements SchemaValidatableElementInterface { + use ExtendableElementTrait; use SchemaValidatableElementTrait; /** * Initialize a X509Data. * - * @param (\SimpleSAML\XML\Chunk| - * \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| + * @param (\SimpleSAML\XMLSecurity\XML\ds\X509Certificate| * \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| * \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName| + * \SimpleSAML\XMLSecurity\XML\ds\X509SKI| + * \SimpleSAML\XMLSecurity\XML\ds\X509CRL| * \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest)[] $data + * @param \SimpleSAML\XML\SerializableElementInterface[] $children */ public function __construct( protected array $data, + protected array $children, ) { + /** + * At least one element from the dsig namespaces should be present and + * additional elements from an external namespace to accompany/complement them. + */ + Assert::minCount($data, 1, ProtocolViolationException::class); Assert::maxCount($data, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOfAny( $data, - [Chunk::class, X509Certificate::class, X509IssuerSerial::class, X509SubjectName::class, X509Digest::class], + [X509Certificate::class, X509IssuerSerial::class, X509SubjectName::class, X509Digest::class, SKI::class, X509CRL::class], InvalidArgumentException::class, ); + + $this->setElements($children); } /** * Collect the value of the data-property * - * @return (\SimpleSAML\XML\Chunk| - * \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| + * @return (\SimpleSAML\XMLSecurity\XML\ds\X509Certificate| * \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| * \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName| + * \SimpleSAML\XMLSecurity\XML\ds\X509SKI| + * \SimpleSAML\XMLSecurity\XML\ds\X509CRL| * \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest)[] */ public function getData(): array @@ -73,7 +86,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'X509Data', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, X509Data::NS, InvalidDOMElementException::class); - $data = []; + $data = $children = []; for ($n = $xml->firstChild; $n !== null; $n = $n->nextSibling) { if (!($n instanceof DOMElement)) { @@ -83,20 +96,20 @@ public static function fromXML(DOMElement $xml): static 'X509Certificate' => X509Certificate::fromXML($n), 'X509IssuerSerial' => X509IssuerSerial::fromXML($n), 'X509SubjectName' => X509SubjectName::fromXML($n), - default => new Chunk($n), + 'X509SKI' => SKI::fromXML($n), + 'X509CRL' => X509CRL::fromXML($n), }; } elseif ($n->namespaceURI === C::NS_XDSIG11) { $data[] = match ($n->localName) { 'X509Digest' => X509Digest::fromXML($n), - default => new Chunk($n), }; } else { - $data[] = new Chunk($n); - continue; + $handler = $registry->getElementHandler($elt->namespaceURI, $elt->localName); + $children[] = ($handler === null) ? Chunk::fromXML($elt) : $handler::fromXML($elt); } } - return new static($data); + return new static($data, $children); } @@ -110,8 +123,12 @@ public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - foreach ($this->getData() as $n) { - $n->toXML($e); + foreach ($this->getData() as $d) { + $d->toXML($e); + } + + foreach ($this->getChildren() as $c) { + $c->toXML($e); } return $e; diff --git a/src/XML/ds/X509IssuerName.php b/src/XML/ds/X509IssuerName.php index 3ddf66b0..9a240f13 100644 --- a/src/XML/ds/X509IssuerName.php +++ b/src/XML/ds/X509IssuerName.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:X509IssuerName element. @@ -13,14 +14,8 @@ */ final class X509IssuerName extends AbstractDsElement { - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/ds/X509SKI.php b/src/XML/ds/X509SKI.php index 0bb61594..04fda0de 100644 --- a/src/XML/ds/X509SKI.php +++ b/src/XML/ds/X509SKI.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:X509SKI element. @@ -13,14 +14,8 @@ */ final class X509SKI extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/ds/X509SerialNumber.php b/src/XML/ds/X509SerialNumber.php index 19ad7d08..144659ab 100644 --- a/src/XML/ds/X509SerialNumber.php +++ b/src/XML/ds/X509SerialNumber.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\IntegerElementTrait; +use SimpleSAML\XML\Type\IntegerValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:X509SerialNumber element. @@ -13,14 +14,8 @@ */ final class X509SerialNumber extends AbstractDsElement { - use IntegerElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = IntegerValue::class; } diff --git a/src/XML/ds/X509SubjectName.php b/src/XML/ds/X509SubjectName.php index 026be96a..018f3aed 100644 --- a/src/XML/ds/X509SubjectName.php +++ b/src/XML/ds/X509SubjectName.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:X509SubjectName element. @@ -13,14 +14,8 @@ */ final class X509SubjectName extends AbstractDsElement { - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/ds/XPath.php b/src/XML/ds/XPath.php index 54c35180..a78a4ee7 100644 --- a/src/XML/ds/XPath.php +++ b/src/XML/ds/XPath.php @@ -7,6 +7,9 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\XML\Type\StringValue; + +use function strval; /** * Class implementing the XPath element. @@ -18,10 +21,10 @@ class XPath extends AbstractDsElement /** * Construct an XPath element. * - * @param string $expression The XPath expression itself. + * @param \SimpleSAML\XML\Type\StringValue $expression The XPath expression itself. */ final public function __construct( - protected string $expression, + protected StringValue $expression, ) { } @@ -29,9 +32,9 @@ final public function __construct( /** * Get the actual XPath expression. * - * @return string + * @return \SimpleSAML\XML\Type\StringValue */ - public function getExpression(): string + public function getExpression(): StringValue { return $this->expression; } @@ -51,7 +54,7 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'XPath', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, self::NS, InvalidDOMElementException::class); - return new static($xml->textContent); + return new static(StringValue::fromString($xml->textContent)); } @@ -62,7 +65,7 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->textContent = $this->getExpression(); + $e->textContent = strval($this->getExpression()); return $e; } diff --git a/src/XML/ds/Y.php b/src/XML/ds/Y.php index 2d8b7daf..57c4f7e9 100644 --- a/src/XML/ds/Y.php +++ b/src/XML/ds/Y.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\ds; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a ds:Y element. @@ -13,14 +14,8 @@ */ final class Y extends AbstractDsElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/dsig11/X509Digest.php b/src/XML/dsig11/X509Digest.php index 607084dc..20dec7a6 100644 --- a/src/XML/dsig11/X509Digest.php +++ b/src/XML/dsig11/X509Digest.php @@ -6,14 +6,15 @@ use DOMElement; use SimpleSAML\Assert\Assert; -use SimpleSAML\XML\Base64ElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; -use SimpleSAML\XML\SchemaValidatableElementInterface; -use SimpleSAML\XML\SchemaValidatableElementTrait; +use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait}; +use SimpleSAML\XML\Type\{AnyURIValue, Base64BinaryValue}; use SimpleSAML\XMLSecurity\Constants as C; use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; +use function strval; + /** * Class representing a dsig11:X509Digest element. * @@ -21,38 +22,45 @@ */ final class X509Digest extends AbstractDsig11Element implements SchemaValidatableElementInterface { - use Base64ElementTrait; use SchemaValidatableElementTrait; /** * Initialize a X509Digest element. * - * @param string $digest - * @param string $algorithm + * @param \SimpleSAML\XML\Type\Base64BinaryValue $digest + * @param \SimpleSAML\XML\Type\AnyURIValue $algorithm */ public function __construct( - string $digest, - protected string $algorithm, + protected Base64BinaryValue $digest, + protected AnyURIValue $algorithm, ) { - Assert::validURI($algorithm, SchemaViolationException::class); Assert::oneOf( - $algorithm, + strval($algorithm), array_keys(C::$DIGEST_ALGORITHMS), 'Invalid digest method: %s', InvalidArgumentException::class, ); + } - $this->setContent($digest); + + /** + * Collect the value of the digest-property + * + * @return \SimpleSAML\XML\Type\Base64BinaryValue + */ + public function getDigest(): Base64BinaryValue + { + return $this->digest; } /** * Collect the value of the algorithm-property * - * @return string + * @return \SimpleSAML\XML\Type\AnyURIValue */ - public function getAlgorithm(): string + public function getAlgorithm(): AnyURIValue { return $this->algorithm; } @@ -72,9 +80,9 @@ public static function fromXML(DOMElement $xml): static Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class); Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class); - $algorithm = self::getAttribute($xml, 'Algorithm'); + $algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class); - return new static($xml->textContent, $algorithm); + return new static(Base64BinaryValue::fromString($xml->textContent), $algorithm); } @@ -87,8 +95,8 @@ public static function fromXML(DOMElement $xml): static public function toXML(?DOMElement $parent = null): DOMElement { $e = $this->instantiateParentElement($parent); - $e->textContent = $this->getContent(); - $e->setAttribute('Algorithm', $this->getAlgorithm()); + $e->textContent = strval($this->getContent()); + $e->setAttribute('Algorithm', strval($this->getAlgorithm())); return $e; } diff --git a/src/XML/xenc/CarriedKeyName.php b/src/XML/xenc/CarriedKeyName.php index 438f1289..3e601443 100644 --- a/src/XML/xenc/CarriedKeyName.php +++ b/src/XML/xenc/CarriedKeyName.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:CarriedKeyName element. @@ -13,14 +14,8 @@ */ final class CarriedKeyName extends AbstractXencElement { - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/xenc/CipherValue.php b/src/XML/xenc/CipherValue.php index 491a6cec..2a5a848e 100644 --- a/src/XML/xenc/CipherValue.php +++ b/src/XML/xenc/CipherValue.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:CipherValue element. @@ -13,14 +14,8 @@ */ final class CipherValue extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/xenc/Generator.php b/src/XML/xenc/Generator.php index 7adba9d2..6e31f231 100644 --- a/src/XML/xenc/Generator.php +++ b/src/XML/xenc/Generator.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:Generator element. @@ -13,14 +14,8 @@ */ final class Generator extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/xenc/KANonce.php b/src/XML/xenc/KANonce.php index 44e72e2a..086b313f 100644 --- a/src/XML/xenc/KANonce.php +++ b/src/XML/xenc/KANonce.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:KA-Nonce element. @@ -13,17 +14,11 @@ */ final class KANonce extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; /** @var string */ - public const LOCALNAME = 'KA-Nonce'; - + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const LOCALNAME = 'KA-Nonce'; } diff --git a/src/XML/xenc/OAEPparams.php b/src/XML/xenc/OAEPparams.php index d3808656..109c9068 100644 --- a/src/XML/xenc/OAEPparams.php +++ b/src/XML/xenc/OAEPparams.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:OAEPparams element. @@ -13,14 +14,8 @@ */ final class OAEPparams extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/xenc/P.php b/src/XML/xenc/P.php index a9f4a530..c2d08a2d 100644 --- a/src/XML/xenc/P.php +++ b/src/XML/xenc/P.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:P element. @@ -13,14 +14,8 @@ */ final class P extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/xenc/PgenCounter.php b/src/XML/xenc/PgenCounter.php index 5350a3c4..cec8c7bb 100644 --- a/src/XML/xenc/PgenCounter.php +++ b/src/XML/xenc/PgenCounter.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:pgenCounter element. @@ -13,17 +14,11 @@ */ final class PgenCounter extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; /** @var string */ - public const LOCALNAME = 'pgenCounter'; - + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const LOCALNAME = 'pgenCounter'; } diff --git a/src/XML/xenc/Q.php b/src/XML/xenc/Q.php index 379ecb54..16fe6c6c 100644 --- a/src/XML/xenc/Q.php +++ b/src/XML/xenc/Q.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64Value; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:Q element. @@ -13,14 +14,8 @@ */ final class Q extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; } diff --git a/src/XML/xenc/Seed.php b/src/XML/xenc/Seed.php index 7eb7823a..3b242b46 100644 --- a/src/XML/xenc/Seed.php +++ b/src/XML/xenc/Seed.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:seed element. @@ -13,17 +14,11 @@ */ final class Seed extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; /** @var string */ - public const LOCALNAME = 'seed'; - + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const LOCALNAME = 'seed'; } diff --git a/src/XML/xenc/XencPublic.php b/src/XML/xenc/XencPublic.php index d8169c0e..e63f79ee 100644 --- a/src/XML/xenc/XencPublic.php +++ b/src/XML/xenc/XencPublic.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc:Public element. @@ -13,17 +14,11 @@ */ final class XencPublic extends AbstractXencElement { - use Base64ElementTrait; + use TypedTextContentTrait; /** @var string */ - public const LOCALNAME = 'Public'; - + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const LOCALNAME = 'Public'; } diff --git a/src/XML/xenc11/DerivedKeyName.php b/src/XML/xenc11/DerivedKeyName.php index c51908b3..6c7ea8b5 100644 --- a/src/XML/xenc11/DerivedKeyName.php +++ b/src/XML/xenc11/DerivedKeyName.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc11; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc11:DerivedKeyName element. @@ -13,14 +14,8 @@ */ final class DerivedKeyName extends AbstractXenc11Element { - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/xenc11/MasterKeyName.php b/src/XML/xenc11/MasterKeyName.php index 6f079faf..dc2c6e21 100644 --- a/src/XML/xenc11/MasterKeyName.php +++ b/src/XML/xenc11/MasterKeyName.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc11; -use SimpleSAML\XML\StringElementTrait; +use SimpleSAML\XML\Type\StringValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc11:MasterKeyName element. @@ -13,14 +14,8 @@ */ final class MasterKeyName extends AbstractXenc11Element { - use StringElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = StringValue::class; } diff --git a/src/XML/xenc11/Specified.php b/src/XML/xenc11/Specified.php index 14ff6437..f45818a6 100644 --- a/src/XML/xenc11/Specified.php +++ b/src/XML/xenc11/Specified.php @@ -4,7 +4,8 @@ namespace SimpleSAML\XMLSecurity\XML\xenc11; -use SimpleSAML\XML\Base64ElementTrait; +use SimpleSAML\XML\Type\Base64BinaryValue; +use SimpleSAML\XML\TypedTextContentTrait; /** * Class representing a xenc11:Specified element. @@ -13,14 +14,8 @@ */ final class Specified extends AbstractXenc11Element { - use Base64ElementTrait; + use TypedTextContentTrait; - - /** - * @param string $content - */ - public function __construct(string $content) - { - $this->setContent($content); - } + /** @var string */ + public const TEXTCONTENT_TYPE = Base64BinaryValue::class; }