-
Notifications
You must be signed in to change notification settings - Fork 4
/
readMetadata.php
executable file
·678 lines (543 loc) · 22.2 KB
/
readMetadata.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<?php // Copyright (c) 2014, SWITCH
// This file is used to dynamically create the list of IdPs and SP to be
// displayed for the WAYF/DS service based on the federation metadata.
// Configuration parameters are specified in config.php.
//
// The list of Identity Providers can also be updated by running the script
// readMetadata.php periodically as web server user, e.g. with a cron entry like:
// 5 * * * * /usr/bin/php readMetadata.php > /dev/null
require_once('functions.php');
require_once('config.php');
// Init log file
openlog("SWITCHwayf.readMetadata.php", LOG_PID | LOG_PERROR, LOG_LOCAL0);
// Make sure this script is not accessed directly
if(isRunViaCLI()){
// Run in cli mode.
// Could be used for testing purposes or to facilitate startup confiduration.
// Results are dumped in $metadataIDPFile (see config.php)
// Set dummy server name
$_SERVER['SERVER_NAME'] = 'localhost';
// Set default config options
initConfigOptions();
// Load Identity Providers
require($IDPConfigFile);
// Check that $IDProviders exists
if (!isset($IDProviders) or !is_array($IDProviders)){
$IDProviders = array();
}
if (
!file_exists($metadataFile)
|| trim(@file_get_contents($metadataFile)) == '') {
exit ("Exiting: File ".$metadataFile." is empty or does not exist\n");
}
// Get an exclusive lock to generate our parsed IdP and SP files.
if (($lockFp = fopen($metadataLockFile, 'a+')) === false) {
$errorMsg = 'Could not open lock file '.$metadataLockFile;
die($errorMsg);
}
if (flock($lockFp, LOCK_EX) === false) {
$errorMsg = 'Could not lock file '.$metadataLockFile;
die($errorMsg);
}
echo 'Parsing metadata file '.$metadataFile."\n";
list($metadataIDProviders, $metadataSProviders) = parseMetadata($metadataFile, $defaultLanguage);
if ($UseDiscojuiceGeolocation) addDiscojuiceGeolocation($metadataIDProviders);
// If $metadataIDProviders is not FALSE, dump results in $metadataIDPFile.
if(is_array($metadataIDProviders)){
echo 'Dumping parsed Identity Providers to file '.$metadataIDPFile."\n";
dumpFile($metadataIDPFile, $metadataIDProviders, 'metadataIDProviders');
}
// If $metadataSProviders is not FALSE, dump results in $metadataSPFile.
if(is_array($metadataSProviders)){
echo 'Dumping parsed Service Providers to file '.$metadataSPFile."\n";
dumpFile($metadataSPFile, $metadataSProviders, 'metadataSProviders');
}
// Release the lock, and close.
flock($lockFp, LOCK_UN);
fclose($lockFp);
// If $metadataIDProviders is not FALSE, update $IDProviders and print the Identity Providers lists.
if(is_array($metadataIDProviders)){
echo 'Merging parsed Identity Providers with data from file '.$metadataFile."\n";
$IDProviders = mergeInfo($IDProviders, $metadataIDProviders, $SAML2MetaOverLocalConf, $includeLocalConfEntries);
/*echo "Printing parsed Identity Providers:\n";
print_r($metadataIDProviders);
echo "Printing effective Identity Providers:\n";
print_r($IDProviders);*/
}
// If $metadataSProviders is not FALSE, update $SProviders and print the list.
if(is_array($metadataSProviders)){
// Fow now copy the array by reference
$SProviders = &$metadataSProviders;
/*echo "Printing parsed Service Providers:\n";
print_r($metadataSProviders);*/
}
} elseif (isRunViaInclude()) {
// Open the metadata lock file.
if (($lockFp = fopen($metadataLockFile, 'a+')) === false) {
$errorMsg = 'Could not open lock file '.$metadataLockFile;
logError($errorMsg);
}
// Check that $IDProviders exists
if (!isset($IDProviders) or !is_array($IDProviders)){
$IDProviders = array();
}
// Run as included file
if(!file_exists($metadataIDPFile) or filemtime($metadataFile) > filemtime($metadataIDPFile)){
// Get an exclusive lock to regenerate the parsed files.
if ($lockFp !== false) {
if (flock($lockFp, LOCK_EX) === false) {
$errorMsg = 'Could not get exclusive lock on '.$metadataLockFile;
logError($errorMsg);
}
}
// Regenerate $metadataIDPFile.
list($metadataIDProviders, $metadataSProviders) = parseMetadata($metadataFile, $defaultLanguage);
if ($UseDiscojuiceGeolocation) addDiscojuiceGeolocation($metadataIDProviders);
// If $metadataIDProviders is not an array (parse error in metadata),
// $IDProviders from $IDPConfigFile will be used.
if(is_array($metadataIDProviders)){
dumpFile($metadataIDPFile, $metadataIDProviders, 'metadataIDProviders');
$IDProviders = mergeInfo($IDProviders, $metadataIDProviders, $SAML2MetaOverLocalConf, $includeLocalConfEntries);
}
if(is_array($metadataSProviders)){
dumpFile($metadataSPFile, $metadataSProviders, 'metadataSProviders');
require($metadataSPFile);
}
// Release the lock.
if ($lockFp !== false) {
flock($lockFp, LOCK_UN);
}
// Now merge IDPs from metadata and static file
$IDProviders = mergeInfo($IDProviders, $metadataIDProviders, $SAML2MetaOverLocalConf, $includeLocalConfEntries);
// Fow now copy the array by reference
$SProviders = &$metadataSProviders;
} elseif (file_exists($metadataIDPFile)){
// Get a shared lock to read the IdP and SP files
// generated from the metadata file.
if ($lockFp !== false) {
if (flock($lockFp, LOCK_SH) === false) {
$errorMsg = 'Could not lock file '.$metadataLockFile;
logError($errorMsg);
}
}
// Read SP and IDP files generated with metadata
require($metadataIDPFile);
if (file_exists($metadataSPFile)){
require($metadataSPFile);
}
// Release the lock.
if ($lockFp !== false) {
flock($lockFp, LOCK_UN);
}
// Now merge IDPs from metadata and static file
$IDProviders = mergeInfo($IDProviders, $metadataIDProviders, $SAML2MetaOverLocalConf, $includeLocalConfEntries);
// Fow now copy the array by reference
$SProviders = &$metadataSProviders;
}
// Close the metadata lock file.
if ($lockFp !== false) {
fclose($lockFp);
}
} else {
exit('No direct script access allowed');
}
closelog();
/*****************************************************************************/
// Function parseMetadata, parses metadata file and returns Array($IdPs, SPs) or
// Array(false, false) if error occurs while parsing metadata file
function parseMetadata($metadataFile, $defaultLanguage){
$metadataSProviders = array();
if(!file_exists($metadataFile)){
$errorMsg = 'File '.$metadataFile." does not exist";
if (isRunViaCLI()){
echo $errorMsg."\n";
} else {
logError($errorMsg);
}
return Array(false, false);
}
if(!is_readable($metadataFile)){
$errorMsg = 'File '.$metadataFile." cannot be read due to insufficient permissions";
if (isRunViaCLI()){
echo $errorMsg."\n";
} else {
logError($errorMsg);
}
return Array(false, false);
}
$CurrentXMLReaderNode = new XMLReader();
if(!$CurrentXMLReaderNode->open($metadataFile, null, LIBXML_PARSEHUGE | LIBXML_NOERROR | LIBXML_NOWARNING | 1)){
$errorMsg = 'Could not parse metadata file '.$metadataFile;
if (isRunViaCLI()){
echo $errorMsg."\n";
} else {
logError($errorMsg);
}
return Array(false, false);
}
// Process individual EntityDescriptors
while( $CurrentXMLReaderNode->read() ) {
if($CurrentXMLReaderNode->nodeType == XMLReader::ELEMENT && $CurrentXMLReaderNode->localName === 'EntityDescriptor') {
$entityID = $CurrentXMLReaderNode->getAttribute('entityID');
$EntityDescriptorXML = $CurrentXMLReaderNode->readOuterXML();
$EntityDescriptorDOM = new DOMDocument();
$EntityDescriptorDOM->loadXML($EntityDescriptorXML);
// Check role descriptors
foreach($EntityDescriptorDOM->documentElement->childNodes as $RoleDescriptor) {
$nodeName = $RoleDescriptor->localName;
switch($nodeName){
case 'IDPSSODescriptor':
$IDP = processIDPRoleDescriptor($RoleDescriptor);
if ($IDP){
$metadataIDProviders[$entityID] = $IDP;
}
break;
case 'SPSSODescriptor':
$SP = processSPRoleDescriptor($RoleDescriptor);
if ($SP){
$metadataSProviders[$entityID] = $SP;
} else {
$errorMsg = "Failed to load SP with entityID $entityID from metadata file $metadataFile";
if (isRunViaCLI()){
echo $errorMsg."\n";
} else {
logWarning($errorMsg);
}
}
break;
default:
}
}
}
}
// Output result
$infoMsg = "Successfully parsed metadata file ".$metadataFile. ". Found ".count($metadataIDProviders)." IdPs and ".count($metadataSProviders)." SPs";
if (isRunViaCLI()){
echo $infoMsg."\n";
} else {
logInfo($infoMsg);
}
return Array($metadataIDProviders, $metadataSProviders);
}
/******************************************************************************/
// Is this script run in CLI mode
function isRunViaCLI(){
return !isset($_SERVER['REMOTE_ADDR']);
}
/******************************************************************************/
// Is this script run in CLI mode
function isRunViaInclude(){
return basename($_SERVER['SCRIPT_NAME']) != 'readMetadata.php';
}
/******************************************************************************/
// Processes an IDPRoleDescriptor XML node and returns an IDP entry or false if
// something went wrong
function processIDPRoleDescriptor($IDPRoleDescriptorNode){
global $defaultLanguage;
$IDP = Array();
$Profiles = Array();
// Get SSO URL
$SSOServices = $IDPRoleDescriptorNode->getElementsByTagNameNS( 'urn:oasis:names:tc:SAML:2.0:metadata', 'SingleSignOnService' );
foreach( $SSOServices as $SSOService ){
$Profiles[$SSOService->getAttribute('Binding')] = $SSOService->getAttribute('Location');
}
// Set SAML1 SSO URL
if (isset($Profiles['urn:mace:shibboleth:1.0:profiles:AuthnRequest'])) {
$IDP['SSO'] = $Profiles['urn:mace:shibboleth:1.0:profiles:AuthnRequest'];
} else if ($Profiles['urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect']) {
$IDP['SSO'] = $Profiles['urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect'];
} else {
$IDP['SSO'] = 'https://no.saml1.or.saml2.sso.url.defined.com/error';
}
// First get MDUI name
$MDUIDisplayNames = getMDUIDisplayNames($IDPRoleDescriptorNode);
if (count($MDUIDisplayNames)){
$IDP['Name'] = current($MDUIDisplayNames);
}
foreach ($MDUIDisplayNames as $lang => $value){
$IDP[$lang]['Name'] = $value;
}
// Then try organization names
if (empty($IDP['Name'])){
$OrgnizationNames = getOrganizationNames($IDPRoleDescriptorNode);
$IDP['Name'] = current($OrgnizationNames);
foreach ($OrgnizationNames as $lang => $value){
$IDP[$lang]['Name'] = $value;
}
}
// As last resort, use entityID
if (empty($IDP['Name'])){
$IDP['Name'] = $IDPRoleDescriptorNode->parentNode->getAttribute('entityID');
}
// Set default name
if (isset($IDP[$defaultLanguage])){
$IDP['Name'] = $IDP[$defaultLanguage]['Name'];
} elseif (isset($IDP['en'])){
$IDP['Name'] = $IDP['en']['Name'];
}
// Get supported protocols
$protocols = $IDPRoleDescriptorNode->getAttribute('protocolSupportEnumeration');
$IDP['Protocols'] = $protocols;
// Get keywords
$MDUIKeywords = getMDUIKeywords($IDPRoleDescriptorNode);
foreach ($MDUIKeywords as $lang => $keywords){
$IDP[$lang]['Keywords'] = $keywords;
}
// Get Logos
$MDUILogos = getMDUILogos($IDPRoleDescriptorNode);
foreach ($MDUILogos as $Logo){
// Skip non-favicon logos
if ($Logo['Height'] != 16 || $Logo['Width'] != 16 ){
continue;
}
// Strip height and width
unset($Logo['Height']);
unset($Logo['Width']);
if ($Logo['Lang'] == ''){
unset($Logo['Lang']);
$IDP['Logo'] = $Logo;
} else {
$lang = $Logo['Lang'];
unset($Logo['Lang']);
$IDP[$lang]['Logo'] = $Logo;
}
}
// Get AttributeValue
$SAMLAttributeValues = getSAMLAttributeValues($IDPRoleDescriptorNode);
if ($SAMLAttributeValues){
$IDP['AttributeValue'] = $SAMLAttributeValues;
}
// Get IPHints
$MDUIIPHints = getMDUIIPHints($IDPRoleDescriptorNode);
if ($MDUIIPHints){
$IDP['IPHint'] = $MDUIIPHints;
}
// Get DomainHints
$MDUIDomainHints = getMDUIDomainHints($IDPRoleDescriptorNode);
if ($MDUIDomainHints){
$IDP['DomainHint'] = $MDUIDomainHints;
}
// Get GeolocationHints
$MDUIGeolocationHints = getMDUIGeolocationHints($IDPRoleDescriptorNode);
if ($MDUIGeolocationHints){
$IDP['GeolocationHint'] = $MDUIGeolocationHints;
}
return $IDP;
}
/******************************************************************************/
// Processes an SPRoleDescriptor XML node and returns an SP entry or false if
// something went wrong
function processSPRoleDescriptor($SPRoleDescriptorNode){
global $defaultLanguage;
$SP = Array();
// Get <idpdisc:DiscoveryResponse> extensions
$DResponses = $SPRoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol', 'DiscoveryResponse');
foreach( $DResponses as $DResponse ){
if ($DResponse->getAttribute('Binding') == 'urn:oasis:names:tc:SAML:profiles:SSO:idp-discovery-protocol'){
$SP['DSURL'][] = $DResponse->getAttribute('Location');
}
}
// First get MDUI name
$MDUIDisplayNames = getMDUIDisplayNames($SPRoleDescriptorNode);
if (count($MDUIDisplayNames)){
$SP['Name'] = current($MDUIDisplayNames);
}
foreach ($MDUIDisplayNames as $lang => $value){
$SP[$lang]['Name'] = $value;
}
// Then try attribute consuming service
if (empty($SP['Name'])){
$ConsumingServiceNames = getAttributeConsumingServiceNames($SPRoleDescriptorNode);
$SP['Name'] = current($ConsumingServiceNames);
foreach ($ConsumingServiceNames as $lang => $value){
$SP[$lang]['Name'] = $value;
}
}
// As last resort, use entityID
if (empty($SP['Name'])){
$SP['Name'] = $SPRoleDescriptorNode->parentNode->getAttribute('entityID');
}
// Set default name
if (isset($SP[$defaultLanguage])){
$SP['Name'] = $SP[$defaultLanguage]['Name'];
} elseif (isset($SP['en'])){
$SP['Name'] = $SP['en']['Name'];
}
// Get Assertion Consumer Services and store their hostnames
$ACServices = $SPRoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:2.0:metadata', 'AssertionConsumerService');
foreach( $ACServices as $ACService ){
$SP['ACURL'][] = $ACService->getAttribute('Location');
}
// Get supported protocols
$protocols = $SPRoleDescriptorNode->getAttribute('protocolSupportEnumeration');
$SP['Protocols'] = $protocols;
// Get keywords
$MDUIKeywords = getMDUIKeywords($SPRoleDescriptorNode);
foreach ($MDUIKeywords as $lang => $keywords){
$SP[$lang]['Keywords'] = $keywords;
}
return $SP;
}
/******************************************************************************/
// Function mergeInfo is used to create the effective $IDProviders array.
// For each IDP found in the metadata, merge the values from IDProvider.conf.php.
// If an IDP is found in IDProvider.conf as well as in metadata, use metadata
// information if $SAML2MetaOverLocalConf is true or else use IDProvider.conf data
function mergeInfo($IDProviders, $metadataIDProviders, $SAML2MetaOverLocalConf, $includeLocalConfEntries){
// If $includeLocalConfEntries parameter is set to true, mergeInfo() will also consider IDPs
// not listed in metadataIDProviders but defined in IDProviders file
// This is required if you need to add local exceptions over the federation metadata
$allIDPS = $metadataIDProviders;
$mergedArray = Array();
if ($includeLocalConfEntries) {
$allIDPS = array_merge($metadataIDProviders, $IDProviders);
}
foreach ($allIDPS as $allIDPsKey => $allIDPsEntry){
if(isset($IDProviders[$allIDPsKey])){
// Entry exists also in local IDProviders.conf.php
if (isset($metadataIDProviders[$allIDPsKey]) && is_array($metadataIDProviders[$allIDPsKey])) {
// Remove IdP if there is a removal rule in local IDProviders.conf.php
if (!is_array($IDProviders[$allIDPsKey])){
unset($metadataIDProviders[$allIDPsKey]);
continue;
}
// Entry exists in both IDProviders sources and is an array
if($SAML2MetaOverLocalConf){
// Metadata entry overwrite local conf
$mergedArray[$allIDPsKey] = array_merge($IDProviders[$allIDPsKey], $metadataIDProviders[$allIDPsKey]);
} else {
// Local conf overwrites metada entry
$mergedArray[$allIDPsKey] = array_merge($metadataIDProviders[$allIDPsKey], $IDProviders[$allIDPsKey]);
}
} else {
// Entry only exists in local IDProviders file
$mergedArray[$allIDPsKey] = $IDProviders[$allIDPsKey];
}
} else {
// Entry doesnt exist in in local IDProviders.conf.php
$mergedArray[$allIDPsKey] = $metadataIDProviders[$allIDPsKey];
}
}
return $mergedArray;
}
/******************************************************************************/
// Get MD Display Names from RoleDescriptor
function getMDUIDisplayNames($RoleDescriptorNode){
$Entity = Array();
$MDUIDisplayNames = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:metadata:ui', 'DisplayName');
foreach( $MDUIDisplayNames as $MDUIDisplayName ){
$lang = $MDUIDisplayName->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'lang')->nodeValue;
$Entity[$lang] = trimToSingleLine($MDUIDisplayName->nodeValue);
}
return $Entity;
}
/******************************************************************************/
// Get MD Keywords from RoleDescriptor
function getMDUIKeywords($RoleDescriptorNode){
$Entity = Array();
$MDUIKeywords = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:metadata:ui', 'Keywords');
foreach( $MDUIKeywords as $MDUIKeywordEntry ){
$lang = $MDUIKeywordEntry->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'lang')->nodeValue;
$Entity[$lang] = trimToSingleLine($MDUIKeywordEntry->nodeValue);
}
return $Entity;
}
/******************************************************************************/
// Get MD Logos from RoleDescriptor. Prefer the favicon logos
function getMDUILogos($RoleDescriptorNode){
$Logos = Array();
$MDUILogos = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:metadata:ui', 'Logo');
foreach( $MDUILogos as $MDUILogoEntry ){
$Logo = Array();
$Logo['URL'] = trimToSingleLine($MDUILogoEntry->nodeValue);
$Logo['Height'] = ($MDUILogoEntry->getAttribute('height') != '') ? trimToSingleLine($MDUILogoEntry->getAttribute('height')) : '16';
$Logo['Width'] = ($MDUILogoEntry->getAttribute('width') != '') ? trimToSingleLine($MDUILogoEntry->getAttribute('width')) : '16';
$Logo['Lang'] = ($MDUILogoEntry->getAttribute('lang') != '') ? trimToSingleLine($MDUILogoEntry->getAttribute('lang')) : '';
$Logos[] = $Logo;
}
return $Logos;
}
/******************************************************************************/
// Get MD Attribute Value(kind) from RoleDescriptor
function getSAMLAttributeValues($RoleDescriptorNode){
$Entity = Array();
$SAMLAttributeValues = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:2.0:assertion', 'AttributeValue');
foreach( $SAMLAttributeValues as $SAMLAttributeValuesEntry ){
$Entity[] = trimToSingleLine($SAMLAttributeValuesEntry->nodeValue);
}
return $Entity;
}
/******************************************************************************/
// Get MD IP Address Hints from RoleDescriptor
function getMDUIIPHints($RoleDescriptorNode){
$Entity = Array();
$MDUIIPHints = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:metadata:ui', 'IPHint');
foreach( $MDUIIPHints as $MDUIIPHintEntry ){
if (preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}$/", trimToSingleLine($MDUIIPHintEntry->nodeValue), $splitIP)){
$Entity[] = trimToSingleLine($splitIP[0]);
} elseif (preg_match("/^.*\:.*\/[0-9]{1,2}$/", trimToSingleLine($MDUIIPHintEntry->nodeValue), $splitIP)){
$Entity[] = trimToSingleLine($splitIP[0]);
}
}
return $Entity;
}
/******************************************************************************/
// Get MD Domain Hints from RoleDescriptor
function getMDUIDomainHints($RoleDescriptorNode){
$Entity = Array();
$MDUIDomainHints = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:metadata:ui', 'DomainHint');
foreach( $MDUIDomainHints as $MDUIDomainHintEntry ){
$Entity[] = trimToSingleLine($MDUIDomainHintEntry->nodeValue);
}
return $Entity;
}
/******************************************************************************/
// Get MD Geolocation Hints from RoleDescriptor
function getMDUIGeolocationHints($RoleDescriptorNode){
$Entity = Array();
$MDUIGeolocationHints = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:metadata:ui', 'GeolocationHint');
foreach( $MDUIGeolocationHints as $MDUIGeolocationHintEntry ){
if (preg_match("/^geo:([0-9]+\.{0,1}[0-9]*,[0-9]+\.{0,1}[0-9]*)$/", trimToSingleLine($MDUIGeolocationHintEntry->nodeValue), $splitGeo)){
$Entity[] = trimToSingleLine($splitGeo[1]);
}
}
return $Entity;
}
/******************************************************************************/
// Get Organization Names from RoleDescriptor
function getOrganizationNames($RoleDescriptorNode){
$Entity = Array();
$Orgnization = $RoleDescriptorNode->parentNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:2.0:metadata', 'Organization' )->item(0);
if ($Orgnization){
$DisplayNames = $Orgnization->getElementsByTagNameNS('urn:oasis:names:tc:SAML:2.0:metadata', 'OrganizationDisplayName');
foreach ($DisplayNames as $DisplayName){
$lang = $DisplayName->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'lang')->nodeValue;
$Entity[$lang] = trimToSingleLine($DisplayName->nodeValue);
}
}
return $Entity;
}
/******************************************************************************/
// Get Attribute Consuming Service
function getAttributeConsumingServiceNames($RoleDescriptorNode){
$Entity = Array();
$ServiceNames = $RoleDescriptorNode->getElementsByTagNameNS('urn:oasis:names:tc:SAML:2.0:metadata', 'ServiceName' );
foreach ($ServiceNames as $ServiceName){
$lang = $ServiceName->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'lang')->nodeValue;
$Entity[$lang] = trimToSingleLine($ServiceName->nodeValue);
}
return $Entity;
}
/******************************************************************************/
// Get GeolocationHint from discojuice feed (rely on discojuiceGeolocation/update.sh run in a cron)
function addDiscojuiceGeolocation(&$metadataIDProviders) {
foreach (glob("Geo-SWITCHwayf/discojuice/*.json") as $file) {
foreach (json_decode(file_get_contents($file)) as $e) {
if (!isset($metadataIDProviders[$e->entityID])) continue;
$IDP = &$metadataIDProviders[$e->entityID];
if (isset($IDP['GeolocationHint'])) continue;
$IDP['GeolocationHint'] = isset($e->geo)?$e->geo->lat . "," . $e->geo->lon:null;
}
}
}
?>