Conditions | 14 |
Paths | 46 |
Total Lines | 99 |
Code Lines | 62 |
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 |
||
148 | public function authenticate(array &$state): void |
||
149 | { |
||
150 | if ( |
||
151 | !isset($_SERVER['SSL_CLIENT_CERT']) || |
||
152 | ($_SERVER['SSL_CLIENT_CERT'] == '') |
||
153 | ) { |
||
154 | $state['authX509.error'] = "NOCERT"; |
||
155 | $this->authFailed($state); |
||
156 | |||
157 | throw new Exception("Should never be reached"); |
||
158 | } |
||
159 | |||
160 | $client_cert = $_SERVER['SSL_CLIENT_CERT']; |
||
161 | $client_cert_data = openssl_x509_parse($client_cert); |
||
162 | if ($client_cert_data === false) { |
||
163 | Logger::error('authX509: invalid cert'); |
||
164 | $state['authX509.error'] = "INVALIDCERT"; |
||
165 | $this->authFailed($state); |
||
166 | |||
167 | throw new Exception("Should never be reached"); |
||
168 | } |
||
169 | |||
170 | $entry = $dn = null; |
||
171 | foreach ($this->x509attributes as $x509_attr => $attr) { |
||
172 | // value is scalar |
||
173 | if (array_key_exists($x509_attr, $client_cert_data['subject'])) { |
||
174 | $value = $client_cert_data['subject'][$x509_attr]; |
||
175 | Logger::info('authX509: cert ' . $x509_attr . ' = ' . $value); |
||
176 | $entry = $this->findUserByAttribute($attr, $value); |
||
177 | if ($entry !== null) { |
||
178 | $dn = $attr; |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if ($entry === null) { |
||
185 | Logger::error('authX509: cert has no matching user in LDAP.'); |
||
186 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
187 | $this->authFailed($state); |
||
188 | |||
189 | throw new Exception("Should never be reached"); |
||
190 | } |
||
191 | |||
192 | if ($this->ldapusercert === null) { |
||
193 | // do not check for certificate match |
||
194 | $attributes = array_intersect_key( |
||
195 | $entry->getAttributes(), |
||
196 | array_fill_keys(array_values($this->x509attributes), null), |
||
197 | ); |
||
198 | |||
199 | $state['Attributes'] = $attributes; |
||
200 | $this->authSuccesful($state); |
||
201 | |||
202 | throw new Exception("Should never be reached"); |
||
203 | } |
||
204 | |||
205 | $ldap_certs = array_map([$entry, 'getAttribute'], $this->ldapusercert); |
||
206 | if (empty($ldap_certs)) { |
||
207 | Logger::error('authX509: no certificate found in LDAP for dn=' . $dn); |
||
208 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
209 | $this->authFailed($state); |
||
210 | |||
211 | throw new Exception("Should never be reached"); |
||
212 | } |
||
213 | |||
214 | |||
215 | $merged_ldapcerts = []; |
||
216 | foreach ($this->ldapusercert as $attr) { |
||
217 | $merged_ldapcerts = array_merge($merged_ldapcerts, $ldap_certs[$attr]); |
||
218 | } |
||
219 | $ldap_certs = $merged_ldapcerts; |
||
220 | |||
221 | $cryptoUtils = new Utils\Crypto(); |
||
222 | foreach ($ldap_certs as $ldap_cert) { |
||
223 | $pem = $cryptoUtils->der2pem($ldap_cert); |
||
224 | $ldap_cert_data = openssl_x509_parse($pem); |
||
225 | if ($ldap_cert_data === false) { |
||
226 | Logger::error('authX509: cert in LDAP is invalid for dn=' . $dn); |
||
227 | continue; |
||
228 | } |
||
229 | |||
230 | if ($ldap_cert_data === $client_cert_data) { |
||
231 | $attributes = array_intersect_key( |
||
232 | $entry->getAttributes(), |
||
233 | array_fill_keys(array_values($this->x509attributes), null) |
||
234 | ); |
||
235 | $state['Attributes'] = $attributes; |
||
236 | $this->authSuccesful($state); |
||
237 | |||
238 | throw new Exception("Should never be reached"); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | Logger::error('authX509: no matching cert in LDAP for dn=' . $dn); |
||
243 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
244 | $this->authFailed($state); |
||
245 | |||
246 | throw new Exception("Should never be reached"); |
||
247 | } |
||
298 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.