| Conditions | 14 |
| Paths | 50 |
| Total Lines | 94 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 95 | public function main(Request $request): Response |
||
| 96 | { |
||
| 97 | if (session_status() != PHP_SESSION_ACTIVE) { |
||
| 98 | session_cache_limiter('nocache'); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->logger::info('FIDO2 - Accessing WebAuthn enrollment validation'); |
||
| 102 | |||
| 103 | $stateId = $request->query->get('StateId'); |
||
| 104 | if ($stateId === null) { |
||
| 105 | throw new Error\BadRequest('Missing required StateId query parameter.'); |
||
| 106 | } |
||
| 107 | |||
| 108 | $debugEnabled = $this->config->getValue('logging.level', Logger::NOTICE) === Logger::DEBUG; |
||
| 109 | |||
| 110 | $state = Auth\State::loadState($stateId, 'webauthn:request'); |
||
| 111 | |||
| 112 | $incomingID = bin2hex(WebAuthnAbstractEvent::base64urlDecode($request->request->get('response_id'))); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * §7.2 STEP 2 - 4 : check that the credential is one of those the particular user owns |
||
| 116 | */ |
||
| 117 | $publicKey = false; |
||
| 118 | $previousCounter = -1; |
||
| 119 | |||
| 120 | foreach ($state['FIDO2Tokens'] as $oneToken) { |
||
| 121 | if ($oneToken[0] == $incomingID) { |
||
| 122 | // Credential ID is eligible for user $state['FIDO2Username']; |
||
| 123 | // using publicKey $oneToken[1] with current counter value $oneToken[2] |
||
| 124 | $publicKey = $oneToken[1]; |
||
| 125 | $previousCounter = $oneToken[2]; |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($publicKey === false) { |
||
| 131 | throw new Exception( |
||
|
|
|||
| 132 | "User attempted to authenticate with an unknown credential ID. This should already have been prevented by the browser!" |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** @psalm-var array $oneToken */ |
||
| 137 | $authObject = new WebAuthnAuthenticationEvent( |
||
| 138 | $request->request->get('type'), |
||
| 139 | ($state['FIDO2Scope'] === null ? $state['FIDO2DerivedScope'] : $state['FIDO2Scope']), |
||
| 140 | $state['FIDO2SignupChallenge'], |
||
| 141 | $state['IdPMetadata']['entityid'], |
||
| 142 | base64_decode($request->request->get('authenticator_data')), |
||
| 143 | base64_decode($request->request->get('client_data_raw')), |
||
| 144 | $oneToken[0], |
||
|
1 ignored issue
–
show
|
|||
| 145 | $oneToken[1], |
||
| 146 | base64_decode($request->request->get('signature')), |
||
| 147 | $debugEnabled |
||
| 148 | ); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * §7.2 STEP 18 : detect physical object cloning on the token |
||
| 152 | */ |
||
| 153 | $counter = $authObject->getCounter(); |
||
| 154 | if (($previousCounter != 0 || $counter != 0) && $counter > $previousCounter) { |
||
| 155 | // Signature counter was incremented compared to last time, good |
||
| 156 | $store = $state['webauthn:store']; |
||
| 157 | $store->updateSignCount($oneToken[0], $counter); |
||
| 158 | } else { |
||
| 159 | throw new Exception( |
||
| 160 | "Signature counter less or equal to a previous authentication! Token cloning likely (old: $previousCounter, new: $counter." |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | // THAT'S IT. The user authenticated successfully. Remember the credential ID that was used. |
||
| 165 | $state['FIDO2AuthSuccessful'] = $oneToken[0]; |
||
| 166 | |||
| 167 | // See if he wants to hang around for token management operations |
||
| 168 | if ($request->request->get('credentialChange') === 'on') { |
||
| 169 | $state['FIDO2WantsRegister'] = true; |
||
| 170 | } else { |
||
| 171 | $state['FIDO2WantsRegister'] = false; |
||
| 172 | } |
||
| 173 | |||
| 174 | $this->authState::saveState($state, 'webauthn:request'); |
||
| 175 | |||
| 176 | if ($debugEnabled) { |
||
| 177 | $response = new StreamedResponse(); |
||
| 178 | $response->setCallback(function () { |
||
| 179 | echo $authObject->getDebugBuffer(); |
||
| 180 | echo $authObject->getValidateBuffer(); |
||
| 181 | echo "Debug mode, not continuing to " . ($state['FIDO2WantsRegister'] ? "credential registration page." : "destination."); |
||
| 182 | }); |
||
| 183 | return $response; |
||
| 184 | } else { |
||
| 185 | if ($state['FIDO2WantsRegister']) { |
||
| 186 | return new RedirectResponse(Module::getModuleURL('webauthn/webauthn.php?StateId=' . urlencode($stateId))); |
||
| 187 | } else { |
||
| 188 | return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]); |
||
| 189 | } |
||
| 193 |