Skip to content

Commit

Permalink
Add xenc11:Salt element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 7, 2024
1 parent 502c615 commit f098143
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/XML/xenc11/Salt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;

use function array_merge;
use function array_pop;

/**
* Class representing <xenc11:Salt>.
*
* @package simplesamlphp/xml-security
*/
final class Salt extends AbstractXenc11Element
{
/**
* Salt constructor.
*
* @param \SimpleSAML\XMLSecurity\XML\xenc11\OtherSource|\SimpleSAML\XMLSecurity\XML\xenc11\Specified $content
*/
public function __construct(
protected OtherSource|Specified $content,
) {
}


/**
* Get the value of the $content property.
*
* @return \SimpleSAML\XMLSecurity\XML\xenc11\OtherSource|\SimpleSAML\XMLSecurity\XML\xenc11\Specified
*/
public function getContent(): OtherSource|Specified
{
return $this->content;
}


/**
* @inheritDoc
*
* @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::getNamespaceURI(), InvalidDOMElementException::class);

$otherSource = OtherSource::getChildrenOfClass($xml);
$specified = Specified::getChildrenOfClass($xml);

$content = array_merge($otherSource, $specified);
Assert::minCount($content, 1, MissingElementException::class);
Assert::maxCount($content, 1, TooManyElementsException::class);

return new static(array_pop($content));
}


/**
* @inheritDoc
*/
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$this->getContent()->toXML($e);

return $e;
}
}
68 changes: 68 additions & 0 deletions tests/XML/xenc11/SaltTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\SAML2\XML\xenc11;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
use SimpleSAML\XMLSecurity\XML\xenc11\OtherSource;
use SimpleSAML\XMLSecurity\XML\xenc11\Parameters;
use SimpleSAML\XMLSecurity\XML\xenc11\Salt;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XMLSecurity\XML\xenc11\SaltTest
*
* @package simplesamlphp/xml-security
*/
#[CoversClass(Salt::class)]
#[CoversClass(AbstractXenc11Element::class)]
final class SaltTest extends TestCase
{
use SerializableElementTestTrait;

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

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


// marshalling


/**
*/
public function testMarshalling(): void
{
$someDoc = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
);

$parameters = new Parameters(
[new Chunk($someDoc->documentElement)],
[new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1')],
);

$otherSource = new OtherSource('urn:x-simplesamlphp:algorithm', $parameters);
$salt = new Salt($otherSource);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($salt),
);
}
}
7 changes: 7 additions & 0 deletions tests/resources/xml/xenc11_Salt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<xenc11:Salt xmlns:xenc11="http://www.w3.org/2009/xmlenc11#">
<xenc11:OtherSource Algorithm="urn:x-simplesamlphp:algorithm">
<xenc11:Parameters xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<ssp:Chunk>Some</ssp:Chunk>
</xenc11:Parameters>
</xenc11:OtherSource>
</xenc11:Salt>

0 comments on commit f098143

Please sign in to comment.