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