| Total Complexity | 42 |
| Total Lines | 425 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like CertificationAuthorityEduPki often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CertificationAuthorityEduPki, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CertificationAuthorityEduPki extends EntityWithDBProperties implements CertificationAuthorityInterface |
||
| 18 | { |
||
| 19 | |||
| 20 | private const LOCATION_RA_CERT = ROOT . "/config/SilverbulletClientCerts/edupki-test-ra.pem"; |
||
| 21 | private const LOCATION_RA_KEY = ROOT . "/config/SilverbulletClientCerts/edupki-test-ra.clearkey"; |
||
| 22 | private const LOCATION_WEBROOT = ROOT . "/config/SilverbulletClientCerts/eduPKI-webserver-root.pem"; |
||
| 23 | private const EDUPKI_RA_ID = 700; |
||
| 24 | private const EDUPKI_CERT_PROFILE = "User SOAP"; |
||
| 25 | private const EDUPKI_RA_PKEY_PASSPHRASE = "..."; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * sets up the environment so that we can talk to eduPKI |
||
| 29 | * |
||
| 30 | * @throws Exception |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Creates an updated OCSP statement. Nothing to be done here - eduPKI have |
||
| 50 | * their own OCSP responder and the certs point to it. So we are not in the |
||
| 51 | * loop. |
||
| 52 | * |
||
| 53 | * @param string $serial serial number of the certificate. Serials are 128 bit, so forcibly a string. |
||
| 54 | * @return string a dummy string instead of a real statement |
||
| 55 | */ |
||
| 56 | public function triggerNewOCSPStatement($serial): string |
||
| 57 | { |
||
| 58 | unset($serial); // not needed |
||
| 59 | return "EXTERNAL"; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * signs a CSR |
||
| 64 | * |
||
| 65 | * @param array $csr the request structure, an array |
||
| 66 | * @param integer $expiryDays how many days should the certificate be valid |
||
| 67 | * @return array the certificate with some meta info |
||
| 68 | * @throws Exception |
||
| 69 | */ |
||
| 70 | public function signRequest($csr, $expiryDays): array |
||
| 222 | ]; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * revokes a certificate |
||
| 227 | * |
||
| 228 | * @param string $serial the serial, as a string because it is a 128 bit number |
||
| 229 | * @return void |
||
| 230 | * @throws Exception |
||
| 231 | */ |
||
| 232 | public function revokeCertificate($serial): void |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * sets up a connection to the eduPKI SOAP interfaces |
||
| 278 | * There is a public interface and an RA-restricted interface; |
||
| 279 | * the latter needs an RA client certificate to identify the operator |
||
| 280 | * |
||
| 281 | * @param string $type to which interface should we connect to - "PUBLIC" or "RA" |
||
| 282 | * @return \SoapClient the connection object |
||
| 283 | * @throws Exception |
||
| 284 | */ |
||
| 285 | private function initEduPKISoapSession($type) |
||
| 286 | { |
||
| 287 | // set context parameters common to both endpoints |
||
| 288 | $context_params = [ |
||
| 289 | 'http' => [ |
||
| 290 | 'timeout' => 60, |
||
| 291 | 'user_agent' => 'Stefan', |
||
| 292 | 'header'=> array( "Accept-language: en" ), |
||
| 293 | 'protocol_version' => 1.1 |
||
| 294 | ], |
||
| 295 | 'ssl' => [ |
||
| 296 | 'verify_peer' => true, |
||
| 297 | 'verify_peer_name' => true, |
||
| 298 | // below is the CA "/C=DE/O=Deutsche Telekom AG/OU=T-TeleSec Trust Center/CN=Deutsche Telekom Root CA 2" |
||
| 299 | 'cafile' => CertificationAuthorityEduPki::LOCATION_WEBROOT, |
||
| 300 | 'verify_depth' => 5, |
||
| 301 | 'capture_peer_cert' => true, |
||
| 302 | ], |
||
| 303 | ]; |
||
| 304 | $url = ""; |
||
| 305 | switch ($type) { |
||
| 306 | case "PUBLIC": |
||
| 307 | $url = "https://pki.edupki.org/edupki-test-ca/cgi-bin/pub/soap?wsdl=1"; |
||
| 308 | $context_params['ssl']['peer_name'] = 'pki.edupki.org'; |
||
| 309 | break; |
||
| 310 | case "RA": |
||
| 311 | $url = "https://ra.edupki.org/edupki-test-ca/cgi-bin/ra/soap?wsdl=1"; |
||
| 312 | $context_params['ssl']['peer_name'] = 'ra.edupki.org'; |
||
| 313 | break; |
||
| 314 | default: |
||
| 315 | throw new Exception("Unknown type of eduPKI interface requested."); |
||
| 316 | } |
||
| 317 | if ($type == "RA") { // add client auth parameters to the context |
||
| 318 | $context_params['ssl']['local_cert'] = CertificationAuthorityEduPki::LOCATION_RA_CERT; |
||
| 319 | $context_params['ssl']['local_pk'] = CertificationAuthorityEduPki::LOCATION_RA_KEY; |
||
| 320 | // $context_params['ssl']['passphrase'] = SilverbulletCertificate::EDUPKI_RA_PKEY_PASSPHRASE; |
||
| 321 | } |
||
| 322 | // initialise connection to eduPKI CA / eduroam RA |
||
| 323 | $soap = new \SoapClient($url, [ |
||
| 324 | 'soap_version' => SOAP_1_1, |
||
| 325 | 'trace' => TRUE, |
||
| 326 | 'exceptions' => TRUE, |
||
| 327 | 'connection_timeout' => 5, // if can't establish the connection within 5 sec, something's wrong |
||
| 328 | 'cache_wsdl' => WSDL_CACHE_NONE, |
||
| 329 | 'user_agent' => 'eduroam CAT to eduPKI SOAP Interface', |
||
| 330 | 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, |
||
| 331 | 'stream_context' => stream_context_create($context_params), |
||
| 332 | 'typemap' => [ |
||
| 333 | [ |
||
| 334 | 'type_ns' => 'http://www.w3.org/2001/XMLSchema', |
||
| 335 | 'type_name' => 'integer', |
||
| 336 | 'from_xml' => 'core\CertificationAuthorityEduPki::soapFromXmlInteger', |
||
| 337 | 'to_xml' => 'core\CertificationAuthorityEduPki::soapToXmlInteger', |
||
| 338 | ], |
||
| 339 | ], |
||
| 340 | ] |
||
| 341 | ); |
||
| 342 | return $soap; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * a function that converts integers beyond PHP_INT_MAX to strings for |
||
| 347 | * sending in XML messages |
||
| 348 | * |
||
| 349 | * taken and adapted from |
||
| 350 | * https://www.uni-muenster.de/WWUCA/de/howto-special-phpsoap.html |
||
| 351 | * |
||
| 352 | * @param string $x the integer as an XML fragment |
||
| 353 | * @return array the integer in array notation |
||
| 354 | */ |
||
| 355 | public function soapFromXmlInteger($x) |
||
| 356 | { |
||
| 357 | $y = simplexml_load_string($x); |
||
| 358 | return array( |
||
| 359 | $y->getName(), |
||
| 360 | $y->__toString() |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * a function that converts integers beyond PHP_INT_MAX to strings for |
||
| 366 | * sending in XML messages |
||
| 367 | * |
||
| 368 | * @param array $x the integer in array notation |
||
| 369 | * @return string the integer as string in an XML fragment |
||
| 370 | */ |
||
| 371 | public function soapToXmlInteger($x) |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * generates a CSR which eduPKI likes (DC components etc.) |
||
| 380 | * |
||
| 381 | * @param \OpenSSLAsymmetricKey $privateKey a private key |
||
| 382 | * @param string $fed name of the federation, for C= field |
||
| 383 | * @param string $username username, for CN= field |
||
| 384 | * @return array the CSR along with some meta information |
||
| 385 | * @throws Exception |
||
| 386 | */ |
||
| 387 | public function generateCompatibleCsr($privateKey, $fed, $username): array |
||
| 417 | ]; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * generates a private key eduPKI can handle |
||
| 422 | * |
||
| 423 | * @return \OpenSSLAsymmetricKey the key |
||
| 424 | * @throws Exception |
||
| 425 | */ |
||
| 426 | public function generateCompatiblePrivateKey() |
||
| 427 | { |
||
| 428 | $key = openssl_pkey_new(['private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA, 'encrypt_key' => FALSE]); |
||
| 429 | if ($key === FALSE || is_resource($key)) { |
||
| 430 | throw new Exception("Unable to generate a private key / not a PHP8 object."); |
||
| 431 | } |
||
| 432 | return $key; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * CAs don't have any local caching or other freshness issues |
||
| 437 | * |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public function updateFreshness() |
||
| 442 | // nothing to be done here. |
||
| 443 | } |
||
| 444 | } |
||
| 445 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths