-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement saml:AudienceRestrictionCondition
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/SAML11/XML/saml/AbstractAudienceRestrictionConditionType.php
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,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; | ||
} | ||
} |
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\SAML11\XML\saml; | ||
|
||
/** | ||
* Class representing a saml:AudienceRestrictionCondition element. | ||
* | ||
* @package simplesamlphp/saml11 | ||
*/ | ||
final class AudienceRestrictionCondition extends AbstractAudienceRestrictionConditionType | ||
{ | ||
} |
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,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
62
tests/src/SAML11/XML/saml/AudienceRestrictionConditionTest.php
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,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), | ||
); | ||
} | ||
} |