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