Skip to content
Snippets Groups Projects
Commit 32b11973 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Reformat code, finish completing phpdoc.

parent 448fa04f
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* This abstract class defines an interface for metadata storage sources. * This abstract class defines an interface for metadata storage sources.
* *
...@@ -9,225 +10,251 @@ ...@@ -9,225 +10,251 @@
* *
* @author Olav Morken, UNINETT AS. * @author Olav Morken, UNINETT AS.
* @author Andreas Aakre Solberg, UNINETT AS. * @author Andreas Aakre Solberg, UNINETT AS.
* @package simpleSAMLphp * @package SimpleSAMLphp
*/ */
abstract class SimpleSAML_Metadata_MetaDataStorageSource { abstract class SimpleSAML_Metadata_MetaDataStorageSource
{
/**
* Parse array with metadata sources. /**
* * Parse array with metadata sources.
* This function accepts an array with metadata sources, and returns an array with *
* each metadata source as an object. * This function accepts an array with metadata sources, and returns an array with
* * each metadata source as an object.
* @param array $sourcesConfig Array with metadata source configuration. *
* @return array Parsed metadata configuration. * @param array $sourcesConfig Array with metadata source configuration.
*/ *
public static function parseSources($sourcesConfig) { * @return array Parsed metadata configuration.
assert('is_array($sourcesConfig)'); *
* @throws Exception If something is wrong in the configuration.
$sources = array(); */
public static function parseSources($sourcesConfig)
foreach ($sourcesConfig as $sourceConfig) { {
if (!is_array($sourceConfig)) { assert('is_array($sourcesConfig)');
throw new Exception('Found an element in metadata source' .
' configuration which wasn\'t an array.'); $sources = array();
}
foreach ($sourcesConfig as $sourceConfig) {
$sources[] = self::getSource($sourceConfig); if (!is_array($sourceConfig)) {
} throw new Exception("Found an element in metadata source configuration which wasn't an array.");
}
return $sources;
} $sources[] = self::getSource($sourceConfig);
}
/** return $sources;
* This function creates a metadata source based on the given configuration. }
* The type of source is based on the 'type' parameter in the configuration.
* The default type is 'flatfile'.
* /**
* @param array $sourceConfig Associative array with the configuration for this metadata source. * This function creates a metadata source based on the given configuration.
* @return SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile| * The type of source is based on the 'type' parameter in the configuration.
* SimpleSAML_Metadata_MetaDataStorageHandlerXML| * The default type is 'flatfile'.
* SimpleSAML_Metadata_MetaDataStorageHandlerSerialize| *
* SimpleSAML_Metadata_MetaDataStorageHandlerMDX| * @param array $sourceConfig Associative array with the configuration for this metadata source.
* SimpleSAML_Metadata_MetaDataStorageHandlerPdo An instance of a metadata source with the given configuration. *
*/ * @return SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile|
public static function getSource($sourceConfig) { * SimpleSAML_Metadata_MetaDataStorageHandlerXML|
* SimpleSAML_Metadata_MetaDataStorageHandlerSerialize|
assert(is_array($sourceConfig)); * SimpleSAML_Metadata_MetaDataStorageHandlerMDX|
* SimpleSAML_Metadata_MetaDataStorageHandlerPdo An instance of a metadata source with the given configuration.
if(array_key_exists('type', $sourceConfig)) { *
$type = $sourceConfig['type']; * @throws Exception If the metadata source type is invalid.
} else { */
$type = 'flatfile'; public static function getSource($sourceConfig)
} {
assert(is_array($sourceConfig));
switch($type) {
case 'flatfile': if (array_key_exists('type', $sourceConfig)) {
return new SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile($sourceConfig); $type = $sourceConfig['type'];
case 'xml': } else {
return new SimpleSAML_Metadata_MetaDataStorageHandlerXML($sourceConfig); $type = 'flatfile';
case 'serialize': }
return new SimpleSAML_Metadata_MetaDataStorageHandlerSerialize($sourceConfig);
case 'mdx': switch ($type) {
return new SimpleSAML_Metadata_MetaDataStorageHandlerMDX($sourceConfig); case 'flatfile':
case 'pdo': return new SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile($sourceConfig);
return new SimpleSAML_Metadata_MetaDataStorageHandlerPdo($sourceConfig); case 'xml':
default: return new SimpleSAML_Metadata_MetaDataStorageHandlerXML($sourceConfig);
throw new Exception('Invalid metadata source type: "' . $type . '".'); case 'serialize':
} return new SimpleSAML_Metadata_MetaDataStorageHandlerSerialize($sourceConfig);
} case 'mdx':
return new SimpleSAML_Metadata_MetaDataStorageHandlerMDX($sourceConfig);
case 'pdo':
/** return new SimpleSAML_Metadata_MetaDataStorageHandlerPdo($sourceConfig);
* This function attempts to generate an associative array with metadata for all entities in the default:
* given set. The key of the array is the entity id. throw new Exception('Invalid metadata source type: "'.$type.'".');
* }
* A subclass should override this function if it is able to easily generate this list. }
*
* @param string $set The set we want to list metadata for.
* @return array An associative array with all entities in the given set, or an empty array if we are /**
* unable to generate this list. * This function attempts to generate an associative array with metadata for all entities in the
*/ * given set. The key of the array is the entity id.
public function getMetadataSet($set) { *
return array(); * A subclass should override this function if it is able to easily generate this list.
} *
* @param string $set The set we want to list metadata for.
*
/** * @return array An associative array with all entities in the given set, or an empty array if we are
* This function resolves an host/path combination to an entity id. * unable to generate this list.
* */
* This class implements this function using the getMetadataSet-function. A subclass should public function getMetadataSet($set)
* override this function if it doesn't implement the getMetadataSet function, or if the {
* implementation of getMetadataSet is slow. return array();
* }
* @param string $hostPath The host/path combination we are looking up.
* @param string $set Which set of metadata we are looking it up in.
* @param string $type Do you want to return the metaindex or the entityID. [entityid|metaindex] /**
* This function resolves an host/path combination to an entity id.
* @return array|null An entity id which matches the given host/path combination, or NULL if *
* we are unable to locate one which matches. * This class implements this function using the getMetadataSet-function. A subclass should
*/ * override this function if it doesn't implement the getMetadataSet function, or if the
public function getEntityIdFromHostPath($hostPath, $set, $type = 'entityid') { * implementation of getMetadataSet is slow.
*
$metadataSet = $this->getMetadataSet($set); * @param string $hostPath The host/path combination we are looking up.
if ($metadataSet === NULL) { * @param string $set Which set of metadata we are looking it up in.
/* This metadata source does not have this metadata set. */ * @param string $type Do you want to return the metaindex or the entityID. [entityid|metaindex]
return NULL; *
} * @return array|null An entity id which matches the given host/path combination, or NULL if
* we are unable to locate one which matches.
foreach($metadataSet AS $index => $entry) { */
public function getEntityIdFromHostPath($hostPath, $set, $type = 'entityid')
if(!array_key_exists('host', $entry)) { {
continue;
} $metadataSet = $this->getMetadataSet($set);
if ($metadataSet === null) {
if($hostPath === $entry['host']) { // this metadata source does not have this metadata set
if ($type === 'entityid') { return null;
return $entry['entityid']; }
} else {
return $index; foreach ($metadataSet as $index => $entry) {
}
} if (!array_key_exists('host', $entry)) {
} continue;
}
/* No entries matched - we should return NULL. */
return NULL; if ($hostPath === $entry['host']) {
} if ($type === 'entityid') {
return $entry['entityid'];
/** } else {
* This function will go through all the metadata, and check the hint.cidr return $index;
* parameter, which defines a network space (ip range) for each remote entry. }
* This function returns the entityID for any of the entities that have an }
* IP range which the IP falls within. }
*
* @param string $set Which set of metadata we are looking it up in. // no entries matched, we should return null
* @param string $ip IP address return null;
* @param string $type Do you want to return the metaindex or the entityID. [entityid|metaindex] }
* @return string The entity id of a entity which have a CIDR hint where the provided
* IP address match.
*/ /**
public function getPreferredEntityIdFromCIDRhint($set, $ip, $type = 'entityid') { * This function will go through all the metadata, and check the hint.cidr
* parameter, which defines a network space (ip range) for each remote entry.
$metadataSet = $this->getMetadataSet($set); * This function returns the entityID for any of the entities that have an
* IP range which the IP falls within.
foreach($metadataSet AS $index => $entry) { *
* @param string $set Which set of metadata we are looking it up in.
if(!array_key_exists('hint.cidr', $entry)) continue; * @param string $ip IP address
if(!is_array($entry['hint.cidr'])) continue; * @param string $type Do you want to return the metaindex or the entityID. [entityid|metaindex]
*
foreach ($entry['hint.cidr'] AS $hint_entry) { * @return string The entity id of a entity which have a CIDR hint where the provided
if (SimpleSAML\Utils\Net::ipCIDRcheck($hint_entry, $ip)) { * IP address match.
if ($type === 'entityid') { */
return $entry['entityid']; public function getPreferredEntityIdFromCIDRhint($set, $ip, $type = 'entityid')
} else { {
return $index;
} $metadataSet = $this->getMetadataSet($set);
}
} foreach ($metadataSet as $index => $entry) {
} if (!array_key_exists('hint.cidr', $entry)) {
continue;
/* No entries matched - we should return NULL. */ }
return NULL; if (!is_array($entry['hint.cidr'])) {
} continue;
}
/*
* foreach ($entry['hint.cidr'] as $hint_entry) {
*/ if (SimpleSAML\Utils\Net::ipCIDRcheck($hint_entry, $ip)) {
private function lookupIndexFromEntityId($entityId, $set) { if ($type === 'entityid') {
return $entry['entityid'];
assert('is_string($entityId)'); } else {
assert('isset($set)'); return $index;
}
$metadataSet = $this->getMetadataSet($set); }
}
/* Check for hostname. */ }
$currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org
if(strpos($currenthost, ":") !== FALSE) { // no entries matched, we should return null
$currenthostdecomposed = explode(":", $currenthost); return null;
$currenthost = $currenthostdecomposed[0]; }
}
foreach($metadataSet AS $index => $entry) { /*
if ($index === $entityId) return $index; *
if ($entry['entityid'] === $entityId) { */
if ($entry['host'] === '__DEFAULT__' || $entry['host'] === $currenthost) return $index; private function lookupIndexFromEntityId($entityId, $set)
} {
} assert('is_string($entityId)');
assert('isset($set)');
return NULL;
} $metadataSet = $this->getMetadataSet($set);
/** // check for hostname
* This function retrieves metadata for the given entity id in the given set of metadata. $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org
* It will return NULL if it is unable to locate the metadata. if (strpos($currenthost, ":") !== false) {
* $currenthostdecomposed = explode(":", $currenthost);
* This class implements this function using the getMetadataSet-function. A subclass should $currenthost = $currenthostdecomposed[0];
* override this function if it doesn't implement the getMetadataSet function, or if the }
* implementation of getMetadataSet is slow.
* foreach ($metadataSet as $index => $entry) {
* @param string $index The entityId or metaindex we are looking up. if ($index === $entityId) {
* @param string $set The set we are looking for metadata in. return $index;
* @return array An associative array with metadata for the given entity, or NULL if we are unable to }
* locate the entity. if ($entry['entityid'] === $entityId) {
*/ if ($entry['host'] === '__DEFAULT__' || $entry['host'] === $currenthost) {
public function getMetaData($index, $set) { return $index;
}
assert('is_string($index)'); }
assert('isset($set)'); }
$metadataSet = $this->getMetadataSet($set); return null;
}
if(array_key_exists($index, $metadataSet))
return $metadataSet[$index];
/**
$indexlookup = $this->lookupIndexFromEntityId($index, $set); * This function retrieves metadata for the given entity id in the given set of metadata.
if (isset($indexlookup) && array_key_exists($indexlookup, $metadataSet)) * It will return NULL if it is unable to locate the metadata.
return $metadataSet[$indexlookup]; *
* This class implements this function using the getMetadataSet-function. A subclass should
return NULL; * override this function if it doesn't implement the getMetadataSet function, or if the
} * implementation of getMetadataSet is slow.
*
* @param string $index The entityId or metaindex we are looking up.
* @param string $set The set we are looking for metadata in.
*
* @return array An associative array with metadata for the given entity, or NULL if we are unable to
* locate the entity.
*/
public function getMetaData($index, $set)
{
assert('is_string($index)');
assert('isset($set)');
$metadataSet = $this->getMetadataSet($set);
if (array_key_exists($index, $metadataSet)) {
return $metadataSet[$index];
}
$indexlookup = $this->lookupIndexFromEntityId($index, $set);
if (isset($indexlookup) && array_key_exists($indexlookup, $metadataSet)) {
return $metadataSet[$indexlookup];
}
return null;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment