diff --git a/src/Assert/CustomAssertionTrait.php b/src/Assert/CustomAssertionTrait.php index c450fa583..8d61a4b65 100644 --- a/src/Assert/CustomAssertionTrait.php +++ b/src/Assert/CustomAssertionTrait.php @@ -63,8 +63,6 @@ private static function validURI(string $value, string $message = ''): void } try { - BaseAssert::notWhitespaceOnly($value, $message ?: '%s is not a SAML2-compliant URI'); - // If it doesn't have a scheme, it's not an absolute URI BaseAssert::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML2-compliant URI'); } catch (AssertionFailedException $e) { @@ -82,6 +80,7 @@ private static function validEntityID(string $value, string $message = ''): void static::validURI($value); try { + BaseAssert::notWhitespaceOnly($value); BaseAssert::maxLength( $value, C::ENTITYID_MAX_LENGTH, diff --git a/src/XML/StringElementTrait.php b/src/XML/StringElementTrait.php new file mode 100644 index 000000000..c2d6fcb0d --- /dev/null +++ b/src/XML/StringElementTrait.php @@ -0,0 +1,39 @@ +baseValidateContent($content); Assert::same($content, 'http://edugain.org/'); } } diff --git a/src/XML/md/AbstractLocalizedName.php b/src/XML/md/AbstractLocalizedName.php index 273e5051f..91cad4395 100644 --- a/src/XML/md/AbstractLocalizedName.php +++ b/src/XML/md/AbstractLocalizedName.php @@ -7,11 +7,12 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Exception\ArrayValidationException; +use SimpleSAML\SAML2\Exception\ProtocolViolationException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\ArrayizableElementInterface; use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\MissingAttributeException; -use SimpleSAML\XML\StringElementTrait; use function array_key_first; @@ -35,25 +36,12 @@ final public function __construct( protected string $language, string $value, ) { - Assert::notEmpty($language, 'xml:lang cannot be empty.'); + Assert::notWhitespaceOnly($language, ProtocolViolationException::class); $this->setContent($value); } - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notEmpty($content); - } - - /** * Get the language this string is localized in. * diff --git a/src/XML/md/AdditionalMetadataLocation.php b/src/XML/md/AdditionalMetadataLocation.php index bc52ea67b..5753c8d3d 100644 --- a/src/XML/md/AdditionalMetadataLocation.php +++ b/src/XML/md/AdditionalMetadataLocation.php @@ -7,11 +7,11 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function trim; @@ -23,7 +23,7 @@ final class AdditionalMetadataLocation extends AbstractMdElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -52,19 +52,6 @@ public function getNamespace(): string } - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content, SchemaViolationException::class); // Covers the empty string - } - - /** * Initialize an AdditionalMetadataLocation element. * diff --git a/src/XML/md/AffiliateMember.php b/src/XML/md/AffiliateMember.php index 0b99a6424..b1d1f8485 100644 --- a/src/XML/md/AffiliateMember.php +++ b/src/XML/md/AffiliateMember.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\md; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing AffiliateMember. diff --git a/src/XML/md/AttributeProfile.php b/src/XML/md/AttributeProfile.php index fa02662ee..f80dcf339 100644 --- a/src/XML/md/AttributeProfile.php +++ b/src/XML/md/AttributeProfile.php @@ -7,10 +7,10 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing AttributeProfile. @@ -30,35 +30,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content); - } - - - /** - * Convert XML into a AttributeProfile - * - * @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, 'AttributeProfile', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, AttributeProfile::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } } diff --git a/src/XML/md/Company.php b/src/XML/md/Company.php index a456d3f46..378bc075f 100644 --- a/src/XML/md/Company.php +++ b/src/XML/md/Company.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\md; use SimpleSAML\Assert\Assert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing Company. @@ -27,17 +27,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notEmpty($content, 'Company cannot be empty'); - } } diff --git a/src/XML/md/EmailAddress.php b/src/XML/md/EmailAddress.php index 460aa9b21..c342fa5e3 100644 --- a/src/XML/md/EmailAddress.php +++ b/src/XML/md/EmailAddress.php @@ -6,10 +6,10 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Exception\ArrayValidationException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\ArrayizableElementInterface; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function array_key_first; use function preg_filter; @@ -26,7 +26,9 @@ final class EmailAddress extends AbstractMdElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use StringElementTrait { + StringElementTrait::validateContent as baseValidateContent; + } /** @@ -47,7 +49,7 @@ public function __construct(string $content) */ protected function validateContent(string $content): void { - Assert::notEmpty($content, 'EmailAddress cannot be empty'); + $this->baseValidateContent($content); Assert::email($content); } diff --git a/src/XML/md/GivenName.php b/src/XML/md/GivenName.php index e6c7dd491..a8c523625 100644 --- a/src/XML/md/GivenName.php +++ b/src/XML/md/GivenName.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\md; use SimpleSAML\Assert\Assert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing GivenName. @@ -27,17 +27,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notEmpty($content, 'GivenName cannot be empty'); - } } diff --git a/src/XML/md/NameIDFormat.php b/src/XML/md/NameIDFormat.php index b271864b6..b48b87ded 100644 --- a/src/XML/md/NameIDFormat.php +++ b/src/XML/md/NameIDFormat.php @@ -7,10 +7,10 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function trim; @@ -22,7 +22,7 @@ final class NameIDFormat extends AbstractMdElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -32,35 +32,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content); - } - - - /** - * Convert XML into an NameIDFormat - * - * @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, 'NameIDFormat', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, NameIDFormat::NS, InvalidDOMElementException::class); - - return new static(trim($xml->textContent)); - } } diff --git a/src/XML/md/SurName.php b/src/XML/md/SurName.php index 4ce4a0325..ab2d34f4f 100644 --- a/src/XML/md/SurName.php +++ b/src/XML/md/SurName.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\md; use SimpleSAML\Assert\Assert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing SurName. @@ -27,17 +27,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notEmpty($content, 'SurName cannot be empty'); - } } diff --git a/src/XML/md/TelephoneNumber.php b/src/XML/md/TelephoneNumber.php index e3e5c191e..2eca6e176 100644 --- a/src/XML/md/TelephoneNumber.php +++ b/src/XML/md/TelephoneNumber.php @@ -6,10 +6,10 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Exception\ArrayValidationException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\ArrayizableElementInterface; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function array_key_first; @@ -35,19 +35,6 @@ public function __construct(string $content) } - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notEmpty($content, 'TelephoneNumber cannot be empty'); - } - - /** * Create a class from an array * diff --git a/src/XML/mdui/DomainHint.php b/src/XML/mdui/DomainHint.php index 7fca048b0..715f9f2cf 100644 --- a/src/XML/mdui/DomainHint.php +++ b/src/XML/mdui/DomainHint.php @@ -6,9 +6,10 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Exception\InvalidArgumentException; +use SimpleSAML\SAML2\Exception\ProtocolViolationException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function filter_var; use function preg_replace; @@ -59,7 +60,7 @@ protected function sanitizeContent(string $content): string protected function validateContent(string $content): void { $sanitizedContent = $this->sanitizeContent($content); - Assert::notEmpty($sanitizedContent, 'DomainHint cannot be empty'); + Assert::notWhitespaceOnly($sanitizedContent, ProtocolViolationException::class); if (!filter_var($sanitizedContent, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { throw new InvalidArgumentException(sprintf('DomainHint is not a valid hostname; %s', $sanitizedContent)); diff --git a/src/XML/mdui/GeolocationHint.php b/src/XML/mdui/GeolocationHint.php index d9b9f1de2..c72a5e05e 100644 --- a/src/XML/mdui/GeolocationHint.php +++ b/src/XML/mdui/GeolocationHint.php @@ -6,9 +6,10 @@ use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\Exception\ProtocolViolationException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing GeolocationHint. @@ -67,7 +68,7 @@ protected function sanitizeContent(string $content): string */ protected function validateContent(string $content): void { - Assert::notEmpty($content, 'GeolocationHint cannot be empty'); + Assert::notWhitespaceOnly($content, ProtocolViolationException::class); // Assert::regex( // $content, // '/^geo:([-+]?\d+(?:\.\d+)?),([-+]?\d+(?:\.\d+)?)(?:\?z=(\d{1,2}))?$/', diff --git a/src/XML/mdui/IPHint.php b/src/XML/mdui/IPHint.php index c5ee890c0..5db3184a9 100644 --- a/src/XML/mdui/IPHint.php +++ b/src/XML/mdui/IPHint.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\mdui; use SimpleSAML\Assert\Assert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class implementing IPHint. @@ -27,17 +27,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notEmpty($content, 'IPHint cannot be empty'); - } } diff --git a/src/XML/mdui/Logo.php b/src/XML/mdui/Logo.php index e8e577180..2c553d356 100644 --- a/src/XML/mdui/Logo.php +++ b/src/XML/mdui/Logo.php @@ -9,11 +9,12 @@ use SimpleSAML\Assert\Assert; //use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; use SimpleSAML\SAML2\Exception\ArrayValidationException; +use SimpleSAML\SAML2\Exception\ProtocolViolationException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\ArrayizableElementInterface; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function filter_var; use function strval; @@ -62,7 +63,8 @@ public function __construct( protected function validateContent(string $content): void { // NOTE: we override the validateContent from the trait to be able to be less restrictive - // SAMLAssert::validURI($content, SchemaViolationException::class); // Covers the empty string + // SAMLAssert::validURI($content, SchemaViolationException::class); + Assert::notWhitespaceOnly($content, ProtocolViolationException::class); if (!filter_var(trim($content), FILTER_VALIDATE_URL) && substr(trim($content), 0, 5) !== 'data:') { throw new InvalidArgumentException('mdui:Logo is not a valid URL.'); } diff --git a/src/XML/saml/Action.php b/src/XML/saml/Action.php index 08f66e0d0..7621ad65a 100644 --- a/src/XML/saml/Action.php +++ b/src/XML/saml/Action.php @@ -7,8 +7,8 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\StringElementTrait; /** * Class representing SAML2 Action diff --git a/src/XML/saml/AssertionIDRef.php b/src/XML/saml/AssertionIDRef.php index 16abdb539..ab905d5b9 100644 --- a/src/XML/saml/AssertionIDRef.php +++ b/src/XML/saml/AssertionIDRef.php @@ -6,11 +6,11 @@ use DOMElement; use SimpleSAML\Assert\Assert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\Exception\SchemaViolationException; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing a saml:AssertionIDRef element. @@ -20,7 +20,9 @@ final class AssertionIDRef extends AbstractSamlElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use StringElementTrait { + StringElementTrait::validateContent as baseValidateContent; + } /** @@ -41,39 +43,7 @@ public function __construct(string $content) */ protected function validateContent(string $content): void { - Assert::validNCName($content, SchemaViolationException::class); // Covers the empty string - } - - - /** - * Convert XML into an AssertionIDRef - * - * @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, 'AssertionIDRef', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, AssertionIDRef::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } - - - /** - * Convert this AssertionIDRef to XML. - * - * @param \DOMElement $parent The element we are converting to XML. - * @return \DOMElement The XML element after adding the data corresponding to this AssertionIDRef. - */ - public function toXML(?DOMElement $parent = null): DOMElement - { - $element = $this->instantiateParentElement($parent); - $element->textContent = $this->getContent(); - - return $element; + $this->baseValidateContent($content); + Assert::validNCName($content, SchemaViolationException::class); } } diff --git a/src/XML/saml/AssertionURIRef.php b/src/XML/saml/AssertionURIRef.php index 364624580..16634123f 100644 --- a/src/XML/saml/AssertionURIRef.php +++ b/src/XML/saml/AssertionURIRef.php @@ -4,13 +4,9 @@ namespace SimpleSAML\SAML2\XML\saml; -use DOMElement; -use SimpleSAML\Assert\Assert; -use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing a saml:AssertionURIRef element. @@ -20,7 +16,7 @@ final class AssertionURIRef extends AbstractSamlElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -30,50 +26,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content); - } - - - /** - * Convert XML into an AssertionURIRef - * - * @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, 'AssertionURIRef', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, AssertionURIRef::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } - - - /** - * Convert this AssertionURIRef to XML. - * - * @param \DOMElement $parent The element we are converting to XML. - * @return \DOMElement The XML element after adding the data corresponding to this Condition. - */ - public function toXML(?DOMElement $parent = null): DOMElement - { - $element = $this->instantiateParentElement($parent); - $element->textContent = $this->getContent(); - - return $element; - } } diff --git a/src/XML/saml/Audience.php b/src/XML/saml/Audience.php index 914e55637..30b56494f 100644 --- a/src/XML/saml/Audience.php +++ b/src/XML/saml/Audience.php @@ -4,13 +4,10 @@ namespace SimpleSAML\SAML2\XML\saml; -use DOMElement; -use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing a saml:Audience element. @@ -20,7 +17,7 @@ final class Audience extends AbstractSamlElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -43,37 +40,4 @@ protected function validateContent(string $content): void { SAMLAssert::validEntityID($content); } - - - /** - * Convert XML into an Audience - * - * @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, 'Audience', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, Audience::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } - - - /** - * Convert this Audience to XML. - * - * @param \DOMElement $parent The element we are converting to XML. - * @return \DOMElement The XML element after adding the data corresponding to this Audience. - */ - public function toXML(?DOMElement $parent = null): DOMElement - { - $element = $this->instantiateParentElement($parent); - $element->textContent = $this->getContent(); - - return $element; - } } diff --git a/src/XML/saml/AuthenticatingAuthority.php b/src/XML/saml/AuthenticatingAuthority.php index f53f21e8d..2fabef427 100644 --- a/src/XML/saml/AuthenticatingAuthority.php +++ b/src/XML/saml/AuthenticatingAuthority.php @@ -4,13 +4,10 @@ namespace SimpleSAML\SAML2\XML\saml; -use DOMElement; -use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing a saml:AuthenticatingAuthority element. @@ -20,7 +17,7 @@ final class AuthenticatingAuthority extends AbstractSamlElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -41,24 +38,6 @@ public function __construct(string $content) */ protected function validateContent(string $content): void { - SAMLAssert::validURI($content); - } - - - /** - * Convert XML into an AuthenticatingAuthority - * - * @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, 'AuthenticatingAuthority', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, AuthenticatingAuthority::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); + SAMLAssert::validEntityID($content); } } diff --git a/src/XML/saml/AuthnContextClassRef.php b/src/XML/saml/AuthnContextClassRef.php index 3e012cfa1..45602812f 100644 --- a/src/XML/saml/AuthnContextClassRef.php +++ b/src/XML/saml/AuthnContextClassRef.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\saml; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing SAML2 AuthnContextClassRef @@ -17,7 +17,7 @@ final class AuthnContextClassRef extends AbstractSamlElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -27,17 +27,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content); - } } diff --git a/src/XML/saml/AuthnContextDeclRef.php b/src/XML/saml/AuthnContextDeclRef.php index 707b08710..f3e2e582e 100644 --- a/src/XML/saml/AuthnContextDeclRef.php +++ b/src/XML/saml/AuthnContextDeclRef.php @@ -5,9 +5,9 @@ namespace SimpleSAML\SAML2\XML\saml; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing SAML2 AuthnContextDeclRef @@ -17,7 +17,7 @@ final class AuthnContextDeclRef extends AbstractSamlElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -27,17 +27,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content); - } } diff --git a/src/XML/saml/NameIDType.php b/src/XML/saml/NameIDType.php index 46f224970..825b118d5 100644 --- a/src/XML/saml/NameIDType.php +++ b/src/XML/saml/NameIDType.php @@ -7,8 +7,8 @@ use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\StringElementTrait; /** * SAML NameIDType abstract data type. diff --git a/src/XML/samlp/GetComplete.php b/src/XML/samlp/GetComplete.php index affc3f535..4e7ea7514 100644 --- a/src/XML/samlp/GetComplete.php +++ b/src/XML/samlp/GetComplete.php @@ -4,14 +4,11 @@ namespace SimpleSAML\SAML2\XML\samlp; -use DOMElement; use SimpleSAML\Assert\Assert; -use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; use SimpleSAML\SAML2\Exception\ArrayValidationException; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function array_key_first; @@ -23,7 +20,7 @@ final class GetComplete extends AbstractSamlpElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -35,37 +32,6 @@ public function __construct(string $content) } - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validURI($content); - } - - - /** - * Convert XML into an GetComplete - * - * @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, 'GetComplete', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, GetComplete::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } - - /** * Create a class from an array * diff --git a/src/XML/samlp/RequesterID.php b/src/XML/samlp/RequesterID.php index 0808dd2ed..741037d8e 100644 --- a/src/XML/samlp/RequesterID.php +++ b/src/XML/samlp/RequesterID.php @@ -4,14 +4,11 @@ namespace SimpleSAML\SAML2\XML\samlp; -use DOMElement; use SimpleSAML\Assert\Assert; -use SimpleSAML\SAML2\Assert\Assert as SAMLAssert; use SimpleSAML\SAML2\Exception\ArrayValidationException; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\URIElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; use function array_key_first; @@ -23,7 +20,7 @@ final class RequesterID extends AbstractSamlpElement implements SchemaValidatableElementInterface { use SchemaValidatableElementTrait; - use StringElementTrait; + use URIElementTrait; /** @@ -35,37 +32,6 @@ public function __construct(string $content) } - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - SAMLAssert::validEntityID($content); - } - - - /** - * Convert XML into an RequesterID - * - * @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, 'RequesterID', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, RequesterID::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } - - /** * Create a class from an array * diff --git a/src/XML/samlp/SessionIndex.php b/src/XML/samlp/SessionIndex.php index 2d58e589d..6d7bee65b 100644 --- a/src/XML/samlp/SessionIndex.php +++ b/src/XML/samlp/SessionIndex.php @@ -4,12 +4,9 @@ namespace SimpleSAML\SAML2\XML\samlp; -use DOMElement; -use SimpleSAML\Assert\Assert; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing a samlp:SessionIndex element. @@ -29,35 +26,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notWhitespaceOnly($content); - } - - - /** - * Convert XML into an SessionIndex - * - * @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, 'SessionIndex', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, SessionIndex::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } } diff --git a/src/XML/samlp/StatusMessage.php b/src/XML/samlp/StatusMessage.php index 378db03a0..00ee4b4f9 100644 --- a/src/XML/samlp/StatusMessage.php +++ b/src/XML/samlp/StatusMessage.php @@ -4,12 +4,9 @@ namespace SimpleSAML\SAML2\XML\samlp; -use DOMElement; -use SimpleSAML\Assert\Assert; -use SimpleSAML\XML\Exception\InvalidDOMElementException; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class representing a samlp:StatusMessage element. @@ -29,35 +26,4 @@ public function __construct(string $content) { $this->setContent($content); } - - - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notWhitespaceOnly($content); - } - - - /** - * Convert XML into an StatusMessage - * - * @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, 'StatusMessage', InvalidDOMElementException::class); - Assert::same($xml->namespaceURI, StatusMessage::NS, InvalidDOMElementException::class); - - return new static($xml->textContent); - } } diff --git a/src/XML/shibmd/Scope.php b/src/XML/shibmd/Scope.php index 6e087dbfa..1635d22da 100644 --- a/src/XML/shibmd/Scope.php +++ b/src/XML/shibmd/Scope.php @@ -6,10 +6,10 @@ use DOMElement; use SimpleSAML\Assert\Assert; +use SimpleSAML\SAML2\XML\StringElementTrait; use SimpleSAML\XML\Exception\InvalidDOMElementException; use SimpleSAML\XML\SchemaValidatableElementInterface; use SimpleSAML\XML\SchemaValidatableElementTrait; -use SimpleSAML\XML\StringElementTrait; /** * Class which represents the Scope element found in Shibboleth metadata. @@ -37,19 +37,6 @@ public function __construct( } - /** - * Validate the content of the element. - * - * @param string $content The value to go in the XML textContent - * @throws \Exception on failure - * @return void - */ - protected function validateContent(string $content): void - { - Assert::notWhitespaceOnly($content); - } - - /** * Collect the value of the regexp-property * diff --git a/tests/SAML2/XML/md/AbstractLocalizedNameTest.php b/tests/SAML2/XML/md/AbstractLocalizedNameTest.php index bd25ac7d6..f33c39bf5 100644 --- a/tests/SAML2/XML/md/AbstractLocalizedNameTest.php +++ b/tests/SAML2/XML/md/AbstractLocalizedNameTest.php @@ -10,10 +10,12 @@ use PHPUnit\Framework\TestCase; use SimpleSAML\Assert\AssertionFailedException; use SimpleSAML\SAML2\Constants as C; +use SimpleSAML\SAML2\Exception\ProtocolViolationException; use SimpleSAML\SAML2\XML\md\AbstractLocalizedName; use SimpleSAML\SAML2\XML\md\AbstractMdElement; use SimpleSAML\SAML2\XML\md\ServiceDescription; use SimpleSAML\XML\DOMDocumentFactory; +use SimpleSAML\XML\Exception\MissingAttributeException; use function dirname; @@ -50,8 +52,8 @@ public static function setUpBeforeClass(): void */ public function testMarshallingWithEmptyLang(): void { - $this->expectException(AssertionFailedException::class); - $this->expectExceptionMessage('xml:lang cannot be empty.'); + $this->expectException(ProtocolViolationException::class); + $this->expectExceptionMessage('Expected a non-whitespace string. Got: ""'); new ServiceDescription('', 'Academic Journals R US and only us'); } @@ -62,8 +64,8 @@ public function testMarshallingWithEmptyLang(): void */ public function testMarshallingWithEmptyValue(): void { - $this->expectException(AssertionFailedException::class); - $this->expectExceptionMessage('Expected a non-empty value. Got: ""'); + $this->expectException(ProtocolViolationException::class); + $this->expectExceptionMessage('Expected a non-whitespace string. Got: ""'); new ServiceDescription('en', ''); } @@ -80,7 +82,7 @@ public function testUnmarshallingWithoutLang(): void $xmlRepresentation = clone self::$xmlRepresentation; $xmlRepresentation->documentElement->removeAttributeNS(C::NS_XML, 'lang'); - $this->expectException(AssertionFailedException::class); + $this->expectException(MissingAttributeException::class); $this->expectExceptionMessage('Missing xml:lang from ServiceDescription'); ServiceDescription::fromXML($xmlRepresentation->documentElement); @@ -95,8 +97,8 @@ public function testUnmarshallingWithEmptyLang(): void $xmlRepresentation = clone self::$xmlRepresentation; $xmlRepresentation->documentElement->setAttributeNS(C::NS_XML, 'lang', ''); - $this->expectException(AssertionFailedException::class); - $this->expectExceptionMessage('xml:lang cannot be empty.'); + $this->expectException(ProtocolViolationException::class); + $this->expectExceptionMessage('Expected a non-whitespace string. Got: ""'); ServiceDescription::fromXML($xmlRepresentation->documentElement); } @@ -110,8 +112,8 @@ public function testUnmarshallingWithEmptyValue(): void $xmlRepresentation = clone self::$xmlRepresentation; $xmlRepresentation->documentElement->textContent = ''; - $this->expectException(AssertionFailedException::class); - $this->expectExceptionMessage('Expected a non-empty value. Got: ""'); + $this->expectException(ProtocolViolationException::class); + $this->expectExceptionMessage('Expected a non-whitespace string. Got: ""'); ServiceDescription::fromXML($xmlRepresentation->documentElement); } diff --git a/tests/SAML2/XML/md/ServiceNameTest.php b/tests/SAML2/XML/md/ServiceNameTest.php index 3c7d8543e..abd7928e7 100644 --- a/tests/SAML2/XML/md/ServiceNameTest.php +++ b/tests/SAML2/XML/md/ServiceNameTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase; use SimpleSAML\Assert\AssertionFailedException; use SimpleSAML\SAML2\Constants as C; +use SimpleSAML\SAML2\Exception\ProtocolViolationException; use SimpleSAML\SAML2\XML\md\AbstractLocalizedName; use SimpleSAML\SAML2\XML\md\AbstractMdElement; use SimpleSAML\SAML2\XML\md\ServiceName; @@ -72,8 +73,8 @@ public function testMarshalling(): void */ public function testMarshallingWithEmptyLang(): void { - $this->expectException(AssertionFailedException::class); - $this->expectExceptionMessage('xml:lang cannot be empty.'); + $this->expectException(ProtocolViolationException::class); + $this->expectExceptionMessage('Expected a non-whitespace string. Got: ""'); new ServiceName('', 'Academic Journals R US'); } @@ -105,8 +106,8 @@ public function testUnmarshallingWithEmptyLang(): void $xmlRepresentation = clone self::$xmlRepresentation; $xmlRepresentation->documentElement->setAttributeNS(C::NS_XML, 'lang', ''); - $this->expectException(AssertionFailedException::class); - $this->expectExceptionMessage('xml:lang cannot be empty.'); + $this->expectException(ProtocolViolationException::class); + $this->expectExceptionMessage('Expected a non-whitespace string. Got: ""'); ServiceName::fromXML($xmlRepresentation->documentElement); }