Skip to content

Commit

Permalink
Backport RegistrationInfo and PublicationInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Nov 18, 2023
1 parent 53bd45f commit 7f3ba83
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
20 changes: 11 additions & 9 deletions src/SAML2/XML/mdrpi/PublicationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimpleSAML\SAML2\XML\mdrpi;

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML2\Constants as C;
Expand All @@ -12,7 +13,6 @@
use SimpleSAML\SAML2\XML\md\AbstractLocalizedURI;
use SimpleSAML\XML\ArrayizableElementInterface;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Utils as XMLUtils;

use function array_change_key_case;
use function array_keys;
Expand All @@ -30,16 +30,18 @@ final class PublicationInfo extends AbstractMdrpiElement implements ArrayizableE
* Create/parse a mdrpi:PublicationInfo element.
*
* @param string $publisher
* @param int|null $creationInstant
* @param \DateTimeImmutable|null $creationInstant
* @param string|null $publicationId
* @param \SimpleSAML\SAML2\XML\mdrpi\UsagePolicy[] $usagePolicy
*/
public function __construct(
protected string $publisher,
protected ?int $creationInstant = null,
protected ?DateTimeImmutable $creationInstant = null,
protected ?string $publicationId = null,
protected array $usagePolicy = [],
) {
Assert::nullOrSame($creationInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class);
Assert::maxCount($usagePolicy, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf($usagePolicy, UsagePolicy::class);

/**
Expand Down Expand Up @@ -75,9 +77,9 @@ public function getPublisher(): string
/**
* Collect the value of the creationInstant-property
*
* @return int|null
* @return \DateTimeImmutable|null
*/
public function getCreationInstant(): ?int
public function getCreationInstant(): ?DateTimeImmutable
{
return $this->creationInstant;
}
Expand Down Expand Up @@ -130,7 +132,7 @@ public static function fromXML(DOMElement $xml): static
$creationInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $creationInstant, 1);

Assert::validDateTimeZulu($creationInstant, ProtocolViolationException::class);
$creationInstant = XMLUtils::xsDateTimeToTimestamp($creationInstant);
$creationInstant = new DateTimeImmutable($creationInstant);
}

$publicationId = self::getOptionalAttribute($xml, 'publicationId', null);
Expand All @@ -152,7 +154,7 @@ public function toXML(DOMElement $parent = null): DOMElement
$e->setAttribute('publisher', $this->getPublisher());

if ($this->getCreationInstant() !== null) {
$e->setAttribute('creationInstant', gmdate('Y-m-d\TH:i:s\Z', $this->getCreationInstant()));
$e->setAttribute('creationInstant', $this->getCreationInstant()->format(C::DATETIME_FORMAT));
}

if ($this->getPublicationId() !== null) {
Expand Down Expand Up @@ -210,7 +212,7 @@ private static function processArrayContents(array $data): array
if (array_key_exists('creationinstant', $data)) {
Assert::string($data['creationinstant'], ArrayValidationException::class);
Assert::validDateTimeZulu($data['creationinstant'], ArrayValidationException::class);
$retval['creationInstant'] = XMLUtils::xsDateTimeToTimestamp($data['creationinstant']);
$retval['creationInstant'] = new DateTimeImmutable($data['creationinstant']);
}

if (array_key_exists('publicationid', $data)) {
Expand Down Expand Up @@ -240,7 +242,7 @@ public function toArray(): array
$data['publisher'] = $this->getPublisher();

if ($this->getCreationInstant() !== null) {
$data['creationInstant'] = gmdate('Y-m-d\TH:i:s\Z', $this->getCreationInstant());
$data['creationInstant'] = $this->getCreationInstant()->format(C::DATETIME_FORMAT);
}

if ($this->getPublicationId() !== null) {
Expand Down
24 changes: 14 additions & 10 deletions src/SAML2/XML/mdrpi/RegistrationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimpleSAML\SAML2\XML\mdrpi;

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML2\Constants as C;
Expand All @@ -12,7 +13,8 @@
use SimpleSAML\SAML2\XML\md\AbstractLocalizedURI;
use SimpleSAML\XML\ArrayizableElementInterface;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Utils as XMLUtils;

use function preg_replace;

/**
* Class for handling the mdrpi:RegistrationInfo element.
Expand All @@ -26,14 +28,16 @@ final class RegistrationInfo extends AbstractMdrpiElement implements Arrayizable
* Create/parse a mdrpi:RegistrationInfo element.
*
* @param string $registrationAuthority
* @param int|null $registrationInstant
* @param \SimpleSAML\SAML2\XML\mdrpi\RegistrationPolicy[] $RegistrationPolicy
* @param \DateTimeImmutable|null $registrationInstant
* @param \SimpleSAML\SAML2\XML\mdrpi\RegistrationPolicy[] $registrationPolicy
*/
public function __construct(
protected string $registrationAuthority,
protected ?int $registrationInstant = null,
protected ?DateTimeImmutable $registrationInstant = null,
protected array $registrationPolicy = [],
) {
Assert::nullOrSame($registrationInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class);
Assert::maxCount($registrationPolicy, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf($registrationPolicy, RegistrationPolicy::class);

/**
Expand Down Expand Up @@ -69,9 +73,9 @@ public function getRegistrationAuthority(): string
/**
* Collect the value of the registrationInstant property
*
* @return int|null
* @return \DateTimeImmutable|null
*/
public function getRegistrationInstant(): ?int
public function getRegistrationInstant(): ?DateTimeImmutable
{
return $this->registrationInstant;
}
Expand Down Expand Up @@ -113,7 +117,7 @@ public static function fromXML(DOMElement $xml): static
$registrationInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $registrationInstant, 1);

Assert::validDateTimeZulu($registrationInstant, ProtocolViolationException::class);
$registrationInstant = XMLUtils::xsDateTimeToTimestamp($registrationInstant);
$registrationInstant = new DateTimeImmutable($registrationInstant);
}
$RegistrationPolicy = RegistrationPolicy::getChildrenOfClass($xml);

Expand All @@ -133,7 +137,7 @@ public function toXML(DOMElement $parent = null): DOMElement
$e->setAttribute('registrationAuthority', $this->getRegistrationAuthority());

if ($this->getRegistrationInstant() !== null) {
$e->setAttribute('registrationInstant', gmdate('Y-m-d\TH:i:s\Z', $this->getRegistrationInstant()));
$e->setAttribute('registrationInstant', $this->getRegistrationInstant()->format(C::DATETIME_FORMAT));
}

foreach ($this->getRegistrationPolicy() as $rp) {
Expand Down Expand Up @@ -186,7 +190,7 @@ private static function processArrayContents(array $data): array
if (array_key_exists('registrationinstant', $data)) {
Assert::string($data['registrationinstant'], ArrayValidationException::class);
Assert::validDateTimeZulu($data['registrationinstant'], ArrayValidationException::class);
$retval['registrationInstant'] = XMLUtils::xsDateTimeToTimestamp($data['registrationinstant']);
$retval['registrationInstant'] = new DateTimeImmutable($data['registrationinstant']);
}

if (array_key_exists('registrationpolicy', $data)) {
Expand All @@ -211,7 +215,7 @@ public function toArray(): array
$data['registrationAuthority'] = $this->getRegistrationAuthority();

if ($this->getRegistrationInstant() !== null) {
$data['registrationInstant'] = gmdate('Y-m-d\TH:i:s\Z', $this->getRegistrationInstant());
$data['registrationInstant'] = $this->getRegistrationInstant()->format(C::DATETIME_FORMAT);
}

if (!empty($this->getRegistrationPolicy())) {
Expand Down
3 changes: 2 additions & 1 deletion tests/SAML2/XML/mdrpi/PublicationInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimpleSAML\Test\SAML2\XML\mdrpi;

use DateTimeImmutable;
use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function testMarshalling(): void
{
$publicationInfo = new PublicationInfo(
'SomePublisher',
1293840000,
new DateTimeImmutable('2011-01-01T00:00:00Z'),
'SomePublicationId',
[
new UsagePolicy('en', 'http://TheEnglishUsagePolicy'),
Expand Down
3 changes: 2 additions & 1 deletion tests/SAML2/XML/mdrpi/RegistrationInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace SimpleSAML\Test\SAML2\XML\mdrpi;

use DateTimeImmutable;
use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function testMarshalling(): void
{
$registrationInfo = new RegistrationInfo(
'https://ExampleAuthority',
1234567890,
new DateTimeImmutable('2009-02-13T23:31:30Z'),
[
new RegistrationPolicy('en', 'http://www.example.org/aai/metadata/en_registration.html'),
new RegistrationPolicy('de', 'http://www.example.org/aai/metadata/de_registration.html'),
Expand Down

0 comments on commit 7f3ba83

Please sign in to comment.