1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\XML\saml\AbstractBaseID; |
10
|
|
|
use SimpleSAML\SAML2\XML\saml\EncryptedID; |
11
|
|
|
use SimpleSAML\SAML2\XML\saml\IdentifierInterface; |
12
|
|
|
use SimpleSAML\SAML2\XML\saml\NameID; |
13
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
14
|
|
|
|
15
|
|
|
use function array_pop; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait grouping common functionality for elements that can hold identifiers. |
19
|
|
|
* |
20
|
|
|
* @package simplesamlphp/saml2 |
21
|
|
|
*/ |
22
|
|
|
trait IdentifierTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The identifier for this element. |
26
|
|
|
* |
27
|
|
|
* @var \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null |
28
|
|
|
*/ |
29
|
|
|
protected ?IdentifierInterface $identifier = null; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Collect the value of the identifier-property |
34
|
|
|
* |
35
|
|
|
* @return \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null |
36
|
|
|
*/ |
37
|
|
|
public function getIdentifier(): ?IdentifierInterface |
38
|
|
|
{ |
39
|
|
|
return $this->identifier; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the value of the identifier-property |
45
|
|
|
* |
46
|
|
|
* @param \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null $identifier |
47
|
|
|
*/ |
48
|
|
|
protected function setIdentifier(?IdentifierInterface $identifier): void |
49
|
|
|
{ |
50
|
|
|
$this->identifier = $identifier; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve an identifier of any type from XML |
56
|
|
|
* |
57
|
|
|
* @param \DOMElement $xml |
58
|
|
|
* @return \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null |
59
|
|
|
* @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified |
60
|
|
|
*/ |
61
|
|
|
protected static function getIdentifierFromXML(DOMElement $xml): ?IdentifierInterface |
62
|
|
|
{ |
63
|
|
|
$class = static::NS_PREFIX . ':' . self::getClassName(static::class); |
64
|
|
|
|
65
|
|
|
$baseId = AbstractBaseID::getChildrenOfClass($xml); |
66
|
|
|
$nameId = NameID::getChildrenOfClass($xml); |
67
|
|
|
$encryptedId = EncryptedID::getChildrenOfClass($xml); |
68
|
|
|
|
69
|
|
|
// We accept only one of BaseID, NameID or EncryptedID |
70
|
|
|
Assert::maxCount( |
71
|
|
|
$baseId, |
72
|
|
|
1, |
73
|
|
|
'More than one <saml:BaseID> in <' . $class . '>.', |
74
|
|
|
TooManyElementsException::class, |
75
|
|
|
); |
76
|
|
|
Assert::maxCount( |
77
|
|
|
$nameId, |
78
|
|
|
1, |
79
|
|
|
'More than one <saml:NameID> in <' . $class . '>.', |
80
|
|
|
TooManyElementsException::class, |
81
|
|
|
); |
82
|
|
|
Assert::maxCount( |
83
|
|
|
$encryptedId, |
84
|
|
|
1, |
85
|
|
|
'More than one <saml:EncryptedID> in <' . $class . '>.', |
86
|
|
|
TooManyElementsException::class, |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$identifiers = array_merge($baseId, $nameId, $encryptedId); |
90
|
|
|
Assert::maxCount( |
91
|
|
|
$identifiers, |
92
|
|
|
1, |
93
|
|
|
'A <' . $class . '> can contain exactly one of <saml:BaseID>, <saml:NameID> or <saml:EncryptedID>.', |
94
|
|
|
TooManyElementsException::class, |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
/** @psalm-var \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null $identifier */ |
98
|
|
|
$identifier = array_pop($identifiers); |
99
|
|
|
|
100
|
|
|
return $identifier; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|