Skip to content

Commit

Permalink
Migrate to value types
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 17, 2025
1 parent feb95e1 commit b88f13d
Show file tree
Hide file tree
Showing 61 changed files with 483 additions and 610 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions src/Exception/ProtocolViolationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\Exception;

/**
* This exception may be raised when a violation of the xmldsig specification is detected
*
* @package simplesamlphp/xml-security
*/
class ProtocolViolationException extends RuntimeException
{
/**
* @param string|null $message
*/
public function __construct(?string $message = null)
{
if ($message === null) {
if (defined('static::DEFAULT_MESSAGE')) {
$message = static::DEFAULT_MESSAGE;
} else {
$message = 'A violation of the XML Signature Syntax and Processing specification occurred.';
}
}

parent::__construct($message);
}
}
3 changes: 1 addition & 2 deletions src/XML/ds/AbstractDSAKeyValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
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 function array_pop;

Expand Down
12 changes: 7 additions & 5 deletions src/XML/ds/AbstractKeyInfoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SerializableElementInterface;
use SimpleSAML\XML\Type\IDValue;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XMLSecurity\Assert\Assert;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;

use function strval;

/**
* Abstract class representing the KeyInfoType.
*
Expand Down Expand Up @@ -44,7 +47,7 @@ abstract class AbstractKeyInfoType extends AbstractDsElement
*/
final public function __construct(
protected array $info,
protected ?string $Id = null,
protected ?IDValue $Id = null,
) {
Assert::notEmpty(
$info,
Expand All @@ -61,7 +64,6 @@ final public function __construct(
SerializableElementInterface::class,
InvalidArgumentException::class,
);
Assert::nullOrValidNCName($Id);

foreach ($info as $item) {
if ($item instanceof AbstractDsElement) {
Expand All @@ -86,9 +88,9 @@ final 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;
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/XML/ds/AbstractPGPDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/XML/ds/AbstractSPKIDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
19 changes: 10 additions & 9 deletions src/XML/ds/CanonicalizationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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,
[
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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;
}
Expand Down
17 changes: 8 additions & 9 deletions src/XML/ds/DigestMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down
18 changes: 6 additions & 12 deletions src/XML/ds/DigestValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Loading

0 comments on commit b88f13d

Please sign in to comment.