ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
sspmod_metarefresh_MetaLoader Class Reference
+ Collaboration diagram for sspmod_metarefresh_MetaLoader:

Public Member Functions

 __construct ($expire=null, $stateFile=null, $oldMetadataSrc=null)
 Constructor. More...
 
 getTypes ()
 Get the types of entities that will be loaded. More...
 
 setTypes ($types)
 Set the types of entities that will be loaded. More...
 
 loadSource ($source)
 This function processes a SAML metadata file. More...
 
 writeState ()
 This function writes the state array back to disk. More...
 
 dumpMetadataStdOut ()
 This function writes the metadata to stdout. More...
 
 writeARPfile ($config)
 This function writes the metadata to an ARP file. More...
 
 writeMetadataFiles ($outputDir)
 This function writes the metadata to to separate files in the output directory. More...
 

Private Member Functions

 createContext ($source)
 Create HTTP context, with any available caches taken into account. More...
 
 addCachedMetadata ($source)
 
 saveState ($source, $responseHeaders)
 Store caching state data for a source. More...
 
 loadXML ($data, $source)
 Parse XML metadata and return entities. More...
 
 addMetadata ($filename, $metadata, $type, $template=null)
 This function adds metadata from the specified file to the list of metadata. More...
 

Private Attributes

 $expire
 
 $metadata
 
 $oldMetadataSrc
 
 $stateFile
 
 $changed
 
 $state
 
 $types
 

Detailed Description

Definition at line 6 of file MetaLoader.php.

Constructor & Destructor Documentation

◆ __construct()

sspmod_metarefresh_MetaLoader::__construct (   $expire = null,
  $stateFile = null,
  $oldMetadataSrc = null 
)

Constructor.

Parameters
integer$expire
string$stateFile
object$oldMetadataSrc

Definition at line 30 of file MetaLoader.php.

31 {
32 $this->expire = $expire;
33 $this->metadata = array();
34 $this->oldMetadataSrc = $oldMetadataSrc;
35 $this->stateFile = $stateFile;
36 $this->changed = false;
37
38 // Read file containing $state from disk
39 if (is_readable($stateFile)) {
40 include $stateFile;
41 }
42
43 $this->state = array();
44
45 }

References $expire, $oldMetadataSrc, and $stateFile.

Member Function Documentation

◆ addCachedMetadata()

sspmod_metarefresh_MetaLoader::addCachedMetadata (   $source)
private

Definition at line 221 of file MetaLoader.php.

222 {
223 if (isset($this->oldMetadataSrc)) {
224 foreach ($this->types as $type) {
225 foreach ($this->oldMetadataSrc->getMetadataSet($type) as $entity) {
226 if (array_key_exists('metarefresh:src', $entity)) {
227 if ($entity['metarefresh:src'] == $source['src']) {
228 $this->addMetadata($source['src'], $entity, $type);
229 }
230 }
231 }
232 }
233 }
234 }
$source
Definition: linkback.php:22
addMetadata($filename, $metadata, $type, $template=null)
This function adds metadata from the specified file to the list of metadata.
Definition: MetaLoader.php:330
$type

References $source, $type, and addMetadata().

Referenced by loadSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addMetadata()

sspmod_metarefresh_MetaLoader::addMetadata (   $filename,
  $metadata,
  $type,
  $template = null 
)
private

This function adds metadata from the specified file to the list of metadata.

This function will return without making any changes if $metadata is NULL.

Parameters
$filenameThe filename the metadata comes from.
$metadataThe metadata.
$typeThe metadata type.

Definition at line 330 of file MetaLoader.php.

331 {
332 if ($metadata === null) {
333 return;
334 }
335
336 if (isset($template)) {
337 $metadata = array_merge($metadata, $template);
338 }
339
340 $metadata['metarefresh:src'] = $filename;
341 if (!array_key_exists($type, $this->metadata)) {
342 $this->metadata[$type] = array();
343 }
344
345 // If expire is defined in constructor...
346 if (!empty($this->expire)) {
347 // If expire is already in metadata
348 if (array_key_exists('expire', $metadata)) {
349 // Override metadata expire with more restrictive global config-
350 if ($this->expire < $metadata['expire']) {
351 $metadata['expire'] = $this->expire;
352 }
353
354 // If expire is not already in metadata use global config
355 } else {
356 $metadata['expire'] = $this->expire;
357 }
358 }
359 $this->metadata[$type][] = array('filename' => $filename, 'metadata' => $metadata);
360 }
$filename
Definition: buildRTE.php:89
$template

Referenced by addCachedMetadata(), and loadSource().

+ Here is the caller graph for this function:

◆ createContext()

sspmod_metarefresh_MetaLoader::createContext (   $source)
private

Create HTTP context, with any available caches taken into account.

Definition at line 195 of file MetaLoader.php.

196 {
198 $name = $config->getString('technicalcontact_name', null);
199 $mail = $config->getString('technicalcontact_email', null);
200
201 $rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n";
202
203 if (isset($source['conditionalGET']) && $source['conditionalGET']) {
204 if (array_key_exists($source['src'], $this->state)) {
205 $sourceState = $this->state[$source['src']];
206
207 if (isset($sourceState['last-modified'])) {
208 $rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n";
209 }
210
211 if (isset($sourceState['etag'])) {
212 $rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n";
213 }
214 }
215 }
216
217 return array('http' => array('header' => $rawheader));
218 }
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$config
Definition: bootstrap.php:15

References $config, $name, $source, and SimpleSAML_Configuration\getInstance().

Referenced by loadSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dumpMetadataStdOut()

sspmod_metarefresh_MetaLoader::dumpMetadataStdOut ( )

This function writes the metadata to stdout.

Definition at line 300 of file MetaLoader.php.

301 {
302 foreach ($this->metadata as $category => $elements) {
303
304 echo '/* The following data should be added to metadata/' . $category . '.php. */' . "\n";
305
306 foreach ($elements as $m) {
307 $filename = $m['filename'];
308 $entityID = $m['metadata']['entityid'];
309
310 echo "\n";
311 echo '/* The following metadata was generated from ' . $filename . ' on ' . $this->getTime() . '. */' . "\n";
312 echo '$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], true) . ';' . "\n";
313 }
314
315 echo "\n";
316 echo '/* End of data which should be added to metadata/' . $category . '.php. */' . "\n";
317 echo "\n";
318 }
319 }
getTime()
Definition: MetaLoader.php:492

References $filename, $m, and getTime().

+ Here is the call graph for this function:

◆ getTypes()

sspmod_metarefresh_MetaLoader::getTypes ( )

Get the types of entities that will be loaded.

Returns
array The entity types allowed.

Definition at line 53 of file MetaLoader.php.

References $types.

◆ loadSource()

sspmod_metarefresh_MetaLoader::loadSource (   $source)

This function processes a SAML metadata file.

Parameters
$source

Definition at line 79 of file MetaLoader.php.

80 {
81 if (preg_match('@^https?://@i', $source['src'])) {
82 // Build new HTTP context
84
85 // GET!
86 try {
87 list($data, $responseHeaders) = \SimpleSAML\Utils\HTTP::fetch($source['src'], $context, true);
88 } catch(Exception $e) {
89 SimpleSAML\Logger::warning('metarefresh: ' . $e->getMessage());
90 }
91
92 // We have response headers, so the request succeeded
93 if (!isset($responseHeaders)) {
94 // No response headers, this means the request failed in some way, so re-use old data
95 SimpleSAML\Logger::debug('No response from ' . $source['src'] . ' - attempting to re-use cached metadata');
97 return;
98 } elseif (preg_match('@^HTTP/1\.[01]\s304\s@', $responseHeaders[0])) {
99 // 304 response
100 SimpleSAML\Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata');
102 return;
103 } elseif (!preg_match('@^HTTP/1\.[01]\s200\s@', $responseHeaders[0])) {
104 // Other error
105 SimpleSAML\Logger::debug('Error from ' . $source['src'] . ' - attempting to re-use cached metadata');
107 return;
108 }
109 } else {
110 // Local file.
111 $data = file_get_contents($source['src']);
112 $responseHeaders = null;
113 }
114
115 // Everything OK. Proceed.
116 if (isset($source['conditionalGET']) && $source['conditionalGET']) {
117 // Stale or no metadata, so a fresh copy
118 SimpleSAML\Logger::debug('Downloaded fresh copy');
119 }
120
121 try {
122 $entities = $this->loadXML($data, $source);
123 } catch(Exception $e) {
124 SimpleSAML\Logger::debug('XML parser error when parsing ' . $source['src'] . ' - attempting to re-use cached metadata');
125 SimpleSAML\Logger::debug('XML parser returned: ' . $e->getMessage());
127 return;
128 }
129
130 foreach ($entities as $entity) {
131
132 if (isset($source['blacklist'])) {
133 if (!empty($source['blacklist']) && in_array($entity->getEntityID(), $source['blacklist'], true)) {
134 SimpleSAML\Logger::info('Skipping "' . $entity->getEntityID() . '" - blacklisted.' . "\n");
135 continue;
136 }
137 }
138
139 if (isset($source['whitelist'])) {
140 if (!empty($source['whitelist']) && !in_array($entity->getEntityID(), $source['whitelist'], true)) {
141 SimpleSAML\Logger::info('Skipping "' . $entity->getEntityID() . '" - not in the whitelist.' . "\n");
142 continue;
143 }
144 }
145
146 if (array_key_exists('certificates', $source) && $source['certificates'] !== null) {
147 if (!$entity->validateSignature($source['certificates'])) {
148 SimpleSAML\Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature using certificate.' . "\n");
149 continue;
150 }
151 }
152
153 if (array_key_exists('validateFingerprint', $source) && $source['validateFingerprint'] !== null) {
154 if (!array_key_exists('certificates', $source) || $source['certificates'] == null) {
155 if (!$entity->validateFingerprint($source['validateFingerprint'])) {
156 SimpleSAML\Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature using fingerprint.' . "\n");
157 continue;
158 }
159 } else {
160 SimpleSAML\Logger::info('Skipping validation with fingerprint since option certificate is set.' . "\n");
161 }
162 }
163
164 $template = null;
165 if (array_key_exists('template', $source)) {
166 $template = $source['template'];
167 }
168
169 if (in_array('shib13-sp-remote', $this->types, true)) {
170 $this->addMetadata($source['src'], $entity->getMetadata1xSP(), 'shib13-sp-remote', $template);
171 }
172 if (in_array('shib13-idp-remote', $this->types, true)) {
173 $this->addMetadata($source['src'], $entity->getMetadata1xIdP(), 'shib13-idp-remote', $template);
174 }
175 if (in_array('saml20-sp-remote', $this->types, true)) {
176 $this->addMetadata($source['src'], $entity->getMetadata20SP(), 'saml20-sp-remote', $template);
177 }
178 if (in_array('saml20-idp-remote', $this->types, true)) {
179 $this->addMetadata($source['src'], $entity->getMetadata20IdP(), 'saml20-idp-remote', $template);
180 }
181 if (in_array('attributeauthority-remote', $this->types, true)) {
182 $attributeAuthorities = $entity->getAttributeAuthorities();
183 if (!empty($attributeAuthorities)) {
184 $this->addMetadata($source['src'], $attributeAuthorities[0], 'attributeauthority-remote', $template);
185 }
186 }
187 }
188
189 $this->saveState($source, $responseHeaders);
190 }
static info($string)
Definition: Logger.php:199
static warning($string)
Definition: Logger.php:177
static debug($string)
Definition: Logger.php:211
static fetch($url, $context=array(), $getHeaders=false)
Helper function to retrieve a file or URL with proxy support, also supporting proxy basic authorizati...
Definition: HTTP.php:408
saveState($source, $responseHeaders)
Store caching state data for a source.
Definition: MetaLoader.php:240
createContext($source)
Create HTTP context, with any available caches taken into account.
Definition: MetaLoader.php:195
loadXML($data, $source)
Parse XML metadata and return entities.
Definition: MetaLoader.php:265
$data
Definition: bench.php:6
$context
Definition: webdav.php:25

References $context, $data, $source, $template, addCachedMetadata(), addMetadata(), createContext(), SimpleSAML\Logger\debug(), SimpleSAML\Utils\HTTP\fetch(), SimpleSAML\Logger\info(), loadXML(), saveState(), and SimpleSAML\Logger\warning().

+ Here is the call graph for this function:

◆ loadXML()

sspmod_metarefresh_MetaLoader::loadXML (   $data,
  $source 
)
private

Parse XML metadata and return entities.

Definition at line 265 of file MetaLoader.php.

266 {
267 try {
269 } catch (Exception $e) {
270 throw new Exception('Failed to read XML from ' . $source['src']);
271 }
272 if ($doc->documentElement === null) {
273 throw new Exception('Opened file is not an XML document: ' . $source['src']);
274 }
276 }
static parseDescriptorsElement(DOMElement $element=null)
This function parses a DOMElement which represents either an EntityDescriptor element or an EntitiesD...
Definition: SAMLParser.php:341

References $data, $source, SAML2\DOMDocumentFactory\fromString(), and SimpleSAML_Metadata_SAMLParser\parseDescriptorsElement().

Referenced by loadSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveState()

sspmod_metarefresh_MetaLoader::saveState (   $source,
  $responseHeaders 
)
private

Store caching state data for a source.

Definition at line 240 of file MetaLoader.php.

241 {
242 if (isset($source['conditionalGET']) && $source['conditionalGET']) {
243 // Headers section
244 if ($responseHeaders !== null) {
245 $candidates = array('last-modified', 'etag');
246
247 foreach ($candidates as $candidate) {
248 if (array_key_exists($candidate, $responseHeaders)) {
249 $this->state[$source['src']][$candidate] = $responseHeaders[$candidate];
250 }
251 }
252 }
253
254 if (!empty($this->state[$source['src']])) {
255 // Timestamp when this src was requested.
256 $this->state[$source['src']]['requested_at'] = $this->getTime();
257 $this->changed = true;
258 }
259 }
260 }

References $source, and getTime().

Referenced by loadSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTypes()

sspmod_metarefresh_MetaLoader::setTypes (   $types)

Set the types of entities that will be loaded.

Parameters
string | array$typesEither a string with the name of one single type allowed, or an array with a list of types. Pass an empty array to reset to all types of entities.

Definition at line 65 of file MetaLoader.php.

66 {
67 if (!is_array($types)) {
68 $types = array($types);
69 }
70 $this->types = $types;
71 }

References $types.

◆ writeARPfile()

sspmod_metarefresh_MetaLoader::writeARPfile (   $config)

This function writes the metadata to an ARP file.

Definition at line 366 of file MetaLoader.php.

367 {
368 assert($config instanceof SimpleSAML_Configuration);
369
370 $arpfile = $config->getValue('arpfile');
371 $types = array('saml20-sp-remote');
372
373 $md = array();
374 foreach ($this->metadata as $category => $elements) {
375 if (!in_array($category, $types, true)) {
376 continue;
377 }
378 $md = array_merge($md, $elements);
379 }
380
381 // $metadata, $attributemap, $prefix, $suffix
382 $arp = new sspmod_metarefresh_ARP($md,
383 $config->getValue('attributemap', ''),
384 $config->getValue('prefix', ''),
385 $config->getValue('suffix', '')
386 );
387
388
389 $arpxml = $arp->getXML();
390
391 SimpleSAML\Logger::info('Writing ARP file: ' . $arpfile . "\n");
392 file_put_contents($arpfile, $arpxml);
393 }

◆ writeMetadataFiles()

sspmod_metarefresh_MetaLoader::writeMetadataFiles (   $outputDir)

This function writes the metadata to to separate files in the output directory.

Definition at line 399 of file MetaLoader.php.

◆ writeState()

sspmod_metarefresh_MetaLoader::writeState ( )

This function writes the state array back to disk.

Definition at line 282 of file MetaLoader.php.

283 {
284 if ($this->changed) {
285 SimpleSAML\Logger::debug('Writing: ' . $this->stateFile);
287 $this->stateFile,
288 "<?php\n/* This file was generated by the metarefresh module at ".$this->getTime() . ".\n".
289 " Do not update it manually as it will get overwritten. */\n".
290 '$state = ' . var_export($this->state, true) . ";\n?>\n",
291 0644
292 );
293 }
294 }
static writeFile($filename, $data, $mode=0600)
Atomically write a file.
Definition: System.php:183

References SimpleSAML\Logger\debug(), getTime(), and SimpleSAML\Utils\System\writeFile().

+ Here is the call graph for this function:

Field Documentation

◆ $changed

sspmod_metarefresh_MetaLoader::$changed
private

Definition at line 12 of file MetaLoader.php.

◆ $expire

sspmod_metarefresh_MetaLoader::$expire
private

Definition at line 8 of file MetaLoader.php.

Referenced by __construct().

◆ $metadata

sspmod_metarefresh_MetaLoader::$metadata
private

Definition at line 9 of file MetaLoader.php.

◆ $oldMetadataSrc

sspmod_metarefresh_MetaLoader::$oldMetadataSrc
private

Definition at line 10 of file MetaLoader.php.

Referenced by __construct().

◆ $state

sspmod_metarefresh_MetaLoader::$state
private

Definition at line 13 of file MetaLoader.php.

◆ $stateFile

sspmod_metarefresh_MetaLoader::$stateFile
private

Definition at line 11 of file MetaLoader.php.

Referenced by __construct().

◆ $types

sspmod_metarefresh_MetaLoader::$types
private
Initial value:
= array(
'saml20-idp-remote',
'saml20-sp-remote',
'shib13-idp-remote',
'shib13-sp-remote',
'attributeauthority-remote'
)

Definition at line 14 of file MetaLoader.php.

Referenced by getTypes(), and setTypes().


The documentation for this class was generated from the following file: