-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
531 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wst; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\ExtendableElementTrait; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\MissingElementException; | ||
use SimpleSAML\XML\Exception\TooManyElementsException; | ||
use SimpleSAML\XML\XsNamespace as NS; | ||
use SimpleSAML\XML\SerializableElementInterface; | ||
|
||
/** | ||
* Class defining the ParticipantType element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
abstract class AbstractParticipantType extends AbstractWstElement | ||
{ | ||
use ExtendableElementTrait; | ||
|
||
/** The namespace-attribute for the xs:any element */ | ||
public const XS_ANY_ELT_NAMESPACE = NS::ANY; | ||
|
||
|
||
/** | ||
* AbstractParticipantType constructor | ||
* | ||
* @param \SimpleSAML\XML\SerializableElementInterface $child | ||
*/ | ||
final public function __construct( | ||
SerializableElementInterface $child | ||
) { | ||
$this->setElements([$child]); | ||
} | ||
|
||
|
||
/** | ||
* 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); | ||
|
||
$children = []; | ||
foreach ($xml->childNodes as $child) { | ||
if (!($child instanceof DOMElement)) { | ||
continue; | ||
} | ||
|
||
$children[] = new Chunk($child); | ||
} | ||
|
||
Assert::minCount($children, 1, MissingElementException::class); | ||
Assert::maxCount($children, 1, TooManyElementsException::class); | ||
|
||
return new static( | ||
array_pop($children), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Add this ParticipantType 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); | ||
|
||
foreach ($this->getElements() as $child) { | ||
if (!$child->isEmptyElement()) { | ||
$child->toXML($e); | ||
} | ||
} | ||
|
||
return $e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wst; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\ExtendableElementTrait; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\TooManyElementsException; | ||
use SimpleSAML\XML\XsNamespace as NS; | ||
use SimpleSAML\XML\SerializableElementInterface; | ||
|
||
/** | ||
* Class defining the ParticipantsType element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
abstract class AbstractParticipantsType extends AbstractWstElement | ||
{ | ||
use ExtendableElementTrait; | ||
|
||
/** The namespace-attribute for the xs:any element */ | ||
public const XS_ANY_ELT_NAMESPACE = NS::OTHER; | ||
|
||
|
||
/** | ||
* AbstractParticipantsType constructor | ||
* | ||
* @param \SimpleSAML\WSSecurity\XML\wst\Primary|null $primary | ||
* @param \SimpleSAML\WSSecurity\XML\wst\Participant[] $participant | ||
* @param \SimpleSAML\XML\SerializableElementInterface[] $children | ||
*/ | ||
final public function __construct( | ||
protected ?Primary $primary = null, | ||
protected array $participant = [], | ||
array $children = [] | ||
) { | ||
$this->setElements($children); | ||
} | ||
|
||
|
||
/** | ||
* @return \SimpleSAML\WSSecurity\XML\wst\Primary|null | ||
*/ | ||
public function getPrimary(): ?Primary | ||
{ | ||
return $this->primary; | ||
} | ||
|
||
|
||
/** | ||
* @return \SimpleSAML\WSSecurity\XML\wst\Participant[] | ||
*/ | ||
public function getParticipant(): array | ||
{ | ||
return $this->participant; | ||
} | ||
|
||
|
||
/** | ||
* 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->getPrimary()) | ||
&& empty($this->getParticipant()) | ||
&& 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); | ||
|
||
$primary = Primary::getChildrenOfClass($xml); | ||
Assert::maxCount($primary, 1, TooManyElementsException::class); | ||
|
||
$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($primary), | ||
Participant::getChildrenOfClass($xml), | ||
$children, | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Add this ParticipantsType to an XML element. | ||
* | ||
* @param \DOMElement $parent The element we should append this element to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = parent::instantiateParentElement($parent); | ||
|
||
$this->getPrimary()?->toXML($e); | ||
|
||
foreach ($this->getParticipant() as $p) { | ||
$p->toXML($e); | ||
} | ||
|
||
foreach ($this->getElements() as $child) { | ||
if (!$child->isEmptyElement()) { | ||
$child->toXML($e); | ||
} | ||
} | ||
|
||
return $e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wst; | ||
|
||
use SimpleSAML\WSSecurity\Constants as C; | ||
use SimpleSAML\XML\AbstractElement; | ||
|
||
/** | ||
* Abstract class to be implemented by all the classes in this namespace | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
abstract class AbstractWstElement extends AbstractElement | ||
{ | ||
/** @var string */ | ||
public const NS = C::NS_TRUST; | ||
|
||
/** @var string */ | ||
public const NS_PREFIX = 'wst'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wst; | ||
|
||
/** | ||
* A Participant element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class Participant extends AbstractParticipantType | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wst; | ||
|
||
/** | ||
* A Participants element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class Participants extends AbstractParticipantsType | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wst; | ||
|
||
/** | ||
* A Primary element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class Primary extends AbstractParticipantType | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 DOMDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Test\WSSecurity\Constants as C; | ||
use SimpleSAML\WSSecurity\XML\wst\Participant; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
|
||
use function dirname; | ||
|
||
/** | ||
* Class \SimpleSAML\WSSecurity\XML\wst\ParticipantTest | ||
* | ||
* @covers \SimpleSAML\WSSecurity\XML\wst\Participant | ||
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractParticipantType | ||
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class ParticipantTest extends TestCase | ||
{ | ||
use SerializableElementTestTrait; | ||
|
||
/** @var \SimpleSAML\XML\Chunk $chunk */ | ||
protected static Chunk $chunk; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = Participant::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 4) . '/resources/xml/wst_Participant.xml', | ||
); | ||
|
||
self::$chunk = new Chunk(DOMDocumentFactory::fromString( | ||
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>' | ||
)->documentElement); | ||
} | ||
|
||
|
||
// test marshalling | ||
|
||
|
||
/** | ||
* Test creating a Participant object from scratch. | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$participant = new Participant(self::$chunk); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($participant), | ||
); | ||
} | ||
} |
Oops, something went wrong.