Passed
Pull Request — master (#305)
by Tim
02:46
created

AbstractContainer::getIdentifierHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Compat;
6
7
use Psr\Log\LoggerInterface;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\AbstractXMLElement;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
12
13
use function array_key_exists;
14
use function explode;
15
use function join;
16
17
abstract class AbstractContainer
18
{
19
    /** @var array */
20
    public array $registry = [];
21
22
    /** @var array|null */
23
    protected ?array $blacklistedEncryptionAlgorithms;
24
25
26
    /**
27
     * Set the list of algorithms that are blacklisted for any encryption operation.
28
     *
29
     * @param string[]|null $algos An array with all algorithm identifiers that are blacklisted,
30
     * or null if we want to use the defaults.
31
     */
32
    abstract public function setBlacklistedAlgorithms(?array $algos): void;
33
34
35
    /**
36
     * Get the list of algorithms that are blacklisted for any encryption operation.
37
     *
38
     * @return string[]|null An array with all algorithm identifiers that are blacklisted, or null if we want to use the
39
     * defaults.
40
     */
41
    public function getBlacklistedEncryptionAlgorithms(): ?array
42
    {
43
        return $this->blacklistedEncryptionAlgorithms;
44
    }
45
46
47
    /**
48
     * Get a PSR-3 compatible logger.
49
     * @return \Psr\Log\LoggerInterface
50
     */
51
    abstract public function getLogger(): LoggerInterface;
52
53
54
    /**
55
     * Generate a random identifier for identifying SAML2 documents.
56
     * @return string
57
     */
58
    abstract public function generateId(): string;
59
60
61
    /**
62
     * Log an incoming message to the debug log.
63
     *
64
     * Type can be either:
65
     * - **in** XML received from third party
66
     * - **out** XML that will be sent to third party
67
     * - **encrypt** XML that is about to be encrypted
68
     * - **decrypt** XML that was just decrypted
69
     *
70
     * @param \DOMElement|string $message
71
     * @param string $type
72
     */
73
    abstract public function debugMessage($message, string $type): void;
74
75
76
    /**
77
     * Trigger the user to perform a GET to the given URL with the given data.
78
     *
79
     * @param string $url
80
     * @param array $data
81
     */
82
    abstract public function redirect(string $url, array $data = []): void;
83
84
85
    /**
86
     * Trigger the user to perform a POST to the given URL with the given data.
87
     *
88
     * @param string $url
89
     * @param array $data
90
     */
91
    abstract public function postRedirect(string $url, array $data = []): void;
92
93
94
    /**
95
     * This function retrieves the path to a directory where temporary files can be saved.
96
     *
97
     * @throws \Exception If the temporary directory cannot be created or it exists and does not belong
98
     * to the current user.
99
     * @return string Path to a temporary directory, without a trailing directory separator.
100
     */
101
    abstract public function getTempDir(): string;
102
103
104
    /**
105
     * Atomically write a file.
106
     *
107
     * This is a helper function for writing data atomically to a file. It does this by writing the file data to a
108
     * temporary file, then renaming it to the required file name.
109
     *
110
     * @param string $filename The path to the file we want to write to.
111
     * @param string $data The data we should write to the file.
112
     * @param int $mode The permissions to apply to the file. Defaults to 0600.
113
     */
114
    abstract public function writeFile(string $filename, string $data, int $mode = null): void;
115
116
117
    /**
118
     * Register a class that can handle given extension points of the standard.
119
     *
120
     * @param string $class The class name of a class extending AbstractXMLElement or implementing ExtensionPointInterface.
121
     * @psalm-param class-string $class
122
     */
123
    public function registerExtensionHandler(string $class): void
124
    {
125
        Assert::subclassOf($class, AbstractXMLElement::class);
126
        if (in_array(ExtensionPointInterface::class, class_implements($class))) {
127
            $key = join(':', [$class::getXsiTypeNamespaceURI(), $class::getXsiTypeName()]);
128
        } else {
129
            $key = join(':', [$class::NS, AbstractXMLElement::getClassName($class)]);
130
        }
131
        $this->registry[$key] = $class;
132
    }
133
134
135
    /**
136
     * Search for a class that implements an $element in the given $namespace.
137
     *
138
     * Such classes must have been registered previously by calling registerExtensionHandler(), and they must
139
     * extend \SimpleSAML\XML\AbstractXMLElement.
140
     *
141
     * @param string $namespace The namespace URI for the given element.
142
     * @param string $element The local name of the element.
143
     *
144
     * @return string|null The fully-qualified name of a class extending \SimpleSAML\XML\AbstractXMLElement and
145
     * implementing support for the given element, or null if no such class has been registered before.
146
     * @psalm-return class-string|null
147
     */
148
    public function getElementHandler(string $namespace, string $elementOrType): ?string
149
    {
150
        Assert::validURI($namespace, SchemaViolationException::class);
151
        Assert::validNCName($elementOrType, SchemaViolationException::class);
152
153
        $key = join(':', [$namespace, $elementOrType]);
154
        if (array_key_exists($key, $this->registry) === true) {
155
            return $this->registry[$key];
156
        }
157
158
        return null;
159
    }
160
}
161