Skip to content

Commit

Permalink
Fix namespaced IncludeToken-attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 23, 2024
1 parent 7dea611 commit 4c710fd
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 115 deletions.
23 changes: 0 additions & 23 deletions src/XML/sp_200702/AbstractIssuedTokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,28 @@ abstract class AbstractIssuedTokenType 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;

/** The exclusions for the xs:anyAttribute element */
public const XS_ANY_ATTR_EXCLUSIONS = [
[null, 'IncludeToken'],
];


/**
* IssuedTokenType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\sp_200702\RequestSecurityTokenTemplate $requestSecurityTokenTemplate
* @param \SimpleSAML\WSSecurity\XML\sp_200702\Issuer|\SimpleSAML\WSSecurity\XML\sp_200702\IssuerName|null $issuer
* @param \SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken|null $includeToken
* @param list<\SimpleSAML\XML\SerializableElementInterface> $elts
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected RequestSecurityTokenTemplate $requestSecurityTokenTemplate,
protected Issuer|IssuerName|null $issuer = null,
?IncludeToken $includeToken = null,
array $elts = [],
array $namespacedAttributes = [],
) {
$this->setIncludeToken($includeToken);
$this->setElements($elts);
$this->setAttributesNS($namespacedAttributes);
}
Expand Down Expand Up @@ -114,16 +105,9 @@ public static function fromXML(DOMElement $xml): static
Assert::minCount($requestSecurityTokenTemplate, 1, MissingElementException::class);
Assert::maxCount($requestSecurityTokenTemplate, 1, TooManyElementsException::class);

$includeToken = self::getOptionalAttribute($xml, 'IncludeToken', null);
try {
$includeToken = IncludeToken::from($includeToken);
} catch (ValueError) {
}

return new static(
$requestSecurityTokenTemplate[0],
array_pop($issuer),
$includeToken,
self::getChildElementsFromXML($xml),
self::getAttributesNSFromXML($xml),
);
Expand All @@ -140,13 +124,6 @@ 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,
);
}

if ($this->getIssuer() !== null) {
$this->getIssuer()->toXML($e);
}
Expand Down
26 changes: 1 addition & 25 deletions src/XML/sp_200702/AbstractKeyValueTokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,24 @@ abstract class AbstractKeyValueTokenType 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;

/** The exclusions for the xs:anyAttribute element */
public const XS_ANY_ATTR_EXCLUSIONS = [
[null, 'IncludeToken'],
];


/**
* KeyValueTokenType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken|null $includeToken
* @param list<\SimpleSAML\XML\SerializableElementInterface> $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);
}
Expand All @@ -63,8 +54,7 @@ final public function __construct(
*/
public function isEmptyElement(): bool
{
return empty($this->getIncludeToken())
&& empty($this->getAttributesNS())
return empty($this->getAttributesNS())
&& empty($this->getElements());
}

Expand All @@ -90,14 +80,7 @@ public static function fromXML(DOMElement $xml): static
InvalidDOMElementException::class,
);

$includeToken = self::getOptionalAttribute($xml, 'IncludeToken', null);
try {
$includeToken = IncludeToken::from($includeToken);
} catch (ValueError) {
}

return new static(
$includeToken,
self::getChildElementsFromXML($xml),
self::getAttributesNSFromXML($xml),
);
Expand All @@ -114,13 +97,6 @@ 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) {
/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
$elt->toXML($e);
Expand Down
24 changes: 0 additions & 24 deletions src/XML/sp_200702/AbstractSecureConversationTokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,26 @@ abstract class AbstractSecureConversationTokenType 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;

/** The exclusions for the xs:anyAttribute element */
public const XS_ANY_ATTR_EXCLUSIONS = [
[null, 'IncludeToken'],
];


/**
* SecureConversationTokenType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\sp_200702\Issuer|\SimpleSAML\WSSecurity\XML\sp_200702\IssuerName|null $issuer
* @param \SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken|null $includeToken
* @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected Issuer|IssuerName|null $issuer,
?IncludeToken $includeToken = null,
array $elts = [],
array $namespacedAttributes = [],
) {
$this->setIncludeToken($includeToken);
$this->setElements($elts);
$this->setAttributesNS($namespacedAttributes);
}
Expand All @@ -78,7 +69,6 @@ public function getIssuer(): Issuer|IssuerName|null
public function isEmptyElement(): bool
{
return empty($this->getIssuer())
&& empty($this->getIncludeToken())
&& empty($this->getAttributesNS())
&& empty($this->getElements());
}
Expand Down Expand Up @@ -109,15 +99,8 @@ public static function fromXML(DOMElement $xml): static
$issuerName = IssuerName::getChildrenOfClass($xml);
$issuer = array_merge($issuer, $issuerName);

$includeToken = self::getOptionalAttribute($xml, 'IncludeToken', null);
try {
$includeToken = IncludeToken::from($includeToken);
} catch (ValueError) {
}

return new static(
array_pop($issuer),
$includeToken,
self::getChildElementsFromXML($xml),
self::getAttributesNSFromXML($xml),
);
Expand All @@ -134,13 +117,6 @@ 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,
);
}

if ($this->getIssuer() !== null) {
$this->getIssuer()->toXML($e);
}
Expand Down
24 changes: 0 additions & 24 deletions src/XML/sp_200702/AbstractSpnegoContextTokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,26 @@ abstract class AbstractSpnegoContextTokenType 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;

/** The exclusions for the xs:anyAttribute element */
public const XS_ANY_ATTR_EXCLUSIONS = [
[null, 'IncludeToken'],
];


/**
* SpnegoContextTokenType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\sp_200702\Issuer|\SimpleSAML\WSSecurity\XML\sp_200702\IssuerName|null $issuer
* @param \SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken|null $includeToken
* @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected Issuer|IssuerName|null $issuer,
?IncludeToken $includeToken = null,
array $elts = [],
array $namespacedAttributes = [],
) {
$this->setIncludeToken($includeToken);
$this->setElements($elts);
$this->setAttributesNS($namespacedAttributes);
}
Expand All @@ -78,7 +69,6 @@ public function getIssuer(): Issuer|IssuerName|null
public function isEmptyElement(): bool
{
return empty($this->getIssuer())
&& empty($this->getIncludeToken())
&& empty($this->getAttributesNS())
&& empty($this->getElements());
}
Expand Down Expand Up @@ -109,15 +99,8 @@ public static function fromXML(DOMElement $xml): static
$issuerName = IssuerName::getChildrenOfClass($xml);
$issuer = array_merge($issuer, $issuerName);

$includeToken = self::getOptionalAttribute($xml, 'IncludeToken', null);
try {
$includeToken = IncludeToken::from($includeToken);
} catch (ValueError) {
}

return new static(
array_pop($issuer),
$includeToken,
self::getChildElementsFromXML($xml),
self::getAttributesNSFromXML($xml),
);
Expand All @@ -134,13 +117,6 @@ 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,
);
}

if ($this->getIssuer() !== null) {
$this->getIssuer()->toXML($e);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/WSSecurity/XML/sp_200702/IssuedTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use SimpleSAML\WSSecurity\XML\sp_200702\AbstractIssuedTokenType;
use SimpleSAML\WSSecurity\XML\sp_200702\AbstractSpElement;
use SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken;
use SimpleSAML\WSSecurity\XML\sp_200702\IncludeTokenTypeTrait;
use SimpleSAML\WSSecurity\XML\sp_200702\IssuedToken;
use SimpleSAML\WSSecurity\XML\sp_200702\IssuerName;
use SimpleSAML\WSSecurity\XML\sp_200702\RequestSecurityTokenTemplate;
Expand All @@ -31,7 +30,6 @@
*/
#[Group('sp')]
#[CoversClass(IssuedToken::class)]
#[CoversClass(IncludeTokenTypeTrait::class)]
#[CoversClass(AbstractIssuedTokenType::class)]
#[CoversClass(AbstractSpElement::class)]
final class IssuedTokenTest extends TestCase
Expand Down Expand Up @@ -60,6 +58,7 @@ public function testMarshallingElementOrdering(): void
{
$issuer = new IssuerName('urn:x-simplesamlphp:issuer');
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1');
$includeToken = new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Always->value);
$chunk = new Chunk(DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
)->documentElement);
Expand All @@ -69,7 +68,7 @@ public function testMarshallingElementOrdering(): void
[$attr],
);

$issuedToken = new IssuedToken($requestSecurityTokenTemplate, $issuer, IncludeToken::Always, [$chunk], [$attr]);
$issuedToken = new IssuedToken($requestSecurityTokenTemplate, $issuer, [$chunk], [$includeToken, $attr]);
$issuedTokenElement = $issuedToken->toXML();

// Test for a Issuer
Expand All @@ -95,6 +94,7 @@ public function testMarshallingElementOrdering(): void
public function testMarshalling(): void
{
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1');
$includeToken = new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Always->value);
$chunk = new Chunk(DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
)->documentElement);
Expand All @@ -106,7 +106,7 @@ public function testMarshalling(): void
[$attr],
);

$issuedToken = new IssuedToken($requestSecurityTokenTemplate, $issuer, IncludeToken::Always, [$chunk], [$attr]);
$issuedToken = new IssuedToken($requestSecurityTokenTemplate, $issuer, [$chunk], [$includeToken, $attr]);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($issuedToken),
Expand Down
5 changes: 2 additions & 3 deletions tests/WSSecurity/XML/sp_200702/KeyValueTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use SimpleSAML\WSSecurity\XML\sp_200702\AbstractKeyValueTokenType;
use SimpleSAML\WSSecurity\XML\sp_200702\AbstractSpElement;
use SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken;
use SimpleSAML\WSSecurity\XML\sp_200702\IncludeTokenTypeTrait;
use SimpleSAML\WSSecurity\XML\sp_200702\KeyValueToken;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
Expand All @@ -28,7 +27,6 @@
*/
#[Group('sp')]
#[CoversClass(KeyValueToken::class)]
#[CoversClass(IncludeTokenTypeTrait::class)]
#[CoversClass(AbstractKeyValueTokenType::class)]
#[CoversClass(AbstractSpElement::class)]
final class KeyValueTokenTest extends TestCase
Expand Down Expand Up @@ -75,11 +73,12 @@ public function testMarshallingEmptyElement(): void
public function testMarshalling(): void
{
$attr = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1');
$includeToken = new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Always->value);
$chunk = new Chunk(DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
)->documentElement);

$keyValueToken = new KeyValueToken(IncludeToken::Always, [$chunk], [$attr]);
$keyValueToken = new KeyValueToken([$chunk], [$includeToken, $attr]);
$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($keyValueToken),
Expand Down
Loading

0 comments on commit 4c710fd

Please sign in to comment.