-
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
16 changed files
with
292 additions
and
8 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,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\XMLSecurity\XML\ds; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\Exception\TooManyElementsException; | ||
use SimpleSAML\XML\ExtendableElementTrait; | ||
use SimpleSAML\XML\XsNamespace as NS; | ||
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; | ||
|
||
use function array_pop; | ||
|
||
/** | ||
* Abstract class representing the PGPDataType. | ||
* | ||
* @package simplesamlphp/xml-security | ||
*/ | ||
abstract class AbstractPGPDataType extends AbstractDsElement | ||
{ | ||
use ExtendableElementTrait; | ||
|
||
/** @var \SimpleSAML\XML\XsNamespace */ | ||
public const XS_ANY_ELT_NAMESPACE = NS::OTHER; | ||
|
||
|
||
/** | ||
* Initialize a PGPData element. | ||
* | ||
* @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null $pgpKeyId | ||
* @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null $pgpKeyPacket | ||
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children | ||
*/ | ||
final public function __construct( | ||
protected ?PGPKeyID $pgpKeyId = null, | ||
protected ?PGPKeyPacket $pgpKeyPacket = null, | ||
array $children = [], | ||
) { | ||
if ($pgpKeyId === null && $pgpKeyPacket === null) { | ||
throw new SchemaViolationException("ds:PGPKeyID and ds:PGPKeyPacket can't both be null."); | ||
} | ||
|
||
$this->setElements($children); | ||
} | ||
|
||
|
||
/** | ||
* Collect the value of the PGPKeyID-property | ||
* | ||
* @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null | ||
*/ | ||
public function getPGPKeyID(): ?PGPKeyID | ||
{ | ||
return $this->pgpKeyId; | ||
} | ||
|
||
|
||
/** | ||
* Collect the value of the PGPKeyPacket-property | ||
* | ||
* @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null | ||
*/ | ||
public function getPGPKeyPacket(): ?PGPKeyPacket | ||
{ | ||
return $this->pgpKeyPacket; | ||
} | ||
|
||
|
||
/** | ||
* Convert XML into a PGPData | ||
* | ||
* @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); | ||
|
||
$pgpKeyId = PGPKeyID::getChildrenOfClass($xml); | ||
Assert::maxCount($pgpKeyId, 1, TooManyElementsException::class); | ||
|
||
$pgpKeyPacket = PGPKeyPacket::getChildrenOfClass($xml); | ||
Assert::maxCount($pgpKeyPacket, 1, TooManyElementsException::class); | ||
|
||
return new static( | ||
array_pop($pgpKeyId), | ||
array_pop($pgpKeyPacket), | ||
self::getChildElementsFromXML($xml), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Convert this PGPData to XML. | ||
* | ||
* @param \DOMElement|null $parent The element we should append this PGPData to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = $this->instantiateParentElement($parent); | ||
|
||
$this->getPGPKeyId()?->toXML($e); | ||
$this->getPGPKeyPacket()?->toXML($e); | ||
|
||
foreach ($this->getElements() as $elt) { | ||
$elt->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:PGPData element. | ||
* | ||
* @package simplesaml/xml-security | ||
*/ | ||
final class PGPData extends AbstractPGPDataType | ||
{ | ||
} |
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
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,95 @@ | ||
<?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\Exception\SchemaViolationException; | ||
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; | ||
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; | ||
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; | ||
use SimpleSAML\XMLSecurity\XML\ds\AbstractPGPDataType; | ||
use SimpleSAML\XMLSecurity\XML\ds\PGPData; | ||
use SimpleSAML\XMLSecurity\XML\ds\PGPKeyID; | ||
use SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket; | ||
use SimpleSAML\XMLSecurity\XML\xenc\P; | ||
|
||
use function dirname; | ||
use function strval; | ||
|
||
/** | ||
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\PGPDataTest | ||
* | ||
* @package simplesamlphp/xml-security | ||
*/ | ||
#[CoversClass(AbstractDsElement::class)] | ||
#[CoversClass(AbstractPGPDataType::class)] | ||
#[CoversClass(PGPData::class)] | ||
final class PGPDataTest extends TestCase | ||
{ | ||
use SchemaValidationTestTrait; | ||
use SerializableElementTestTrait; | ||
|
||
/** | ||
*/ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
self::$testedClass = PGPData::class; | ||
|
||
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd'; | ||
|
||
self::$xmlRepresentation = DOMDocumentFactory::fromFile( | ||
dirname(__FILE__, 3) . '/resources/xml/ds_PGPData.xml', | ||
); | ||
} | ||
|
||
|
||
/** | ||
*/ | ||
public function testMarshalling(): void | ||
{ | ||
$pgpKeyId = new PGPKeyID('GpM7'); | ||
$pgpKeyPacket = new PGPKeyPacket('GpM8'); | ||
$p = new P('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); | ||
|
||
$pgpData = new PGPData($pgpKeyId, $pgpKeyPacket, [$p]); | ||
|
||
$this->assertEquals( | ||
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), | ||
strval($pgpData), | ||
); | ||
} | ||
|
||
|
||
/** | ||
*/ | ||
public function testMarshallingBothIdAndPacketNullThrowsException(): void | ||
{ | ||
$this->expectException(SchemaViolationException::class); | ||
|
||
new PGPData(null, null, []); | ||
} | ||
|
||
|
||
/** | ||
*/ | ||
public function testMarshallingReferenceElementOrdering(): void | ||
{ | ||
$pgpKeyId = new PGPKeyID('GpM7'); | ||
$pgpKeyPacket = new PGPKeyPacket('GpM8'); | ||
$p = new P('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); | ||
|
||
$pgpData = new PGPData($pgpKeyId, $pgpKeyPacket, [$p]); | ||
|
||
$pgpDataElement = $pgpData->toXML(); | ||
/** @var \DOMElement[] $children */ | ||
$children = $pgpDataElement->childNodes; | ||
|
||
$this->assertEquals('ds:PGPKeyID', $children[0]->tagName); | ||
$this->assertEquals('ds:PGPKeyPacket', $children[1]->tagName); | ||
$this->assertEquals('xenc:P', $children[2]->tagName); | ||
} | ||
} |
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.