-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport samlp:RequestedAuthnContext
- Loading branch information
Showing
5 changed files
with
281 additions
and
65 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
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,125 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\SAML2\XML\samlp; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\SAML2\Utils; | ||
use SimpleSAML\SAML2\XML\saml\AuthnContextClassRef; | ||
use SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef; | ||
use SimpleSAML\XML\Constants as C; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
|
||
use function array_merge; | ||
|
||
/** | ||
* Class representing SAML2 RequestedAuthnContext | ||
* | ||
* @package simplesamlphp/saml2 | ||
*/ | ||
final class RequestedAuthnContext extends AbstractSamlpElement | ||
{ | ||
/** | ||
* Initialize a RequestedAuthnContext. | ||
* | ||
* @param ( | ||
* \SimpleSAML\SAML2\XML\saml\AuthnContextClassRef| | ||
* \SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef | ||
* )[] $requestedAuthnContexts | ||
* @param string $Comparison | ||
*/ | ||
public function __construct( | ||
protected array $requestedAuthnContexts = [], | ||
protected ?string $Comparison = null, | ||
) { | ||
Assert::maxCount($requestedAuthnContexts, C::UNBOUNDED_LIMIT); | ||
Assert::minCount($requestedAuthnContexts, 1); | ||
Assert::allIsInstanceOfAny($requestedAuthnContexts, [AuthnContextClassRef::class, AuthnContextDeclRef::class]); | ||
|
||
if ($requestedAuthnContexts[0] instanceof AuthnContextClassRef) { | ||
Assert::allIsInstanceOf( | ||
$requestedAuthnContexts, | ||
AuthnContextClassRef::class, | ||
'You need either AuthnContextClassRef or AuthnContextDeclRef, not both.', | ||
); | ||
} else { // Can only be AuthnContextDeclRef | ||
Assert::allIsInstanceOf( | ||
$requestedAuthnContexts, | ||
AuthnContextDeclRef::class, | ||
'You need either AuthnContextClassRef or AuthnContextDeclRef, not both.', | ||
); | ||
} | ||
Assert::nullOrOneOf($Comparison, ['exact', 'minimum', 'maximum', 'better']); | ||
} | ||
|
||
|
||
/** | ||
* Collect the value of the requestedAuthnContexts-property | ||
* | ||
* @return (\SimpleSAML\SAML2\XML\saml\AuthnContextClassRef|\SimpleSAML\SAML2\XML\saml\AuthnContextDeclRef)[] | ||
*/ | ||
public function getRequestedAuthnContexts(): array | ||
{ | ||
return $this->requestedAuthnContexts; | ||
} | ||
|
||
|
||
/** | ||
* Collect the value of the Comparison-property | ||
* | ||
* @return string|null | ||
*/ | ||
public function getComparison(): ?string | ||
{ | ||
return $this->Comparison; | ||
} | ||
|
||
|
||
/** | ||
* Convert XML into a RequestedAuthnContext | ||
* | ||
* @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, 'RequestedAuthnContext', InvalidDOMElementException::class); | ||
Assert::same($xml->namespaceURI, RequestedAuthnContext::NS, InvalidDOMElementException::class); | ||
|
||
return new static( | ||
array_merge( | ||
AuthnContextClassRef::getChildrenOfClass($xml), | ||
AuthnContextDeclRef::getChildrenOfClass($xml), | ||
), | ||
self::getOptionalAttribute($xml, 'Comparison', null), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Convert this RequestedAuthnContext to XML. | ||
* | ||
* @param \DOMElement|null $parent The element we should append this RequestedAuthnContext to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
/** @psalm-var \DOMDocument $e->ownerDocument */ | ||
$e = $this->instantiateParentElement($parent); | ||
|
||
foreach ($this->getRequestedAuthnContexts() as $context) { | ||
$context->toXML($e); | ||
} | ||
|
||
if ($this->getComparison() !== null) { | ||
$e->setAttribute('Comparison', $this->getComparison()); | ||
} | ||
|
||
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
Oops, something went wrong.