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 27, 2024
1 parent 3b8075b commit 322b539
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 62 deletions.
111 changes: 111 additions & 0 deletions src/XML/wst/AbstractBinarySecretType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Base64ElementTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function array_map;
use function explode;
use function implode;

/**
* A BinarySecertType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractBinarySecretType extends AbstractWstElement
{
use Base64ElementTrait;
use ExtendableAttributesTrait;

/** @var string|\SimpleSAML\XML\XsNamespace */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* @param string $content
* @param (\SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string)[]|null $Type
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
final public function __construct(
string $content,
protected ?array $Type = null,
array $namespacedAttributes = []
) {
if ($Type !== null) {
$Type = array_map(
function (BinarySecretTypeEnum|string $v): string {
return ($v instanceof BinarySecretTypeEnum) ? $v->value : $v;
},
$Type,
);
Assert::allValidURI($Type, SchemaViolationException::class);
$this->Type = $Type;
}

$this->setContent($content);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Get the Type property.
*
* @return (\SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string)[]

Check failure on line 62 in src/XML/wst/AbstractBinarySecretType.php

View workflow job for this annotation

GitHub Actions / Quality control

InvalidNullableReturnType

src/XML/wst/AbstractBinarySecretType.php:62:16: InvalidNullableReturnType: The declared return type 'array<array-key, SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string>' for SimpleSAML\WSSecurity\XML\wst\AbstractBinarySecretType::getType is not nullable, but 'array<array-key, SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string>|null' contains null (see https://psalm.dev/144)
*/
public function getType(): array
{
return $this->Type;

Check failure on line 66 in src/XML/wst/AbstractBinarySecretType.php

View workflow job for this annotation

GitHub Actions / Quality control

NullableReturnStatement

src/XML/wst/AbstractBinarySecretType.php:66:16: NullableReturnStatement: The declared return type 'array<array-key, SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string>' for SimpleSAML\WSSecurity\XML\wst\AbstractBinarySecretType::getType is not nullable, but the function returns 'array<array-key, SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string>|null' (see https://psalm.dev/139)
}


/**
* Convert XML into a class instance
*
* @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
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new static(
$xml->textContent,
explode(' ', self::getAttribute($xml, 'Type')),
self::getAttributesNSFromXML($xml),
);
}


/**
* 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);
$e->textContent = $this->getContent();

$e->setAttribute('Type', implode(' ', $this->getType()));

Check failure on line 103 in src/XML/wst/AbstractBinarySecretType.php

View workflow job for this annotation

GitHub Actions / Quality control

InvalidArgument

src/XML/wst/AbstractBinarySecretType.php:103:47: InvalidArgument: Argument 2 of implode expects array<array-key, null|object{__tostring()}|scalar>, but array<array-key, SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum|string> provided (see https://psalm.dev/004)

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

return $e;
}
}
60 changes: 0 additions & 60 deletions src/XML/wst/AbstractBinarySecretTypeOpenEnumType.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/XML/wst/BinarySecret.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;

/**
* A BinarySecret element
*
* @package tvdijen/ws-security
*/
final class BinarySecret extends AbstractBinarySecretType
{
}
68 changes: 68 additions & 0 deletions tests/WSSecurity/XML/wst/BinarySecretTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wst\BinarySecret;
use SimpleSAML\WSSecurity\XML\wst\BinarySecretTypeEnum;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\BinarySecretTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\BinarySecret
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractBinarySecretType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class BinarySecretTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-trust.xsd';

self::$testedClass = BinarySecret::class;

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


// test marshalling


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

$binarySecret = new BinarySecret(
'/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=',
[BinarySecretTypeEnum::Nonce],
[$attr1],
);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($binarySecret),
);
}
}
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/IssuedTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use SimpleSAML\SOAP\Constants as SOAP;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wsa\MessageID;
use SimpleSAML\WSSecurity\XML\wst\RequestSecurityTokenResponse;
use SimpleSAML\WSSecurity\XML\wst\IssuedTokens;
use SimpleSAML\WSSecurity\XML\wst\RequestSecurityTokenResponse;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
Expand Down
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/wst/IssuerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use DOMElement;
use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\wst\Issuer;
use SimpleSAML\WSSecurity\XML\wsa\Address;
use SimpleSAML\WSSecurity\XML\wsa\Metadata;
use SimpleSAML\WSSecurity\XML\wsa\ReferenceParameters;
use SimpleSAML\WSSecurity\XML\wst\Issuer;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
Expand Down
1 change: 1 addition & 0 deletions tests/resources/xml/wst_BinarySecret.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<wst:BinarySecret xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512/" xmlns:ssp="urn:x-simplesamlphp:namespace" Type="http://docs.oasis-open.org/ws-sx/ws-trust/200512/Nonce" ssp:attr1="testval1">/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI=</wst:BinarySecret>

0 comments on commit 322b539

Please sign in to comment.