ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
MetaDataStorageHandler.php
Go to the documentation of this file.
1 <?php
2 
3 
11 {
12 
13 
21  private static $metadataHandler = null;
22 
23 
30  private $sources;
31 
32 
40  public static function getMetadataHandler()
41  {
42  if (self::$metadataHandler === null) {
43  self::$metadataHandler = new SimpleSAML_Metadata_MetaDataStorageHandler();
44  }
45 
46  return self::$metadataHandler;
47  }
48 
49 
54  protected function __construct()
55  {
57 
58  $sourcesConfig = $config->getArray('metadata.sources', null);
59 
60  // for backwards compatibility, and to provide a default configuration
61  if ($sourcesConfig === null) {
62  $type = $config->getString('metadata.handler', 'flatfile');
63  $sourcesConfig = array(array('type' => $type));
64  }
65 
66  try {
67  $this->sources = SimpleSAML_Metadata_MetaDataStorageSource::parseSources($sourcesConfig);
68  } catch (Exception $e) {
69  throw new Exception(
70  "Invalid configuration of the 'metadata.sources' configuration option: ".$e->getMessage()
71  );
72  }
73  }
74 
75 
85  public function getGenerated($property, $set)
86  {
87  // first we check if the user has overridden this property in the metadata
88  try {
89  $metadataSet = $this->getMetaDataCurrent($set);
90  if (array_key_exists($property, $metadataSet)) {
91  return $metadataSet[$property];
92  }
93  } catch (Exception $e) {
94  // probably metadata wasn't found. In any case we continue by generating the metadata
95  }
96 
97  // get the configuration
99  assert($config instanceof SimpleSAML_Configuration);
100 
101  $baseurl = \SimpleSAML\Utils\HTTP::getSelfURLHost().$config->getBasePath();
102 
103  if ($set == 'saml20-sp-hosted') {
104  if ($property === 'SingleLogoutServiceBinding') {
105  return \SAML2\Constants::BINDING_HTTP_REDIRECT;
106  }
107  } elseif ($set == 'saml20-idp-hosted') {
108  switch ($property) {
109  case 'SingleSignOnService':
110  return $baseurl.'saml2/idp/SSOService.php';
111 
112  case 'SingleSignOnServiceBinding':
113  return \SAML2\Constants::BINDING_HTTP_REDIRECT;
114 
115  case 'SingleLogoutService':
116  return $baseurl.'saml2/idp/SingleLogoutService.php';
117 
118  case 'SingleLogoutServiceBinding':
119  return \SAML2\Constants::BINDING_HTTP_REDIRECT;
120  }
121  } elseif ($set == 'shib13-idp-hosted') {
122  if ($property === 'SingleSignOnService') {
123  return $baseurl.'shib13/idp/SSOService.php';
124  }
125  }
126 
127  throw new Exception('Could not generate metadata property '.$property.' for set '.$set.'.');
128  }
129 
130 
139  public function getList($set = 'saml20-idp-remote')
140  {
141  assert('is_string($set)');
142 
143  $result = array();
144 
145  foreach ($this->sources as $source) {
146  $srcList = $source->getMetadataSet($set);
147 
148  foreach ($srcList as $key => $le) {
149  if (array_key_exists('expire', $le)) {
150  if ($le['expire'] < time()) {
151  unset($srcList[$key]);
153  "Dropping metadata entity ".var_export($key, true).", expired ".
154  SimpleSAML\Utils\Time::generateTimestamp($le['expire'])."."
155  );
156  }
157  }
158  }
159 
160  /* $result is the last argument to array_merge because we want the content already
161  * in $result to have precedence.
162  */
163  $result = array_merge($srcList, $result);
164  }
165 
166  return $result;
167  }
168 
169 
178  public function getMetaDataCurrent($set)
179  {
180  return $this->getMetaData(null, $set);
181  }
182 
183 
194  public function getMetaDataCurrentEntityID($set, $type = 'entityid')
195  {
196  assert('is_string($set)');
197 
198  // first we look for the hostname/path combination
199  $currenthostwithpath = \SimpleSAML\Utils\HTTP::getSelfHostWithPath(); // sp.example.org/university
200 
201  foreach ($this->sources as $source) {
202  $index = $source->getEntityIdFromHostPath($currenthostwithpath, $set, $type);
203  if ($index !== null) {
204  return $index;
205  }
206  }
207 
208  // then we look for the hostname
209  $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org
210 
211  foreach ($this->sources as $source) {
212  $index = $source->getEntityIdFromHostPath($currenthost, $set, $type);
213  if ($index !== null) {
214  return $index;
215  }
216  }
217 
218  // then we look for the DEFAULT entry
219  foreach ($this->sources as $source) {
220  $entityId = $source->getEntityIdFromHostPath('__DEFAULT__', $set, $type);
221  if ($entityId !== null) {
222  return $entityId;
223  }
224  }
225 
226  // we were unable to find the hostname/path in any metadata source
227  throw new Exception(
228  'Could not find any default metadata entities in set ['.$set.'] for host ['.$currenthost.' : '.
229  $currenthostwithpath.']'
230  );
231  }
232 
233 
244  public function getPreferredEntityIdFromCIDRhint($set, $ip)
245  {
246  foreach ($this->sources as $source) {
247  $entityId = $source->getPreferredEntityIdFromCIDRhint($set, $ip);
248  if ($entityId !== null) {
249  return $entityId;
250  }
251  }
252 
253  return null;
254  }
255 
256 
269  public function getMetaData($index, $set)
270  {
271  assert('is_string($set)');
272 
273  if ($index === null) {
274  $index = $this->getMetaDataCurrentEntityID($set, 'metaindex');
275  }
276 
277  assert('is_string($index)');
278 
279  foreach ($this->sources as $source) {
280  $metadata = $source->getMetaData($index, $set);
281 
282  if ($metadata !== null) {
283 
284  if (array_key_exists('expire', $metadata)) {
285  if ($metadata['expire'] < time()) {
286  throw new Exception(
287  'Metadata for the entity ['.$index.'] expired '.
288  (time() - $metadata['expire']).' seconds ago.'
289  );
290  }
291  }
292 
293  $metadata['metadata-index'] = $index;
294  $metadata['metadata-set'] = $set;
295  assert('array_key_exists("entityid", $metadata)');
296  return $metadata;
297  }
298  }
299 
301  }
302 
303 
315  public function getMetaDataConfig($entityId, $set)
316  {
317  assert('is_string($entityId)');
318  assert('is_string($set)');
319 
320  $metadata = $this->getMetaData($entityId, $set);
321  return SimpleSAML_Configuration::loadFromArray($metadata, $set.'/'.var_export($entityId, true));
322  }
323 
324 
334  public function getMetaDataConfigForSha1($sha1, $set)
335  {
336  assert('is_string($sha1)');
337  assert('is_string($set)');
338 
339  $result = array();
340 
341  foreach ($this->sources as $source) {
342  $srcList = $source->getMetadataSet($set);
343 
344  /* $result is the last argument to array_merge because we want the content already
345  * in $result to have precedence.
346  */
347  $result = array_merge($srcList, $result);
348  }
349  foreach ($result as $remote_provider) {
350 
351  if (sha1($remote_provider['entityid']) == $sha1) {
352  $remote_provider['metadata-set'] = $set;
353 
355  $remote_provider,
356  $set.'/'.var_export($remote_provider['entityid'], true)
357  );
358  }
359  }
360 
361  return null;
362  }
363 }
getMetaDataCurrent($set)
This function retrieves metadata for the current entity based on the hostname/path the request was di...
static getMetadataHandler()
This function retrieves the current instance of the metadata handler.
$result
$type
getPreferredEntityIdFromCIDRhint($set, $ip)
This method will call getPreferredEntityIdFromCIDRhint() on all of the sources.
$baseurl
Definition: demo.php:25
$index
Definition: metadata.php:60
$metadata['__DYNAMIC:1__']
getMetaDataConfig($entityId, $set)
Retrieve the metadata as a configuration object.
Attribute-related utility methods.
static warning($string)
Definition: Logger.php:179
static getSelfHost()
Retrieve our own host.
Definition: HTTP.php:699
static getSelfHostWithPath()
Retrieve our own host together with the URL path.
Definition: HTTP.php:738
Create styles array
The data for the language used.
__construct()
This constructor initializes this metadata storage handler.
getGenerated($property, $set)
This function is used to generate some metadata elements automatically.
if($source===null) if(!($source instanceof sspmod_saml_Auth_Source_SP)) $entityId
Definition: metadata.php:22
getMetaData($index, $set)
This function looks up the metadata for the given entity id in the given set.
getList($set='saml20-idp-remote')
This function lists all known metadata in the given set.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
$source
Definition: linkback.php:22
getMetaDataConfigForSha1($sha1, $set)
Search for an entity&#39;s metadata, given the SHA1 digest of its entity ID.
getMetaDataCurrentEntityID($set, $type='entityid')
This function locates the current entity id based on the hostname/path combination the user accessed...
$key
Definition: croninfo.php:18
static parseSources($sourcesConfig)
Parse array with metadata sources.
static loadFromArray($config, $location='[ARRAY]', $instance=null)
Loads a configuration from the given array.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.