Skip to content

Commit

Permalink
Implement saml:AudienceRestrictionCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Feb 27, 2024
1 parent 5d99a53 commit b86ead2
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/SAML11/XML/saml/AbstractAudienceRestrictionConditionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\SAML11\XML\saml;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\SAML11\XML\saml\AbstractConditionType;
use SimpleSAML\SAML11\XML\saml\Audience;
use SimpleSAML\SAML11\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;

use function array_pop;

/**
* @package simplesamlphp\saml11
*/
abstract class AbstractAudienceRestrictionConditionType extends AbstractConditionType
{
/**
* AudienceRestrictionConditionType constructor.
*
* @param \SimpleSAML\SAML11\XML\saml\Audience[] $audience
*/
public function __construct(
protected array $audience,
) {
Assert::allIsInstanceOf($audience, Audience::class, SchemaViolationException::class);
}


/**
* Get the value of the audience-attribute.
*
* @return \SimpleSAML\SAML11\XML\saml\Audience[]
*/
public function getAudience(): array
{
return $this->audience;
}


/**
* Convert XML into a AudienceStatement
*
* @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::getNamespaceURI(), InvalidDOMElementException::class);

$audience = Audience::getChildrenOfClass($xml);
Assert::minCount($audience, 1, MissingElementException::class);

return new static(
$audience,
);
}


/**
* Convert this AudienceStatement to XML.
*
* @param \DOMElement $parent The element we are converting to XML.
* @return \DOMElement The XML element after adding the data corresponding to this AudienceStatement.
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);

foreach ($this->getAudience() as $a) {
$a->toXML($e);
}

return $e;
}
}
14 changes: 14 additions & 0 deletions src/SAML11/XML/saml/AudienceRestrictionCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\SAML11\XML\saml;

/**
* Class representing a saml:AudienceRestrictionCondition element.
*
* @package simplesamlphp/saml11
*/
final class AudienceRestrictionCondition extends AbstractAudienceRestrictionConditionType
{
}
3 changes: 3 additions & 0 deletions tests/resources/xml/saml_AudienceRestrictionCondition.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<saml:AudienceRestrictionCondition xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion">
<saml:Audience>urn:x-simplesamlphp:audience</saml:Audience>
</saml:AudienceRestrictionCondition>
62 changes: 62 additions & 0 deletions tests/src/SAML11/XML/saml/AudienceRestrictionConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\SAML11\XML\saml;

use PHPUnit\Framework\TestCase;
use SimpleSAML\SAML11\XML\saml\Audience;
use SimpleSAML\SAML11\XML\saml\AudienceRestrictionCondition;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Tests for AudienceRestictionCondition elements.
*
* @covers \SimpleSAML\SAML11\XML\saml\AudienceRestrictionCondition
* @covers \SimpleSAML\SAML11\XML\saml\AbstractConditionType
* @covers \SimpleSAML\SAML11\XML\saml\AbstractSamlElement
*
* @package simplesamlphp/saml11
*/
final class AudienceRestrictionConditionTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 6) . '/resources/schemas/oasis-sstc-saml-schema-assertion-1.1.xsd';

self::$testedClass = AudienceRestrictionCondition::class;

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


// marshalling


/**
* Test creating an Audience from scratch
*/
public function testMarshalling(): void
{
$audience = new Audience('urn:x-simplesamlphp:audience');
$audienceRestrictionCondition = new AudienceRestrictionCondition([$audience]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($audienceRestrictionCondition),
);
}
}

0 comments on commit b86ead2

Please sign in to comment.