Test Failed
Push — master ( 95d561...502d18 )
by Tomasz
09:46
created

DeviceXML::writeInstaller()   F

Complexity

Conditions 20
Paths 168

Size

Total Lines 98
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 1
Metric Value
eloc 61
c 7
b 2
f 1
dl 0
loc 98
rs 3.6
cc 20
nc 168
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/*
4
 * *****************************************************************************
5
 * Contributions to this work were made on behalf of the GÉANT project, a 
6
 * project that has received funding from the European Union’s Framework 
7
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
8
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
9
 * 691567 (GN4-1) and No. 731122 (GN4-2).
10
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
11
 * of the copyright in all material which was developed by a member of the GÉANT
12
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
13
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
14
 * UK as a branch of GÉANT Vereniging.
15
 * 
16
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
17
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
18
 *
19
 * License: see the web/copyright.inc.php file in the file structure or
20
 *          <base_url>/copyright.php after deploying the software
21
 */
22
23
/**
24
 * This file defines an abstract class used for generic XML
25
 * devices
26
 * actual modules only define available EAP types.
27
 *
28
 * @author Maja Gorecka-Wolniewicz <[email protected]>
29
 * @author Tomasz Wolniewicz <[email protected]>
30
 *
31
 * @package ModuleWriting
32
 */
33
34
namespace devices\eap_config;
35
36
use Exception;
37
38
/**
39
 * This class implements full functionality of the generic XML device
40
 * the only fuction of the extenstions of this class is to specify
41
 * supported EAP methods.
42
 * Instead of specifying supported EAPS an extension can set $all_eaps to true
43
 * this will cause the installer to configure all EAP methods supported by 
44
 * the current profile and declared by the given device.
45
 */
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()
79
    {
80
        parent::__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
    public $singleEAPProvider = false;
113
114
    private $eapId;
115
    private $namespace;
116
    private $providerInfo;
117
    private $authMethodsList;
118
    
119
    /**
120
     * create HTML code explaining the installer
121
     * 
122
     * @return string
123
     */
124
    public function writeDeviceInfo()
125
    {
126
        \core\common\Entity::intoThePotatoes();
127
        $out = "<p>";
128
        $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");
129
        \core\common\Entity::outOfThePotatoes();
130
        return $out;
131
    }
132
133
    /**
134
     * create the actual XML file
135
     * 
136
     * @return string filename of the generated installer
137
     * @throws Exception
138
     *
139
     */
140
    public function writeInstaller()
141
    {
142
        \core\common\Entity::intoThePotatoes();
143
        $rootname = 'EAPIdentityProviderList';
144
        $dom = new \DOMDocument('1.0', 'utf-8');
145
        $root = $dom->createElement($rootname);
146
        $dom->appendChild($root);
147
        $ns = $dom->createAttributeNS( 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:noNamespaceSchemaLocation' );
148
        $ns->value = "eap-metadata.xsd";
149
        $root->appendChild($ns);
150
        $this->openRoamingToU = sprintf(_("I have read and agree to OpenRoaming Terms of Use at %s."), "https://wballiance.com/openroaming/toc-2020/");
0 ignored issues
show
Bug Best Practice introduced by
The property openRoamingToU does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
151
        foreach ($this->languageInstance->getAllTranslations("I have read and agree to OpenRoaming Terms of Use at %s", "device") as $lang => $message) {
152
            $this->openRoamingToUArray[$lang] = sprintf($message, "https://wballiance.com/openroaming/toc-2020/");
153
        }
154
        
155
        if (empty($this->attributes['internal:realm'][0])) {
156
            $this->eapId = 'undefined';
157
            $this->namespace = 'urn:undefined';
158
        } else {
159
            $this->eapId = $this->attributes['internal:realm'][0];
160
            $this->namespace = 'urn:RFC4282:realm';
161
        }
162
        
163
        $this->authMethodsList = $this->getAuthMethodsList();
164
        $this->loggerInstance->debug(5, $this->attributes['internal:networks'], "NETWORKS:", "\n");
165
        /*
166
         * This approach is forced by geteduroam compatibility. We pack all networks into a single Provider
167
         * with the exception of the openroaming one which we pack separately.
168
         */
169
        
170
        if ($this->singleEAPProvider === true) {
171
            /*
172
             * if "condition" is set to openroaming, OpenRoaming terms of use must be mentioned
173
             * unless the preagreed is set for openroaming
174
             * if "ask" is set then we generate a separate OR profile which needs to contain the OR ToU
175
             * the ToU is not needed in the eduroam-only profile
176
             */
177
            $ssids = [];
178
            $ois = [];
179
            $orNetwork = [];
180
            foreach ($this->attributes['internal:networks'] as $netName => $netDefinition) {
181
                if ($netDefinition['condition'] === 'internal:openroaming' &&
182
                        $this->attributes['internal:openroaming']) {
183
                    $this->setORtou();
184
                    if (preg_match("/^ask/",$this->attributes['media:openroaming'][0])) {
185
                        $orNetwork = $netDefinition;
186
                        continue;                        
187
                    }
188
                }
189
                foreach ($netDefinition['ssid'] as $ssid) {
190
                    $ssids[] = $ssid;
191
                }
192
                foreach ($netDefinition['oi'] as $oi) {
193
                    $ois[] = $oi;
194
                }
195
            }
196
197
            if (!empty($orNetwork)) {
198
                $this->addORtou = false;
199
            }
200
            $this->providerInfo = $this->getProviderInfo();
201
            
202
            if (!empty($ssids) || !empty($ois)) {
203
                \core\DeviceXMLmain::marshalObject($dom, $root, 'EAPIdentityProvider', $this->eapIdp($ssids, $ois));
204
            }
205
            
206
            if (!empty($orNetwork)) {
207
                // here we need to add the Tou unless preagreed is set
208
                $this->setORtou();
209
                $this->providerInfo = $this->getProviderInfo();
210
                \core\DeviceXMLmain::marshalObject($dom, $root, 'EAPIdentityProvider', $this->eapIdp($orNetwork['ssid'], $orNetwork['oi']));
211
            }
212
        } else {
213
214
            foreach ($this->attributes['internal:networks'] as $netName => $netDefinition) {
215
                if ($netDefinition['condition'] === 'internal:openroaming' &&
216
                        $this->attributes['internal:openroaming']) {
217
                    $this->setORtou();
218
                } else {
219
                    $this->addORtou = false;
220
                }
221
                $this->providerInfo = $this->getProviderInfo();
222
                $ssids = $netDefinition['ssid'];
223
                $ois = $netDefinition['oi'];
224
                if (!empty($ssids) || !empty($ois)) {
225
                    \core\DeviceXMLmain::marshalObject($dom, $root, 'EAPIdentityProvider', $this->eapIdp($ssids, $ois));
226
                }
227
            }
228
        }
229
        
230
        if ($dom->schemaValidate(ROOT.'/devices/eap_config/eap-metadata.xsd') === FALSE) {
231
            throw new Exception("Schema validation failed for eap-metadata");
232
        }
233
234
        $dom->formatOutput = true;
235
        file_put_contents($this->installerBasename.'.eap-config', $dom->saveXML($dom));
236
        \core\common\Entity::outOfThePotatoes();
237
        return($this->installerBasename.'.eap-config');
238
    }
239
    
240
    private function setORtou() {
241
        if (preg_match("/preagreed/",$this->attributes['media:openroaming'][0])) {
242
            $this->addORtou = false;
243
        } else {
244
            $this->addORtou = true;
245
        }
246
    }
247
    
248
    /**
249
     * determines the inner authentication. Is it EAP, and which mechanism is used to convey actual auth data
250
     * @param array $eap the EAP type for which we want to get the inner auth
251
     * @return array
252
     */    
253
    private function eapIdp($ssids, $oids)
254
    {
255
        $eapIdp = new \core\DeviceXMLmain();
256
        $eapIdp->setAttribute('version', '1');
257
        if ($this->langScope === 'single') {
258
            $eapIdp->setAttribute('lang', $this->languageInstance->getLang());
259
        }
260
        $eapIdp->setAttribute('ID', $this->eapId);
261
        $eapIdp->setAttribute('namespace', $this->namespace);
262
        $authMethods = new \core\DeviceXMLmain();
263
        $authMethods->setChild('AuthenticationMethod', $this->authMethodsList);
264
        $eapIdp->setChild('AuthenticationMethods', $authMethods);
265
        $eapIdp->setChild('CredentialApplicability', $this->getCredentialApplicability($ssids,$oids));
266
// TODO   $eap_idp->setChild('ValidUntil',$this->getValidUntil());
267
        $eapIdp->setChild('ProviderInfo', $this->providerInfo);
268
// TODO   $eap_idp->setChild('VendorSpecific',$this->getVendorSpecific());
269
        return($eapIdp);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $eapIdp returns the type core\DeviceXMLmain which is incompatible with the documented return type array.
Loading history...
270
    }
271
272
    /**
273
     * determines the inner authentication. Is it EAP, and which mechanism is used to convey actual auth data
274
     * @param array $eap the EAP type for which we want to get the inner auth
275
     * @return array
276
     */  
277
    private function innerAuth($eap)
278
    {
279
        $out = [];
280
        $out['EAP'] = 0;
281
        switch ($eap["INNER"]) {
282
            case \core\common\EAP::NE_MSCHAP2:
283
                if ($this->eduroamCATcompatibility === TRUE) {
284
                    $out['METHOD'] = \core\common\EAP::MSCHAP2;
285
                    $out['EAP'] = 1;
286
                } else {
287
                    $out['METHOD'] = $eap["INNER"];
288
                }
289
                break;
290
            case \core\common\EAP::NE_SILVERBULLET:
291
                $out['METHOD'] = \core\common\EAP::NONE;
292
                break;
293
            default:
294
                $out['METHOD'] = $eap["INNER"];
295
                break;
296
        }
297
        // override if there is an inner EAP
298
        if ($eap["INNER"] > 0) { // there is an inner EAP method
299
            $out['EAP'] = 1;
300
        }
301
        return $out;
302
    }
303
    
304
    /**
305
     * 
306
     * @param string $attrName the attribute name
307
     * @return array of values for this attribute
308
     */
309
    private function getSimpleMLAttribute($attrName)
310
    {
311
        if (empty($this->attributes[$attrName][0])) {
312
            return([]);
313
        }
314
        $attributeList = $this->attributes[$attrName];
315
        $objs = [];
316
        if ($this->langScope === 'global') {
317
            foreach ($attributeList['langs'] as $language => $value) {
318
                $language = ($language === 'C' ? 'any' : $language);
319
                $obj = new \core\DeviceXMLmain();
320
                $obj->setValue($value);
321
                $obj->setAttributes(['lang' => $language]);
322
                $objs[] = $obj;
323
            }
324
        } else {
325
            $objs[] = $attributeList[0];
326
        }
327
        return($objs);
328
    }
329
    
330
    /**
331
     * constructs the name of the institution and puts it into the XML.
332
     * consists of the best-language-match inst name, and if the inst has more 
333
     * than one profile also the best-language-match profile name
334
     * 
335
     * @return \core\DeviceXMLmain[]
336
     */
337
    private function getDisplayName()
338
    {
339
        $attr = $this->attributes;
340
        $objs = [];
341
        if ($this->langScope === 'global') {
342
            $instNameLangs = $attr['general:instname']['langs'];
343
            if ($attr['internal:profile_count'][0] > 1) {
344
                $profileNameLangs = $attr['profile:name']['langs'];
345
            }
346
            foreach ($instNameLangs as $language => $value) {
347
                $language = ($language === 'C' ? 'any' : $language);
348
                $displayname = new \core\DeviceXMLmain();
349
                if (isset($profileNameLangs)) {
350
                    $langOrC = isset($profileNameLangs[$language]) ? $profileNameLangs[$language] : $profileNameLangs['C'];
351
                    $value .= ' - '.$langOrC;
352
                }
353
                $displayname->setValue($value);
354
                $displayname->setAttributes(['lang' => $language]);
355
                $objs[] = $displayname;
356
            }
357
        } else {
358
            $displayname = new \core\DeviceXMLmain();
359
            $value = $attr['general:instname'][0];
360
            if ($attr['internal:profile_count'][0] > 1) {
361
                $value .= ' - '.$attr['profile:name'][0];
362
            }
363
            $displayname->setValue($value);
364
            $objs[] = $displayname;
365
        }
366
        return $objs;
367
    }
368
369
    /**
370
     * retrieves the provider logo and puts it into the XML structure
371
     * 
372
     * @return \core\DeviceXMLmain
373
     */
374
    private function getProviderLogo()
375
    {
376
        $attr = $this->attributes;
377
        if (isset($attr['general:logo_file'][0])) {
378
            $logoString = base64_encode($attr['general:logo_file'][0]);
379
            $logoMime = 'image/'.$attr['internal:logo_file'][0]['mime'];
380
            $providerlogo = new \core\DeviceXMLmain();
381
            $providerlogo->setAttributes(['mime' => $logoMime, 'encoding' => 'base64']);
382
            $providerlogo->setValue($logoString);
383
            return $providerlogo;
384
        }
385
        return NULL;
386
    }
387
388
    /**
389
     * retrieves provider information and puts it into the XML structure.
390
     * contains the profile description and the ToU file, if any
391
     * 
392
     * @return \core\DeviceXMLmain
393
     */
394
    private function getProviderInfo()
395
    {
396
        $providerinfo = new \core\DeviceXMLmain();
397
        $providerinfo->setChild('DisplayName', $this->getDisplayName());
398
        $providerinfo->setChild('Description', $this->getSimpleMLAttribute('profile:description'));
399
        $providerinfo->setChild('ProviderLocation', $this->getProviderLocation());
400
        $providerinfo->setChild('ProviderLogo', $this->getProviderLogo());
401
        $providerinfo->setChild('TermsOfUse', $this->getProviderTou(), null, 'cdata');
402
        $providerinfo->setChild('Helpdesk', $this->getHelpdesk());
403
        return $providerinfo; 
404
    }
405
    
406
    private function getProviderTou() {
407
        $standardTou = $this->getSimpleMLAttribute('support:info_file');
408
        if ($this->addORtou === false) {
409
            return $standardTou;
410
        }
411
        $out = [];
412
        if ($this->langScope === 'global') {
413
            foreach ($standardTou as $touObj) {
414
                $tou = $touObj->getValue();
415
                $lngAttr = $touObj->getAttributes();
416
                $lng = $lngAttr['lang'] === 'any' ? \config\Master::APPEARANCE['defaultlocale'] : $lngAttr['lang'];
417
                $tou .= "\n".$this->openRoamingToUArray[$lng];
418
                $touObj->setValue($tou);
419
                $out[] =  $touObj;
420
            } 
421
        } else {
422
            $tou = $standardTou[0];
423
            $tou .= "\n".$this->openRoamingToU;
424
            $out = [$tou];
425
        }
426
        return $out;
427
    }
428
429
    /**
430
     * retrieves the location information and puts it into the XML structure
431
     * 
432
     * @return \core\DeviceXMLmain[]
433
     */
434
    private function getProviderLocation()
435
    {
436
        $attr = $this->attributes;
437
        if (isset($attr['general:geo_coordinates'])) {
438
            $attrCoordinates = $attr['general:geo_coordinates'];
439
            $location = [];
440
            foreach ($attrCoordinates as $a) {
441
                $providerlocation = new \core\DeviceXMLmain();
442
                $b = json_decode($a, true);
443
                $providerlocation->setChild('Longitude', $b['lon']);
444
                $providerlocation->setChild('Latitude', $b['lat']);
445
                $location[] = $providerlocation;
446
            }           
447
            return $location;
448
        }
449
        return NULL;
450
    }
451
452
    /**
453
     * retrieves helpdesk contact information and puts it into the XML structure
454
     * 
455
     * @return \core\DeviceXMLmain
456
     */
457
    private function getHelpdesk()
458
    {
459
        $helpdesk = new \core\DeviceXMLmain();
460
        $helpdesk->setChild('EmailAddress', $this->getSimpleMLAttribute('support:email'));
461
        $helpdesk->setChild('WebAddress', $this->getSimpleMLAttribute('support:url'));
462
        $helpdesk->setChild('Phone', $this->getSimpleMLAttribute('support:phone'));
463
        return $helpdesk;
464
    }
465
466
    /**
467
     * determine where this credential should be applicable
468
     * 
469
     * @return \core\DeviceXMLmain
470
     */
471
    private function getCredentialApplicability($ssids, $oids)
472
    {
473
        $setWired = isset($this->attributes['media:wired'][0]) && 
474
                $this->attributes['media:wired'][0] == 'on' ? 1 : 0;        
475
        $credentialapplicability = new \core\DeviceXMLmain();
476
        $ieee80211s = [];
477
        foreach ($ssids as $ssid) {
478
            $ieee80211 = new \core\DeviceXMLmain();
479
            $ieee80211->setChild('SSID', $ssid);
480
            $ieee80211->setChild('MinRSNProto', 'CCMP');
481
            $ieee80211s[] = $ieee80211;
482
        }
483
        foreach ($oids as $oid) {
484
            $ieee80211 = new \core\DeviceXMLmain();
485
            $ieee80211->setChild('ConsortiumOID', $oid);
486
            $ieee80211s[] = $ieee80211;
487
        }
488
        $credentialapplicability->setChild('IEEE80211', $ieee80211s);
489
        if ($setWired) {
490
            $credentialapplicability->setChild('IEEE8023', '');
491
        }
492
        return $credentialapplicability;
493
    }
494
495
    /**
496
     * retrieves the parameters needed for the given EAP method and creates
497
     * appropriate nodes in the XML structure for them
498
     * 
499
     * @param array $eap the EAP type in question
500
     * @return array a recap of the findings
501
     */
502
    private function getAuthenticationMethodParams($eap)
503
    {
504
        $inner = $this->innerAuth($eap);
505
        $outerMethod = $eap["OUTER"];
506
507
        if (isset($inner["METHOD"]) && $inner["METHOD"]) {
508
            $innerauthmethod = new \core\DeviceXMLmain();
509
            $typeOfInner = ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod');
510
            $eapmethod = new \core\DeviceXMLmain();
511
            $eapmethod->setChild('Type', abs($inner['METHOD']));
512
            $innerauthmethod->setChild($typeOfInner, $eapmethod);
513
            return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']];
514
        } else {
515
            return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0];
516
        }
517
    }
518
519
    /**
520
     * sets the server-side credentials for a given EAP type
521
     * 
522
     * @param \devices\XML\Type $eaptype the EAP type
523
     * @return \core\DeviceXMLmain
524
     */
525
    private function getServerSideCredentials($eap)
526
    {
527
        $attr = $this->attributes;
528
        $children = $this->authMethodElements['server'][$eap];
529
        $serversidecredential = new \core\DeviceXMLmain();
530
// Certificates and server names
531
        $cAlist = [];
532
        $attrCaList = $attr['internal:CAs'][0];
533
        foreach ($attrCaList as $ca) {
534
            $caObject = new \core\DeviceXMLmain();
535
            $caObject->setValue(base64_encode($ca['der']));
536
            $caObject->setAttributes(['format' => 'X.509', 'encoding' => 'base64']);
537
            $cAlist[] = $caObject;
538
        }
539
        $serverids = [];
540
        $servers = $attr['eap:server_name'];
541
        foreach ($servers as $server) {
542
            $serverid = new \core\DeviceXMLmain();
543
            $serverid->setValue($server);
544
            $serverids[] = $serverid;
545
        }
546
        if (in_array('CA', $children)) {
547
            $serversidecredential->setChild('CA', $cAlist);
548
        }
549
        if (in_array('ServerID', $children)) {
550
            $serversidecredential->setChild('ServerID', $serverids);
551
        }
552
        return $serversidecredential;
553
    }
554
555
    /**
556
     * sets the realm information for the client-side credential
557
     * 
558
     * @param \core\DeviceXMLmain $clientsidecredential the ClientSideCredential to which the realm info is to be added
559
     * @return void
560
     */
561
    private function setClientSideRealm($clientsidecredential)
562
    {
563
        $attr = $this->attributes;
564
        $realm = \core\common\Entity::getAttributeValue($attr, 'internal:realm', 0);
565
        if ($realm === NULL) {
566
            return;
567
        }
568
        if (\core\common\Entity::getAttributeValue($attr, 'internal:verify_userinput_suffix', 0) !== 1) {
569
            return;
570
        }
571
        $clientsidecredential->setChild('InnerIdentitySuffix', $realm);
572
        if (\core\common\Entity::getAttributeValue($attr, 'internal:hint_userinput_suffix', 0) === 1) {
573
            $clientsidecredential->setChild('InnerIdentityHint', 'true');
574
        }
575
    }
576
577
    /**
578
     * sets the client certificate
579
     * 
580
     * @return \core\DeviceXMLmain
581
     */
582
    private function getClientCertificate()
583
    {
584
        $clientCertificateObject = new \core\DeviceXMLmain();
585
        $clientCertificateObject->setValue(base64_encode($this->clientCert["certdata"]));
586
        $clientCertificateObject->setAttributes(['format' => 'PKCS12', 'encoding' => 'base64']);
587
        return $clientCertificateObject;
588
    }
589
590
    /**
591
     * sets the client-side credentials for the given EAP type
592
     * 
593
     * @param array $eapParams the EAP parameters
594
     * @return \core\DeviceXMLmain
595
     */
596
    private function getClientSideCredentials($eap)
597
    {
598
        $children = $this->authMethodElements['client'][$eap];
599
        $clientsidecredential = new \core\DeviceXMLmain();
600
        $outerId = $this->determineOuterIdString();
601
        $this->loggerInstance->debug(5, $eap, "XMLOI:", "\n");
602
        if (in_array('OuterIdentity', $children)) {
603
            if ($outerId !== NULL) {
604
                $clientsidecredential->setChild('OuterIdentity', $outerId);
605
            }
606
        }
607
        $this->setClientSideRealm($clientsidecredential);
608
//        $clientsidecredential->setChild('EAPType', $eapParams['inner_methodID'] ? $eapParams['inner_methodID'] : $eapParams['methodID']);
609
610
        // Client Certificate
611
        if ($this->selectedEap == \core\common\EAP::EAPTYPE_SILVERBULLET) {
612
            $attr = $this->attributes;
613
            $outerId = \core\common\Entity::getAttributeValue($attr, 'internal:username', 0);
614
            $clientsidecredential->setChild('OuterIdentity', $outerId);
615
            $clientsidecredential->setChild('ClientCertificate', $this->getClientCertificate());
616
        }
617
        return $clientsidecredential;
618
    }
619
620
    /**
621
     * sets the EAP method
622
     * 
623
     * @param \devices\XML\Type $eaptype the EAP type XMLObject
624
     * @return \core\DeviceXMLmain
625
     */
626
    private function getEapMethod($eaptype)
627
    {
628
        $eapmethod = new \core\DeviceXMLmain();
629
        $eapmethod->setChild('Type', $eaptype);
630
        if (isset($this->VendorSpecific)) {
631
            $vendorspecifics = [];
632
            foreach ($this->VendorSpecific as $vs) {
633
                $vendorspecific = new \core\DeviceXMLmain();
634
                $vs['value']->addAttribute('xsi:noNamespaceSchemaLocation', "xxx.xsd");
635
                $vendorspecific->setValue($vs['value']);
636
                $vendorspecific->setAttributes(['vendor' => $vs['vendor']]);
637
                $vendorspecifics[] = $vendorspecific;
638
            }
639
            $eapmethod->setChild('VendorSpecific', $vendorspecifics);
640
        }
641
        return($eapmethod);
642
    }
643
644
    /**
645
     * determines the authentication method to use
646
     * 
647
     * @param array $eap the EAP methods, in array representation
648
     * @return \core\DeviceXMLmain
649
     */
650
    private function getAuthMethod($eap)
651
    {
652
        $authmethod = new \core\DeviceXMLmain();
653
        $eapParams = $this->getAuthenticationMethodParams($eap);
654
        $eaptype = new \core\DeviceXMLmain();
655
        $eaptype->setValue($eapParams['methodID']);
656
// Type
657
        $authmethod->setChild('EAPMethod', $this->getEapMethod($eaptype));
0 ignored issues
show
Bug introduced by
$eaptype of type core\DeviceXMLmain is incompatible with the type devices\XML\Type expected by parameter $eaptype of devices\eap_config\DeviceXML::getEapMethod(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

657
        $authmethod->setChild('EAPMethod', $this->getEapMethod(/** @scrutinizer ignore-type */ $eaptype));
Loading history...
658
659
// ServerSideCredentials
660
        $authmethod->setChild('ServerSideCredential', $this->getServerSideCredentials($eap['OUTER']));
661
662
// ClientSideCredentials
663
        $authmethod->setChild('ClientSideCredential', $this->getClientSideCredentials($eap['INNER']));
664
665
        if ($eapParams['inner_method']) {
666
            $authmethod->setChild('InnerAuthenticationMethod', $eapParams['inner_method']);
667
        }
668
        return $authmethod;
669
    }
670
    
671
    private function getAuthMethodsList() {
672
        $methodList = [];
673
        if ($this->allEaps) {
674
            $eapmethods = [];
675
            foreach ($this->attributes['all_eaps'] as $eap) {
676
                $eapRep = $eap->getArrayRep();
677
                if (in_array($eapRep, $this->supportedEapMethods)) {
678
                    $eapmethods[] = $eapRep;
679
                }
680
            }
681
        } else {
682
            $eapmethods = [$this->selectedEap];
683
        }
684
        foreach ($eapmethods as $eap) {
685
            $methodList[] = $this->getAuthMethod($eap);
686
        }
687
        return $methodList;
688
    }
689
    
690
    private $openRoamingToUArray;
691
    private $addORtou = false;
692
693
}
694