Conditions | 15 |
Paths | 78 |
Total Lines | 151 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
84 | public function main(Request $request): Response |
||
85 | { |
||
86 | $this->logger::info('FIDO2 - Accessing WebAuthn enrollment validation'); |
||
87 | |||
88 | $stateId = $request->query->get('StateId'); |
||
89 | if ($stateId === null) { |
||
90 | throw new Error\BadRequest('Missing required StateId query parameter.'); |
||
91 | } |
||
92 | |||
93 | $moduleConfig = Configuration::getOptionalConfig('module_webauthn.php'); |
||
94 | $debugEnabled = $moduleConfig->getOptionalBoolean('debug', false); |
||
95 | |||
96 | $state = $this->authState::loadState($stateId, 'webauthn:request'); |
||
97 | |||
98 | // registering a credential is only allowed for new users or after being authenticated |
||
99 | if (WebAuthn::workflowStateMachine($state) !== WebAuthn::STATE_MGMT) { |
||
|
|||
100 | throw new Exception("Attempt to register new token in unacceptable context."); |
||
101 | } |
||
102 | |||
103 | $fido2Scope = ($state['FIDO2Scope'] === null ? $state['FIDO2DerivedScope'] : $state['FIDO2Scope']); |
||
104 | if ($fido2Scope === null) { |
||
105 | throw new Exception("FIDO2Scope cannot be null!"); |
||
106 | } |
||
107 | |||
108 | $regObject = new WebAuthnRegistrationEvent( |
||
109 | $request->request->get('type'), |
||
110 | $fido2Scope, |
||
111 | $state['FIDO2SignupChallenge'], |
||
112 | base64_decode($request->request->get('attestation_object')), |
||
113 | $request->request->get('response_id'), |
||
114 | $request->request->get('attestation_client_data_json'), |
||
115 | ($request->request->get('passwordless') == "on" ? |
||
116 | $state['authenticatorAcceptabilityPasswordless'] : $state['authenticatorAcceptability2FA']), |
||
117 | $debugEnabled, |
||
118 | ); |
||
119 | // at this point, we need to talk to the DB |
||
120 | /** |
||
121 | * STEP 19 of the validation procedure in § 7.1 of the spec: see if this credential is already registered |
||
122 | */ |
||
123 | $store = $state['webauthn:store']; |
||
124 | if ($store->doesCredentialExist(bin2hex($regObject->getCredentialId())) === false) { |
||
125 | // credential does not exist yet in database, good. |
||
126 | } else { |
||
127 | throw new Exception("The credential with ID " . $regObject->getCredentialId() . " already exists."); |
||
128 | } |
||
129 | |||
130 | // THAT'S IT. This is a valid credential and can be enrolled to the user. |
||
131 | $friendlyName = $request->request->get('tokenname'); |
||
132 | |||
133 | // if we know the token model, add it to the name |
||
134 | $model = Translate::noop('unknown model'); |
||
135 | $aaguiddict = AAGUID::getInstance(); |
||
136 | if ($aaguiddict->hasToken($regObject->getAAGUID())) { |
||
137 | $token = $aaguiddict->get($regObject->getAAGUID()); |
||
138 | $model = $token['metadataStatement']['description']; |
||
139 | } |
||
140 | $friendlyName .= " ($model)"; |
||
141 | |||
142 | /** |
||
143 | * STEP 20 of the validation procedure in § 7.1 of the spec: store credentialId, credential, |
||
144 | * signCount and associate with user |
||
145 | */ |
||
146 | |||
147 | /* |
||
148 | * Observed with YubiKey 5: the transaction counter is 0 if the key has NEVER been used, but |
||
149 | * the first transaction is also transaction #0. |
||
150 | * i.e. 0 means "before first transaction OR the very first transaction has already taken place" |
||
151 | * |
||
152 | * The very first use of a key should not trigger the physical object cloning alert, so a |
||
153 | * transaction counter == 0 should be allowed for the first authentication of a new key. |
||
154 | * The best way to do this is to set the current counter value to -1 when registering a key |
||
155 | * with a transaction counter of 0. |
||
156 | */ |
||
157 | $currentCounterValue = -1; |
||
158 | if ($regObject->getCounter() > 0) { |
||
159 | $currentCounterValue = $regObject->getCounter(); |
||
160 | } |
||
161 | |||
162 | // did we get any client extensions? |
||
163 | $isResidentKey = 0; |
||
164 | if ( |
||
165 | strlen($request->request->get('clientext')) > 0 && |
||
166 | count(json_decode($request->request->get('clientext'), true)) > 0 |
||
167 | ) { |
||
168 | $extensions = json_decode($request->request->get('clientext'), true); |
||
169 | if ($extensions['credProps']['rk'] === true) { |
||
170 | $isResidentKey = 1; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | // we also need to store the hased user_id in case we need to retrieve |
||
175 | // tokens in passwordless mode |
||
176 | // use identical hashing as in JS generation step |
||
177 | $configUtils = new Utils\Config(); |
||
178 | $username = hash('sha512', $state['FIDO2Username'] . '|' . $configUtils->getSecretSalt()); |
||
179 | $store->storeTokenData( |
||
180 | $state['FIDO2Username'], |
||
181 | $regObject->getCredentialId(), |
||
182 | $regObject->getCredential(), |
||
183 | $regObject->getAlgo(), |
||
184 | $regObject->getPresenceLevel(), |
||
185 | // we deny saving resident key properties if Passwordless mode |
||
186 | // was not requested |
||
187 | ($request->request->get('passwordless') == "on" ? $isResidentKey : 0), |
||
188 | $currentCounterValue, |
||
189 | $friendlyName, |
||
190 | $username, |
||
191 | $regObject->getAAGUID(), |
||
192 | $regObject->getAttestationLevel(), |
||
193 | ); |
||
194 | |||
195 | // make sure $state gets the news, the token is to be displayed to the user on the next page |
||
196 | $state['FIDO2Tokens'][] = [ |
||
197 | 0 => $regObject->getCredentialId(), |
||
198 | 1 => $regObject->getCredential(), |
||
199 | 2 => $currentCounterValue, |
||
200 | 3 => $friendlyName, |
||
201 | 4 => $regObject->getAlgo(), |
||
202 | 5 => $regObject->getPresenceLevel(), |
||
203 | 6 => $isResidentKey, |
||
204 | ]; |
||
205 | |||
206 | $id = $this->authState::saveState($state, 'webauthn:request'); |
||
207 | if ($debugEnabled === true) { |
||
208 | $response = new RunnableResponse( |
||
209 | function (WebAuthnRegistrationEvent $regObject, string $id) { |
||
210 | echo $regObject->getDebugBuffer(); |
||
211 | echo $regObject->getValidateBuffer(); |
||
212 | echo "<form id='regform' method='POST' action='" . |
||
213 | Module::getModuleURL('webauthn/webauthn?StateId=' . urlencode($id)) . "'>"; |
||
214 | echo "<button type='submit'>Return to previous page.</button>"; |
||
215 | }, |
||
216 | [$regObject, $id], |
||
217 | ); |
||
218 | } elseif (array_key_exists('Registration', $state)) { |
||
219 | $response = new RedirectResponse(Module::getModuleURL('webauthn/webauthn?StateId=' . urlencode($id))); |
||
220 | } else { |
||
221 | $response = new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]); |
||
222 | } |
||
223 | |||
224 | $response->setExpires(new DateTime('Thu, 19 Nov 1981 08:52:00 GMT')); |
||
225 | $response->setCache([ |
||
226 | 'must_revalidate' => true, |
||
227 | 'no_cache' => true, |
||
228 | 'no_store' => true, |
||
229 | 'no_transform' => false, |
||
230 | 'public' => false, |
||
231 | 'private' => false, |
||
232 | ]); |
||
233 | |||
234 | return $response; |
||
235 | } |
||
237 |