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