-
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
571 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\sp; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\MissingElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\Exception\TooManyElementsException; | ||
use SimpleSAML\XML\ExtendableAttributesTrait; | ||
use SimpleSAML\XML\ExtendableElementTrait; | ||
use SimpleSAML\XML\XsNamespace as NS; | ||
use ValueError; | ||
|
||
use function array_pop; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
/** | ||
* Class representing WS security policy TokenAssertionType. | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
abstract class AbstractTokenAssertionType extends AbstractSpElement | ||
{ | ||
use ExtendableAttributesTrait; | ||
use ExtendableElementTrait; | ||
use IncludeTokenTypeTrait; | ||
|
||
/** The namespace-attribute for the xs:any element */ | ||
public const XS_ANY_ELT_NAMESPACE = NS::OTHER; | ||
|
||
/** The namespace-attribute for the xs:anyAttribute element */ | ||
public const XS_ANY_ATTR_NAMESPACE = NS::ANY; | ||
|
||
|
||
/** | ||
* TokenAssertionType constructor. | ||
* | ||
* @param \SimpleSAML\WSSecurity\XML\sp\IncludeToken|null $includeToken | ||
* @param list<\SimpleSAML\XML\ElementInterface> $elts | ||
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes | ||
*/ | ||
final public function __construct( | ||
?IncludeToken $includeToken = null, | ||
array $elts = [], | ||
array $namespacedAttributes = [] | ||
) { | ||
$this->setIncludeToken($includeToken); | ||
$this->setElements($elts); | ||
$this->setAttributesNS($namespacedAttributes); | ||
} | ||
|
||
|
||
/** | ||
* 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->getIncludeToken()) | ||
&& empty($this->getAttributesNS()) | ||
&& empty($this->getElements()); | ||
} | ||
|
||
|
||
/** | ||
* Initialize an TokenAssertionType. | ||
* | ||
* Note: this method cannot be used when extending this class, if the constructor has a different signature. | ||
* | ||
* @param \DOMElement $xml The XML element we should load. | ||
* @return static | ||
* | ||
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException | ||
* if the qualified name of the supplied element is wrong | ||
*/ | ||
public static function fromXML(DOMElement $xml): static | ||
{ | ||
$qualifiedName = static::getClassName(static::class); | ||
Assert::eq( | ||
$xml->localName, | ||
$qualifiedName, | ||
sprintf('Unexpected name for TokenAssertionType: %s. Expected: %s.', $xml->localName, $qualifiedName), | ||
InvalidDOMElementException::class | ||
); | ||
|
||
try { | ||
$includeToken = IncludeToken::from(self::getOptionalAttribute($xml, 'IncludeToken', null)); | ||
} catch (ValueError) { | ||
self::getOptionalAttribute($xml, 'IncludeToken', null); | ||
} | ||
|
||
$elements = []; | ||
foreach ($xml->childNodes as $element) { | ||
if ($element->namespaceURI === static::NS) { | ||
continue; | ||
} elseif (!($element instanceof DOMElement)) { | ||
continue; | ||
} | ||
|
||
$elements[] = new Chunk($element); | ||
} | ||
|
||
$namespacedAttributes = self::getAttributesNSFromXML($xml); | ||
foreach ($namespacedAttributes as $i => $attr) { | ||
if ($attr->getNamespaceURI() === null) { | ||
if ($attr->getAttrName() === 'IncludeToken') { | ||
unset($namespacedAttributes[$i]); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
return new static( | ||
$includeToken, | ||
$elements, | ||
$namespacedAttributes, | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Convert this element to XML. | ||
* | ||
* @param \DOMElement|null $parent The element we should append this element to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
|
||
if ($this->getIncludeToken() !== null) { | ||
$e->setAttribute( | ||
'IncludeToken', | ||
is_string($this->getIncludeToken()) ? $this->getIncludeToken() : $this->getIncludeToken()->value, | ||
); | ||
} | ||
|
||
foreach ($this->getElements() as $elt) { | ||
$elt->toXML($e); | ||
} | ||
|
||
foreach ($this->getAttributesNS() as $attr) { | ||
$attr->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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\sp; | ||
|
||
use SimpleSAML\Assert\Assert; | ||
|
||
/** | ||
* An HttpsToken element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class HttpsToken extends AbstractTokenAssertionType | ||
{ | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\sp; | ||
|
||
use SimpleSAML\Assert\Assert; | ||
|
||
/** | ||
* An RelToken element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class RelToken extends AbstractTokenAssertionType | ||
{ | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\sp; | ||
|
||
use SimpleSAML\Assert\Assert; | ||
|
||
/** | ||
* An SamlToken element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class SamlToken extends AbstractTokenAssertionType | ||
{ | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\sp; | ||
|
||
use SimpleSAML\Assert\Assert; | ||
|
||
/** | ||
* An UsernameToken element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class UsernameToken extends AbstractTokenAssertionType | ||
{ | ||
} |
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\WSSecurity\XML\sp; | ||
|
||
use DOMDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Test\WSSecurity\Constants as C; | ||
use SimpleSAML\WSSecurity\Utils\XPath; | ||
use SimpleSAML\WSSecurity\XML\sp\HttpsToken; | ||
use SimpleSAML\WSSecurity\XML\sp\IncludeToken; | ||
use SimpleSAML\XML\Attribute as XMLAttribute; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
|
||
use function dirname; | ||
|
||
/** | ||
* Class \SimpleSAML\WSSecurity\XML\sp\HttpsTokenTest | ||
* | ||
* @covers \SimpleSAML\WSSecurity\XML\sp\HttpsToken | ||
* @covers \SimpleSAML\WSSecurity\XML\sp\AbstractAssertionTokenType | ||
* @covers \SimpleSAML\WSSecurity\XML\sp\AbstractSpElement | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class HttpsTokenTest extends TestCase | ||
{ | ||
use SchemaValidationTestTrait; | ||
use SerializableElementTestTrait; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-securitypolicy-1.2.xsd'; | ||
|
||
self::$testedClass = HttpsToken::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 4) . '/resources/xml/sp_HttpsToken.xml', | ||
); | ||
} | ||
|
||
|
||
// test marshalling | ||
|
||
|
||
/** | ||
* Adding an empty HttpsToken element should yield an empty element. | ||
*/ | ||
public function testMarshallingEmptyElement(): void | ||
{ | ||
$spns = C::NS_SEC_POLICY; | ||
$httpsToken = new HttpsToken(); | ||
$this->assertEquals( | ||
"<sp:HttpsToken xmlns:sp=\"$spns\"/>", | ||
strval($httpsToken), | ||
); | ||
$this->assertTrue($httpsToken->isEmptyElement()); | ||
} | ||
|
||
|
||
/** | ||
* Test that creating a HttpsToken from scratch works. | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1'); | ||
$chunk = new Chunk(DOMDocumentFactory::fromString( | ||
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>' | ||
)->documentElement); | ||
|
||
$httpsToken = new HttpsToken(IncludeToken::Always, [$chunk], [$attr]); | ||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($httpsToken), | ||
); | ||
} | ||
} |
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\WSSecurity\XML\sp; | ||
|
||
use DOMDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Test\WSSecurity\Constants as C; | ||
use SimpleSAML\WSSecurity\Utils\XPath; | ||
use SimpleSAML\WSSecurity\XML\sp\IncludeToken; | ||
use SimpleSAML\WSSecurity\XML\sp\RelToken; | ||
use SimpleSAML\XML\Attribute as XMLAttribute; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\DOMDocumentFactory; | ||
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
|
||
use function dirname; | ||
|
||
/** | ||
* Class \SimpleSAML\WSSecurity\XML\sp\RelTokenTest | ||
* | ||
* @covers \SimpleSAML\WSSecurity\XML\sp\RelToken | ||
* @covers \SimpleSAML\WSSecurity\XML\sp\AbstractAssertionTokenType | ||
* @covers \SimpleSAML\WSSecurity\XML\sp\AbstractSpElement | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
final class RelTokenTest extends TestCase | ||
{ | ||
use SchemaValidationTestTrait; | ||
use SerializableElementTestTrait; | ||
|
||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-securitypolicy-1.2.xsd'; | ||
|
||
self::$testedClass = RelToken::class; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 4) . '/resources/xml/sp_RelToken.xml', | ||
); | ||
} | ||
|
||
|
||
// test marshalling | ||
|
||
|
||
/** | ||
* Adding an empty RelToken element should yield an empty element. | ||
*/ | ||
public function testMarshallingEmptyElement(): void | ||
{ | ||
$spns = C::NS_SEC_POLICY; | ||
$relToken = new RelToken(); | ||
$this->assertEquals( | ||
"<sp:RelToken xmlns:sp=\"$spns\"/>", | ||
strval($relToken), | ||
); | ||
$this->assertTrue($relToken->isEmptyElement()); | ||
} | ||
|
||
|
||
/** | ||
* Test that creating a RelToken from scratch works. | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1'); | ||
$chunk = new Chunk(DOMDocumentFactory::fromString( | ||
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>' | ||
)->documentElement); | ||
|
||
$relToken = new RelToken(IncludeToken::Always, [$chunk], [$attr]); | ||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($relToken), | ||
); | ||
} | ||
} |
Oops, something went wrong.