Store   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 12
c 5
b 0
f 0
dl 0
loc 162
rs 10
wmc 4

3 Methods

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