-
Notifications
You must be signed in to change notification settings - Fork 2
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
15 changed files
with
289 additions
and
7 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
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,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XMLSecurity\XML\ds; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\Exception\TooManyElementsException; | ||
use SimpleSAML\XML\Registry\ElementRegistry; | ||
use SimpleSAML\XML\SerializableElementInterface; | ||
use SimpleSAML\XML\XsNamespace as NS; | ||
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; | ||
|
||
/** | ||
* Abstract class representing the SPKIDataType. | ||
* | ||
* @package simplesamlphp/xml-security | ||
*/ | ||
abstract class AbstractSPKIDataType extends AbstractDsElement | ||
{ | ||
/** | ||
* Initialize a SPKIData element. | ||
* | ||
* @param array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> $tuples | ||
*/ | ||
final public function __construct( | ||
protected array $tuples, | ||
) { | ||
Assert::allIsArray($tuples, SchemaViolationException::class); | ||
Assert::allCount($tuples, 2); | ||
|
||
foreach ($tuples as $tuple) { | ||
list($spkisExp, $other) = $tuple; | ||
Assert::isInstanceOf($spkisExp, SPKISexp::class, SchemaViolationException::class); | ||
Assert::nullOrIsInstanceOf($other, SerializableElementInterface::class, SchemaViolationException::class); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Collect the value of the SPKISexp-property | ||
* | ||
* @return array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> | ||
*/ | ||
public function getTuples(): array | ||
{ | ||
return $this->tuples; | ||
} | ||
|
||
|
||
/** | ||
* Convert XML into a SPKIData | ||
* | ||
* @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); | ||
|
||
$registry = ElementRegistry::getInstance(); | ||
$tuples = []; | ||
$tuple = [null, null]; | ||
foreach ($xml->childNodes as $node) { | ||
if ($node instanceof DOMElement) { | ||
if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') { | ||
if ($tuple[0] !== null) { | ||
$tuples[] = $tuple; | ||
} | ||
$tuple = [SPKISexp::fromXML($node), null]; | ||
} elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) { | ||
$handler = $registry->getElementHandler($node->namespaceURI, $node->localName); | ||
$tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node); | ||
$tuples[] = $tuple; | ||
$tuple = [null, null]; | ||
} | ||
} | ||
} | ||
|
||
if ($tuple[0] !== null) { | ||
$tuples[] = $tuple; | ||
} | ||
|
||
return new static($tuples); | ||
} | ||
|
||
|
||
/** | ||
* Convert this SPKIData to XML. | ||
* | ||
* @param \DOMElement|null $parent The element we should append this SPKIData to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(?DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
|
||
foreach ($this->getTuples() as $tuple) { | ||
list($spkisExp, $other) = $tuple; | ||
|
||
$spkisExp->toXML($e); | ||
$other?->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
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XMLSecurity\XML\ds; | ||
|
||
/** | ||
* Class representing a ds:SPKIData element. | ||
* | ||
* @package simplesaml/xml-security | ||
*/ | ||
final class SPKIData extends AbstractSPKIDataType | ||
{ | ||
} |
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
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
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
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XMLSecurity\Test\XML\ds; | ||
|
||
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\ds\AbstractDsElement; | ||
use SimpleSAML\XMLSecurity\XML\ds\AbstractSPKIData; | ||
use SimpleSAML\XMLSecurity\XML\ds\SPKIData; | ||
use SimpleSAML\XMLSecurity\XML\ds\SPKISexp; | ||
use SimpleSAML\XMLSecurity\XML\xenc\CarriedKeyName; | ||
use SimpleSAML\XMLSecurity\XML\xenc\Seed; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\SPKIDataTest | ||
* | ||
* @package simplesamlphp/xml-security | ||
*/ | ||
#[CoversClass(AbstractDsElement::class)] | ||
#[CoversClass(AbstractSPKIData::class)] | ||
#[CoversClass(SPKIData::class)] | ||
final class SPKIDataTest extends TestCase | ||
{ | ||
use SchemaValidationTestTrait; | ||
use SerializableElementTestTrait; | ||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = SPKIData::class; | ||
|
||
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd'; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 3) . '/resources/xml/ds_SPKIData.xml', | ||
); | ||
} | ||
|
||
|
||
/** | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$SPKISexp1 = new SPKISexp('GpM6'); | ||
$seed = new Seed('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); | ||
$SPKISexp2 = new SPKISexp('GpM7'); | ||
$SPKISexp3 = new SPKISexp('GpM8'); | ||
$carriedKeyName = new CarriedKeyName('Some label'); | ||
|
||
$SPKIData = new SPKIData([ | ||
[$SPKISexp1, $seed], | ||
[$SPKISexp2, null], | ||
[$SPKISexp3, $carriedKeyName], | ||
]); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($SPKIData), | ||
); | ||
} | ||
} |
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
Oops, something went wrong.