Skip to content

Commit

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

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

/**
* Class representing <xenc11:AbstractMGFType>.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractMGFType extends AbstractAlgorithmIdentifierType
{
/**
* MGFType constructor.
*
* @param string $Algorithm
*/
public function __construct(
string $Algorithm,
) {
parent::__construct($Algorithm, null);
}
}
33 changes: 33 additions & 0 deletions src/XML/xenc11/MGF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

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

/**
* A class implementing the xenc11:MGF element.
*
* @package simplesamlphp/xml-security
*/
final class MGF extends AbstractMGFType
{
/**
* @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);

return new static(
self::getOptionalAttribute($xml, 'Algorithm', null),
);
}
}
62 changes: 62 additions & 0 deletions tests/XML/xenc11/MGFTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\SAML2\XML\xenc11;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractAlgorithmIdentifierType;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractMGFType;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
use SimpleSAML\XMLSecurity\XML\xenc11\MGF;

use function dirname;
use function strval;

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

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

self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xenc-schema-11.xsd';

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


// marshalling


/**
*/
public function testMarshalling(): void
{
$mgf = new MGF('urn:x-simplesamlphp:algorithm');

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($mgf),
);
}
}
1 change: 1 addition & 0 deletions tests/resources/xml/xenc11_MGF.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<xenc11:MGF xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" Algorithm="urn:x-simplesamlphp:algorithm" />

0 comments on commit 502c615

Please sign in to comment.