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 6, 2024
1 parent 3fe7246 commit dc144d9
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 0 deletions.
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;
}
}
14 changes: 14 additions & 0 deletions src/XML/fed/ReferenceDigest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

/**
* A ReferenceDigest element
*
* @package tvdijen/ws-security
*/
final class ReferenceDigest extends AbstractReferenceDigestType
{
}
14 changes: 14 additions & 0 deletions src/XML/fed/ReferenceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

/**
* A ReferenceType element
*
* @package tvdijen/ws-security
*/
final class ReferenceType extends AbstractAttributeExtensibleURIType
{
}
14 changes: 14 additions & 0 deletions src/XML/fed/SerialNo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

/**
* A SerialNo element
*
* @package tvdijen/ws-security
*/
final class SerialNo extends AbstractAttributeExtensibleURIType
{
}
58 changes: 58 additions & 0 deletions tests/WSSecurity/XML/fed/ReferenceDigestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\sp;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\fed\ReferenceDigest;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\fed\ReferenceDigestTest
*
* @covers \SimpleSAML\WSSecurity\XML\fed\ReferenceDigest
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractReferenceDigestType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
*
* @package tvdijen/ws-security
*/
final class ReferenceDigestTest extends TestCase
{
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = ReferenceDigest::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/fed_ReferenceDigest.xml',
);
}


// test marshalling


/**
* Test creating a ReferenceDigest object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'testval1');
$referenceDigest = new ReferenceDigest('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=', [$attr1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($referenceDigest),
);
}
}
58 changes: 58 additions & 0 deletions tests/WSSecurity/XML/fed/ReferenceTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\sp;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\fed\ReferenceType;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\fed\ReferenceTypeTest
*
* @covers \SimpleSAML\WSSecurity\XML\fed\ReferenceType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractAttributeExtensibleURIType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
*
* @package tvdijen/ws-security
*/
final class ReferenceTypeTest extends TestCase
{
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = ReferenceType::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/fed_ReferenceType.xml',
);
}


// test marshalling


/**
* Test creating a ReferenceType object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'testval1');
$referenceType = new ReferenceType(C::NAMESPACE, [$attr1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($referenceType),
);
}
}
58 changes: 58 additions & 0 deletions tests/WSSecurity/XML/fed/SerialNoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\sp;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\fed\SerialNo;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\fed\SerialNoTest
*
* @covers \SimpleSAML\WSSecurity\XML\fed\SerialNo
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractAttributeExtensibleURIType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
*
* @package tvdijen/ws-security
*/
final class SerialNoTest extends TestCase
{
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = SerialNo::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/fed_SerialNo.xml',
);
}


// test marshalling


/**
* Test creating a SerialNo object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'testval1');
$serialNo = new SerialNo(C::NAMESPACE, [$attr1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($serialNo),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/fed_ReferenceDigest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<fed:ReferenceDigest xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=</fed:ReferenceDigest>
1 change: 1 addition & 0 deletions tests/resources/xml/fed_ReferenceType.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<fed:ReferenceType xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">urn:x-simplesamlphp:namespace</fed:ReferenceType>
1 change: 1 addition & 0 deletions tests/resources/xml/fed_SerialNo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<fed:SerialNo xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">urn:x-simplesamlphp:namespace</fed:SerialNo>

0 comments on commit dc144d9

Please sign in to comment.