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)) { |
|
|
|
|
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])) { |
|
|
|
|
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); |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* This function adds metadata from the specified file to the list of metadata. |
516
|
|
|
* This function will return without making any changes if $metadata is NULL. |
517
|
|
|
* |
518
|
|
|
* @param string $filename The filename the metadata comes from. |
519
|
|
|
* @param \SAML2\XML\md\AttributeAuthorityDescriptor[]|null $metadata The metadata. |
520
|
|
|
* @param string $type The metadata type. |
521
|
|
|
* @param array|null $template The template. |
522
|
|
|
*/ |
523
|
|
|
private function addMetadata(string $filename, ?array $metadata, string $type, array $template = null): void |
524
|
|
|
{ |
525
|
|
|
if ($metadata === null) { |
|
|
|
|
526
|
|
|
return; |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
if (isset($template)) { |
530
|
|
|
$metadata = array_merge($metadata, $template); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
$metadata['metarefresh:src'] = $filename; |
534
|
|
|
if (!array_key_exists($type, $this->metadata)) { |
535
|
|
|
$this->metadata[$type] = []; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
// If expire is defined in constructor... |
539
|
|
|
if (!empty($this->expire)) { |
540
|
|
|
// If expire is already in metadata |
541
|
|
|
if (array_key_exists('expire', $metadata)) { |
542
|
|
|
// Override metadata expire with more restrictive global config |
543
|
|
|
if ($this->expire < $metadata['expire']) { |
544
|
|
|
$metadata['expire'] = $this->expire; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
// If expire is not already in metadata use global config |
548
|
|
|
} else { |
549
|
|
|
$metadata['expire'] = $this->expire; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
$this->metadata[$type][] = ['filename' => $filename, 'metadata' => $metadata]; |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* This function writes the metadata to an ARP file |
558
|
|
|
* |
559
|
|
|
* @param \SimpleSAML\Configuration $config |
560
|
|
|
*/ |
561
|
|
|
public function writeARPfile(Configuration $config): void |
562
|
|
|
{ |
563
|
|
|
$arpfile = $config->getString('arpfile'); |
564
|
|
|
$types = ['saml20-sp-remote']; |
565
|
|
|
|
566
|
|
|
$md = []; |
567
|
|
|
foreach ($this->metadata as $category => $elements) { |
568
|
|
|
if (!in_array($category, $types, true)) { |
569
|
|
|
continue; |
570
|
|
|
} |
571
|
|
|
$md = array_merge($md, $elements); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
// $metadata, $attributemap, $prefix, $suffix |
575
|
|
|
$arp = new ARP( |
576
|
|
|
$md, |
577
|
|
|
$config->getOptionalString('attributemap', ''), |
578
|
|
|
$config->getOptionalString('prefix', ''), |
579
|
|
|
$config->getOptionalString('suffix', '') |
580
|
|
|
); |
581
|
|
|
|
582
|
|
|
|
583
|
|
|
$arpxml = $arp->getXML(); |
584
|
|
|
|
585
|
|
|
Logger::info('Writing ARP file: ' . $arpfile . "\n"); |
586
|
|
|
file_put_contents($arpfile, $arpxml); |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* This function writes the metadata to to separate files in the output directory. |
592
|
|
|
* |
593
|
|
|
* @param string $outputDir |
594
|
|
|
*/ |
595
|
|
|
public function writeMetadataFiles(string $outputDir): void |
596
|
|
|
{ |
597
|
|
|
while (strlen($outputDir) > 0 && $outputDir[strlen($outputDir) - 1] === '/') { |
598
|
|
|
$outputDir = substr($outputDir, 0, strlen($outputDir) - 1); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
if (!file_exists($outputDir)) { |
602
|
|
|
Logger::info('Creating directory: ' . $outputDir . "\n"); |
603
|
|
|
$res = @mkdir($outputDir, 0777, true); |
604
|
|
|
if ($res === false) { |
605
|
|
|
throw new Exception('Error creating directory: ' . $outputDir); |
606
|
|
|
} |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
foreach ($this->types as $type) { |
610
|
|
|
$filename = $outputDir . '/' . $type . '.php'; |
611
|
|
|
|
612
|
|
|
if (array_key_exists($type, $this->metadata)) { |
613
|
|
|
$elements = $this->metadata[$type]; |
614
|
|
|
Logger::debug('Writing: ' . $filename); |
615
|
|
|
|
616
|
|
|
$content = '<?php' . "\n" . '/* This file was generated by the metarefresh module at '; |
617
|
|
|
$content .= $this->getTime() . "\nDo not update it manually as it will get overwritten\n" . '*/' . "\n"; |
618
|
|
|
|
619
|
|
|
foreach ($elements as $m) { |
620
|
|
|
$entityID = $m['metadata']['entityid']; |
621
|
|
|
$content .= "\n" . '$metadata[\''; |
622
|
|
|
$content .= addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n"; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
$sysUtils = new Utils\System(); |
626
|
|
|
$sysUtils->writeFile($filename, $content, 0644); |
627
|
|
|
} elseif (is_file($filename)) { |
628
|
|
|
if (unlink($filename)) { |
629
|
|
|
Logger::debug('Deleting stale metadata file: ' . $filename); |
630
|
|
|
} else { |
631
|
|
|
Logger::warning('Could not delete stale metadata file: ' . $filename); |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Save metadata for loading with the 'serialize' metadata loader. |
640
|
|
|
* |
641
|
|
|
* @param string $outputDir The directory we should save the metadata to. |
642
|
|
|
*/ |
643
|
|
|
public function writeMetadataSerialize(string $outputDir): void |
644
|
|
|
{ |
645
|
|
|
$metaHandler = new Metadata\MetaDataStorageHandlerSerialize(['directory' => $outputDir]); |
646
|
|
|
|
647
|
|
|
// First we add all the metadata entries to the metadata handler |
648
|
|
|
foreach ($this->metadata as $set => $elements) { |
649
|
|
|
foreach ($elements as $m) { |
650
|
|
|
$entityId = $m['metadata']['entityid']; |
651
|
|
|
|
652
|
|
|
Logger::debug( |
653
|
|
|
'metarefresh: Add metadata entry ' . |
654
|
|
|
var_export($entityId, true) . ' in set ' . var_export($set, true) . '.' |
655
|
|
|
); |
656
|
|
|
$metaHandler->saveMetadata($entityId, $set, $m['metadata']); |
657
|
|
|
} |
658
|
|
|
} |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* This function uses the `PDO` metadata handler to upsert metadata in database. |
664
|
|
|
* |
665
|
|
|
* @author - Tamilselvan Sekar, University of California, Davis. <[email protected]> |
666
|
|
|
* |
667
|
|
|
* @param \SimpleSAML\Configuration $globalConfig |
668
|
|
|
* @param array $config An associative array with the configuration for `PDO` handler. |
669
|
|
|
* |
670
|
|
|
* @return void |
671
|
|
|
*/ |
672
|
|
|
public function writeMetadataPdo(Configuration $globalConfig, array $config = []): void |
673
|
|
|
{ |
674
|
|
|
$metaHandler = new Metadata\MetaDataStorageHandlerPdo($globalConfig, $config); |
|
|
|
|
675
|
|
|
|
676
|
|
|
foreach ($this->metadata as $set => $elements) { |
677
|
|
|
foreach ($elements as $m) { |
678
|
|
|
$entityId = $m['metadata']['entityid']; |
679
|
|
|
|
680
|
|
|
Logger::debug("PDO Metarefresh: Upsert metadata entry `{$entityId}` in set `{$set}`."); |
681
|
|
|
$metaHandler->addEntry($entityId, $set, $m['metadata']); |
682
|
|
|
} |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* @return string |
689
|
|
|
*/ |
690
|
|
|
private function getTime(): string |
691
|
|
|
{ |
692
|
|
|
// The current date, as a string |
693
|
|
|
return gmdate('Y-m-d\\TH:i:s\\Z'); |
694
|
|
|
} |
695
|
|
|
} |
696
|
|
|
|