Passed
Pull Request — master (#304)
by Tim
04:30 queued 02:15
created

BaseID::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\IDNameQualifiersTrait;
13
use SimpleSAML\SAML2\XML\saml\BaseIdentifierInterface;
14
use SimpleSAML\XML\Exception\SchemaViolationException;
15
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
16
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
17
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
18
19
use function trim;
20
21
/**
22
 * SAML BaseID data type.
23
 *
24
 * @package simplesamlphp/saml2
25
 */
26
abstract class BaseID extends AbstractSamlElement implements BaseIdentifierInterface, EncryptableElementInterface
27
{
28
    use EncryptableElementTrait;
29
    use IDNameQualifiersTrait;
30
31
    /** @var string */
32
    public const LOCALNAME = 'BaseID';
33
34
    /** @var string */
35
    protected string $type;
36
37
38
    /**
39
     * Initialize a saml:BaseID from scratch
40
     *
41
     * @param string $type
42
     * @param string|null $NameQualifier
43
     * @param string|null $SPNameQualifier
44
     */
45
    protected function __construct(
46
        string $type,
47
        ?string $NameQualifier = null,
48
        ?string $SPNameQualifier = null
49
    ) {
50
        $this->setType($type);
51
        $this->setNameQualifier($NameQualifier);
52
        $this->setSPNameQualifier($SPNameQualifier);
53
    }
54
55
56
    /**
57
     * Get the type of this BaseID (expressed in the xsi:type attribute).
58
     *
59
     * @return string
60
     */
61
    public function getType(): string
62
    {
63
        return $this->type;
64
    }
65
66
67
    /**
68
     * Set the type of this BaseID (in the xsi:type attribute)
69
     *
70
     * @param string $type
71
     */
72
    protected function setType(string $type): void
73
    {
74
        Assert::validQName($type, SchemaViolationException::class); // Covers the empty string
75
        $this->type = $type;
76
    }
77
78
79
    public function getBlacklistedAlgorithms(): ?array
80
    {
81
        $container = ContainerSingleton::getInstance();
82
        return $container->getBlacklistedEncryptionAlgorithms();
83
    }
84
85
86
    public function getEncryptionBackend(): ?EncryptionBackend
87
    {
88
        // return the encryption backend you want to use,
89
        // or null if you are fine with the default
90
        return null;
91
    }
92
93
94
    /**
95
     * Convert an XML element into a message.
96
     *
97
     * @param \DOMElement $xml The root XML element
98
     * @return \SimpleSAML\SAML2\XML\saml\BaseID The identifier
99
     *
100
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
101
     */
102
    public static function fromXML(DOMElement $xml): object
103
    {
104
        Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\sam...alidDOMElementException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
106
        Assert::true(
107
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
108
            'Missing required xsi:type in <saml:BaseID> element.',
109
            InvalidDOMElementException::class
110
        );
111
112
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
113
        Assert::validQName($type, SchemaViolationException::class);
114
115
        list($prefix, $element) = explode(':', $type, 2);
116
        $ns = $xml->lookupNamespaceUri($prefix);
117
        $handler = Utils::getContainer()->getElementHandler($ns, $element);
118
119
        Assert::notNull($handler, 'Unknown BaseID type `' . $type . '`.');
120
        Assert::isAOf($handler, BaseID::class);
121
122
        return $handler::fromXML($xml);
123
    }
124
125
126
    /**
127
     * Convert this BaseID to XML.
128
     *
129
     * @param \DOMElement $parent The element we are converting to XML.
130
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
131
     */
132
    public function toXML(DOMElement $parent = null): DOMElement
133
    {
134
        $e = $this->instantiateParentElement($parent);
135
        $e->setAttribute('xmlns:' . static::XSI_TYPE_PREFIX, static::XSI_TYPE_NS);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\XML\saml\BaseID::XSI_TYPE_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant SimpleSAML\SAML2\XML\saml\BaseID::XSI_TYPE_NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
136
        $e->setAttributeNS(C::NS_XSI, 'xsi:type', $this->type);
137
138
        if ($this->NameQualifier !== null) {
139
            $e->setAttribute('NameQualifier', $this->NameQualifier);
140
        }
141
142
        if ($this->SPNameQualifier !== null) {
143
            $e->setAttribute('SPNameQualifier', $this->SPNameQualifier);
144
        }
145
146
        return $e;
147
    }
148
}
149