Passed
Pull Request — master (#23)
by
unknown
02:06
created

MetaLoader::getTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SimpleSAML\Module\metarefresh;
4
5
use Exception;
6
use RobRichards\XMLSecLibs\XMLSecurityDSig;
7
use SAML2\DOMDocumentFactory;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Metadata;
12
use SimpleSAML\Utils;
13
14
/**
15
 * @package SimpleSAMLphp
16
 */
17
class MetaLoader
18
{
19
    /** @var int|null */
20
    private ?int $expire;
21
22
    /** @var array */
23
    private array $metadata = [];
24
25
    /** @var object|null */
26
    private ?object $oldMetadataSrc;
27
28
    /** @var string|null */
29
    private ?string $stateFile = null;
30
31
    /** @var bool */
32
    private bool $changed = false;
33
34
    /** @var array */
35
    private array $state = [];
36
37
    /** @var array */
38
    private array $types = [
39
        'saml20-idp-remote',
40
        'saml20-sp-remote',
41
        'attributeauthority-remote'
42
    ];
43
44
45
    /**
46
     * Constructor
47
     *
48
     * @param int|null $expire
49
     * @param string|null  $stateFile
50
     * @param object|null  $oldMetadataSrc
51
     */
52
    public function __construct(int $expire = null, string $stateFile = null, object $oldMetadataSrc = null)
53
    {
54
        $this->expire = $expire;
55
        $this->oldMetadataSrc = $oldMetadataSrc;
56
        $this->stateFile = $stateFile;
57
58
        // Read file containing $state from disk
59
        /** @psalm-var array|null */
60
        $state = null;
61
        if (!is_null($stateFile) && is_readable($stateFile)) {
62
            include($stateFile);
63
        }
64
65
        if (!empty($state)) {
0 ignored issues
show
introduced by
The condition empty($state) is always false.
Loading history...
66
            $this->state = $state;
67
        }
68
    }
69
70
71
    /**
72
     * Get the types of entities that will be loaded.
73
     *
74
     * @return array The entity types allowed.
75
     */
76
    public function getTypes(): array
77
    {
78
        return $this->types;
79
    }
80
81
82
    /**
83
     * Set the types of entities that will be loaded.
84
     *
85
     * @param string|array $types Either a string with the name of one single type allowed, or an array with a list of
86
     * types. Pass an empty array to reset to all types of entities.
87
     */
88
    public function setTypes($types): void
89
    {
90
        if (!is_array($types)) {
91
            $types = [$types];
92
        }
93
        $this->types = $types;
94
    }
95
96
97
    /**
98
     * This function processes a SAML metadata file.
99
     *
100
     * @param array $source
101
     */
102
    public function loadSource(array $source): void
103
    {
104
        if (preg_match('@^https?://@i', $source['src'])) {
105
            // Build new HTTP context
106
            $context = $this->createContext($source);
107
108
            $httpUtils = new Utils\HTTP();
109
            // GET!
110
            try {
111
                /** @var array $response  We know this because we set the third parameter to `true` */
112
                $response = $httpUtils->fetch($source['src'], $context, true);
113
                list($data, $responseHeaders) = $response;
114
            } catch (Exception $e) {
115
                Logger::warning('metarefresh: ' . $e->getMessage());
116
            }
117
118
            // We have response headers, so the request succeeded
119
            if (!isset($responseHeaders)) {
120
                // No response headers, this means the request failed in some way, so re-use old data
121
                Logger::info('No response from ' . $source['src'] . ' - attempting to re-use cached metadata');
122
                $this->addCachedMetadata($source);
123
                return;
124
            } elseif (preg_match('@^HTTP/1\.[01]\s304\s@', $responseHeaders[0])) {
125
                // 304 response
126
                Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata');
127
                $this->addCachedMetadata($source);
128
                return;
129
            } elseif (!preg_match('@^HTTP/1\.[01]\s200\s@', $responseHeaders[0])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $responseHeaders does not seem to be defined for all execution paths leading up to this point.
Loading history...
130
                // Other error
131
                Logger::info('Error from ' . $source['src'] . ' - attempting to re-use cached metadata');
132
                $this->addCachedMetadata($source);
133
                return;
134
            }
135
        } else {
136
            // Local file.
137
            $data = file_get_contents($source['src']);
138
            $responseHeaders = null;
139
        }
140
141
        // Everything OK. Proceed.
142
        if (isset($source['conditionalGET']) && $source['conditionalGET']) {
143
            // Stale or no metadata, so a fresh copy
144
            Logger::debug('Downloaded fresh copy');
145
        }
146
147
        try {
148
            $entities = $this->loadXML($data, $source);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
149
        } catch (Exception $e) {
150
            Logger::notice(
151
                'XML parser error when parsing ' . $source['src'] . ' - attempting to re-use cached metadata'
152
            );
153
            Logger::debug('XML parser returned: ' . $e->getMessage());
154
            $this->addCachedMetadata($source);
155
            return;
156
        }
157
158
        foreach ($entities as $entity) {
159
            if (!$this->processBlacklist($entity, $source)) {
160
                continue;
161
            }
162
            if (!$this->processWhitelist($entity, $source)) {
163
                continue;
164
            }
165
            if (!$this->processAttributeWhitelist($entity, $source)) {
166
                continue;
167
            }
168
            if (!$this->processCertificates($entity, $source)) {
169
                continue;
170
            }
171
172
            $template = null;
173
            if (array_key_exists('template', $source)) {
174
                $template = $source['template'];
175
            }
176
177
            if (array_key_exists('regex-template', $source)) {
178
                foreach ($source['regex-template'] as $e => $t) {
179
                    if (preg_match($e, $entity->getEntityID())) {
180
                        if (is_array($template)) {
181
                            $template = array_merge($template, $t);
182
                        } else {
183
                            $template = $t;
184
                        }
185
                    }
186
                }
187
            }
188
189
            if (in_array('saml20-sp-remote', $this->types, true)) {
190
                $this->addMetadata($source['src'], $entity->getMetadata20SP(), 'saml20-sp-remote', $template);
191
            }
192
            if (in_array('saml20-idp-remote', $this->types, true)) {
193
                $this->addMetadata($source['src'], $entity->getMetadata20IdP(), 'saml20-idp-remote', $template);
194
            }
195
            if (in_array('attributeauthority-remote', $this->types, true)) {
196
                $attributeAuthorities = $entity->getAttributeAuthorities();
197
                if (!empty($attributeAuthorities)) {
198
                    $this->addMetadata(
199
                        $source['src'],
200
                        $attributeAuthorities,
201
                        'attributeauthority-remote',
202
                        $template
203
                    );
204
                }
205
            }
206
        }
207
208
        Logger::debug(sprintf('Found %d entities', count($entities)));
209
        $this->saveState($source, $responseHeaders);
210
    }
211
212
213
    /**
214
     * @param \SimpleSAML\Metadata\SAMLParser $entity
215
     * @param array $source
216
     * @bool
217
     */
218
    private function processCertificates(Metadata\SAMLParser $entity, array $source): bool
219
    {
220
        if (array_key_exists('certificates', $source) && ($source['certificates'] !== null)) {
221
            if (!$entity->validateSignature($source['certificates'])) {
222
                $entityId = $entity->getEntityId();
223
                Logger::notice(
224
                    'Skipping "' . $entityId . '" - could not verify signature using certificate.' . "\n"
225
                );
226
                return false;
227
            }
228
        }
229
        return true;
230
    }
231
232
233
    /**
234
     * @param \SimpleSAML\Metadata\SAMLParser $entity
235
     * @param array $source
236
     * @bool
237
     */
238
    private function processBlacklist(Metadata\SAMLParser $entity, array $source): bool
239
    {
240
        if (isset($source['blacklist'])) {
241
            if (!empty($source['blacklist']) && in_array($entity->getEntityId(), $source['blacklist'], true)) {
242
                Logger::info('Skipping "' . $entity->getEntityId() . '" - blacklisted.' . "\n");
243
                return false;
244
            }
245
        }
246
        return true;
247
    }
248
249
250
    /**
251
     * @param \SimpleSAML\Metadata\SAMLParser $entity
252
     * @param array $source
253
     * @bool
254
     */
255
    private function processWhitelist(Metadata\SAMLParser $entity, array $source): bool
256
    {
257
        if (isset($source['whitelist'])) {
258
            if (!empty($source['whitelist']) && !in_array($entity->getEntityId(), $source['whitelist'], true)) {
259
                Logger::info('Skipping "' . $entity->getEntityId() . '" - not in the whitelist.' . "\n");
260
                return false;
261
            }
262
        }
263
        return true;
264
    }
265
266
267
    /**
268
     * @param \SimpleSAML\Metadata\SAMLParser $entity
269
     * @param array $source
270
     * @bool
271
     */
272
    private function processAttributeWhitelist(Metadata\SAMLParser $entity, array $source): bool
273
    {
274
        /* Do we have an attribute whitelist? */
275
        if (isset($source['attributewhitelist']) && !empty($source['attributewhitelist'])) {
276
            $idpMetadata = $entity->getMetadata20IdP();
277
            if (!isset($idpMetadata)) {
278
                /* Skip non-IdPs */
279
                return false;
280
            }
281
282
            /**
283
             * Do a recursive comparison for each whitelist of the attributewhitelist with the idpMetadata for this
284
             * IdP. At least one of these whitelists should match
285
             */
286
            $match = false;
287
            foreach ($source['attributewhitelist'] as $whitelist) {
288
                if ($this->containsArray($whitelist, $idpMetadata)) {
289
                    $match = true;
290
                    break;
291
                }
292
            }
293
            if (!$match) {
294
                /* No match found -> next IdP */
295
                return false;
296
            }
297
            Logger::debug('Whitelisted entityID: ' . $entity->getEntityID());
298
        }
299
        return true;
300
    }
301
302
303
    /**
304
     * @param array|string $src
305
     * @param array|string $dst
306
     * @return bool
307
     *
308
     * Recursively checks whether array $dst contains array $src. If $src
309
     * is not an array, a literal comparison is being performed.
310
     */
311
    private function containsArray($src, $dst): bool
312
    {
313
        if (is_array($src)) {
314
            if (!is_array($dst)) {
315
                return false;
316
            }
317
            $dstKeys = array_keys($dst);
318
319
            /* Loop over all src keys */
320
            foreach ($src as $srcKey => $srcval) {
321
                if (is_int($srcKey)) {
322
                    /* key is number, check that the key appears as one
323
                     * of the destination keys: if not, then src has
324
                     * more keys than dst */
325
                    if (!array_key_exists($srcKey, $dst)) {
326
                        return false;
327
                    }
328
329
                    /* loop over dest keys, to find value: we don't know
330
                     * whether they are in the same order */
331
                    $submatch = false;
332
                    foreach ($dstKeys as $dstKey) {
333
                        if ($this->containsArray($srcval, $dst[$dstKey])) {
334
                            $submatch = true;
335
                            break;
336
                        }
337
                    }
338
                    if (!$submatch) {
339
                        return false;
340
                    }
341
                } else {
342
                    /* key is regexp: find matching keys */
343
                    /** @var array|false $matchingDstKeys */
344
                    $matchingDstKeys = preg_grep($srcKey, $dstKeys);
345
                    if (!is_array($matchingDstKeys)) {
346
                        return false;
347
                    }
348
349
                    $match = false;
350
                    foreach ($matchingDstKeys as $dstKey) {
351
                        if ($this->containsArray($srcval, $dst[$dstKey])) {
352
                            /* Found a match */
353
                            $match = true;
354
                            break;
355
                        }
356
                    }
357
                    if (!$match) {
358
                        /* none of the keys has a matching value */
359
                        return false;
360
                    }
361
                }
362
            }
363
            /* each src key/value matches */
364
            return true;
365
        } else {
366
            /* src is not an array, do a regexp match against dst */
367
            return (preg_match($src, strval($dst)) === 1);
368
        }
369
    }
370
371
    /**
372
     * Create HTTP context, with any available caches taken into account
373
     *
374
     * @param array $source
375
     * @return array
376
     */
377
    private function createContext(array $source): array
378
    {
379
        $config = Configuration::getInstance();
380
        $name = $config->getOptionalString('technicalcontact_name', null);
381
        $mail = $config->getOptionalString('technicalcontact_email', null);
382
383
        $rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n";
384
385
        if (isset($source['conditionalGET']) && $source['conditionalGET']) {
386
            if (array_key_exists($source['src'], $this->state)) {
387
                $sourceState = $this->state[$source['src']];
388
389
                if (isset($sourceState['last-modified'])) {
390
                    $rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n";
391
                }
392
393
                if (isset($sourceState['etag'])) {
394
                    $rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n";
395
                }
396
            }
397
        }
398
399
        return ['http' => ['header' => $rawheader]];
400
    }
401
402
    private function addCachedMetadata(array $source): void
403
    {
404
        if (!isset($this->oldMetadataSrc)) {
405
            Logger::info('No oldMetadataSrc, cannot re-use cached metadata');
406
            return;
407
        }
408
409
        foreach ($this->types as $type) {
410
            foreach ($this->oldMetadataSrc->getMetadataSet($type) as $entity) {
0 ignored issues
show
Bug introduced by
The method getMetadataSet() does not exist on null. ( Ignorable by Annotation )

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

410
            foreach ($this->oldMetadataSrc->/** @scrutinizer ignore-call */ getMetadataSet($type) as $entity) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
411
                if (array_key_exists('metarefresh:src', $entity)) {
412
                    if ($entity['metarefresh:src'] == $source['src']) {
413
                        $this->addMetadata($source['src'], $entity, $type);
414
                    }
415
                }
416
            }
417
        }
418
    }
419
420
421
    /**
422
     * Store caching state data for a source
423
     *
424
     * @param array $source
425
     * @param array|null $responseHeaders
426
     */
427
    private function saveState(array $source, ?array $responseHeaders): void
428
    {
429
        if (isset($source['conditionalGET']) && $source['conditionalGET']) {
430
            // Headers section
431
            if ($responseHeaders !== null) {
0 ignored issues
show
introduced by
The condition $responseHeaders !== null is always true.
Loading history...
432
                $candidates = ['last-modified', 'etag'];
433
434
                foreach ($candidates as $candidate) {
435
                    if (array_key_exists($candidate, $responseHeaders)) {
436
                        $this->state[$source['src']][$candidate] = $responseHeaders[$candidate];
437
                    }
438
                }
439
            }
440
441
            if (!empty($this->state[$source['src']])) {
442
                // Timestamp when this src was requested.
443
                $this->state[$source['src']]['requested_at'] = $this->getTime();
444
                $this->changed = true;
445
            }
446
        }
447
    }
448
449
450
    /**
451
     * Parse XML metadata and return entities
452
     *
453
     * @param string $data
454
     * @param array $source
455
     * @return \SimpleSAML\Metadata\SAMLParser[]
456
     * @throws \Exception
457
     */
458
    private function loadXML(string $data, array $source): array
459
    {
460
        try {
461
            $doc = DOMDocumentFactory::fromString($data);
462
        } catch (Exception $e) {
463
            throw new Exception('Failed to read XML from ' . $source['src']);
464
        }
465
        return Metadata\SAMLParser::parseDescriptorsElement($doc->documentElement);
466
    }
467
468
469
    /**
470
     * This function writes the state array back to disk
471
     *
472
     */
473
    public function writeState(): void
474
    {
475
        if ($this->changed && !is_null($this->stateFile)) {
476
            Logger::debug('Writing: ' . $this->stateFile);
477
            $sysUtils = new Utils\System();
478
            $sysUtils->writeFile(
479
                $this->stateFile,
480
                "<?php\n/* This file was generated by the metarefresh module at " . $this->getTime() . ".\n" .
481
                " Do not update it manually as it will get overwritten. */\n" .
482
                '$state = ' . var_export($this->state, true) . ";\n",
483
                0644
484
            );
485
        }
486
    }
487
488
489
    /**
490
     * This function writes the metadata to stdout.
491
     *
492
     */
493
    public function dumpMetadataStdOut(): void
494
    {
495
        foreach ($this->metadata as $category => $elements) {
496
            echo '/* The following data should be added to metadata/' . $category . '.php. */' . "\n";
497
498
            foreach ($elements as $m) {
499
                $filename = $m['filename'];
500
                $entityID = $m['metadata']['entityid'];
501
                $time = $this->getTime();
502
                echo "\n";
503
                echo '/* The following metadata was generated from ' . $filename . ' on ' . $time . '. */' . "\n";
504
                echo '$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n";
505
            }
506
507
            echo "\n";
508
            echo '/* End of data which should be added to metadata/' . $category . '.php. */' . "\n";
509
            echo "\n";
510
        }
511
    }
512
    private function addMetaData($filename, $metadata, $type, array $template = null)
513
    {
514
        if ($metadata === null) {
515
            return;
516
        }
517
518
        if (isset($template)) {
519
            $metadata = array_merge($metadata, $template);
520
        }
521
522
        $metadata['metarefresh:src'] = $filename;
523
        $entityId = $metadata['entityid'];
524
        if (!array_key_exists($type, $this->metadata)) {
525
            $this->metadata[$type] = [];
526
        }
527
528
        // If metadata with the same entity ID already exists, merge the keys arrays
529
        $existingMetadataIndex = $this->findMetadataIndexByEntityId($entityId);
530
        if ($existingMetadataIndex !== false) {
531
            $existingMetadata = $this->metadata[$type][$existingMetadataIndex]['metadata'];
532
            $existingKeys = $existingMetadata['keys'];
533
            $newKeys = $metadata['keys'];
534
            $mergedKeys = array_merge($existingKeys, $newKeys);
535
            $metadata['keys'] = $mergedKeys;
536
537
            // Replace the existing metadata with the merged metadata
538
            $this->metadata[$type][$existingMetadataIndex] = ['filename' => $filename, 'metadata' => $metadata];
539
        } else {
540
            // If expire is defined in constructor...
541
            if (!empty($this->expire)) {
542
                // If expire is already in metadata
543
                if (array_key_exists('expire', $metadata)) {
544
                    // Override metadata expire with more restrictive global config
545
                    if ($this->expire < $metadata['expire']) {
546
                        $metadata['expire'] = $this->expire;
547
                    }
548
549
                    // If expire is not already in metadata use global config
550
                } else {
551
                    $metadata['expire'] = $this->expire;
552
                }
553
            }
554
            $this->metadata[$type][] = ['filename' => $filename, 'metadata' => $metadata];
555
        }
556
    }
557
558
    private function findMetadataIndexByEntityId($entityId)
559
    {
560
        foreach ($this->metadata as $type => $metadataList) {
561
            foreach ($metadataList as $index => $metadata) {
562
                if ($metadata['metadata']['entityid'] === $entityId) {
563
                    return $index;
564
                }
565
            }
566
        }
567
        return false;
568
    }
569
570
571
    /**
572
     * This function writes the metadata to an ARP file
573
     *
574
     * @param \SimpleSAML\Configuration $config
575
     */
576
    public function writeARPfile(Configuration $config): void
577
    {
578
        $arpfile = $config->getString('arpfile');
579
        $types = ['saml20-sp-remote'];
580
581
        $md = [];
582
        foreach ($this->metadata as $category => $elements) {
583
            if (!in_array($category, $types, true)) {
584
                continue;
585
            }
586
            $md = array_merge($md, $elements);
587
        }
588
589
        // $metadata, $attributemap, $prefix, $suffix
590
        $arp = new ARP(
591
            $md,
592
            $config->getOptionalString('attributemap', ''),
593
            $config->getOptionalString('prefix', ''),
594
            $config->getOptionalString('suffix', '')
595
        );
596
597
598
        $arpxml = $arp->getXML();
599
600
        Logger::info('Writing ARP file: ' . $arpfile . "\n");
601
        file_put_contents($arpfile, $arpxml);
602
    }
603
604
605
    /**
606
     * This function writes the metadata to to separate files in the output directory.
607
     *
608
     * @param string $outputDir
609
     */
610
    public function writeMetadataFiles(string $outputDir): void
611
    {
612
        while (strlen($outputDir) > 0 && $outputDir[strlen($outputDir) - 1] === '/') {
613
            $outputDir = substr($outputDir, 0, strlen($outputDir) - 1);
614
        }
615
616
        if (!file_exists($outputDir)) {
617
            Logger::info('Creating directory: ' . $outputDir . "\n");
618
            $res = @mkdir($outputDir, 0777, true);
619
            if ($res === false) {
620
                throw new Exception('Error creating directory: ' . $outputDir);
621
            }
622
        }
623
624
        foreach ($this->types as $type) {
625
            $filename = $outputDir . '/' . $type . '.php';
626
627
            if (array_key_exists($type, $this->metadata)) {
628
                $elements = $this->metadata[$type];
629
                Logger::debug('Writing: ' . $filename);
630
631
                $content  = '<?php' . "\n" . '/* This file was generated by the metarefresh module at ';
632
                $content .= $this->getTime() . "\nDo not update it manually as it will get overwritten\n" . '*/' . "\n";
633
634
                foreach ($elements as $m) {
635
                    $entityID = $m['metadata']['entityid'];
636
                    $content .= "\n" . '$metadata[\'';
637
                    $content .= addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n";
638
                }
639
640
                $sysUtils = new Utils\System();
641
                $sysUtils->writeFile($filename, $content, 0644);
642
            } elseif (is_file($filename)) {
643
                if (unlink($filename)) {
644
                    Logger::debug('Deleting stale metadata file: ' . $filename);
645
                } else {
646
                    Logger::warning('Could not delete stale metadata file: ' . $filename);
647
                }
648
            }
649
        }
650
    }
651
652
653
    /**
654
     * Save metadata for loading with the 'serialize' metadata loader.
655
     *
656
     * @param string $outputDir  The directory we should save the metadata to.
657
     */
658
    public function writeMetadataSerialize(string $outputDir): void
659
    {
660
        $metaHandler = new Metadata\MetaDataStorageHandlerSerialize(['directory' => $outputDir]);
661
662
        // First we add all the metadata entries to the metadata handler
663
        foreach ($this->metadata as $set => $elements) {
664
            foreach ($elements as $m) {
665
                $entityId = $m['metadata']['entityid'];
666
667
                Logger::debug(
668
                    'metarefresh: Add metadata entry ' .
669
                    var_export($entityId, true) . ' in set ' . var_export($set, true) . '.'
670
                );
671
                $metaHandler->saveMetadata($entityId, $set, $m['metadata']);
672
            }
673
        }
674
    }
675
676
677
    /**
678
     * @return string
679
     */
680
    private function getTime(): string
681
    {
682
        // The current date, as a string
683
        return gmdate('Y-m-d\\TH:i:s\\Z');
684
    }
685
}
686