Total Complexity | 69 |
Total Lines | 550 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 2 | 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 | * A flag to preserve backwards compatibility for eduroamCAT application |
||
110 | */ |
||
111 | public $eduroamCATcompatibility = FALSE; |
||
112 | |||
113 | private $eapId; |
||
114 | private $namespace; |
||
115 | private $providerInfo; |
||
116 | private $authMethodsList; |
||
117 | |||
118 | /** |
||
119 | * create HTML code explaining the installer |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function writeDeviceInfo() |
||
124 | { |
||
125 | \core\common\Entity::intoThePotatoes(); |
||
126 | $out = "<p>"; |
||
127 | $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"); |
||
128 | \core\common\Entity::outOfThePotatoes(); |
||
129 | return $out; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * create the actual XML file |
||
134 | * |
||
135 | * @return string filename of the generated installer |
||
136 | * @throws Exception |
||
137 | * |
||
138 | */ |
||
139 | public function writeInstaller() |
||
140 | { |
||
141 | \core\common\Entity::intoThePotatoes(); |
||
142 | $rootname = 'EAPIdentityProviderList'; |
||
143 | $dom = new \DOMDocument('1.0', 'utf-8'); |
||
144 | $root = $dom->createElement($rootname); |
||
145 | $dom->appendChild($root); |
||
146 | $ns = $dom->createAttributeNS( 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' ); |
||
147 | $ns->value = "eap-metadata.xsd"; |
||
148 | $root->appendChild($ns); |
||
149 | |||
150 | if (empty($this->attributes['internal:realm'][0])) { |
||
151 | $this->eapId = 'undefined'; |
||
152 | $this->namespace = 'urn:undefined'; |
||
153 | } else { |
||
154 | $this->eapId = $this->attributes['internal:realm'][0]; |
||
155 | $this->namespace = 'urn:RFC4282:realm'; |
||
156 | } |
||
157 | |||
158 | $this->providerInfo = $this->getProviderInfo(); |
||
159 | $this->authMethodsList = $this->getAuthMethodsList(); |
||
160 | |||
161 | foreach ($this->attributes['internal:networks'] as $netName => $netDefinition) { |
||
162 | $ssids = $netDefinition['ssid']; |
||
163 | $ois = $netDefinition['oi']; |
||
164 | if (!empty($ssids) || !empty($ois)) { |
||
165 | \core\DeviceXMLmain::marshalObject($dom, $root, 'EAPIdentityProvider', $this->eapIdp($ssids, $ois)); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | if ($dom->schemaValidate(ROOT.'/devices/eap_config/eap-metadata.xsd') === FALSE) { |
||
170 | throw new Exception("Schema validation failed for eap-metadata"); |
||
171 | } |
||
172 | |||
173 | $dom->formatOutput = true; |
||
174 | file_put_contents($this->installerBasename.'.eap-config', $dom->saveXML($dom)); |
||
175 | \core\common\Entity::outOfThePotatoes(); |
||
176 | return($this->installerBasename.'.eap-config'); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * determines the inner authentication. Is it EAP, and which mechanism is used to convey actual auth data |
||
181 | * @param array $eap the EAP type for which we want to get the inner auth |
||
182 | * @return array |
||
183 | */ |
||
184 | private function eapIdp($ssids, $oids) |
||
185 | { |
||
186 | $eapIdp = new \core\DeviceXMLmain(); |
||
187 | $eapIdp->setAttribute('version', '1'); |
||
188 | if ($this->langScope === 'single') { |
||
189 | $eapIdp->setAttribute('lang', $this->languageInstance->getLang()); |
||
190 | } |
||
191 | $eapIdp->setAttribute('ID', $this->eapId); |
||
192 | $eapIdp->setAttribute('namespace', $this->namespace); |
||
193 | $authMethods = new \core\DeviceXMLmain(); |
||
194 | $authMethods->setChild('AuthenticationMethod', $this->authMethodsList); |
||
195 | $eapIdp->setChild('AuthenticationMethods', $authMethods); |
||
196 | $eapIdp->setChild('CredentialApplicability', $this->getCredentialApplicability($ssids,$oids)); |
||
197 | // TODO $eap_idp->setChild('ValidUntil',$this->getValidUntil()); |
||
198 | $eapIdp->setChild('ProviderInfo', $this->providerInfo); |
||
199 | // TODO $eap_idp->setChild('VendorSpecific',$this->getVendorSpecific()); |
||
200 | return($eapIdp); |
||
|
|||
201 | } |
||
202 | |||
203 | /** |
||
204 | * determines the inner authentication. Is it EAP, and which mechanism is used to convey actual auth data |
||
205 | * @param array $eap the EAP type for which we want to get the inner auth |
||
206 | * @return array |
||
207 | */ |
||
208 | private function innerAuth($eap) |
||
209 | { |
||
210 | $out = []; |
||
211 | $out['EAP'] = 0; |
||
212 | switch ($eap["INNER"]) { |
||
213 | case \core\common\EAP::NE_MSCHAP2: |
||
214 | if ($this->eduroamCATcompatibility === TRUE) { |
||
215 | $out['METHOD'] = \core\common\EAP::MSCHAP2; |
||
216 | $out['EAP'] = 1; |
||
217 | } else { |
||
218 | $out['METHOD'] = $eap["INNER"]; |
||
219 | } |
||
220 | break; |
||
221 | case \core\common\EAP::NE_SILVERBULLET: |
||
222 | $out['METHOD'] = \core\common\EAP::NONE; |
||
223 | break; |
||
224 | default: |
||
225 | $out['METHOD'] = $eap["INNER"]; |
||
226 | break; |
||
227 | } |
||
228 | // override if there is an inner EAP |
||
229 | if ($eap["INNER"] > 0) { // there is an inner EAP method |
||
230 | $out['EAP'] = 1; |
||
231 | } |
||
232 | return $out; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * |
||
237 | * @param string $attrName the attribute name |
||
238 | * @return array of values for this attribute |
||
239 | */ |
||
240 | private function getSimpleMLAttribute($attrName) |
||
241 | { |
||
242 | if (empty($this->attributes[$attrName][0])) { |
||
243 | return([]); |
||
244 | } |
||
245 | $attributeList = $this->attributes[$attrName]; |
||
246 | $objs = []; |
||
247 | if ($this->langScope === 'global') { |
||
248 | foreach ($attributeList['langs'] as $language => $value) { |
||
249 | $language = ($language === 'C' ? 'any' : $language); |
||
250 | $obj = new \core\DeviceXMLmain(); |
||
251 | $obj->setValue($value); |
||
252 | $obj->setAttributes(['lang' => $language]); |
||
253 | $objs[] = $obj; |
||
254 | } |
||
255 | } else { |
||
256 | $objs[] = $attributeList[0]; |
||
257 | } |
||
258 | return($objs); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * constructs the name of the institution and puts it into the XML. |
||
263 | * consists of the best-language-match inst name, and if the inst has more |
||
264 | * than one profile also the best-language-match profile name |
||
265 | * |
||
266 | * @return \core\DeviceXMLmain[] |
||
267 | */ |
||
268 | private function getDisplayName() |
||
269 | { |
||
270 | $attr = $this->attributes; |
||
271 | $objs = []; |
||
272 | if ($this->langScope === 'global') { |
||
273 | $instNameLangs = $attr['general:instname']['langs']; |
||
274 | if ($attr['internal:profile_count'][0] > 1) { |
||
275 | $profileNameLangs = $attr['profile:name']['langs']; |
||
276 | } |
||
277 | foreach ($instNameLangs as $language => $value) { |
||
278 | $language = ($language === 'C' ? 'any' : $language); |
||
279 | $displayname = new \core\DeviceXMLmain(); |
||
280 | if (isset($profileNameLangs)) { |
||
281 | $langOrC = isset($profileNameLangs[$language]) ? $profileNameLangs[$language] : $profileNameLangs['C']; |
||
282 | $value .= ' - '.$langOrC; |
||
283 | } |
||
284 | $displayname->setValue($value); |
||
285 | $displayname->setAttributes(['lang' => $language]); |
||
286 | $objs[] = $displayname; |
||
287 | } |
||
288 | } else { |
||
289 | $displayname = new \core\DeviceXMLmain(); |
||
290 | $value = $attr['general:instname'][0]; |
||
291 | if ($attr['internal:profile_count'][0] > 1) { |
||
292 | $value .= ' - '.$attr['profile:name'][0]; |
||
293 | } |
||
294 | $displayname->setValue($value); |
||
295 | $objs[] = $displayname; |
||
296 | } |
||
297 | return $objs; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * retrieves the provider logo and puts it into the XML structure |
||
302 | * |
||
303 | * @return \core\DeviceXMLmain |
||
304 | */ |
||
305 | private function getProviderLogo() |
||
306 | { |
||
307 | $attr = $this->attributes; |
||
308 | if (isset($attr['general:logo_file'][0])) { |
||
309 | $logoString = base64_encode($attr['general:logo_file'][0]); |
||
310 | $logoMime = 'image/'.$attr['internal:logo_file'][0]['mime']; |
||
311 | $providerlogo = new \core\DeviceXMLmain(); |
||
312 | $providerlogo->setAttributes(['mime' => $logoMime, 'encoding' => 'base64']); |
||
313 | $providerlogo->setValue($logoString); |
||
314 | return $providerlogo; |
||
315 | } |
||
316 | return NULL; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * retrieves provider information and puts it into the XML structure. |
||
321 | * contains the profile description and the ToU file, if any |
||
322 | * |
||
323 | * @return \core\DeviceXMLmain |
||
324 | */ |
||
325 | private function getProviderInfo() |
||
326 | { |
||
327 | $providerinfo = new \core\DeviceXMLmain(); |
||
328 | $providerinfo->setChild('DisplayName', $this->getDisplayName()); |
||
329 | $providerinfo->setChild('Description', $this->getSimpleMLAttribute('profile:description')); |
||
330 | $providerinfo->setChild('ProviderLocation', $this->getProviderLocation()); |
||
331 | $providerinfo->setChild('ProviderLogo', $this->getProviderLogo()); |
||
332 | $providerinfo->setChild('TermsOfUse', $this->getSimpleMLAttribute('support:info_file'), null, 'cdata'); |
||
333 | $providerinfo->setChild('Helpdesk', $this->getHelpdesk()); |
||
334 | return $providerinfo; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * retrieves the location information and puts it into the XML structure |
||
339 | * |
||
340 | * @return \core\DeviceXMLmain[] |
||
341 | */ |
||
342 | private function getProviderLocation() |
||
343 | { |
||
344 | $attr = $this->attributes; |
||
345 | if (isset($attr['general:geo_coordinates'])) { |
||
346 | $attrCoordinates = $attr['general:geo_coordinates']; |
||
347 | $location = []; |
||
348 | foreach ($attrCoordinates as $a) { |
||
349 | $providerlocation = new \core\DeviceXMLmain(); |
||
350 | $b = json_decode($a, true); |
||
351 | $providerlocation->setChild('Longitude', $b['lon']); |
||
352 | $providerlocation->setChild('Latitude', $b['lat']); |
||
353 | $location[] = $providerlocation; |
||
354 | } |
||
355 | return $location; |
||
356 | } |
||
357 | return NULL; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * retrieves helpdesk contact information and puts it into the XML structure |
||
362 | * |
||
363 | * @return \core\DeviceXMLmain |
||
364 | */ |
||
365 | private function getHelpdesk() |
||
366 | { |
||
367 | $helpdesk = new \core\DeviceXMLmain(); |
||
368 | $helpdesk->setChild('EmailAddress', $this->getSimpleMLAttribute('support:email')); |
||
369 | $helpdesk->setChild('WebAddress', $this->getSimpleMLAttribute('support:url')); |
||
370 | $helpdesk->setChild('Phone', $this->getSimpleMLAttribute('support:phone')); |
||
371 | return $helpdesk; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * determine where this credential should be applicable |
||
376 | * |
||
377 | * @return \core\DeviceXMLmain |
||
378 | */ |
||
379 | private function getCredentialApplicability($ssids, $oids) |
||
380 | { |
||
381 | $setWired = isset($this->attributes['media:wired'][0]) && |
||
382 | $this->attributes['media:wired'][0] == 'on' ? 1 : 0; |
||
383 | $credentialapplicability = new \core\DeviceXMLmain(); |
||
384 | $ieee80211s = []; |
||
385 | foreach ($ssids as $ssid) { |
||
386 | $ieee80211 = new \core\DeviceXMLmain(); |
||
387 | $ieee80211->setChild('SSID', $ssid); |
||
388 | $ieee80211->setChild('MinRSNProto', 'CCMP'); |
||
389 | $ieee80211s[] = $ieee80211; |
||
390 | } |
||
391 | foreach ($oids as $oid) { |
||
392 | $ieee80211 = new \core\DeviceXMLmain(); |
||
393 | $ieee80211->setChild('ConsortiumOID', $oid); |
||
394 | $ieee80211s[] = $ieee80211; |
||
395 | } |
||
396 | $credentialapplicability->setChild('IEEE80211', $ieee80211s); |
||
397 | if ($setWired) { |
||
398 | $credentialapplicability->setChild('IEEE8023', ''); |
||
399 | } |
||
400 | return $credentialapplicability; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * retrieves the parameters needed for the given EAP method and creates |
||
405 | * appropriate nodes in the XML structure for them |
||
406 | * |
||
407 | * @param array $eap the EAP type in question |
||
408 | * @return array a recap of the findings |
||
409 | */ |
||
410 | private function getAuthenticationMethodParams($eap) |
||
411 | { |
||
412 | $inner = $this->innerAuth($eap); |
||
413 | $outerMethod = $eap["OUTER"]; |
||
414 | |||
415 | if (isset($inner["METHOD"]) && $inner["METHOD"]) { |
||
416 | $innerauthmethod = new \core\DeviceXMLmain(); |
||
417 | $typeOfInner = ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod'); |
||
418 | $eapmethod = new \core\DeviceXMLmain(); |
||
419 | $eapmethod->setChild('Type', abs($inner['METHOD'])); |
||
420 | $innerauthmethod->setChild($typeOfInner, $eapmethod); |
||
421 | return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']]; |
||
422 | } else { |
||
423 | return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0]; |
||
424 | } |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * sets the server-side credentials for a given EAP type |
||
429 | * |
||
430 | * @param \devices\XML\Type $eaptype the EAP type |
||
431 | * @return \core\DeviceXMLmain |
||
432 | */ |
||
433 | private function getServerSideCredentials($eap) |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * sets the realm information for the client-side credential |
||
465 | * |
||
466 | * @param \core\DeviceXMLmain $clientsidecredential the ClientSideCredential to which the realm info is to be added |
||
467 | * @return void |
||
468 | */ |
||
469 | private function setClientSideRealm($clientsidecredential) |
||
470 | { |
||
471 | $attr = $this->attributes; |
||
472 | $realm = \core\common\Entity::getAttributeValue($attr, 'internal:realm', 0); |
||
473 | if ($realm === NULL) { |
||
474 | return; |
||
475 | } |
||
476 | if (\core\common\Entity::getAttributeValue($attr, 'internal:verify_userinput_suffix', 0) !== 1) { |
||
477 | return; |
||
478 | } |
||
479 | $clientsidecredential->setChild('InnerIdentitySuffix', $realm); |
||
480 | if (\core\common\Entity::getAttributeValue($attr, 'internal:hint_userinput_suffix', 0) === 1) { |
||
481 | $clientsidecredential->setChild('InnerIdentityHint', 'true'); |
||
482 | } |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * sets the client certificate |
||
487 | * |
||
488 | * @return \core\DeviceXMLmain |
||
489 | */ |
||
490 | private function getClientCertificate() |
||
491 | { |
||
492 | $clientCertificateObject = new \core\DeviceXMLmain(); |
||
493 | $clientCertificateObject->setValue(base64_encode($this->clientCert["certdata"])); |
||
494 | $clientCertificateObject->setAttributes(['format' => 'PKCS12', 'encoding' => 'base64']); |
||
495 | return $clientCertificateObject; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * sets the client-side credentials for the given EAP type |
||
500 | * |
||
501 | * @param array $eapParams the EAP parameters |
||
502 | * @return \core\DeviceXMLmain |
||
503 | */ |
||
504 | private function getClientSideCredentials($eap) |
||
505 | { |
||
506 | $children = $this->authMethodElements['client'][$eap]; |
||
507 | $clientsidecredential = new \core\DeviceXMLmain(); |
||
508 | $outerId = $this->determineOuterIdString(); |
||
509 | $this->loggerInstance->debug(5, $eap, "XMLOI:", "\n"); |
||
510 | if (in_array('OuterIdentity', $children)) { |
||
511 | if ($outerId !== NULL) { |
||
512 | $clientsidecredential->setChild('OuterIdentity', $outerId); |
||
513 | } |
||
514 | } |
||
515 | $this->setClientSideRealm($clientsidecredential); |
||
516 | // $clientsidecredential->setChild('EAPType', $eapParams['inner_methodID'] ? $eapParams['inner_methodID'] : $eapParams['methodID']); |
||
517 | |||
518 | // Client Certificate |
||
519 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) { |
||
520 | $attr = $this->attributes; |
||
521 | $outerId = \core\common\Entity::getAttributeValue($attr, 'internal:username', 0); |
||
522 | $clientsidecredential->setChild('OuterIdentity', $outerId); |
||
523 | $clientsidecredential->setChild('ClientCertificate', $this->getClientCertificate()); |
||
524 | } |
||
525 | return $clientsidecredential; |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * sets the EAP method |
||
530 | * |
||
531 | * @param \devices\XML\Type $eaptype the EAP type XMLObject |
||
532 | * @return \core\DeviceXMLmain |
||
533 | */ |
||
534 | private function getEapMethod($eaptype) |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * determines the authentication method to use |
||
554 | * |
||
555 | * @param array $eap the EAP methods, in array representation |
||
556 | * @return \core\DeviceXMLmain |
||
557 | */ |
||
558 | private function getAuthMethod($eap) |
||
559 | { |
||
560 | $authmethod = new \core\DeviceXMLmain(); |
||
561 | $eapParams = $this->getAuthenticationMethodParams($eap); |
||
562 | $eaptype = new \core\DeviceXMLmain(); |
||
563 | $eaptype->setValue($eapParams['methodID']); |
||
564 | // Type |
||
565 | $authmethod->setChild('EAPMethod', $this->getEapMethod($eaptype)); |
||
566 | |||
567 | // ServerSideCredentials |
||
568 | $authmethod->setChild('ServerSideCredential', $this->getServerSideCredentials($eap['OUTER'])); |
||
569 | |||
570 | // ClientSideCredentials |
||
571 | $authmethod->setChild('ClientSideCredential', $this->getClientSideCredentials($eap['INNER'])); |
||
572 | |||
573 | if ($eapParams['inner_method']) { |
||
574 | $authmethod->setChild('InnerAuthenticationMethod', $eapParams['inner_method']); |
||
575 | } |
||
576 | return $authmethod; |
||
577 | } |
||
578 | |||
579 | private function getAuthMethodsList() { |
||
596 | } |
||
597 | |||
598 | |||
599 | |||
601 |