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