1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace SimpleSAML\Module\metarefresh; |
||||
6 | |||||
7 | use Exception; |
||||
8 | use SimpleSAML\Configuration; |
||||
9 | use SimpleSAML\Logger; |
||||
10 | use SimpleSAML\Metadata; |
||||
11 | use SimpleSAML\Utils; |
||||
12 | use SimpleSAML\XML\DOMDocumentFactory; |
||||
13 | use Symfony\Component\VarExporter\VarExporter; |
||||
14 | |||||
15 | /** |
||||
16 | * @package SimpleSAMLphp |
||||
17 | */ |
||||
18 | class MetaLoader |
||||
19 | { |
||||
20 | /** @var int|null */ |
||||
21 | private ?int $expire; |
||||
22 | |||||
23 | /** @var array */ |
||||
24 | private array $metadata = []; |
||||
25 | |||||
26 | /** @var object|null */ |
||||
27 | private ?object $oldMetadataSrc; |
||||
28 | |||||
29 | /** @var string|null */ |
||||
30 | private ?string $stateFile = null; |
||||
31 | |||||
32 | /** @var bool */ |
||||
33 | private bool $changed = false; |
||||
34 | |||||
35 | /** @var array */ |
||||
36 | private array $state = []; |
||||
37 | |||||
38 | /** @var array */ |
||||
39 | private array $types = [ |
||||
40 | 'saml20-idp-remote', |
||||
41 | 'saml20-sp-remote', |
||||
42 | 'attributeauthority-remote', |
||||
43 | ]; |
||||
44 | |||||
45 | |||||
46 | /** |
||||
47 | * Constructor |
||||
48 | * |
||||
49 | * @param int|null $expire |
||||
50 | * @param string|null $stateFile |
||||
51 | * @param object|null $oldMetadataSrc |
||||
52 | */ |
||||
53 | public function __construct(?int $expire = null, ?string $stateFile = null, ?object $oldMetadataSrc = null) |
||||
54 | { |
||||
55 | $this->expire = $expire; |
||||
56 | $this->oldMetadataSrc = $oldMetadataSrc; |
||||
57 | $this->stateFile = $stateFile; |
||||
58 | |||||
59 | // Read file containing $state from disk |
||||
60 | /** @psalm-var array|null */ |
||||
61 | $state = null; |
||||
62 | if (!is_null($stateFile) && is_readable($stateFile)) { |
||||
63 | include($stateFile); |
||||
64 | } |
||||
65 | |||||
66 | if (!empty($state)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
67 | $this->state = $state; |
||||
68 | } |
||||
69 | } |
||||
70 | |||||
71 | |||||
72 | /** |
||||
73 | * Get the types of entities that will be loaded. |
||||
74 | * |
||||
75 | * @return array The entity types allowed. |
||||
76 | */ |
||||
77 | public function getTypes(): array |
||||
78 | { |
||||
79 | return $this->types; |
||||
80 | } |
||||
81 | |||||
82 | |||||
83 | /** |
||||
84 | * Set the types of entities that will be loaded. |
||||
85 | * |
||||
86 | * @param string|array $types Either a string with the name of one single type allowed, or an array with a list of |
||||
87 | * types. Pass an empty array to reset to all types of entities. |
||||
88 | */ |
||||
89 | public function setTypes($types): void |
||||
90 | { |
||||
91 | if (!is_array($types)) { |
||||
92 | $types = [$types]; |
||||
93 | } |
||||
94 | $this->types = $types; |
||||
95 | } |
||||
96 | |||||
97 | |||||
98 | /** |
||||
99 | * This function processes a SAML metadata file. |
||||
100 | * |
||||
101 | * @param array $source |
||||
102 | */ |
||||
103 | public function loadSource(array $source): void |
||||
104 | { |
||||
105 | if (preg_match('@^https?://@i', $source['src'])) { |
||||
106 | // Build new HTTP context |
||||
107 | $context = $this->createContext($source); |
||||
108 | |||||
109 | $httpUtils = new Utils\HTTP(); |
||||
110 | // GET! |
||||
111 | try { |
||||
112 | /** @var array $response We know this because we set the third parameter to `true` */ |
||||
113 | $response = $httpUtils->fetch($source['src'], $context, true); |
||||
114 | list($data, $responseHeaders) = $response; |
||||
115 | } catch (Exception $e) { |
||||
116 | Logger::warning('metarefresh: ' . $e->getMessage()); |
||||
117 | } |
||||
118 | |||||
119 | // We have response headers, so the request succeeded |
||||
120 | if (!isset($responseHeaders)) { |
||||
121 | // No response headers, this means the request failed in some way, so re-use old data |
||||
122 | Logger::info('No response from ' . $source['src'] . ' - attempting to re-use cached metadata'); |
||||
123 | $this->addCachedMetadata($source); |
||||
124 | return; |
||||
125 | } elseif (preg_match('@^HTTP/(2\.0|1\.[01])\s304\s@', $responseHeaders[0])) { |
||||
126 | // 304 response |
||||
127 | Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata'); |
||||
128 | $this->addCachedMetadata($source); |
||||
129 | return; |
||||
130 | } elseif (!preg_match('@^HTTP/(2\.0|1\.[01])\s200\s@', $responseHeaders[0])) { |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
131 | // Other error |
||||
132 | Logger::info('Error from ' . $source['src'] . ' - attempting to re-use cached metadata'); |
||||
133 | $this->addCachedMetadata($source); |
||||
134 | return; |
||||
135 | } |
||||
136 | } else { |
||||
137 | // Local file. |
||||
138 | $data = file_get_contents($source['src']); |
||||
139 | $responseHeaders = null; |
||||
140 | } |
||||
141 | |||||
142 | // Everything OK. Proceed. |
||||
143 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||||
144 | // Stale or no metadata, so a fresh copy |
||||
145 | Logger::debug('Downloaded fresh copy'); |
||||
146 | } |
||||
147 | |||||
148 | try { |
||||
149 | $entities = $this->loadXML($data, $source); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
150 | } catch (Exception $e) { |
||||
151 | Logger::notice( |
||||
152 | 'XML parser error when parsing ' . $source['src'] . ' - attempting to re-use cached metadata', |
||||
153 | ); |
||||
154 | Logger::debug('XML parser returned: ' . $e->getMessage()); |
||||
155 | $this->addCachedMetadata($source); |
||||
156 | return; |
||||
157 | } |
||||
158 | |||||
159 | foreach ($entities as $entity) { |
||||
160 | if (!$this->processBlacklist($entity, $source)) { |
||||
161 | continue; |
||||
162 | } |
||||
163 | if (!$this->processWhitelist($entity, $source)) { |
||||
164 | continue; |
||||
165 | } |
||||
166 | if (!$this->processAttributeWhitelist($entity, $source)) { |
||||
167 | continue; |
||||
168 | } |
||||
169 | if (!$this->processCertificates($entity, $source)) { |
||||
170 | continue; |
||||
171 | } |
||||
172 | |||||
173 | $template = null; |
||||
174 | if (array_key_exists('template', $source)) { |
||||
175 | $template = $source['template']; |
||||
176 | } |
||||
177 | |||||
178 | if (array_key_exists('regex-template', $source)) { |
||||
179 | foreach ($source['regex-template'] as $e => $t) { |
||||
180 | if (preg_match($e, $entity->getEntityID())) { |
||||
181 | if (is_array($template)) { |
||||
182 | $template = array_merge($template, $t); |
||||
183 | } else { |
||||
184 | $template = $t; |
||||
185 | } |
||||
186 | } |
||||
187 | } |
||||
188 | } |
||||
189 | |||||
190 | if (in_array('saml20-sp-remote', $this->types, true)) { |
||||
191 | $this->addMetadata($source['src'], $entity->getMetadata20SP(), 'saml20-sp-remote', $template); |
||||
192 | } |
||||
193 | if (in_array('saml20-idp-remote', $this->types, true)) { |
||||
194 | $this->addMetadata($source['src'], $entity->getMetadata20IdP(), 'saml20-idp-remote', $template); |
||||
195 | } |
||||
196 | if (in_array('attributeauthority-remote', $this->types, true)) { |
||||
197 | $attributeAuthorities = $entity->getAttributeAuthorities(); |
||||
198 | if (count($attributeAuthorities) && !empty($attributeAuthorities[0])) { |
||||
199 | $this->addMetadata( |
||||
200 | $source['src'], |
||||
201 | $attributeAuthorities[0], |
||||
202 | 'attributeauthority-remote', |
||||
203 | $template, |
||||
204 | ); |
||||
205 | } |
||||
206 | } |
||||
207 | } |
||||
208 | |||||
209 | Logger::debug(sprintf('Found %d entities', count($entities))); |
||||
210 | $this->saveState($source, $responseHeaders); |
||||
211 | } |
||||
212 | |||||
213 | |||||
214 | /** |
||||
215 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||||
216 | * @param array $source |
||||
217 | * @return bool |
||||
218 | */ |
||||
219 | private function processCertificates(Metadata\SAMLParser $entity, array $source): bool |
||||
220 | { |
||||
221 | if (array_key_exists('certificates', $source) && ($source['certificates'] !== null)) { |
||||
222 | if (!$entity->validateSignature($source['certificates'])) { |
||||
223 | $entityId = $entity->getEntityId(); |
||||
224 | Logger::notice( |
||||
225 | 'Skipping "' . $entityId . '" - could not verify signature using certificate.' . "\n", |
||||
226 | ); |
||||
227 | return false; |
||||
228 | } |
||||
229 | } |
||||
230 | return true; |
||||
231 | } |
||||
232 | |||||
233 | |||||
234 | /** |
||||
235 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||||
236 | * @param array $source |
||||
237 | * @return bool |
||||
238 | */ |
||||
239 | private function processBlacklist(Metadata\SAMLParser $entity, array $source): bool |
||||
240 | { |
||||
241 | if (isset($source['blacklist'])) { |
||||
242 | if (!empty($source['blacklist']) && in_array($entity->getEntityId(), $source['blacklist'], true)) { |
||||
243 | Logger::info('Skipping "' . $entity->getEntityId() . '" - blacklisted.' . "\n"); |
||||
244 | return false; |
||||
245 | } |
||||
246 | } |
||||
247 | return true; |
||||
248 | } |
||||
249 | |||||
250 | |||||
251 | /** |
||||
252 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||||
253 | * @param array $source |
||||
254 | * @return bool |
||||
255 | */ |
||||
256 | private function processWhitelist(Metadata\SAMLParser $entity, array $source): bool |
||||
257 | { |
||||
258 | if (isset($source['whitelist'])) { |
||||
259 | if (!empty($source['whitelist']) && !in_array($entity->getEntityId(), $source['whitelist'], true)) { |
||||
260 | Logger::info('Skipping "' . $entity->getEntityId() . '" - not in the whitelist.' . "\n"); |
||||
261 | return false; |
||||
262 | } |
||||
263 | } |
||||
264 | return true; |
||||
265 | } |
||||
266 | |||||
267 | |||||
268 | /** |
||||
269 | * @param \SimpleSAML\Metadata\SAMLParser $entity |
||||
270 | * @param array $source |
||||
271 | * @return bool |
||||
272 | */ |
||||
273 | private function processAttributeWhitelist(Metadata\SAMLParser $entity, array $source): bool |
||||
274 | { |
||||
275 | /* Do we have an attribute whitelist? */ |
||||
276 | if (isset($source['attributewhitelist']) && !empty($source['attributewhitelist'])) { |
||||
277 | $idpMetadata = $entity->getMetadata20IdP(); |
||||
278 | if (!isset($idpMetadata)) { |
||||
279 | /* Skip non-IdPs */ |
||||
280 | return false; |
||||
281 | } |
||||
282 | |||||
283 | /** |
||||
284 | * Do a recursive comparison for each whitelist of the attributewhitelist with the idpMetadata for this |
||||
285 | * IdP. At least one of these whitelists should match |
||||
286 | */ |
||||
287 | $match = false; |
||||
288 | foreach ($source['attributewhitelist'] as $whitelist) { |
||||
289 | if ($this->containsArray($whitelist, $idpMetadata)) { |
||||
290 | $match = true; |
||||
291 | break; |
||||
292 | } |
||||
293 | } |
||||
294 | |||||
295 | if (!$match) { |
||||
296 | /* No match found -> next IdP */ |
||||
297 | return false; |
||||
298 | } |
||||
299 | Logger::debug('Whitelisted entityID: ' . $entity->getEntityID()); |
||||
300 | } |
||||
301 | return true; |
||||
302 | } |
||||
303 | |||||
304 | |||||
305 | /** |
||||
306 | * @param array|string $src |
||||
307 | * @param array|string $dst |
||||
308 | * @return bool |
||||
309 | * |
||||
310 | * Recursively checks whether array $dst contains array $src. If $src |
||||
311 | * is not an array, a literal comparison is being performed. |
||||
312 | */ |
||||
313 | private function containsArray($src, $dst): bool |
||||
314 | { |
||||
315 | if (is_array($src)) { |
||||
316 | if (!is_array($dst)) { |
||||
317 | return false; |
||||
318 | } |
||||
319 | $dstKeys = array_keys($dst); |
||||
320 | |||||
321 | /* Loop over all src keys */ |
||||
322 | foreach ($src as $srcKey => $srcval) { |
||||
323 | if (is_int($srcKey)) { |
||||
324 | /* key is number, check that the key appears as one |
||||
325 | * of the destination keys: if not, then src has |
||||
326 | * more keys than dst */ |
||||
327 | if (!array_key_exists($srcKey, $dst)) { |
||||
328 | return false; |
||||
329 | } |
||||
330 | |||||
331 | /* loop over dest keys, to find value: we don't know |
||||
332 | * whether they are in the same order */ |
||||
333 | $submatch = false; |
||||
334 | foreach ($dstKeys as $dstKey) { |
||||
335 | if ($this->containsArray($srcval, $dst[$dstKey])) { |
||||
336 | $submatch = true; |
||||
337 | break; |
||||
338 | } |
||||
339 | } |
||||
340 | if (!$submatch) { |
||||
341 | return false; |
||||
342 | } |
||||
343 | } else { |
||||
344 | /* key is regexp: find matching keys */ |
||||
345 | /** @var array|false $matchingDstKeys */ |
||||
346 | $matchingDstKeys = preg_grep($srcKey, $dstKeys); |
||||
347 | if (!is_array($matchingDstKeys)) { |
||||
348 | return false; |
||||
349 | } |
||||
350 | |||||
351 | $match = false; |
||||
352 | foreach ($matchingDstKeys as $dstKey) { |
||||
353 | if ($this->containsArray($srcval, $dst[$dstKey])) { |
||||
354 | /* Found a match */ |
||||
355 | $match = true; |
||||
356 | break; |
||||
357 | } |
||||
358 | } |
||||
359 | if (!$match) { |
||||
360 | /* none of the keys has a matching value */ |
||||
361 | return false; |
||||
362 | } |
||||
363 | } |
||||
364 | } |
||||
365 | /* each src key/value matches */ |
||||
366 | return true; |
||||
367 | } else { |
||||
368 | /* src is not an array, do a regexp match against dst */ |
||||
369 | return (preg_match($src, strval($dst)) === 1); |
||||
370 | } |
||||
371 | } |
||||
372 | |||||
373 | /** |
||||
374 | * Create HTTP context, with any available caches taken into account |
||||
375 | * |
||||
376 | * @param array $source |
||||
377 | * @return array |
||||
378 | */ |
||||
379 | private function createContext(array $source): array |
||||
380 | { |
||||
381 | $config = Configuration::getInstance(); |
||||
382 | $name = $config->getOptionalString('technicalcontact_name', null); |
||||
383 | $mail = $config->getOptionalString('technicalcontact_email', null); |
||||
384 | |||||
385 | $rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n"; |
||||
386 | |||||
387 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||||
388 | if (array_key_exists($source['src'], $this->state)) { |
||||
389 | $sourceState = $this->state[$source['src']]; |
||||
390 | |||||
391 | if (isset($sourceState['last-modified'])) { |
||||
392 | $rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n"; |
||||
393 | } |
||||
394 | |||||
395 | if (isset($sourceState['etag'])) { |
||||
396 | $rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n"; |
||||
397 | } |
||||
398 | } |
||||
399 | } |
||||
400 | |||||
401 | return ['http' => ['header' => $rawheader]]; |
||||
402 | } |
||||
403 | |||||
404 | private function addCachedMetadata(array $source): void |
||||
405 | { |
||||
406 | if (!isset($this->oldMetadataSrc)) { |
||||
407 | Logger::info('No oldMetadataSrc, cannot re-use cached metadata'); |
||||
408 | return; |
||||
409 | } |
||||
410 | |||||
411 | foreach ($this->types as $type) { |
||||
412 | foreach ($this->oldMetadataSrc->getMetadataSet($type) as $entity) { |
||||
0 ignored issues
–
show
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
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. ![]() |
|||||
413 | if (array_key_exists('metarefresh:src', $entity)) { |
||||
414 | if ($entity['metarefresh:src'] == $source['src']) { |
||||
415 | $this->addMetadata($source['src'], $entity, $type); |
||||
416 | } |
||||
417 | } |
||||
418 | } |
||||
419 | } |
||||
420 | } |
||||
421 | |||||
422 | |||||
423 | /** |
||||
424 | * Store caching state data for a source |
||||
425 | * |
||||
426 | * @param array $source |
||||
427 | * @param array|null $responseHeaders |
||||
428 | */ |
||||
429 | private function saveState(array $source, ?array $responseHeaders): void |
||||
430 | { |
||||
431 | if (isset($source['conditionalGET']) && $source['conditionalGET']) { |
||||
432 | // Headers section |
||||
433 | if ($responseHeaders !== null) { |
||||
0 ignored issues
–
show
|
|||||
434 | $candidates = ['last-modified', 'etag']; |
||||
435 | |||||
436 | foreach ($candidates as $candidate) { |
||||
437 | if (array_key_exists($candidate, $responseHeaders)) { |
||||
438 | $this->state[$source['src']][$candidate] = $responseHeaders[$candidate]; |
||||
439 | } |
||||
440 | } |
||||
441 | } |
||||
442 | |||||
443 | if (!empty($this->state[$source['src']])) { |
||||
444 | // Timestamp when this src was requested. |
||||
445 | $this->state[$source['src']]['requested_at'] = $this->getTime(); |
||||
446 | $this->changed = true; |
||||
447 | } |
||||
448 | } |
||||
449 | } |
||||
450 | |||||
451 | |||||
452 | /** |
||||
453 | * Parse XML metadata and return entities |
||||
454 | * |
||||
455 | * @param string $data |
||||
456 | * @param array $source |
||||
457 | * @return \SimpleSAML\Metadata\SAMLParser[] |
||||
458 | * @throws \Exception |
||||
459 | */ |
||||
460 | private function loadXML(string $data, array $source): array |
||||
461 | { |
||||
462 | try { |
||||
463 | $doc = DOMDocumentFactory::fromString($data, DOMDocumentFactory::DEFAULT_OPTIONS | LIBXML_PARSEHUGE); |
||||
464 | } catch (Exception $e) { |
||||
465 | throw new Exception('Failed to read XML from ' . $source['src']); |
||||
466 | } |
||||
467 | return Metadata\SAMLParser::parseDescriptorsElement($doc->documentElement); |
||||
468 | } |
||||
469 | |||||
470 | |||||
471 | /** |
||||
472 | * This function writes the state array back to disk |
||||
473 | * |
||||
474 | */ |
||||
475 | public function writeState(): void |
||||
476 | { |
||||
477 | if ($this->changed && !is_null($this->stateFile)) { |
||||
478 | Logger::debug('Writing: ' . $this->stateFile); |
||||
479 | $sysUtils = new Utils\System(); |
||||
480 | $sysUtils->writeFile( |
||||
481 | $this->stateFile, |
||||
482 | "<?php\n/* This file was generated by the metarefresh module at " . $this->getTime() . ".\n" . |
||||
483 | " Do not update it manually as it will get overwritten. */\n" . |
||||
484 | '$state = ' . var_export($this->state, true) . ";\n", |
||||
485 | 0644, |
||||
486 | ); |
||||
487 | } |
||||
488 | } |
||||
489 | |||||
490 | |||||
491 | /** |
||||
492 | * This function writes the metadata to stdout. |
||||
493 | * |
||||
494 | */ |
||||
495 | public function dumpMetadataStdOut(): void |
||||
496 | { |
||||
497 | foreach ($this->metadata as $category => $elements) { |
||||
498 | echo '/* The following data should be added to metadata/' . $category . '.php. */' . "\n"; |
||||
499 | |||||
500 | foreach ($elements as $m) { |
||||
501 | $filename = $m['filename']; |
||||
502 | $entityID = $m['metadata']['entityid']; |
||||
503 | $time = $this->getTime(); |
||||
504 | echo "\n"; |
||||
505 | echo '/* The following metadata was generated from ' . $filename . ' on ' . $time . '. */' . "\n"; |
||||
506 | echo '$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n"; |
||||
507 | } |
||||
508 | |||||
509 | echo "\n"; |
||||
510 | echo '/* End of data which should be added to metadata/' . $category . '.php. */' . "\n"; |
||||
511 | echo "\n"; |
||||
512 | } |
||||
513 | } |
||||
514 | |||||
515 | |||||
516 | /** |
||||
517 | * This function adds metadata from the specified file to the list of metadata. |
||||
518 | * This function will return without making any changes if $metadata is NULL. |
||||
519 | * |
||||
520 | * @param string $filename The filename the metadata comes from. |
||||
521 | * @param \SAML2\XML\md\AttributeAuthorityDescriptor[]|null $metadata The metadata. |
||||
522 | * @param string $type The metadata type. |
||||
523 | * @param array|null $template The template. |
||||
524 | */ |
||||
525 | private function addMetadata(string $filename, ?array $metadata, string $type, ?array $template = null): void |
||||
526 | { |
||||
527 | if ($metadata === null) { |
||||
0 ignored issues
–
show
|
|||||
528 | return; |
||||
529 | } |
||||
530 | |||||
531 | if (isset($template)) { |
||||
532 | $metadata = array_merge($metadata, $template); |
||||
533 | } |
||||
534 | |||||
535 | $metadata['metarefresh:src'] = $filename; |
||||
536 | if (!array_key_exists($type, $this->metadata)) { |
||||
537 | $this->metadata[$type] = []; |
||||
538 | } |
||||
539 | |||||
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 | /** |
||||
559 | * This function writes the metadata to an ARP file |
||||
560 | * |
||||
561 | * @param \SimpleSAML\Configuration $config |
||||
562 | */ |
||||
563 | public function writeARPfile(Configuration $config): void |
||||
564 | { |
||||
565 | $arpfile = $config->getString('arpfile'); |
||||
566 | $types = ['saml20-sp-remote']; |
||||
567 | |||||
568 | $md = []; |
||||
569 | foreach ($this->metadata as $category => $elements) { |
||||
570 | if (!in_array($category, $types, true)) { |
||||
571 | continue; |
||||
572 | } |
||||
573 | $md = array_merge($md, $elements); |
||||
574 | } |
||||
575 | |||||
576 | // $metadata, $attributemap, $prefix, $suffix |
||||
577 | $arp = new ARP( |
||||
578 | $md, |
||||
579 | $config->getOptionalString('attributemap', ''), |
||||
0 ignored issues
–
show
It seems like
$config->getOptionalString('attributemap', '') can also be of type null ; however, parameter $attributemap_filename of SimpleSAML\Module\metarefresh\ARP::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
580 | $config->getOptionalString('prefix', ''), |
||||
0 ignored issues
–
show
It seems like
$config->getOptionalString('prefix', '') can also be of type null ; however, parameter $prefix of SimpleSAML\Module\metarefresh\ARP::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
581 | $config->getOptionalString('suffix', ''), |
||||
0 ignored issues
–
show
It seems like
$config->getOptionalString('suffix', '') can also be of type null ; however, parameter $suffix of SimpleSAML\Module\metarefresh\ARP::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
582 | ); |
||||
583 | |||||
584 | |||||
585 | $arpxml = $arp->getXML(); |
||||
586 | |||||
587 | Logger::info('Writing ARP file: ' . $arpfile . "\n"); |
||||
588 | file_put_contents($arpfile, $arpxml); |
||||
589 | } |
||||
590 | |||||
591 | |||||
592 | /** |
||||
593 | * This function writes the metadata to to separate files in the output directory. |
||||
594 | * |
||||
595 | * @param string $outputDir |
||||
596 | */ |
||||
597 | public function writeMetadataFiles(string $outputDir): void |
||||
598 | { |
||||
599 | while (strlen($outputDir) > 0 && $outputDir[strlen($outputDir) - 1] === '/') { |
||||
600 | $outputDir = substr($outputDir, 0, strlen($outputDir) - 1); |
||||
601 | } |
||||
602 | |||||
603 | if (!file_exists($outputDir)) { |
||||
604 | Logger::info('Creating directory: ' . $outputDir . "\n"); |
||||
605 | $res = @mkdir($outputDir, 0777, true); |
||||
606 | if ($res === false) { |
||||
607 | throw new Exception('Error creating directory: ' . $outputDir); |
||||
608 | } |
||||
609 | } |
||||
610 | |||||
611 | foreach ($this->types as $type) { |
||||
612 | $filename = $outputDir . '/' . $type . '.php'; |
||||
613 | |||||
614 | if (array_key_exists($type, $this->metadata)) { |
||||
615 | $elements = $this->metadata[$type]; |
||||
616 | Logger::debug('Writing: ' . $filename); |
||||
617 | |||||
618 | $content = '<?php' . "\n" . '/* This file was generated by the metarefresh module at '; |
||||
619 | $content .= $this->getTime() . "\nDo not update it manually as it will get overwritten\n" . '*/' . "\n"; |
||||
620 | |||||
621 | foreach ($elements as $m) { |
||||
622 | $entityID = $m['metadata']['entityid']; |
||||
623 | $content .= "\n" . '$metadata[\''; |
||||
624 | $content .= addslashes($entityID) . '\'] = ' . VarExporter::export($m['metadata']) . ';' . "\n"; |
||||
625 | } |
||||
626 | |||||
627 | $sysUtils = new Utils\System(); |
||||
628 | $sysUtils->writeFile($filename, $content, 0644); |
||||
629 | } elseif (is_file($filename)) { |
||||
630 | if (unlink($filename)) { |
||||
631 | Logger::debug('Deleting stale metadata file: ' . $filename); |
||||
632 | } else { |
||||
633 | Logger::warning('Could not delete stale metadata file: ' . $filename); |
||||
634 | } |
||||
635 | } |
||||
636 | } |
||||
637 | } |
||||
638 | |||||
639 | |||||
640 | /** |
||||
641 | * Save metadata for loading with the 'serialize' metadata loader. |
||||
642 | * |
||||
643 | * @param string $outputDir The directory we should save the metadata to. |
||||
644 | */ |
||||
645 | public function writeMetadataSerialize(string $outputDir): void |
||||
646 | { |
||||
647 | $metaHandler = new Metadata\MetaDataStorageHandlerSerialize(['directory' => $outputDir]); |
||||
648 | |||||
649 | // First we add all the metadata entries to the metadata handler |
||||
650 | foreach ($this->metadata as $set => $elements) { |
||||
651 | foreach ($elements as $m) { |
||||
652 | $entityId = $m['metadata']['entityid']; |
||||
653 | |||||
654 | Logger::debug(sprintf( |
||||
655 | 'metarefresh: Add metadata entry %s in set %s.', |
||||
656 | var_export($entityId, true), |
||||
657 | var_export($set, true), |
||||
658 | )); |
||||
659 | $metaHandler->saveMetadata($entityId, $set, $m['metadata']); |
||||
660 | } |
||||
661 | } |
||||
662 | } |
||||
663 | |||||
664 | |||||
665 | /** |
||||
666 | * This function uses the `PDO` metadata handler to upsert metadata in database. |
||||
667 | * |
||||
668 | * @param \SimpleSAML\Configuration $globalConfig |
||||
669 | * @param array $config An associative array with the configuration for `PDO` handler. |
||||
670 | * |
||||
671 | * @return void |
||||
672 | */ |
||||
673 | public function writeMetadataPdo(Configuration $globalConfig, array $config = []): void |
||||
0 ignored issues
–
show
The parameter
$globalConfig is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
674 | { |
||||
675 | $metaHandler = new Metadata\MetaDataStorageHandlerPdo($config); |
||||
676 | |||||
677 | foreach ($this->metadata as $set => $elements) { |
||||
678 | foreach ($elements as $m) { |
||||
679 | $entityId = $m['metadata']['entityid']; |
||||
680 | |||||
681 | Logger::debug("PDO Metarefresh: Upsert metadata entry `{$entityId}` in set `{$set}`."); |
||||
682 | $metaHandler->addEntry($entityId, $set, $m['metadata']); |
||||
683 | } |
||||
684 | } |
||||
685 | } |
||||
686 | |||||
687 | |||||
688 | /** |
||||
689 | * @return string |
||||
690 | */ |
||||
691 | private function getTime(): string |
||||
692 | { |
||||
693 | // The current date, as a string |
||||
694 | return gmdate('Y-m-d\\TH:i:s\\Z'); |
||||
695 | } |
||||
696 | } |
||||
697 |