AbstractContainer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 171
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBlacklistedEncryptionAlgorithms() 0 3 1
A registerElementHandler() 0 5 1
A getElementHandler() 0 9 2
A getExtensionHandler() 0 9 2
A registerExtensionHandler() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Compat;
6
7
use Psr\Clock\ClockInterface;
8
use Psr\Log\LoggerInterface;
9
use SimpleSAML\SAML2\Assert\Assert;
10
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
11
use SimpleSAML\XML\AbstractElement;
12
use SimpleSAML\XML\ElementInterface;
13
use SimpleSAML\XMLSchema\Type\QNameValue;
14
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
15
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
16
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
17
18
use function array_key_exists;
19
use function constant;
20
21
abstract class AbstractContainer
22
{
23
    /** @var array */
24
    protected array $registry = [];
25
26
    /** @var array */
27
    protected array $extRegistry = [];
28
29
    /** @var array|null */
30
    protected ?array $blacklistedEncryptionAlgorithms = [
31
        EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
32
        KeyTransportAlgorithmFactory::DEFAULT_BLACKLIST,
33
        SignatureAlgorithmFactory::DEFAULT_BLACKLIST,
34
    ];
35
36
37
    /**
38
     * Get the list of algorithms that are blacklisted for any encryption operation.
39
     *
40
     * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null if we want to use the
41
     * defaults.
42
     */
43
    public function getBlacklistedEncryptionAlgorithms(): ?array
44
    {
45
        return $this->blacklistedEncryptionAlgorithms;
46
    }
47
48
49
    /**
50
     * Register a class that can handle a given element.
51
     *
52
     * @param string $class The class name of a class extending AbstractElement
53
     * @psalm-param class-string $class
54
     */
55
    public function registerElementHandler(string $class): void
56
    {
57
        Assert::subclassOf($class, AbstractElement::class);
58
        $key = '{' . constant($class::NS) . '}' . AbstractElement::getClassName($class);
59
        $this->registry[$key] = $class;
60
    }
61
62
63
    /**
64
     * Register a class that can handle given extension points of the standard.
65
     *
66
     * @param string $class The class name of a class extending AbstractElement or implementing ExtensionPointInterface.
67
     * @psalm-param class-string $class
68
     */
69
    public function registerExtensionHandler(string $class): void
70
    {
71
        Assert::subclassOf($class, ExtensionPointInterface::class);
72
        $key = '{' . $class::getXsiTypeNamespaceURI() . '}' . $class::getXsiTypeName();
73
        $this->extRegistry[$key] = $class;
74
    }
75
76
77
    /**
78
     * Search for a class that implements an element in the given $namespace.
79
     *
80
     * Such classes must have been registered previously by calling registerExtensionHandler(), and they must
81
     * extend \SimpleSAML\XML\AbstractElement.
82
     *
83
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $qName The qualified name of the element.
84
     *
85
     * @return string|null The fully-qualified name of a class extending \SimpleSAML\XML\AbstractElement and
86
     * implementing support for the given element, or null if no such class has been registered before.
87
     * @psalm-return class-string|null
88
     */
89
    public function getElementHandler(QNameValue $qName): ?string
90
    {
91
        $key = '{' . $qName->getNameSpaceURI()->getValue() . '}' . $qName->getLocalName()->getValue();
92
        if (array_key_exists($key, $this->registry) === true) {
93
            Assert::implementsInterface($this->registry[$key], ElementInterface::class);
94
            return $this->registry[$key];
95
        }
96
97
        return null;
98
    }
99
100
101
    /**
102
     * Search for a class that implements a custom element type.
103
     *
104
     * Such classes must have been registered previously by calling registerExtensionHandler(), and they must
105
     * implement \SimpleSAML\SAML11\XML\saml\ExtensionPointInterface.
106
     *
107
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $qName The qualified name of the extension.
108
     * @return string|null The fully-qualified name of a class implementing
109
     *  \SimpleSAML\SAML11\XML\saml\ExtensionPointInterface or null if no such class has been registered before.
110
     * @psalm-return class-string|null
111
     */
112
    public function getExtensionHandler(QNameValue $qName): ?string
113
    {
114
        $key = '{' . $qName->getNameSpaceURI()->getValue() . '}' . $qName->getLocalName()->getValue();
115
        if (array_key_exists($key, $this->extRegistry) === true) {
116
            Assert::implementsInterface($this->extRegistry[$key], ExtensionPointInterface::class);
117
            return $this->extRegistry[$key];
118
        }
119
120
        return null;
121
    }
122
123
124
    /**
125
     * Set the list of algorithms that are blacklisted for any encryption operation.
126
     *
127
     * @param string[]|null $algos An array with all algorithm identifiers that are blacklisted,
128
     * or null if we want to use the defaults.
129
     */
130
    abstract public function setBlacklistedAlgorithms(?array $algos): void;
131
132
133
    /**
134
     * Get a PSR-3 compatible logger.
135
     * @return \Psr\Log\LoggerInterface
136
     */
137
    abstract public function getLogger(): LoggerInterface;
138
139
140
    /**
141
     * Log an incoming message to the debug log.
142
     *
143
     * Type can be either:
144
     * - **in** XML received from third party
145
     * - **out** XML that will be sent to third party
146
     * - **encrypt** XML that is about to be encrypted
147
     * - **decrypt** XML that was just decrypted
148
     *
149
     * @param \DOMElement|string $message
150
     * @param string $type
151
     */
152
    abstract public function debugMessage($message, string $type): void;
153
154
155
    /**
156
     * Trigger the user to perform a POST to the given URL with the given data.
157
     *
158
     * @param string $url
159
     * @param array $data
160
     * @return string
161
     */
162
    abstract public function getPOSTRedirectURL(string $url, array $data = []): string;
163
164
165
    /**
166
     * This function retrieves the path to a directory where temporary files can be saved.
167
     *
168
     * @throws \Exception If the temporary directory cannot be created or it exists and does not belong
169
     * to the current user.
170
     * @return string Path to a temporary directory, without a trailing directory separator.
171
     */
172
    abstract public function getTempDir(): string;
173
174
175
    /**
176
     * Atomically write a file.
177
     *
178
     * This is a helper function for writing data atomically to a file. It does this by writing the file data to a
179
     * temporary file, then renaming it to the required file name.
180
     *
181
     * @param string $filename The path to the file we want to write to.
182
     * @param string $data The data we should write to the file.
183
     * @param int|null $mode The permissions to apply to the file. Defaults to 0600.
184
     */
185
    abstract public function writeFile(string $filename, string $data, ?int $mode = null): void;
186
187
188
    /**
189
     * Get the system clock, using UTC for a timezone
190
     */
191
    abstract public function getClock(): ClockInterface;
192
}
193