| Conditions | 27 |
| Paths | 250 |
| Total Lines | 140 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| 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 |
||
| 114 | public function authenticate(array &$state): void |
||
| 115 | { |
||
| 116 | // set the default backend to config |
||
| 117 | $state['LogoutState'] = [ |
||
| 118 | 'negotiate:backend' => $this->fallback, |
||
| 119 | ]; |
||
| 120 | $state['negotiate:authId'] = $this->authId; |
||
| 121 | |||
| 122 | |||
| 123 | // check for disabled SPs. The disable flag is stored in the SP metadata |
||
| 124 | if (array_key_exists('SPMetadata', $state) && $this->spDisabledInMetadata($state['SPMetadata'])) { |
||
| 125 | $this->fallBack($state); |
||
| 126 | } |
||
| 127 | |||
| 128 | /* Go straight to fallback if Negotiate is disabled or if you are sent back to the IdP directly from the SP |
||
| 129 | after having logged out. */ |
||
| 130 | $session = Session::getSessionFromRequest(); |
||
| 131 | $disabled = $session->getData('negotiate:disable', 'session'); |
||
| 132 | |||
| 133 | if ( |
||
| 134 | $disabled || |
||
| 135 | (array_key_exists('NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT', $_COOKIE) && |
||
| 136 | $_COOKIE['NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT'] === 'true') |
||
| 137 | ) { |
||
| 138 | Logger::debug('Negotiate - session disabled. falling back'); |
||
| 139 | $this->fallBack($state); |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!$this->checkMask()) { |
||
| 144 | Logger::debug('Negotiate - IP matches blacklisted subnets. falling back'); |
||
| 145 | $this->fallBack($state); |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | Logger::debug('Negotiate - authenticate(): looking for authentication header'); |
||
| 150 | if (array_key_exists('HTTP_AUTHORIZATION', $_SERVER) && !empty($_SERVER['HTTP_AUTHORIZATION'])) { |
||
| 151 | Logger::debug('Negotiate - authenticate(): Authentication header found'); |
||
| 152 | |||
| 153 | Assert::true(is_string($this->spn) || (is_int($this->spn) && ($this->spn === 0)) || is_null($this->spn)); |
||
| 154 | |||
| 155 | list($mech,) = explode(' ', $_SERVER['HTTP_AUTHORIZATION'], 2); |
||
| 156 | if (strtolower($mech) === 'basic') { |
||
| 157 | Logger::debug('Negotiate - authenticate(): Basic found. Skipping.'); |
||
| 158 | } elseif (strtolower($mech) !== 'negotiate') { |
||
| 159 | Logger::debug('Negotiate - authenticate(): No "Negotiate" found. Skipping.'); |
||
| 160 | } else { |
||
| 161 | // attempt Kerberos authentication |
||
| 162 | $reply = $auth = null; |
||
| 163 | |||
| 164 | try { |
||
| 165 | if (version_compare(phpversion('krb5'), '1.1.6', '<')) { |
||
| 166 | Logger::debug( |
||
| 167 | 'Negotiate - authenticate(): Trying to authenticate (channel binding not available).', |
||
| 168 | ); |
||
| 169 | $auth = new KRB5NegotiateAuth($this->keytab, $this->spn); |
||
| 170 | $reply = $this->doAuthentication($auth); |
||
| 171 | } elseif (empty($this->allowedCertificateHashes) && $this->enforceChannelBinding === false) { |
||
| 172 | Logger::debug('Negotiate - authenticate(): Trying to authenticate without channel binding.'); |
||
| 173 | $auth = new KRB5NegotiateAuth($this->keytab, $this->spn); |
||
| 174 | $reply = $this->doAuthentication($auth); |
||
| 175 | } else { |
||
| 176 | Logger::debug('Negotiate - authenticate(): Trying to authenticate with channel binding.'); |
||
| 177 | |||
| 178 | $hashes = str_replace(':', '', $this->allowedCertificateHashes); |
||
| 179 | foreach ($hashes as $hash) { |
||
| 180 | $binding = $this->createBinding($hash); |
||
| 181 | $auth = new KRB5NegotiateAuth($this->keytab, $this->spn, $binding); |
||
| 182 | |||
| 183 | try { |
||
| 184 | $reply = $this->doAuthentication($auth, $hash); |
||
| 185 | break; |
||
| 186 | } catch (Exception $e) { |
||
| 187 | continue; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($auth === null || !$auth->isChannelBound()) { |
||
| 192 | throw new Error\Exception( |
||
| 193 | 'Negotiate - authenticate(): Failed to perform channel binding using ' |
||
| 194 | . 'any of the configured certificate hashes.', |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } catch (Exception $e) { |
||
| 199 | Logger::error('Negotiate - authenticate(): doAuthentication() exception: ' . $e->getMessage()); |
||
| 200 | } |
||
| 201 | |||
| 202 | if ($reply && $auth !== null) { |
||
| 203 | // success! krb TGS received |
||
| 204 | $userPrincipalName = $auth->getAuthenticatedUser(); |
||
| 205 | Logger::info('Negotiate - authenticate(): ' . $userPrincipalName . ' authenticated.'); |
||
| 206 | |||
| 207 | // Search for the corresponding realm and set current variables |
||
| 208 | @list($uid, $realmName) = preg_split('/@/', $userPrincipalName, 2); |
||
| 209 | Assert::notNull($realmName); |
||
| 210 | |||
| 211 | // Use the correct realm |
||
| 212 | if (isset($this->realms[$realmName])) { |
||
| 213 | Logger::info(sprintf('Negotiate - setting realm parameters for "%s".', $realmName)); |
||
| 214 | $this->backend = $this->realms[$realmName]; |
||
| 215 | } elseif (isset($this->realms['*'])) { |
||
| 216 | // Use default realm ("*"), if set |
||
| 217 | Logger::info('Negotiate - setting realm parameters with default realm.'); |
||
| 218 | $this->backend = $this->realms['*']; |
||
| 219 | } else { |
||
| 220 | // No corresponding realm found, cancel |
||
| 221 | $this->fallBack($state); |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | if (($lookup = $this->lookupUserData($uid)) !== null) { |
||
| 226 | $state['Attributes'] = $lookup; |
||
| 227 | // Override the backend so logout will know what to look for |
||
| 228 | $state['LogoutState'] = [ |
||
| 229 | 'negotiate:backend' => $this->backend, |
||
| 230 | ]; |
||
| 231 | Logger::info('Negotiate - authenticate(): ' . $userPrincipalName . ' authorized.'); |
||
| 232 | Auth\Source::completeAuth($state); |
||
| 233 | return; |
||
| 234 | } |
||
| 235 | } else { |
||
| 236 | // Some error in the received ticket. Expired? |
||
| 237 | Logger::info('Negotiate - authenticate(): Kerberos authN failed. Skipping.'); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } else { |
||
| 241 | // Save the $state array, so that we can restore if after a redirect |
||
| 242 | Logger::debug('Negotiate - fallback: ' . $state['LogoutState']['negotiate:backend']); |
||
| 243 | $id = Auth\State::saveState($state, self::STAGEID); |
||
| 244 | $params = ['AuthState' => $id]; |
||
| 245 | |||
| 246 | // No auth token. Send it. |
||
| 247 | Logger::debug('Negotiate - authenticate(): Sending Negotiate.'); |
||
| 248 | $this->sendNegotiate($params); // never returns |
||
| 249 | } |
||
| 250 | |||
| 251 | Logger::info('Negotiate - authenticate(): Client failed Negotiate. Falling back'); |
||
| 252 | $this->fallBack($state); |
||
| 253 | return; |
||
| 254 | } |
||
| 465 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.