EncryptedElementTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 4
A getEncryptionBackend() 0 5 1
A getBlacklistedAlgorithms() 0 4 1
A toXML() 0 11 2
A fromXML() 0 22 1
A getDecryptionKeys() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Compat\ContainerSingleton;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\AbstractElement;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
15
use SimpleSAML\XMLSecurity\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Constants 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...
16
use SimpleSAML\XMLSecurity\XML\EncryptedElementTrait as ParentEncryptedElementTrait;
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
18
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
19
20
/**
21
 * Trait aggregating functionality for elements that are encrypted.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
trait EncryptedElementTrait
26
{
27
    use ParentEncryptedElementTrait;
28
29
30
    /**
31
     * Constructor for encrypted elements.
32
     *
33
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData $encryptedData The EncryptedData object.
34
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey[] $decryptionKeys The EncryptedKey objects.
35
     */
36
    final public function __construct(
37
        protected EncryptedData $encryptedData,
38
        protected array $decryptionKeys = [],
39
    ) {
40
        Assert::allIsInstanceOf($decryptionKeys, EncryptedKey::class, ProtocolViolationException::class);
41
42
        /**
43
         * 6.2: The <EncryptedData> element's Type attribute SHOULD be used and, if it is
44
         * present, MUST have the value http://www.w3.org/2001/04/xmlenc#Element.
45
         *
46
         */
47
        Assert::nullOrSame($encryptedData->getType()->getValue(), C::XMLENC_ELEMENT);
48
49
        $keyInfo = $this->encryptedData->getKeyInfo();
50
        if ($keyInfo === null) {
51
            return;
52
        }
53
54
        foreach ($keyInfo->getInfo() as $info) {
55
            if ($info instanceof EncryptedKey) {
56
                $this->encryptedKey = [$info];
57
                break;
58
            }
59
        }
60
    }
61
62
63
    /**
64
     * @return array|null
65
     */
66
    public function getBlacklistedAlgorithms(): ?array
67
    {
68
        $container = ContainerSingleton::getInstance();
69
        return $container->getBlacklistedEncryptionAlgorithms();
70
    }
71
72
73
    /**
74
     * @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend|null
75
     */
76
    public function getEncryptionBackend(): ?EncryptionBackend
77
    {
78
        // return the encryption backend you want to use,
79
        // or null if you are fine with the default
80
        return null;
81
    }
82
83
84
    public function getDecryptionKeys(): array
85
    {
86
        return $this->decryptionKeys;
87
    }
88
89
90
    /**
91
     * @inheritDoc
92
     *
93
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
94
     *   If the qualified name of the supplied element is wrong
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same(
99
            $xml->localName,
100
            AbstractElement::getClassName(static::class),
101
            InvalidDOMElementException::class,
102
        );
103
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\XML\EncryptedElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
105
        $ed = EncryptedData::getChildrenOfClass($xml);
106
        Assert::count(
107
            $ed,
108
            1,
109
            sprintf(
110
                'No more or less than one EncryptedData element allowed in %s.',
111
                AbstractElement::getClassName(static::class),
112
            ),
113
            TooManyElementsException::class,
114
        );
115
116
        $ek = EncryptedKey::getChildrenOfClass($xml);
117
        return new static($ed[0], $ek);
118
    }
119
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function toXML(?DOMElement $parent = null): DOMElement
125
    {
126
        $e = $this->instantiateParentElement($parent);
127
128
        $this->encryptedData->toXML($e);
129
130
        foreach ($this->getDecryptionKeys() as $key) {
131
            $key->toXML($e);
132
        }
133
134
        return $e;
135
    }
136
}
137