Store::parseStoreConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 10
nc 2
nop 1
dl 0
loc 18
c 4
b 0
f 0
cc 2
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\webauthn;
6
7
use Exception;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\Module;
10
use SimpleSAML\Utils;
11
12
/**
13
 * Base class for consent storage handlers.
14
 *
15
 * @package SimpleSAMLphp
16
 * @author Olav Morken <[email protected]>
17
 * @author JAcob Christiansen <[email protected]>
18
 */
19
20
abstract class Store
21
{
22
    /**
23
     * Constructor for the base class.
24
     *
25
     * This constructor should always be called first in any class which implements this class.
26
     *
27
     * @param array &$config The configuration for this storage handler.
28
     *
29
     * @phpstan-ignore constructor.unusedParameter
30
     */
31
    protected function __construct(array &$config)
32
    {
33
    }
34
35
36
    /**
37
     * is the user subject to 2nd factor at all?
38
     *
39
     * This function checks whether a given user has been enabled for WebAuthn.
40
     *
41
     * @param string $userId The hash identifying the user at an IdP.
42
     * @param bool $defaultIfNx if not found in the DB, should the user be considered enabled (true)
43
     *                              or disabled(false)
44
     * @param bool $useDatabase a bool that determines whether to use local database or not
45
     * @param bool $toggle variable which is associated with $force because it determines its meaning, it either
46
     *                     simply means whether to trigger webauthn authentication or switch the default settings,
47
     * @param bool $force switch that determines how $toggle will be used, if true then value of $toggle
48
     *                    will mean whether to trigger (true) or not (false) the webauthn authentication,
49
     *                    if false then $toggle means whether to switch the value of $defaultEnabled and then use that
50
     *
51
     * @return bool True if the user is enabled for 2FA, false if not
52
     */
53
    abstract public function is2FAEnabled(
54
        string $userId,
55
        bool $defaultIfNx,
56
        bool $useDatabase = true,
57
        bool $toggle = false,
58
        bool $force = true,
59
    ): bool;
60
61
62
    /**
63
     * does a given credentialID already exist?
64
     *
65
     * This function checks whether a given credential ID already exists in the database
66
     *
67
     * @param string $credIdHex The hex representation of the credentialID to look for.
68
     *
69
     * @return bool True if the credential exists, false if not
70
     */
71
    abstract public function doesCredentialExist(string $credIdHex): bool;
72
73
74
    /**
75
     * store newly enrolled token data
76
     *
77
     * @param string $userId        The user.
78
     * @param string $credentialId  The id identifying the credential.
79
     * @param string $credential    The credential.
80
     * @param int    $algo          The algorithm used.
81
     * @param int    $presenceLevel UV or UP?
82
     * @param int    $signCounter   The signature counter for this credential.
83
     * @param string $friendlyName  A user-supplied name for this token.
84
     * @param string $hashedId      hashed ID of the user
85
     */
86
    abstract public function storeTokenData(
87
        string $userId,
88
        string $credentialId,
89
        string $credential,
90
        int $algo,
91
        int $presenceLevel,
92
        int $isResidentKey,
93
        int $signCounter,
94
        string $friendlyName,
95
        string $hashedId,
96
        string $aaguid,
97
        string $attLevel,
98
    ): true;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\webauthn\true 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...
99
100
101
    /**
102
     * remove an existing credential from the database
103
     *
104
     * @param string $credentialId the credential
105
     */
106
    abstract public function deleteTokenData(string $credentialId): true;
107
108
109
    /**
110
     * increment the signature counter after a successful authentication
111
     *
112
     * @param string $credentialId the credential
113
     * @param int    $signCounter  the new counter value
114
     */
115
    abstract public function updateSignCount(string $credentialId, int $signCounter): true;
116
117
118
    /**
119
     * Retrieve existing token data
120
     *
121
     * @param string $userId the username
122
     * @return array Array of all crypto data we have on file.
123
     */
124
    abstract public function getTokenData(string $userId): array;
125
126
127
    /**
128
     * Retrieve username, given a credential ID
129
     *
130
     * @param string $hashedId the credential ID
131
     * @return string the username, if found (otherwise, empty string)
132
     */
133
    abstract public function getUsernameByHashedId(string $hashedId): string;
134
135
136
    /**
137
     * Get statistics for all consent given in the consent store
138
     *
139
     * @return mixed Statistics from the consent store
140
     *
141
     * @throws \Exception
142
     */
143
    public function getStatistics()
144
    {
145
        throw new Exception('Not implemented: getStatistics()');
146
    }
147
148
149
    /**
150
     * Parse consent storage configuration.
151
     *
152
     * This function parses the configuration for a consent storage method. An exception will be thrown if
153
     * configuration parsing fails.
154
     *
155
     * @param string|array $config The configuration.
156
     * @return \SimpleSAML\Module\webauthn\Store An object which implements the \SimpleSAML\Module\webauthn\Store class.
157
     *
158
     * @throws \Exception if the configuration is invalid.
159
     */
160
    public static function parseStoreConfig(string|array $config): Store
161
    {
162
        if (is_string($config)) {
0 ignored issues
show
introduced by
The condition is_string($config) is always false.
Loading history...
163
            $arrayUtils = new Utils\Arrays();
164
            $config = $arrayUtils->arrayize($config);
165
        }
166
167
        Assert::isArray($config, 'Invalid configuration for consent store option: ' . var_export($config, true));
168
        Assert::keyExists($config, 0, 'Consent store without name given.');
169
170
        $className = Module::resolveClass(
171
            $config[0],
172
            'WebAuthn\Store',
173
            '\SimpleSAML\Module\webauthn\Store',
174
        );
175
176
        /** @var \SimpleSAML\Module\webauthn\Store */
177
        return new $className($config);
178
    }
179
}
180