simplesamlphp /
xml-security
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XMLSecurity\XML\ds; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use SimpleSAML\Assert\Assert; |
||
| 9 | use SimpleSAML\XML\Chunk; |
||
| 10 | use SimpleSAML\XML\ExtendableElementTrait; |
||
| 11 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 12 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 13 | use SimpleSAML\XML\SerializableElementInterface; |
||
| 14 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 15 | use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
||
| 16 | use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
||
| 17 | use SimpleSAML\XMLSchema\XML\Constants\NS; |
||
| 18 | use SimpleSAML\XMLSecurity\Constants as C; |
||
| 19 | use SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue; |
||
| 20 | |||
| 21 | use function array_merge; |
||
| 22 | use function array_pop; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class representing a ds:KeyValue element. |
||
| 26 | * |
||
| 27 | * @package simplesamlphp/xml-security |
||
| 28 | */ |
||
| 29 | final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface |
||
| 30 | { |
||
| 31 | // We use our own getter instead of the trait's one, so we prevent their use by marking them private |
||
| 32 | use ExtendableElementTrait { |
||
| 33 | getElements as private; |
||
| 34 | setElements as private; |
||
| 35 | } |
||
| 36 | |||
| 37 | |||
| 38 | use SchemaValidatableElementTrait; |
||
| 39 | |||
| 40 | |||
| 41 | /** The namespace-attribute for the xs:any element */ |
||
| 42 | public const string XS_ANY_ELT_NAMESPACE = NS::OTHER; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Initialize an KeyValue. |
||
| 47 | * |
||
| 48 | * @param \SimpleSAML\XML\SerializableElementInterface $keyValue |
||
| 49 | */ |
||
| 50 | final public function __construct( |
||
| 51 | protected RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface $keyValue, |
||
| 52 | ) { |
||
| 53 | /** @var \SimpleSAML\XML\AbstractElement|\SimpleSAML\XML\Chunk $keyValue */ |
||
| 54 | if ( |
||
| 55 | !($keyValue instanceof RSAKeyValue |
||
| 56 | || $keyValue instanceof DSAKeyValue |
||
| 57 | || $keyValue instanceof ECKeyValue) |
||
| 58 | ) { |
||
| 59 | Assert::true( |
||
| 60 | (($keyValue instanceof Chunk) ? $keyValue->getNamespaceURI() : $keyValue::getNameSpaceURI()) |
||
| 61 | !== C::NS_XDSIG, |
||
| 62 | 'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue ' |
||
| 63 | . 'or an element in namespace ##other', |
||
| 64 | SchemaViolationException::class, |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Collect the value of the RSAKeyValue-property |
||
| 72 | * |
||
| 73 | * @return (\SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue| |
||
| 74 | * \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue| |
||
| 75 | * \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue| |
||
| 76 | * \SimpleSAML\XML\SerializableElementInterface) |
||
| 77 | */ |
||
| 78 | public function getKeyValue(): RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface |
||
| 79 | { |
||
| 80 | return $this->keyValue; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Convert XML into a KeyValue |
||
| 86 | * |
||
| 87 | * @param \DOMElement $xml The XML element we should load |
||
| 88 | * |
||
| 89 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 90 | * If the qualified name of the supplied element is wrong |
||
| 91 | */ |
||
| 92 | public static function fromXML(DOMElement $xml): static |
||
| 93 | { |
||
| 94 | Assert::same($xml->localName, 'KeyValue', InvalidDOMElementException::class); |
||
| 95 | Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class); |
||
| 96 | |||
| 97 | $keyValue = array_merge( |
||
| 98 | RSAKeyValue::getChildrenOfClass($xml), |
||
| 99 | DSAKeyValue::getChildrenOfClass($xml), |
||
| 100 | self::getChildElementsFromXML($xml), |
||
| 101 | ); |
||
| 102 | |||
| 103 | Assert::count( |
||
| 104 | $keyValue, |
||
| 105 | 1, |
||
| 106 | 'A <ds:KeyValue> must contain exactly one child element', |
||
| 107 | TooManyElementsException::class, |
||
| 108 | ); |
||
| 109 | |||
| 110 | return new static(array_pop($keyValue)); |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Convert this KeyValue element to XML. |
||
| 116 | * |
||
| 117 | * @param \DOMElement|null $parent The element we should append this KeyValue element to. |
||
| 118 | */ |
||
| 119 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 120 | { |
||
| 121 | $e = $this->instantiateParentElement($parent); |
||
| 122 | |||
| 123 | $this->getKeyValue()->toXML($e); |
||
| 124 | |||
| 125 | return $e; |
||
| 126 | } |
||
| 127 | } |
||
| 128 |