Skip to content

Commit

Permalink
Add wst classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 23, 2024
1 parent 3f2b112 commit 00c0eca
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 20 deletions.
25 changes: 19 additions & 6 deletions src/XML/wst/AbstractAuthenticatorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
use function array_pop;

/**
* Class defining the AuthenticatorTokenType element
* Class defining the AuthenticatorType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractAuthenticatorTokenType extends AbstractWstElement
abstract class AbstractAuthenticatorType extends AbstractWstElement
{
use ExtendableElementTrait;

Expand All @@ -27,19 +27,28 @@ abstract class AbstractAuthenticatorTokenType extends AbstractWstElement


/**
* AbstractAuthenticatorTokenType constructor
* AbstractAuthenticatorType constructor
*
* @param \SimpleSAML\WSSecurity\XML\wst\CombinedHash|null $combinedHash
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
*/
final public function __construct(
protected ?CombinedHash $combinedHashh = null,
protected ?CombinedHash $combinedHash = null,
array $children = []
) {
$this->setElements($children);
}


/**
* @return \SimpleSAML\WSSecurity\XML\wst\CombinedHash|null
*/
public function getCombinedHash(): ?CombinedHash
{
return $this->combinedHash;
}


/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
Expand Down Expand Up @@ -72,20 +81,22 @@ public static function fromXML(DOMElement $xml): static
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
} elseif ($child->namespaceURI === static::NS) {
continue;
}

$children[] = new Chunk($child);
}

return new static(
array_pop($combined_hash),
array_pop($combinedHash),
$children,
);
}


/**
* Add this AuthenticatorTokenType to an XML element.
* Add this AuthenticatorType to an XML element.
*
* @param \DOMElement $parent The element we should append this username token to.
* @return \DOMElement
Expand All @@ -94,6 +105,8 @@ public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);

$this->getCombinedHash()?->toXML($e);

foreach ($this->getElements() as $child) {
if (!$child->isEmptyElement()) {
$child->toXML($e);
Expand Down
118 changes: 118 additions & 0 deletions src/XML/wst/AbstractSignChallengeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function array_pop;

/**
* Class defining the SignChallengeType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractSignChallengeType extends AbstractWstElement
{
use ExtendableElementTrait;

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


/**
* AbstractSignChallengeType constructor
*
* @param \SimpleSAML\WSSecurity\XML\wst\Challenge|null $Challenge
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
*/
final public function __construct(
protected ?Challenge $Challenge = null,
array $children = []
) {
$this->setElements($children);
}


/**
* @return \SimpleSAML\WSSecurity\XML\wst\Challenge|null
*/
public function getChallenge(): ?Challenge
{
return $this->Challenge;
}


/**
* Test if an object, at the state it's in, would produce an empty XML-element
*
* @return bool
*/
public function isEmptyElement(): bool
{
return empty($this->getChallenge())
&& empty($this->getElements());
}


/**
* 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);

$challenge = Challenge::getChildrenOfClass($xml);

$children = [];
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
} elseif ($child->namespaceURI === static::NS) {
continue;
}

$children[] = new Chunk($child);
}

return new static(
array_pop($challenge),
$children,
);
}


/**
* Add this SignChallengeType to an XML element.
*
* @param \DOMElement $parent The element we should append this username token to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);

$this->getChallenge()?->toXML($e);

foreach ($this->getElements() as $child) {
if (!$child->isEmptyElement()) {
$child->toXML($e);
}
}

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

/**
* An SignChallenge element
*
* @package tvdijen/ws-security
*/
final class SignChallenge extends AbstractSignChallengeType
{
}
28 changes: 14 additions & 14 deletions tests/WSSecurity/XML/wst/AuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\wst\AuthenticatorToken;
use SimpleSAML\WSSecurity\XML\wst\Authenticator;
use SimpleSAML\WSSecurity\XML\wst\CombinedHash;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
Expand All @@ -14,15 +14,15 @@
use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\AuthenticatorTokenTest
* Class \SimpleSAML\WSSecurity\XML\wst\AuthenticatorTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\AuthenticatorToken
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractAuthenticatorTokenType
* @covers \SimpleSAML\WSSecurity\XML\wst\Authenticator
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractAuthenticatorType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class AuthenticatorTokenTest extends TestCase
final class AuthenticatorTest extends TestCase
{
use SerializableElementTestTrait;

Expand All @@ -34,10 +34,10 @@ final class AuthenticatorTokenTest extends TestCase
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = AuthenticatorToken::class;
self::$testedClass = Authenticator::class;

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

self::$chunk = new Chunk(DOMDocumentFactory::fromString(
Expand All @@ -50,27 +50,27 @@ public static function setUpBeforeClass(): void


/**
* Test creating a AuthenticatorToken object from scratch.
* Test creating a Authenticator object from scratch.
*/
public function testMarshalling(): void
{
$combinedHash = new CombinedHash('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI');
$authenticatorToken = new AuthenticatorToken($combinedHash, [self::$chunk]);
$combinedHash = new CombinedHash('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=');
$authenticator = new Authenticator($combinedHash, [self::$chunk]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($authenticatorToken),
strval($authenticator),
);
}


/**
* Test creating an empty AuthenticatorToken object from scratch.
* Test creating an empty Authenticator object from scratch.
*/
public function testMarshallingEmpty(): void
{
$authenticatorToken = new AuthenticatorToken();
$authenticator = new Authenticator();

$this->assertTrue($authenticatorToken->isEmptyElement());
$this->assertTrue($authenticator->isEmptyElement());
}
}
65 changes: 65 additions & 0 deletions tests/WSSecurity/XML/wst/SignChallengeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\wst\SignChallenge;
use SimpleSAML\WSSecurity\XML\wst\Challenge;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\SignChallengeTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\SignChallenge
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractSignChallengeType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class SignChallengeTest extends TestCase
{
use SerializableElementTestTrait;

/** @var \SimpleSAML\XML\Chunk $chunk */
protected static Chunk $chunk;


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

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

self::$chunk = new Chunk(DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>'
)->documentElement);
}


// test marshalling


/**
* Test creating a SignChallenge object from scratch.
*/
public function testMarshalling(): void
{
$challenge = new Challenge('accepted');
$signChallenge = new SignChallenge($challenge, [self::$chunk]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($signChallenge),
);
}
}
4 changes: 4 additions & 0 deletions tests/resources/xml/wst_SignChallenge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wst:SignChallenge xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512/">
<wst:Challenge>accepted</wst:Challenge>
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>
</wst:SignChallenge>

0 comments on commit 00c0eca

Please sign in to comment.