1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\saml; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Constants as C; |
10
|
|
|
use SimpleSAML\SAML2\Utils; |
11
|
|
|
use SimpleSAML\SAML2\XML\ExtensionPointInterface; |
12
|
|
|
use SimpleSAML\SAML2\XML\ExtensionPointTrait; |
13
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
14
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
15
|
|
|
|
16
|
|
|
use function count; |
17
|
|
|
use function explode; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SAML Condition data type. |
21
|
|
|
* |
22
|
|
|
* @package simplesamlphp/saml2 |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractCondition extends AbstractConditionType implements ExtensionPointInterface |
25
|
|
|
{ |
26
|
|
|
use ExtensionPointTrait; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
public const LOCALNAME = 'Condition'; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
protected string $type; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initialize a custom saml:Condition element. |
37
|
|
|
* |
38
|
|
|
* @param string $type |
39
|
|
|
*/ |
40
|
|
|
protected function __construct(string $type) |
41
|
|
|
{ |
42
|
|
|
$this->type = $type; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
public function getXsiType(): string |
50
|
|
|
{ |
51
|
|
|
return $this->type; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Convert an XML element into a Condition. |
57
|
|
|
* |
58
|
|
|
* @param \DOMElement $xml The root XML element |
59
|
|
|
* @return \SimpleSAML\SAML2\XML\saml\AbstractCondition The condition |
60
|
|
|
* |
61
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong |
62
|
|
|
*/ |
63
|
|
|
public static function fromXML(DOMElement $xml): object |
64
|
|
|
{ |
65
|
|
|
Assert::same($xml->localName, 'Condition', InvalidDOMElementException::class); |
66
|
|
|
Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class); |
67
|
|
|
Assert::true( |
68
|
|
|
$xml->hasAttributeNS(C::NS_XSI, 'type'), |
69
|
|
|
'Missing required xsi:type in <saml:Condition> element.', |
70
|
|
|
SchemaViolationException::class |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$type = $xml->getAttributeNS(C::NS_XSI, 'type'); |
74
|
|
|
Assert::validQName($type, SchemaViolationException::class); |
75
|
|
|
|
76
|
|
|
$qname = explode(':', $type, 2); |
77
|
|
|
if (count($qname) === 2) { |
78
|
|
|
list($prefix, $element) = $qname; |
79
|
|
|
} else { |
80
|
|
|
$prefix = null; |
81
|
|
|
list($element) = $qname; |
82
|
|
|
} |
83
|
|
|
$ns = $xml->lookupNamespaceUri($prefix); |
84
|
|
|
$handler = Utils::getContainer()->getElementHandler($ns, $element); |
85
|
|
|
|
86
|
|
|
Assert::notNull($handler, 'Unknown Condition type `' . $type . '`.'); |
87
|
|
|
Assert::isAOf($handler, Condition::class); |
88
|
|
|
|
89
|
|
|
return $handler::fromXML($xml); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Convert this Condition to XML. |
95
|
|
|
* |
96
|
|
|
* @param \DOMElement $parent The element we are converting to XML. |
97
|
|
|
* @return \DOMElement The XML element after adding the data corresponding to this Condition. |
98
|
|
|
*/ |
99
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
100
|
|
|
{ |
101
|
|
|
$e = $this->instantiateParentElement($parent); |
102
|
|
|
|
103
|
|
|
$e->setAttribute('xmlns:' . static::getXsiTypePrefix(), static::getXsiTypeNamespaceURI()); |
104
|
|
|
$e->setAttributeNS(C::NS_XSI, 'xsi:type', $this->getXsiType()); |
105
|
|
|
|
106
|
|
|
return $e; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|