| Total Complexity | 57 |
| Total Lines | 397 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 44 | abstract class DeviceXML extends \core\DeviceConfig { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * construct the device |
||
| 48 | */ |
||
| 49 | public function __construct() { |
||
| 50 | parent::__construct(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * $lang_scope can be 'global' wheb all lang and all lang-specific information |
||
| 55 | * is dumped or 'single' when only the selected lang (and defaults) are passed |
||
| 56 | * NOTICE: 'global' is not yet supported |
||
| 57 | */ |
||
| 58 | public $langScope; |
||
| 59 | public $allEaps = FALSE; |
||
| 60 | public $VendorSpecific; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * create HTML code explaining the installer |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function writeDeviceInfo() { |
||
| 68 | \core\common\Entity::intoThePotatoes(); |
||
| 69 | $out = "<p>"; |
||
| 70 | $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"); |
||
| 71 | \core\common\Entity::outOfThePotatoes(); |
||
| 72 | return $out; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * create the actual XML file |
||
| 77 | * |
||
| 78 | * @return string filename of the generated installer |
||
| 79 | * |
||
| 80 | */ |
||
| 81 | public function writeInstaller() { |
||
| 82 | $attr = $this->attributes; |
||
| 83 | $NAMESPACE = 'urn:RFC4282:realm'; |
||
| 84 | //EAPIdentityProvider begin |
||
| 85 | $eapIdp = new EAPIdentityProvider(); |
||
| 86 | $eapIdp->setProperty('CredentialApplicability', $this->getCredentialApplicability()); |
||
| 87 | // $eap_idp->setProperty('ValidUntil',$this->getValidUntil()); |
||
| 88 | // ProviderInfo-> |
||
| 89 | $eapIdp->setProperty('ProviderInfo', $this->getProviderInfo()); |
||
| 90 | // TODO $eap_idp->setProperty('VendorSpecific',$this->getVendorSpecific()); |
||
| 91 | //AuthenticationMethods |
||
| 92 | // TODO |
||
| 93 | //ID attribute |
||
| 94 | //lang attribute |
||
| 95 | $methodList = []; |
||
| 96 | if ($this->allEaps) { |
||
| 97 | $eapmethods = []; |
||
| 98 | foreach ($attr['all_eaps'] as $eap) { |
||
| 99 | $eapRep = $eap->getArrayRep(); |
||
| 100 | if (in_array($eapRep, $this->supportedEapMethods)) { |
||
| 101 | $eapmethods[] = $eapRep; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $eapmethods = [$this->selectedEap]; |
||
| 106 | } |
||
| 107 | foreach ($eapmethods as $eap) { |
||
| 108 | $methodList[] = $this->getAuthMethod($eap); |
||
| 109 | } |
||
| 110 | $authMethods = new AuthenticationMethods(); |
||
| 111 | $authMethods->setProperty('AuthenticationMethods', $methodList); |
||
| 112 | $eapIdp->setProperty('AuthenticationMethods', $authMethods); |
||
| 113 | if (empty($attr['internal:realm'][0])) { |
||
| 114 | $eapIdp->setAttribute('ID', 'undefined'); |
||
| 115 | $eapIdp->setAttribute('namespace', 'urn:undefined'); |
||
| 116 | } else { |
||
| 117 | $eapIdp->setAttribute('ID', $attr['internal:realm'][0]); |
||
| 118 | $eapIdp->setAttribute('namespace', $NAMESPACE); |
||
| 119 | } |
||
| 120 | if ($this->langScope === 'single') { |
||
| 121 | $eapIdp->setAttribute('lang', $this->languageInstance->getLang()); |
||
| 122 | } |
||
| 123 | $eapIdp->setAttribute('version', '1'); |
||
| 124 | |||
| 125 | |||
| 126 | // EAPIdentityProvider end |
||
| 127 | // Generate XML |
||
| 128 | |||
| 129 | $rootname = 'EAPIdentityProviderList'; |
||
| 130 | $root = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><{$rootname} xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"eap-metadata.xsd\"></{$rootname}>"); |
||
| 131 | |||
| 132 | marshalObject($root, $eapIdp); |
||
| 133 | $dom = dom_import_simplexml($root)->ownerDocument; |
||
| 134 | //TODO schema validation makes sense so probably should be used |
||
| 135 | if ($dom->schemaValidate(ROOT . '/devices/xml/eap-metadata.xsd') === FALSE) { |
||
| 136 | throw new Exception("Schema validation failed for eap-metadata"); |
||
| 137 | } |
||
| 138 | file_put_contents($this->installerBasename . '.eap-config', $dom->saveXML()); |
||
| 139 | return($this->installerBasename . '.eap-config'); |
||
| 140 | } |
||
| 141 | |||
| 142 | private $AttributeNames = [ |
||
| 143 | 'support:email' => 'EmailAddress', |
||
| 144 | 'support:url' => 'WebAddress', |
||
| 145 | 'support:phone' => 'Phone', |
||
| 146 | 'profile:description' => 'Description', |
||
| 147 | 'support:info_file' => 'TermsOfUse', |
||
| 148 | 'general:logo_file' => 'ProviderLogo', |
||
| 149 | ]; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * |
||
| 153 | * @param string $attrName the attribute name |
||
| 154 | * @return array of values for this attribute |
||
| 155 | */ |
||
| 156 | private function getSimpleMLAttribute($attrName) { |
||
| 157 | if (empty($this->attributes[$attrName][0])) { |
||
| 158 | return([]); |
||
| 159 | } |
||
| 160 | $attributeList = $this->attributes[$attrName]; |
||
| 161 | if (!isset($this->AttributeNames[$attrName])) { |
||
| 162 | $this->loggerInstance->debug(4, "Missing class definition for $attrName\n"); |
||
| 163 | return([]); |
||
| 164 | } |
||
| 165 | $className = "\devices\xml\\" . $this->AttributeNames[$attrName]; |
||
| 166 | $objs = []; |
||
| 167 | if ($this->langScope === 'global') { |
||
| 168 | foreach ($attributeList['langs'] as $language => $value) { |
||
| 169 | $language = ($language === 'C' ? 'any' : $language); |
||
| 170 | $obj = new $className(); |
||
| 171 | $obj->setValue($value); |
||
| 172 | $obj->setAttributes(['lang' => $language]); |
||
| 173 | $objs[] = $obj; |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | $obj = new $className(); |
||
| 177 | $obj->setValue($attributeList[0]); |
||
| 178 | $objs[] = $obj; |
||
| 179 | } |
||
| 180 | return($objs); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * constructs the name of the institution and puts it into the XML. |
||
| 185 | * consists of the best-language-match inst name, and if the inst has more |
||
| 186 | * than one profile also the best-language-match profile name |
||
| 187 | * |
||
| 188 | * @return \devices\xml\DisplayName[] |
||
| 189 | */ |
||
| 190 | private function getDisplayName() { |
||
| 191 | $attr = $this->attributes; |
||
| 192 | $objs = []; |
||
| 193 | if ($this->langScope === 'global') { |
||
| 194 | $instNameLangs = $attr['general:instname']['langs']; |
||
| 195 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 196 | $profileNameLangs = $attr['profile:name']['langs']; |
||
| 197 | } |
||
| 198 | foreach ($instNameLangs as $language => $value) { |
||
| 199 | $language = ($language === 'C' ? 'any' : $language); |
||
| 200 | $displayname = new DisplayName(); |
||
| 201 | if (isset($profileNameLangs)) { |
||
| 202 | $langOrC = isset($profileNameLangs[$language]) ? $profileNameLangs[$language] : $profileNameLangs['C']; |
||
| 203 | $value .= ' - ' . $langOrC; |
||
| 204 | } |
||
| 205 | $displayname->setValue($value); |
||
| 206 | $displayname->setAttributes(['lang' => $language]); |
||
| 207 | $objs[] = $displayname; |
||
| 208 | } |
||
| 209 | } else { |
||
| 210 | $displayname = new DisplayName(); |
||
| 211 | $value = $attr['general:instname'][0]; |
||
| 212 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 213 | $value .= ' - ' . $attr['profile:name'][0]; |
||
| 214 | } |
||
| 215 | $displayname->setValue($value); |
||
| 216 | $objs[] = $displayname; |
||
| 217 | } |
||
| 218 | return $objs; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * retrieves the provider logo and puts it into the XML structure |
||
| 223 | * |
||
| 224 | * @return \devices\xml\ProviderLogo |
||
| 225 | */ |
||
| 226 | private function getProviderLogo() { |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * retrieves provider information and puts it into the XML structure. |
||
| 240 | * contains the profile description and the ToU file, if any |
||
| 241 | * |
||
| 242 | * @return \devices\xml\ProviderInfo |
||
| 243 | */ |
||
| 244 | private function getProviderInfo() { |
||
| 245 | $providerinfo = new ProviderInfo(); |
||
| 246 | $providerinfo->setProperty('DisplayName', $this->getDisplayName()); |
||
| 247 | $providerinfo->setProperty('Description', $this->getSimpleMLAttribute('profile:description')); |
||
| 248 | $providerinfo->setProperty('ProviderLocation', $this->getProviderLocation()); |
||
| 249 | $providerinfo->setProperty('ProviderLogo', $this->getProviderLogo()); |
||
| 250 | $providerinfo->setProperty('TermsOfUse', $this->getSimpleMLAttribute('support:info_file')); |
||
| 251 | $providerinfo->setProperty('Helpdesk', $this->getHelpdesk()); |
||
| 252 | return $providerinfo; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * retrieves the location information and puts it into the XML structure |
||
| 257 | * |
||
| 258 | * @return \devices\xml\ProviderLocation|\devices\xml\ProviderLocation[] |
||
| 259 | */ |
||
| 260 | private function getProviderLocation() { |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * retrieves helpdesk contact information and puts it into the XML structure |
||
| 286 | * |
||
| 287 | * @return \devices\xml\Helpdesk |
||
| 288 | */ |
||
| 289 | private function getHelpdesk() { |
||
| 295 | } |
||
| 296 | |||
| 297 | private function getCredentialApplicability() { |
||
| 298 | $ssids = $this->attributes['internal:SSID']; |
||
| 299 | $oids = $this->attributes['internal:consortia']; |
||
| 300 | $credentialapplicability = new CredentialApplicability(); |
||
| 301 | $ieee80211s = []; |
||
| 302 | foreach ($ssids as $ssid => $ciph) { |
||
| 303 | $ieee80211 = new IEEE80211(); |
||
| 304 | $ieee80211->setProperty('SSID', $ssid); |
||
| 305 | $ieee80211->setProperty('MinRSNProto', $ciph == 'AES' ? 'CCMP' : 'TKIP'); |
||
| 306 | $ieee80211s[] = $ieee80211; |
||
| 307 | } |
||
| 308 | foreach ($oids as $oid) { |
||
| 309 | $ieee80211 = new IEEE80211(); |
||
| 310 | $ieee80211->setProperty('ConsortiumOID', $oid); |
||
| 311 | $ieee80211s[] = $ieee80211; |
||
| 312 | } |
||
| 313 | $credentialapplicability->setProperty('IEEE80211', $ieee80211s); |
||
| 314 | return($credentialapplicability); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * retrieves the parameters needed for the given EAP method and creates |
||
| 319 | * appropriate nodes in the XML structure for them |
||
| 320 | * |
||
| 321 | * @param array $eap the EAP type in question |
||
| 322 | * @return array a recap of the findings |
||
| 323 | */ |
||
| 324 | private function getAuthenticationMethodParams($eap) { |
||
| 325 | $inner = \core\common\EAP::innerAuth($eap); |
||
| 326 | $outerMethod = $eap["OUTER"]; |
||
| 327 | |||
| 328 | if (isset($inner["METHOD"]) && $inner["METHOD"]) { |
||
| 329 | $innerauthmethod = new InnerAuthenticationMethod(); |
||
| 330 | $typeOfInner = "\devices\xml\\" . ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod'); |
||
| 331 | $eapmethod = new $typeOfInner(); |
||
| 332 | $eaptype = new Type(); |
||
| 333 | $eaptype->setValue($inner['METHOD']); |
||
| 334 | $eapmethod->setProperty('Type', $eaptype); |
||
| 335 | $innerauthmethod->setProperty($typeOfInner, $eapmethod); |
||
| 336 | return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']]; |
||
| 337 | } else { |
||
| 338 | return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0]; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | private function setServerSideCredentials($eaptype) { |
||
| 343 | $attr = $this->attributes; |
||
| 344 | $serversidecredential = new ServerSideCredential(); |
||
| 345 | // Certificates and server names |
||
| 346 | $cAlist = []; |
||
| 347 | $attrCaList = $attr['internal:CAs'][0]; |
||
| 348 | foreach ($attrCaList as $ca) { |
||
| 349 | $caObject = new CA(); |
||
| 350 | $caObject->setValue(base64_encode($ca['der'])); |
||
| 351 | $caObject->setAttributes(['format' => 'X.509', 'encoding' => 'base64']); |
||
| 352 | $cAlist[] = $caObject; |
||
| 353 | } |
||
| 354 | $serverids = []; |
||
| 355 | $servers = $attr['eap:server_name']; |
||
| 356 | foreach ($servers as $server) { |
||
| 357 | $serverid = new ServerID(); |
||
| 358 | $serverid->setValue($server); |
||
| 359 | $serverids[] = $serverid; |
||
| 360 | } |
||
| 361 | $serversidecredential->setProperty('EAPType', $eaptype->getValue()); |
||
| 362 | $serversidecredential->setProperty('CA', $cAlist); |
||
| 363 | $serversidecredential->setProperty('ServerID', $serverids); |
||
| 364 | return($serversidecredential); |
||
| 365 | } |
||
| 366 | |||
| 367 | private function setClientSideRealm ($clientsidecredential) { |
||
| 368 | $attr = $this->attributes; |
||
| 369 | $realm = \core\common\Entity::getAttributeValue($attr, 'internal:realm', 0); |
||
| 370 | if ($realm === NULL) { |
||
| 371 | return; |
||
| 372 | } |
||
| 373 | if (\core\common\Entity::getAttributeValue($attr, 'internal:verify_userinput_suffix', 0) !== 1) { |
||
| 374 | return; |
||
| 375 | } |
||
| 376 | $clientsidecredential->setProperty('InnerIdentitySuffix', $realm); |
||
| 377 | if (\core\common\Entity::getAttributeValue($attr, 'internal:hint_userinput_suffix', 0) === 1) { |
||
| 378 | $clientsidecredential->setProperty('InnerIdentityHint', 'true'); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | private function setClientCetificate() { |
||
| 387 | } |
||
| 388 | |||
| 389 | private function setClientSideCredentials($eapParams) { |
||
| 390 | $clientsidecredential = new ClientSideCredential(); |
||
| 403 | } |
||
| 404 | |||
| 405 | private function setEapMethod($eaptype) { |
||
| 406 | $eapmethod = new EAPMethod(); |
||
| 407 | $eapmethod->setProperty('Type', $eaptype); |
||
| 408 | if (isset($this->VendorSpecific)) { |
||
| 409 | $vendorspecifics = []; |
||
| 410 | foreach ($this->VendorSpecific as $vs) { |
||
| 411 | $vendorspecific = new VendorSpecific(); |
||
| 412 | $vs['value']->addAttribute('xsi:noNamespaceSchemaLocation', "xxx.xsd"); |
||
| 413 | $vendorspecific->setValue($vs['value']); |
||
| 414 | $vendorspecific->setAttributes(['vendor' => $vs['vendor']]); |
||
| 415 | $vendorspecifics[] = $vendorspecific; |
||
| 416 | } |
||
| 417 | $eapmethod->setProperty('VendorSpecific', $vendorspecifics); |
||
| 418 | } |
||
| 419 | return($eapmethod); |
||
| 420 | } |
||
| 421 | |||
| 422 | private function getAuthMethod($eap) { |
||
| 441 | |||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | |||
| 446 | |||
| 447 | } |
||
| 448 |