| Conditions | 12 |
| Paths | 48 |
| Total Lines | 101 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 97 | public function main(Request $request): Response |
||
| 98 | { |
||
| 99 | if (session_status() != PHP_SESSION_ACTIVE) { |
||
| 100 | session_cache_limiter('nocache'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->logger::info('FIDO2 - Accessing WebAuthn enrollment validation'); |
||
| 104 | |||
| 105 | $stateId = $request->query->get('StateId'); |
||
| 106 | if ($stateId === null) { |
||
| 107 | throw new Error\BadRequest('Missing required StateId query parameter.'); |
||
| 108 | } |
||
| 109 | |||
| 110 | $debugEnabled = $this->config->getValue('logging.level', Logger::NOTICE) === Logger::DEBUG; |
||
| 111 | |||
| 112 | $state = $this->authState::loadState($stateId, 'webauthn:request'); |
||
| 113 | |||
| 114 | // registering a credential is only allowed for new users or after being authenticated |
||
| 115 | if (count($state['FIDO2Tokens']) > 0 && $state['FIDO2AuthSuccessful'] === false) { |
||
| 116 | throw new Exception("Attempt to register new token in unacceptable context."); |
||
| 117 | } |
||
| 118 | |||
| 119 | $fido2Scope = ($state['FIDO2Scope'] === null ? $state['FIDO2DerivedScope'] : $state['FIDO2Scope']); |
||
| 120 | if ($fido2Scope === null) { |
||
| 121 | throw new Exception("FIDO2Scope cannot be null!"); |
||
| 122 | } |
||
| 123 | |||
| 124 | $regObject = new WebAuthnRegistrationEvent( |
||
| 125 | $request->request->get('type'), |
||
| 126 | $fido2Scope, |
||
| 127 | $state['FIDO2SignupChallenge'], |
||
| 128 | $state['IdPMetadata']['entityid'], |
||
| 129 | base64_decode($_POST['attestation_object']), |
||
| 130 | $request->request->get('response_id'), |
||
| 131 | $request->request->get('attestation_client_data_json'), |
||
| 132 | $debugEnabled |
||
| 133 | ); |
||
| 134 | |||
| 135 | // at this point, we need to talk to the DB |
||
| 136 | /** |
||
| 137 | * STEP 19 of the validation procedure in § 7.1 of the spec: see if this credential is already registered |
||
| 138 | */ |
||
| 139 | $store = $state['webauthn:store']; |
||
| 140 | if ($store->doesCredentialExist(bin2hex($regObject->getCredentialId())) === false) { |
||
| 141 | // credential does not exist yet in database, good. |
||
| 142 | } else { |
||
| 143 | throw new Exception("The credential with ID " . $regObject->getCredentialId() . " already exists."); |
||
| 144 | } |
||
| 145 | |||
| 146 | // THAT'S IT. This is a valid credential and can be enrolled to the user. |
||
| 147 | $friendlyName = $request->request->get('tokenname'); |
||
| 148 | |||
| 149 | // if we have requested the token model, add it to the name |
||
| 150 | if ($state['requestTokenModel']) { |
||
| 151 | $model = Translate::noop('unknown model'); |
||
| 152 | $vendor = Translate::noop('unknown vendor'); |
||
| 153 | $aaguiddict = AAGUID::getInstance(); |
||
| 154 | if ($aaguiddict->hasToken($regObject->getAAGUID())) { |
||
| 155 | $token = $aaguiddict->get($regObject->getAAGUID()); |
||
| 156 | $model = $token['model']; |
||
| 157 | $vendor = $token['O']; |
||
| 158 | } |
||
| 159 | $friendlyName .= " ($model [$vendor])"; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * STEP 20 of the validation procedure in § 7.1 of the spec: store credentialId, credential, |
||
| 164 | * signCount and associate with user |
||
| 165 | */ |
||
| 166 | |||
| 167 | $store->storeTokenData( |
||
| 168 | $state['FIDO2Username'], |
||
| 169 | $regObject->getCredentialId(), |
||
| 170 | $regObject->getCredential(), |
||
| 171 | $regObject->getCounter(), |
||
| 172 | $friendlyName |
||
| 173 | ); |
||
| 174 | |||
| 175 | // make sure $state gets the news, the token is to be displayed to the user on the next page |
||
| 176 | $state['FIDO2Tokens'][] = [ |
||
| 177 | 0 => $regObject->getCredentialId(), |
||
| 178 | 1 => $regObject->getCredential(), |
||
| 179 | 2 => $regObject->getCounter(), |
||
| 180 | 3 => $friendlyName |
||
| 181 | ]; |
||
| 182 | |||
| 183 | $id = $this->authState::saveState($state, 'webauthn:request'); |
||
|
|
|||
| 184 | if ($debugEnabled === true) { |
||
| 185 | $response = new StreamedResponse(); |
||
| 186 | $response->setCallback(function ($regObject, $id) { |
||
| 187 | echo $regObject->getDebugBuffer(); |
||
| 188 | echo $regObject->getValidateBuffer(); |
||
| 189 | echo "<form id='regform' method='POST' action='" . |
||
| 190 | Module::getModuleURL('webauthn/webauthn.php?StateId=' . urlencode($id)) . "'>"; |
||
| 191 | echo "<button type='submit'>Return to previous page.</button>"; |
||
| 192 | }); |
||
| 193 | return $response; |
||
| 194 | } elseif (array_key_exists('Registration', $state)) { |
||
| 195 | return new RedirectResponse(Module::getModuleURL('webauthn/webauthn.php?StateId=' . urlencode($id))); |
||
| 196 | } else { |
||
| 197 | return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]); |
||
| 198 | } |
||
| 201 |