Skip to content

Commit

Permalink
WIP: create fed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 7, 2024
1 parent 4ba3c7e commit 7a741eb
Show file tree
Hide file tree
Showing 29 changed files with 1,279 additions and 1 deletion.
87 changes: 87 additions & 0 deletions src/XML/fed/AbstractAttributeExtensibleURIType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\StringElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* An AbstractAttributeExtensibleURIType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractAttributeExtensibleURIType extends AbstractFedElement
{
use ExtendableAttributesTrait;
use StringElementTrait;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* @param string $content
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
public function __construct(string $content, array $namespacedAttributes)
{
$this->setContent($content);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Validate the content of the element.
*
* @param string $content The value to go in the XML textContent
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
* @return void
*/
protected function validateContent(string $content): void
{
Assert::validURI($content, SchemaViolationException::class);
}


/**
* Create a class from XML
*
* @param \DOMElement $xml
* @return static
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new static(
$xml->textContent,
self::getAttributesNSFromXML($xml),
);
}


/**
* Create XML from this class
*
* @param \DOMElement|null $parent
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->textContent = $this->getContent();

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

return $e;
}
}
125 changes: 125 additions & 0 deletions src/XML/fed/AbstractFreshnessType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\StringElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function intval;
use function sprintf;
use function strval;

/**
* Class defining the FreshnessType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractFreshnessType extends AbstractFedElement
{
use StringElementTrait;
use ExtendableAttributesTrait;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* AbstractFreshnessType constructor
*
* @param int $content
* @param bool|null $AllowCache
* @param array $namespacedAttributes
*/
public function __construct(
int $content,
protected ?bool $AllowCache = null,
array $namespacedAttributes = []
) {
$this->setContent(strval($content));
$this->setAttributesNS($namespacedAttributes);
}


/**
* 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::natural(
intval($content),
sprintf(
'The value \'%s\' of an %s:%s element must an unsigned integer.',
$content,
static::NS_PREFIX,
static::getLocalName(),
),
);
}


/**
* @return bool|null
*/
public function getAllowCache(): ?bool
{
return $this->AllowCache;
}


/**
* Create an instance of this object from its XML representation.
*
* @param \DOMElement $xml
* @return static
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* if the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
Assert::integerish($xml->textContent, SchemaValidationException::class);

return new static(
intval($xml->textContent),
self::getOptionalBooleanAttribute($xml, 'AllowCache', null),
self::getAttributesNSFromXML($xml),
);
}


/**
* Add this IssuerNameType to an XML element.
*
* @param \DOMElement $parent The element we should append this issuer name to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);
$e->textContent = $this->getContent();

if ($this->getAllowCache() !== null) {
$e->setAttribute('AllowCache', $this->getAllowCache() ? 'true' : 'false');
}

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

return $e;
}
}
74 changes: 74 additions & 0 deletions src/XML/fed/AbstractReferenceDigestType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Base64ElementTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* An AbstractReferenceDigestType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractReferenceDigestType extends AbstractFedElement
{
use ExtendableAttributesTrait;
use Base64ElementTrait;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* @param string $content
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
public function __construct(string $content, array $namespacedAttributes)
{
$this->setContent($content);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Create a class from XML
*
* @param \DOMElement $xml
* @return static
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new static(
$xml->textContent,
self::getAttributesNSFromXML($xml),
);
}


/**
* Create XML from this class
*
* @param \DOMElement|null $parent
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->textContent = $this->getContent();

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

return $e;
}
}
Loading

0 comments on commit 7a741eb

Please sign in to comment.