Passed
Pull Request — master (#305)
by Jaime Pérez
02:20
created

AbstractBaseID   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 15 3
A __construct() 0 8 1
A getXsiType() 0 3 1
A fromXML() 0 41 4
A getBlacklistedAlgorithms() 0 4 1
A getEncryptionBackend() 0 5 1
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\Compat\ContainerSingleton;
10
use SimpleSAML\SAML2\Constants as C;
11
use SimpleSAML\SAML2\Utils;
12
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
13
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
14
use SimpleSAML\SAML2\XML\IDNameQualifiersTrait;
15
use SimpleSAML\XML\Chunk;
16
use SimpleSAML\XML\Exception\InvalidDOMElementException;
17
use SimpleSAML\XML\Exception\SchemaViolationException;
18
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
19
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
20
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
21
22
use function count;
23
use function explode;
24
25
/**
26
 * SAML BaseID data type.
27
 *
28
 * @package simplesamlphp/saml2
29
 */
30
abstract class AbstractBaseID extends AbstractSamlElement implements
31
     BaseIdentifierInterface,
32
     EncryptableElementInterface,
33
     ExtensionPointInterface
34
{
35
    use EncryptableElementTrait;
36
    use ExtensionPointTrait;
37
    use IDNameQualifiersTrait;
38
39
    /** @var string */
40
    public const LOCALNAME = 'BaseID';
41
42
    /** @var string */
43
    protected string $type;
44
45
46
    /**
47
     * Initialize a saml:BaseID from scratch
48
     *
49
     * @param string $type
50
     * @param string|null $NameQualifier
51
     * @param string|null $SPNameQualifier
52
     */
53
    protected function __construct(
54
        string $type,
55
        ?string $NameQualifier = null,
56
        ?string $SPNameQualifier = null
57
    ) {
58
        $this->type = $type;
59
        $this->setNameQualifier($NameQualifier);
60
        $this->setSPNameQualifier($SPNameQualifier);
61
    }
62
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function getXsiType(): string
68
    {
69
        return $this->type;
70
    }
71
72
73
    public function getBlacklistedAlgorithms(): ?array
74
    {
75
        $container = ContainerSingleton::getInstance();
76
        return $container->getBlacklistedEncryptionAlgorithms();
77
    }
78
79
80
    public function getEncryptionBackend(): ?EncryptionBackend
81
    {
82
        // return the encryption backend you want to use,
83
        // or null if you are fine with the default
84
        return null;
85
    }
86
87
88
    /**
89
     * Convert XML into an BaseID
90
     *
91
     * @param \DOMElement $xml The XML element we should load
92
     * @return \SimpleSAML\SAML2\XML\saml\BaseIdentifierInterface
93
     *
94
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
95
     */
96
    public static function fromXML(DOMElement $xml): object
97
    {
98
        Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
100
        Assert::true(
101
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
102
            'Missing required xsi:type in <saml:BaseID> element.',
103
            SchemaViolationException::class
104
        );
105
106
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
107
        Assert::validQName($type, SchemaViolationException::class);
108
109
        // first, try to resolve the type to a full namespaced version
110
        $qname = explode(':', $type, 2);
111
        if (count($qname) === 2) {
112
            list($prefix, $element) = $qname;
113
        } else {
114
            $prefix = null;
115
            list($element) = $qname;
116
        }
117
        $ns = $xml->lookupNamespaceUri($prefix);
118
        $type = ($ns === null ) ? $element : implode(':', [$ns, $element]);
119
120
        // now check if we have a handler registered for it
121
        $handler = Utils::getContainer()->getExtensionHandler($type);
122
        if ($handler === null) {
123
            // we don't have a handler, proceed with unknown identifier
124
            return new UnknownID(
125
                new Chunk($xml),
126
                $type,
127
                self::getAttribute($xml, 'NameQualifier', null),
128
                self::getAttribute($xml, 'SPNameQualifier', null)
129
            );
130
        }
131
132
        Assert::subclassOf(
133
            $handler,
134
            AbstractBaseID::class,
135
            'Elements implementing BaseID must extend \SimpleSAML\SAML2\XML\saml\AbstractBaseID.');
136
        return $handler::fromXML($xml);
137
    }
138
139
140
    /**
141
     * Convert this BaseID to XML.
142
     *
143
     * @param \DOMElement $parent The element we are converting to XML.
144
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
145
     */
146
    public function toXML(DOMElement $parent = null): DOMElement
147
    {
148
        $e = $this->instantiateParentElement($parent);
149
        $e->setAttribute('xmlns:' . static::getXsiTypePrefix(), static::getXsiTypeNamespaceURI());
150
        $e->setAttributeNS(C::NS_XSI, 'xsi:type', $this->getXsiType());
151
152
        if ($this->NameQualifier !== null) {
153
            $e->setAttribute('NameQualifier', $this->NameQualifier);
154
        }
155
156
        if ($this->SPNameQualifier !== null) {
157
            $e->setAttribute('SPNameQualifier', $this->SPNameQualifier);
158
        }
159
160
        return $e;
161
    }
162
}
163