ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
 
 $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
array$sourcesSources...

Definition at line 29 of file MetaLoader.php.

29 {
30 $this->expire = $expire;
31 $this->metadata = array();
32 $this->oldMetadataSrc = $oldMetadataSrc;
33 $this->stateFile = $stateFile;
34 $this->changed = FALSE;
35
36 // Read file containing $state from disk
37 if(is_readable($stateFile)) {
38 require($stateFile);
39 }
40
41 $this->state = (isset($state)) ? $state : array();
42
43 }
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10

References $expire, $oldMetadataSrc, $state, and $stateFile.

Member Function Documentation

◆ addCachedMetadata()

sspmod_metarefresh_MetaLoader::addCachedMetadata (   $source)
private

Definition at line 209 of file MetaLoader.php.

209 {
210 if(isset($this->oldMetadataSrc)) {
211 foreach($this->types as $type) {
212 foreach($this->oldMetadataSrc->getMetadataSet($type) as $entity) {
213 if(array_key_exists('metarefresh:src', $entity)) {
214 if($entity['metarefresh:src'] == $source['src']) {
215 $this->addMetadata($source['src'], $entity, $type);
216 }
217 }
218 }
219 }
220 }
221 }
$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:320
$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 320 of file MetaLoader.php.

320 {
321
322 if($metadata === NULL) {
323 return;
324 }
325
326 if (isset($template)) {
327 $metadata = array_merge($metadata, $template);
328 }
329
330 $metadata['metarefresh:src'] = $filename;
331 if(!array_key_exists($type, $this->metadata)) {
332 $this->metadata[$type] = array();
333 }
334
335 // If expire is defined in constructor...
336 if (!empty($this->expire)) {
337
338 // If expire is already in metadata
339 if (array_key_exists('expire', $metadata)) {
340
341 // Override metadata expire with more restrictive global config-
342 if ($this->expire < $metadata['expire'])
343 $metadata['expire'] = $this->expire;
344
345 // If expire is not already in metadata use global config
346 } else {
347 $metadata['expire'] = $this->expire;
348 }
349 }
350
351
352
353 $this->metadata[$type][] = array('filename' => $filename, 'metadata' => $metadata);
354 }
$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 180 of file MetaLoader.php.

180 {
181
182 $context = NULL;
183
185 $name = $config->getString('technicalcontact_name', NULL);
186 $mail = $config->getString('technicalcontact_email', NULL);
187
188 $rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n";
189
190 if (isset($source['conditionalGET']) && $source['conditionalGET']) {
191 if(array_key_exists($source['src'], $this->state)) {
192
193 $sourceState = $this->state[$source['src']];
194
195 if(isset($sourceState['last-modified'])) {
196 $rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n";
197 }
198
199 if(isset($sourceState['etag'])) {
200 $rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n";
201 }
202 }
203 }
204
205 return array('http' => array('header' => $rawheader));
206 }
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
if($format !==null) $name
Definition: metadata.php:146

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 288 of file MetaLoader.php.

288 {
289
290 foreach($this->metadata as $category => $elements) {
291
292 echo('/* The following data should be added to metadata/' . $category . '.php. */' . "\n");
293
294
295 foreach($elements as $m) {
296 $filename = $m['filename'];
297 $entityID = $m['metadata']['entityid'];
298
299 echo("\n");
300 echo('/* The following metadata was generated from ' . $filename . ' on ' . $this->getTime() . '. */' . "\n");
301 echo('$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], TRUE) . ';' . "\n");
302 }
303
304
305 echo("\n");
306 echo('/* End of data which should be added to metadata/' . $category . '.php. */' . "\n");
307 echo("\n");
308 }
309 }
getTime()
Definition: MetaLoader.php:479

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 51 of file MetaLoader.php.

References $types.

◆ loadSource()

sspmod_metarefresh_MetaLoader::loadSource (   $source)

This function processes a SAML metadata file.

Parameters
$source

Definition at line 77 of file MetaLoader.php.

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

References $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 253 of file MetaLoader.php.

253 {
254 $entities = array();
255 try {
257 } catch (Exception $e) {
258 throw new Exception('Failed to read XML from ' . $source['src']);
259 }
260 if ($doc->documentElement === NULL) {
261 throw new Exception('Opened file is not an XML document: ' . $source['src']);
262 }
263 $entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsElement($doc->documentElement);
264 return $entities;
265 }
static parseDescriptorsElement(DOMElement $element=null)
This function parses a DOMElement which represents either an EntityDescriptor element or an EntitiesD...
Definition: SAMLParser.php:359

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 227 of file MetaLoader.php.

227 {
228
229 if (isset($source['conditionalGET']) && $source['conditionalGET']) {
230
231 // Headers section
232 $candidates = array('last-modified', 'etag');
233
234 foreach($candidates as $candidate) {
235 if(array_key_exists($candidate, $responseHeaders)) {
236 $this->state[$source['src']][$candidate] = $responseHeaders[$candidate];
237 }
238 }
239
240 if(!empty($this->state[$source['src']])) {
241 // Timestamp when this src was requested.
242 $this->state[$source['src']]['requested_at'] = $this->getTime();
243
244 $this->changed = TRUE;
245 }
246 }
247 }

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 63 of file MetaLoader.php.

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

References $types.

◆ writeARPfile()

sspmod_metarefresh_MetaLoader::writeARPfile (   $config)

This function writes the metadata to an ARP file.

Definition at line 360 of file MetaLoader.php.

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

◆ writeMetadataFiles()

sspmod_metarefresh_MetaLoader::writeMetadataFiles (   $outputDir)

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

Definition at line 392 of file MetaLoader.php.

References $filename, $m, $outputDir, $res, $type, SimpleSAML\Logger\debug(), getTime(), and SimpleSAML\Logger\info().

+ Here is the call graph for this function:

◆ writeState()

sspmod_metarefresh_MetaLoader::writeState ( )

This function writes the state array back to disk.

Definition at line 271 of file MetaLoader.php.

271 {
272 if($this->changed) {
273 SimpleSAML\Logger::debug('Writing: ' . $this->stateFile);
275 $this->stateFile,
276 "<?php\n/* This file was generated by the metarefresh module at ".$this->getTime() . ".\n".
277 " Do not update it manually as it will get overwritten. */\n".
278 '$state = ' . var_export($this->state, TRUE) . ";\n?>\n",
279 0644
280 );
281 }
282 }
static writeFile($filename, $data, $mode=0600)
Atomically write a file.
Definition: System.php:176

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 13 of file MetaLoader.php.

◆ $expire

sspmod_metarefresh_MetaLoader::$expire
private

Definition at line 9 of file MetaLoader.php.

Referenced by __construct().

◆ $metadata

sspmod_metarefresh_MetaLoader::$metadata
private

Definition at line 10 of file MetaLoader.php.

◆ $oldMetadataSrc

sspmod_metarefresh_MetaLoader::$oldMetadataSrc
private

Definition at line 11 of file MetaLoader.php.

Referenced by __construct().

◆ $stateFile

sspmod_metarefresh_MetaLoader::$stateFile
private

Definition at line 12 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: