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