| Total Complexity | 65 |
| Total Lines | 517 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like DeviceXML 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 DeviceXML, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | abstract class DeviceXML extends \core\DeviceConfig |
||
| 47 | { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array $AuthMethodElements is used to limit |
||
| 51 | * XML elements present within ServerSideCredentials and |
||
| 52 | * ClientSideCredentials to ones which are relevant |
||
| 53 | * for a given EAP method. |
||
| 54 | * @var array of XLM element names which are allowed |
||
| 55 | * EAP method names are defined in core/EAP.php |
||
| 56 | */ |
||
| 57 | private $authMethodElements = [ |
||
| 58 | 'server' => [ |
||
| 59 | \core\common\EAP::TLS => ['CA', 'ServerID'], |
||
| 60 | \core\common\EAP::FAST => ['CA', 'ServerID'], |
||
| 61 | \core\common\EAP::PEAP => ['CA', 'ServerID'], |
||
| 62 | \core\common\EAP::TTLS => ['CA', 'ServerID'], |
||
| 63 | \core\common\EAP::PWD => ['ServerID'], |
||
| 64 | ], |
||
| 65 | 'client' => [ |
||
| 66 | \core\common\EAP::TLS => ['UserName', 'Password', 'ClientCertificate'], |
||
| 67 | \core\common\EAP::NE_MSCHAP2 => ['UserName', 'Password', 'OuterIdentity', 'InnerIdentitySuffix', 'InnerIdentityHint'], |
||
| 68 | \core\common\EAP::MSCHAP2 => ['UserName', 'Password', 'OuterIdentity', 'InnerIdentitySuffix', 'InnerIdentityHint'], |
||
| 69 | \core\common\EAP::GTC => ['UserName', 'OneTimeToken'], |
||
| 70 | \core\common\EAP::NE_PAP => ['UserName', 'Password', 'OuterIdentity', 'InnerIdentitySuffix', 'InnerIdentityHint'], |
||
| 71 | \core\common\EAP::NE_SILVERBULLET => ['UserName', 'ClientCertificate', 'OuterIdentity'], |
||
| 72 | ] |
||
| 73 | ]; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * construct the device |
||
| 77 | */ |
||
| 78 | public function __construct() |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * $langScope can be 'global' when all lang and all lang-specific information |
||
| 85 | * is dumped or 'single' when only the selected lang (and defaults) are passed |
||
| 86 | * NOTICE: 'global' is not yet supported |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | public $langScope; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * whether all EAP types should be included in the file or only the |
||
| 94 | * preferred one |
||
| 95 | * |
||
| 96 | * @var boolean |
||
| 97 | */ |
||
| 98 | public $allEaps = FALSE; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * vendor-specific additional information, this is nit yest fully |
||
| 102 | * implemented due to lack of use cases. |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | public $VendorSpecific; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * create HTML code explaining the installer |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function writeDeviceInfo() |
||
| 114 | { |
||
| 115 | \core\common\Entity::intoThePotatoes(); |
||
| 116 | $out = "<p>"; |
||
| 117 | $out .= sprintf(_("This is a generic configuration file in the IETF <a href='%s'>EAP Metadata -00</a> XML format."), "https://tools.ietf.org/html/draft-winter-opsawg-eap-metadata-00"); |
||
| 118 | \core\common\Entity::outOfThePotatoes(); |
||
| 119 | return $out; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * create the actual XML file |
||
| 124 | * |
||
| 125 | * @return string filename of the generated installer |
||
| 126 | * @throws Exception |
||
| 127 | * |
||
| 128 | */ |
||
| 129 | public function writeInstaller() |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * determines the inner authentication. Is it EAP, and which mechanism is used to convey actual auth data |
||
| 170 | * @param array $eap the EAP type for which we want to get the inner auth |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | private function innerAuth($eap) |
||
| 174 | { |
||
| 175 | $out = []; |
||
| 176 | $out['EAP'] = 0; |
||
| 177 | // this is a hack - eduroamCAT does not handle NE_MSCHAP2 however |
||
| 178 | // treats the inner NE_MSCHAP2 correctly wheb set to the EAP type |
||
| 179 | switch ($eap["INNER"]) { |
||
| 180 | case \core\common\EAP::NE_MSCHAP2: |
||
| 181 | $out['METHOD'] = \core\common\EAP::MSCHAP2; |
||
| 182 | $out['EAP'] = 1; |
||
| 183 | break; |
||
| 184 | case \core\common\EAP::NE_SILVERBULLET: |
||
| 185 | $out['METHOD'] = \core\common\EAP::NONE; |
||
| 186 | break; |
||
| 187 | default: |
||
| 188 | $out['METHOD'] = $eap["INNER"]; |
||
| 189 | break; |
||
| 190 | } |
||
| 191 | // override if there is an inner EAP |
||
| 192 | if ($eap["INNER"] > 0) { // there is an inner EAP method |
||
| 193 | $out['EAP'] = 1; |
||
| 194 | } |
||
| 195 | return $out; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * |
||
| 200 | * @param string $attrName the attribute name |
||
| 201 | * @return array of values for this attribute |
||
| 202 | */ |
||
| 203 | private function getSimpleMLAttribute($attrName) |
||
| 204 | { |
||
| 205 | if (empty($this->attributes[$attrName][0])) { |
||
| 206 | return([]); |
||
| 207 | } |
||
| 208 | $attributeList = $this->attributes[$attrName]; |
||
| 209 | $objs = []; |
||
| 210 | if ($this->langScope === 'global') { |
||
| 211 | foreach ($attributeList['langs'] as $language => $value) { |
||
| 212 | $language = ($language === 'C' ? 'any' : $language); |
||
| 213 | $obj = new \core\DeviceXMLmain(); |
||
| 214 | $obj->setValue($value); |
||
| 215 | $obj->setAttributes(['lang' => $language]); |
||
| 216 | $objs[] = $obj; |
||
| 217 | } |
||
| 218 | } else { |
||
| 219 | $objs[] = $attributeList[0]; |
||
| 220 | } |
||
| 221 | return($objs); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * constructs the name of the institution and puts it into the XML. |
||
| 226 | * consists of the best-language-match inst name, and if the inst has more |
||
| 227 | * than one profile also the best-language-match profile name |
||
| 228 | * |
||
| 229 | * @return \core\DeviceXMLmain[] |
||
| 230 | */ |
||
| 231 | private function getDisplayName() |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * retrieves the provider logo and puts it into the XML structure |
||
| 265 | * |
||
| 266 | * @return \core\DeviceXMLmain |
||
| 267 | */ |
||
| 268 | private function getProviderLogo() |
||
| 269 | { |
||
| 270 | $attr = $this->attributes; |
||
| 271 | if (isset($attr['general:logo_file'][0])) { |
||
| 272 | $logoString = base64_encode($attr['general:logo_file'][0]); |
||
| 273 | $logoMime = 'image/'.$attr['internal:logo_file'][0]['mime']; |
||
| 274 | $providerlogo = new \core\DeviceXMLmain(); |
||
| 275 | $providerlogo->setAttributes(['mime' => $logoMime, 'encoding' => 'base64']); |
||
| 276 | $providerlogo->setValue($logoString); |
||
| 277 | return $providerlogo; |
||
| 278 | } |
||
| 279 | return NULL; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * retrieves provider information and puts it into the XML structure. |
||
| 284 | * contains the profile description and the ToU file, if any |
||
| 285 | * |
||
| 286 | * @return \core\DeviceXMLmain |
||
| 287 | */ |
||
| 288 | private function getProviderInfo() |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * retrieves the location information and puts it into the XML structure |
||
| 302 | * |
||
| 303 | * @return \core\DeviceXMLmain[] |
||
| 304 | */ |
||
| 305 | private function getProviderLocation() |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * retrieves helpdesk contact information and puts it into the XML structure |
||
| 325 | * |
||
| 326 | * @return \core\DeviceXMLmain |
||
| 327 | */ |
||
| 328 | private function getHelpdesk() |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * determine where this credential should be applicable |
||
| 339 | * |
||
| 340 | * @return \core\DeviceXMLmain |
||
| 341 | */ |
||
| 342 | private function getCredentialApplicability() |
||
| 343 | { |
||
| 344 | $setWired = isset($this->attributes['media:wired'][0]) && |
||
| 345 | $this->attributes['media:wired'][0] == 'on' ? 1 : 0; |
||
| 346 | $ssids = $this->attributes['internal:SSID']; |
||
| 347 | $oids = $this->attributes['internal:consortia']; |
||
| 348 | $credentialapplicability = new \core\DeviceXMLmain(); |
||
| 349 | $ieee80211s = []; |
||
| 350 | foreach ($ssids as $ssid => $ciph) { |
||
| 351 | $ieee80211 = new \core\DeviceXMLmain(); |
||
| 352 | $ieee80211->setChild('SSID', $ssid); |
||
| 353 | $ieee80211->setChild('MinRSNProto', $ciph == 'AES' ? 'CCMP' : 'TKIP'); |
||
| 354 | $ieee80211s[] = $ieee80211; |
||
| 355 | } |
||
| 356 | foreach ($oids as $oid) { |
||
| 357 | $ieee80211 = new \core\DeviceXMLmain(); |
||
| 358 | $ieee80211->setChild('ConsortiumOID', $oid); |
||
| 359 | $ieee80211s[] = $ieee80211; |
||
| 360 | } |
||
| 361 | $credentialapplicability->setChild('IEEE80211', $ieee80211s); |
||
| 362 | if ($setWired) { |
||
| 363 | // $ieee8023 = new \core\DeviceXMLmain(); |
||
| 364 | // $ieee8023->setChild('NetworkID', "1"); |
||
| 365 | $credentialapplicability->setChild('IEEE8023', ''); |
||
| 366 | } |
||
| 367 | return $credentialapplicability; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * retrieves the parameters needed for the given EAP method and creates |
||
| 372 | * appropriate nodes in the XML structure for them |
||
| 373 | * |
||
| 374 | * @param array $eap the EAP type in question |
||
| 375 | * @return array a recap of the findings |
||
| 376 | */ |
||
| 377 | private function getAuthenticationMethodParams($eap) |
||
| 378 | { |
||
| 379 | $inner = $this->innerAuth($eap); |
||
| 380 | $outerMethod = $eap["OUTER"]; |
||
| 381 | |||
| 382 | if (isset($inner["METHOD"]) && $inner["METHOD"]) { |
||
| 383 | $innerauthmethod = new \core\DeviceXMLmain(); |
||
| 384 | $typeOfInner = ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod'); |
||
| 385 | $eapmethod = new \core\DeviceXMLmain(); |
||
| 386 | $eapmethod->setChild('Type', abs($inner['METHOD'])); |
||
| 387 | $innerauthmethod->setChild($typeOfInner, $eapmethod); |
||
| 388 | return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']]; |
||
| 389 | } else { |
||
| 390 | return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0]; |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * sets the server-side credentials for a given EAP type |
||
| 396 | * |
||
| 397 | * @param \devices\XML\Type $eaptype the EAP type |
||
| 398 | * @return \core\DeviceXMLmain |
||
| 399 | */ |
||
| 400 | private function getServerSideCredentials($eap) |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * sets the realm information for the client-side credential |
||
| 432 | * |
||
| 433 | * @param \core\DeviceXMLmain $clientsidecredential the ClientSideCredential to which the realm info is to be added |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | private function setClientSideRealm($clientsidecredential) |
||
| 437 | { |
||
| 438 | $attr = $this->attributes; |
||
| 439 | $realm = \core\common\Entity::getAttributeValue($attr, 'internal:realm', 0); |
||
| 440 | if ($realm === NULL) { |
||
| 441 | return; |
||
| 442 | } |
||
| 443 | if (\core\common\Entity::getAttributeValue($attr, 'internal:verify_userinput_suffix', 0) !== 1) { |
||
| 444 | return; |
||
| 445 | } |
||
| 446 | $clientsidecredential->setChild('InnerIdentitySuffix', $realm); |
||
| 447 | if (\core\common\Entity::getAttributeValue($attr, 'internal:hint_userinput_suffix', 0) === 1) { |
||
| 448 | $clientsidecredential->setChild('InnerIdentityHint', 'true'); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * sets the client certificate |
||
| 454 | * |
||
| 455 | * @return \core\DeviceXMLmain |
||
| 456 | */ |
||
| 457 | private function getClientCertificate() |
||
| 458 | { |
||
| 459 | $clientCertificateObject = new \core\DeviceXMLmain(); |
||
| 460 | $clientCertificateObject->setValue(base64_encode($this->clientCert["certdata"])); |
||
| 461 | $clientCertificateObject->setAttributes(['format' => 'PKCS12', 'encoding' => 'base64']); |
||
| 462 | return $clientCertificateObject; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * sets the client-side credentials for the given EAP type |
||
| 467 | * |
||
| 468 | * @param array $eapParams the EAP parameters |
||
| 469 | * @return \core\DeviceXMLmain |
||
| 470 | */ |
||
| 471 | private function getClientSideCredentials($eap) |
||
| 472 | { |
||
| 473 | $children = $this->authMethodElements['client'][$eap]; |
||
| 474 | $clientsidecredential = new \core\DeviceXMLmain(); |
||
| 475 | $outerId = $this->determineOuterIdString(); |
||
| 476 | $this->loggerInstance->debug(5, $eap, "XMLOI:", "\n"); |
||
| 477 | if (in_array('OuterIdentity', $children)) { |
||
| 478 | if ($outerId !== NULL) { |
||
| 479 | $clientsidecredential->setChild('OuterIdentity', $outerId); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | $this->setClientSideRealm($clientsidecredential); |
||
| 483 | // $clientsidecredential->setChild('EAPType', $eapParams['inner_methodID'] ? $eapParams['inner_methodID'] : $eapParams['methodID']); |
||
| 484 | |||
| 485 | // Client Certificate |
||
| 486 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
| 487 | $attr = $this->attributes; |
||
| 488 | $outerId = \core\common\Entity::getAttributeValue($attr, 'internal:username', 0); |
||
| 489 | $clientsidecredential->setChild('OuterIdentity', $outerId); |
||
| 490 | $clientsidecredential->setChild('ClientCertificate', $this->getClientCertificate()); |
||
| 491 | } |
||
| 492 | return $clientsidecredential; |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * sets the EAP method |
||
| 497 | * |
||
| 498 | * @param \devices\XML\Type $eaptype the EAP type XMLObject |
||
| 499 | * @return \core\DeviceXMLmain |
||
| 500 | */ |
||
| 501 | private function getEapMethod($eaptype) |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * determines the authentication method to use |
||
| 521 | * |
||
| 522 | * @param array $eap the EAP methods, in array representation |
||
| 523 | * @return \core\DeviceXMLmain |
||
| 524 | */ |
||
| 525 | private function getAuthMethod($eap) |
||
| 526 | { |
||
| 527 | $authmethod = new \core\DeviceXMLmain(); |
||
| 528 | $eapParams = $this->getAuthenticationMethodParams($eap); |
||
| 529 | $eaptype = new \core\DeviceXMLmain(); |
||
| 530 | $eaptype->setValue($eapParams['methodID']); |
||
| 531 | // Type |
||
| 532 | $authmethod->setChild('EAPMethod', $this->getEapMethod($eaptype)); |
||
|
|
|||
| 533 | |||
| 534 | // ServerSideCredentials |
||
| 535 | $authmethod->setChild('ServerSideCredential', $this->getServerSideCredentials($eap['OUTER'])); |
||
| 536 | |||
| 537 | // ClientSideCredentials |
||
| 538 | $authmethod->setChild('ClientSideCredential', $this->getClientSideCredentials($eap['INNER'])); |
||
| 539 | |||
| 540 | if ($eapParams['inner_method']) { |
||
| 541 | $authmethod->setChild('InnerAuthenticationMethod', $eapParams['inner_method']); |
||
| 542 | } |
||
| 543 | return $authmethod; |
||
| 544 | } |
||
| 545 | |||
| 546 | private function getAuthMethodsList() { |
||
| 563 | } |
||
| 564 | |||
| 568 |