Conditions | 15 |
Paths | 82 |
Total Lines | 103 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 = []; |
||
206 | foreach ($this->ldapusercert as $attr) { |
||
207 | $ldap_certs[$attr] = $entry->getAttribute($attr); |
||
208 | } |
||
209 | |||
210 | if (empty($ldap_certs)) { |
||
211 | Logger::error('authX509: no certificate found in LDAP for dn=' . $dn); |
||
212 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
213 | $this->authFailed($state); |
||
214 | |||
215 | throw new Exception("Should never be reached"); |
||
216 | } |
||
217 | |||
218 | |||
219 | $merged_ldapcerts = []; |
||
220 | foreach ($this->ldapusercert as $attr) { |
||
221 | $merged_ldapcerts = array_merge($merged_ldapcerts, $ldap_certs[$attr]); |
||
222 | } |
||
223 | $ldap_certs = $merged_ldapcerts; |
||
224 | |||
225 | $cryptoUtils = new Utils\Crypto(); |
||
226 | foreach ($ldap_certs as $ldap_cert) { |
||
227 | $pem = $cryptoUtils->der2pem($ldap_cert); |
||
228 | $ldap_cert_data = openssl_x509_parse($pem); |
||
229 | if ($ldap_cert_data === false) { |
||
230 | Logger::error('authX509: cert in LDAP is invalid for dn=' . $dn); |
||
231 | continue; |
||
232 | } |
||
233 | |||
234 | if ($ldap_cert_data === $client_cert_data) { |
||
235 | $attributes = array_intersect_key( |
||
236 | $entry->getAttributes(), |
||
237 | array_fill_keys(array_values($this->x509attributes), null) |
||
238 | ); |
||
239 | $state['Attributes'] = $attributes; |
||
240 | $this->authSuccesful($state); |
||
241 | |||
242 | throw new Exception("Should never be reached"); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | Logger::error('authX509: no matching cert in LDAP for dn=' . $dn); |
||
247 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
248 | $this->authFailed($state); |
||
249 | |||
250 | throw new Exception("Should never be reached"); |
||
251 | } |
||
302 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.