ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MetaDataStorageSource.php
Go to the documentation of this file.
1 <?php
2 
3 
16 {
17 
18 
31  public static function parseSources($sourcesConfig)
32  {
33  assert(is_array($sourcesConfig));
34 
35  $sources = array();
36 
37  foreach ($sourcesConfig as $sourceConfig) {
38  if (!is_array($sourceConfig)) {
39  throw new Exception("Found an element in metadata source configuration which wasn't an array.");
40  }
41 
42  $sources[] = self::getSource($sourceConfig);
43  }
44 
45  return $sources;
46  }
47 
48 
60  public static function getSource($sourceConfig)
61  {
62  assert(is_array($sourceConfig));
63 
64  if (array_key_exists('type', $sourceConfig)) {
65  $type = $sourceConfig['type'];
66  } else {
67  $type = 'flatfile';
68  }
69 
70  switch ($type) {
71  case 'flatfile':
73  case 'xml':
74  return new SimpleSAML_Metadata_MetaDataStorageHandlerXML($sourceConfig);
75  case 'serialize':
77  case 'mdx':
78  case 'mdq':
79  return new \SimpleSAML\Metadata\Sources\MDQ($sourceConfig);
80  case 'pdo':
81  return new SimpleSAML_Metadata_MetaDataStorageHandlerPdo($sourceConfig);
82  default:
83  // metadata store from module
84  try {
86  $type,
87  'MetadataStore',
88  'SimpleSAML_Metadata_MetaDataStorageSource'
89  );
90  } catch (Exception $e) {
92  "Invalid 'type' for metadata source. Cannot find store '$type'.",
93  null
94  );
95  }
96  return new $className($sourceConfig);
97  }
98  }
99 
100 
112  public function getMetadataSet($set)
113  {
114  return array();
115  }
116 
117 
132  public function getEntityIdFromHostPath($hostPath, $set, $type = 'entityid')
133  {
134 
135  $metadataSet = $this->getMetadataSet($set);
136  if ($metadataSet === null) {
137  // this metadata source does not have this metadata set
138  return null;
139  }
140 
141  foreach ($metadataSet as $index => $entry) {
142  if (!array_key_exists('host', $entry)) {
143  continue;
144  }
145 
146  if ($hostPath === $entry['host']) {
147  if ($type === 'entityid') {
148  return $entry['entityid'];
149  } else {
150  return $index;
151  }
152  }
153  }
154 
155  // no entries matched, we should return null
156  return null;
157  }
158 
159 
173  public function getPreferredEntityIdFromCIDRhint($set, $ip, $type = 'entityid')
174  {
175 
176  $metadataSet = $this->getMetadataSet($set);
177 
178  foreach ($metadataSet as $index => $entry) {
179  $cidrHints = array();
180 
181  // support hint.cidr for idp discovery
182  if (array_key_exists('hint.cidr', $entry) && is_array($entry['hint.cidr'])) {
183  $cidrHints = $entry['hint.cidr'];
184  }
185 
186  // support discohints in idp metadata for idp discovery
187  if (array_key_exists('DiscoHints', $entry)
188  && array_key_exists('IPHint', $entry['DiscoHints'])
189  && is_array($entry['DiscoHints']['IPHint'])) {
190  // merge with hints derived from discohints, but prioritize hint.cidr in case it is used
191  $cidrHints = array_merge($entry['DiscoHints']['IPHint'], $cidrHints);
192  }
193 
194  if (empty($cidrHints)) {
195  continue;
196  }
197 
198  foreach ($cidrHints as $hint_entry) {
199  if (SimpleSAML\Utils\Net::ipCIDRcheck($hint_entry, $ip)) {
200  if ($type === 'entityid') {
201  return $entry['entityid'];
202  } else {
203  return $index;
204  }
205  }
206  }
207  }
208 
209  // no entries matched, we should return null
210  return null;
211  }
212 
213 
214  /*
215  *
216  */
217  private function lookupIndexFromEntityId($entityId, $set)
218  {
219  assert(is_string($entityId));
220  assert(isset($set));
221 
222  $metadataSet = $this->getMetadataSet($set);
223 
224  // check for hostname
225  $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org
226 
227  foreach ($metadataSet as $index => $entry) {
228  if ($index === $entityId) {
229  return $index;
230  }
231  if ($entry['entityid'] === $entityId) {
232  if ($entry['host'] === '__DEFAULT__' || $entry['host'] === $currenthost) {
233  return $index;
234  }
235  }
236  }
237 
238  return null;
239  }
240 
241 
256  public function getMetaData($index, $set)
257  {
258 
259  assert(is_string($index));
260  assert(isset($set));
261 
262  $metadataSet = $this->getMetadataSet($set);
263 
264  if (array_key_exists($index, $metadataSet)) {
265  return $metadataSet[$index];
266  }
267 
268  $indexlookup = $this->lookupIndexFromEntityId($index, $set);
269  if (isset($indexlookup) && array_key_exists($indexlookup, $metadataSet)) {
270  return $metadataSet[$indexlookup];
271  }
272 
273  return null;
274  }
275 }
$type
getEntityIdFromHostPath($hostPath, $set, $type='entityid')
This function resolves an host/path combination to an entity id.
$index
Definition: metadata.php:60
Attribute-related utility methods.
getMetadataSet($set)
This function attempts to generate an associative array with metadata for all entities in the given s...
getPreferredEntityIdFromCIDRhint($set, $ip, $type='entityid')
This function will go through all the metadata, and check the DiscoHints->IPHint parameter, which defines a network space (ip range) for each remote entry.
getMetaData($index, $set)
This function retrieves metadata for the given entity id in the given set of metadata.
static getSelfHost()
Retrieve our own host.
Definition: HTTP.php:697
static getSource($sourceConfig)
This function creates a metadata source based on the given configuration.
if($source===null) if(!($source instanceof sspmod_saml_Auth_Source_SP)) $entityId
Definition: metadata.php:22
static resolveClass($id, $type, $subclass=null)
Resolve module class.
Definition: Module.php:169
static parseSources($sourcesConfig)
Parse array with metadata sources.