| Conditions | 17 |
| Paths | 102 |
| Total Lines | 111 |
| Code Lines | 70 |
| 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 |
||
| 149 | public function authenticate(array &$state): void |
||
| 150 | { |
||
| 151 | if ( |
||
| 152 | !isset($_SERVER['SSL_CLIENT_CERT']) || |
||
| 153 | ($_SERVER['SSL_CLIENT_CERT'] == '') |
||
| 154 | ) { |
||
| 155 | $state['authX509.error'] = "NOCERT"; |
||
| 156 | $this->authFailed($state); |
||
| 157 | |||
| 158 | throw new Exception("Should never be reached"); |
||
| 159 | } |
||
| 160 | |||
| 161 | $client_cert = $_SERVER['SSL_CLIENT_CERT']; |
||
| 162 | $client_cert_data = openssl_x509_parse($client_cert); |
||
| 163 | if ($client_cert_data === false) { |
||
| 164 | Logger::error('authX509: invalid cert'); |
||
| 165 | $state['authX509.error'] = "INVALIDCERT"; |
||
| 166 | $this->authFailed($state); |
||
| 167 | |||
| 168 | throw new Exception("Should never be reached"); |
||
| 169 | } |
||
| 170 | |||
| 171 | $entry = $dn = null; |
||
| 172 | foreach ($this->x509attributes as $x509_attr => $attr) { |
||
| 173 | // value is scalar |
||
| 174 | if (array_key_exists($x509_attr, $client_cert_data['subject'])) { |
||
| 175 | $value = $client_cert_data['subject'][$x509_attr]; |
||
| 176 | Logger::info('authX509: cert ' . $x509_attr . ' = ' . $value); |
||
| 177 | $entry = $this->findUserByAttribute($attr, $value); |
||
| 178 | if ($entry !== null) { |
||
| 179 | $dn = $attr; |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | if ($entry === null) { |
||
| 186 | Logger::error('authX509: cert has no matching user in LDAP.'); |
||
| 187 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
| 188 | $this->authFailed($state); |
||
| 189 | |||
| 190 | throw new Exception("Should never be reached"); |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($this->ldapusercert === null) { |
||
| 194 | // do not check for certificate match |
||
| 195 | if (is_null($this->ldapConfig->getOptionalArray('attributes', null))) { |
||
| 196 | $attributes = $entry->getAttributes(); |
||
| 197 | } else { |
||
| 198 | $attributes = array_intersect_key( |
||
| 199 | $entry->getAttributes(), |
||
| 200 | array_fill_keys(array_values($this->ldapConfig->getArray('attributes')), null), |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | $state['Attributes'] = $attributes; |
||
| 205 | $this->authSuccesful($state); |
||
| 206 | |||
| 207 | throw new Exception("Should never be reached"); |
||
| 208 | } |
||
| 209 | |||
| 210 | $ldap_certs = []; |
||
| 211 | foreach ($this->ldapusercert as $attr) { |
||
| 212 | $ldap_certs[$attr] = $entry->getAttribute($attr); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (empty($ldap_certs)) { |
||
| 216 | Logger::error('authX509: no certificate found in LDAP for dn=' . $dn); |
||
| 217 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
| 218 | $this->authFailed($state); |
||
| 219 | |||
| 220 | throw new Exception("Should never be reached"); |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | $merged_ldapcerts = []; |
||
| 225 | foreach ($this->ldapusercert as $attr) { |
||
| 226 | $merged_ldapcerts = array_merge($merged_ldapcerts, $ldap_certs[$attr]); |
||
| 227 | } |
||
| 228 | $ldap_certs = $merged_ldapcerts; |
||
| 229 | |||
| 230 | $cryptoUtils = new Utils\Crypto(); |
||
| 231 | foreach ($ldap_certs as $ldap_cert) { |
||
| 232 | $pem = $cryptoUtils->der2pem($ldap_cert); |
||
| 233 | $ldap_cert_data = openssl_x509_parse($pem); |
||
| 234 | if ($ldap_cert_data === false) { |
||
| 235 | Logger::error('authX509: cert in LDAP is invalid for dn=' . $dn); |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | |||
| 239 | if ($ldap_cert_data === $client_cert_data) { |
||
| 240 | if (is_null($this->ldapConfig->getOptionalArray('attributes', null))) { |
||
| 241 | $attributes = $entry->getAttributes(); |
||
| 242 | } else { |
||
| 243 | $attributes = array_intersect_key( |
||
| 244 | $entry->getAttributes(), |
||
| 245 | array_fill_keys(array_values($this->ldapConfig->getArray('attributes')), null), |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | $state['Attributes'] = $attributes; |
||
| 249 | $this->authSuccesful($state); |
||
| 250 | |||
| 251 | throw new Exception("Should never be reached"); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | Logger::error('authX509: no matching cert in LDAP for dn=' . $dn); |
||
| 256 | $state['authX509.error'] = "UNKNOWNCERT"; |
||
| 257 | $this->authFailed($state); |
||
| 258 | |||
| 259 | throw new Exception("Should never be reached"); |
||
| 260 | } |
||
| 311 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.