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

Reformat SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile.

parent ed96bc37
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* This file defines a flat file metadata source. * This file defines a flat file metadata source.
* Instantiation of session handler objects should be done through * Instantiation of session handler objects should be done through
* the class method getMetadataHandler(). * the class method getMetadataHandler().
* *
* @author Andreas kre Solberg, UNINETT AS. <andreas.solberg@uninett.no> * @author Andreas Åkre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
* @package simpleSAMLphp * @package SimpleSAMLphp
*/ */
class SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile extends SimpleSAML_Metadata_MetaDataStorageSource { class SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile extends SimpleSAML_Metadata_MetaDataStorageSource
{
/**
* This is the directory we will load metadata files from. The path will always end /**
* with a '/'. * This is the directory we will load metadata files from. The path will always end
*/ * with a '/'.
private $directory; *
* @var string
*/
/** private $directory;
* This is an associative array which stores the different metadata sets we have loaded.
*/
private $cachedMetadata = array(); /**
* This is an associative array which stores the different metadata sets we have loaded.
*
/** * @var array
* This constructor initializes the flatfile metadata storage handler with the */
* specified configuration. The configuration is an associative array with the following private $cachedMetadata = array();
* possible elements:
* - 'directory': The directory we should load metadata from. The default directory is
* set in the 'metadatadir' configuration option in 'config.php'. /**
* * This constructor initializes the flatfile metadata storage handler with the
* @param array $config An associative array with the configuration for this handler. * specified configuration. The configuration is an associative array with the following
*/ * possible elements:
protected function __construct($config) { * - 'directory': The directory we should load metadata from. The default directory is
assert('is_array($config)'); * set in the 'metadatadir' configuration option in 'config.php'.
*
/* Get the configuration. */ * @param array $config An associative array with the configuration for this handler.
$globalConfig = SimpleSAML_Configuration::getInstance(); */
protected function __construct($config)
{
/* Find the path to the directory we should search for metadata in. */ assert('is_array($config)');
if(array_key_exists('directory', $config)) {
$this->directory = $config['directory']; // get the configuration
} else { $globalConfig = SimpleSAML_Configuration::getInstance();
$this->directory = $globalConfig->getString('metadatadir', 'metadata/');
} // find the path to the directory we should search for metadata in
if (array_key_exists('directory', $config)) {
/* Resolve this directory relative to the simpleSAMLphp directory (unless it is $this->directory = $config['directory'];
* an absolute path). } else {
*/ $this->directory = $globalConfig->getString('metadatadir', 'metadata/');
$this->directory = $globalConfig->resolvePath($this->directory) . '/'; }
}
/* Resolve this directory relative to the simpleSAMLphp directory (unless it is
* an absolute path).
/** */
* This function loads the given set of metadata from a file our metadata directory. $this->directory = $globalConfig->resolvePath($this->directory).'/';
* This function returns NULL if it is unable to locate the given set in the metadata directory. }
*
* @param string $set The set of metadata we are loading.
* @return array An associative array with the metadata, or NULL if we are unable to load metadata from the given file. /**
* @throws Exception If the metadata set cannot be loaded. * This function loads the given set of metadata from a file our metadata directory.
*/ * This function returns null if it is unable to locate the given set in the metadata directory.
private function load($set) { *
* @param string $set The set of metadata we are loading.
$metadatasetfile = $this->directory . $set . '.php'; *
* @return array An associative array with the metadata, or null if we are unable to load metadata from the given
if (!file_exists($metadatasetfile)) { * file.
return NULL; * @throws Exception If the metadata set cannot be loaded.
} */
private function load($set)
$metadata = array(); {
$metadatasetfile = $this->directory.$set.'.php';
include($metadatasetfile);
if (!file_exists($metadatasetfile)) {
if (!is_array($metadata)) { return null;
throw new Exception('Could not load metadata set [' . $set . '] from file: ' . $metadatasetfile); }
}
$metadata = array();
return $metadata;
} include($metadatasetfile);
if (!is_array($metadata)) {
/** throw new Exception('Could not load metadata set ['.$set.'] from file: '.$metadatasetfile);
* This function retrieves the given set of metadata. It will return an empty array if it is }
* unable to locate it.
* return $metadata;
* @param string $set The set of metadata we are retrieving. }
* @return array An asssociative array with the metadata. Each element in the array is an entity, and the
* key is the entity id.
*/ /**
public function getMetadataSet($set) { * This function retrieves the given set of metadata. It will return an empty array if it is
* unable to locate it.
if(array_key_exists($set, $this->cachedMetadata)) { *
return $this->cachedMetadata[$set]; * @param string $set The set of metadata we are retrieving.
} *
* @return array An associative array with the metadata. Each element in the array is an entity, and the
$metadataSet = $this->load($set); * key is the entity id.
if($metadataSet === NULL) { */
$metadataSet = array(); public function getMetadataSet($set)
} {
if (array_key_exists($set, $this->cachedMetadata)) {
/* Add the entity id of an entry to each entry in the metadata. */ return $this->cachedMetadata[$set];
foreach ($metadataSet AS $entityId => &$entry) { }
if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
$entry['entityid'] = $this->generateDynamicHostedEntityID($set); $metadataSet = $this->load($set);
} else { if ($metadataSet === null) {
$entry['entityid'] = $entityId; $metadataSet = array();
} }
}
// add the entity id of an entry to each entry in the metadata
$this->cachedMetadata[$set] = $metadataSet; foreach ($metadataSet as $entityId => &$entry) {
if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
return $metadataSet; $entry['entityid'] = $this->generateDynamicHostedEntityID($set);
} } else {
$entry['entityid'] = $entityId;
private function generateDynamicHostedEntityID($set) { }
}
/* Get the configuration. */
$baseurl = \SimpleSAML\Utils\HTTP::getBaseURL(); $this->cachedMetadata[$set] = $metadataSet;
if ($set === 'saml20-idp-hosted') { return $metadataSet;
return $baseurl . 'saml2/idp/metadata.php'; }
} elseif($set === 'shib13-idp-hosted') {
return $baseurl . 'shib13/idp/metadata.php';
} elseif($set === 'wsfed-sp-hosted') { private function generateDynamicHostedEntityID($set)
return 'urn:federation:' . \SimpleSAML\Utils\HTTP::getSelfHost(); {
} elseif($set === 'adfs-idp-hosted') { // get the configuration
return 'urn:federation:' . \SimpleSAML\Utils\HTTP::getSelfHost() . ':idp'; $baseurl = \SimpleSAML\Utils\HTTP::getBaseURL();
} else {
throw new Exception('Can not generate dynamic EntityID for metadata of this type: [' . $set . ']'); if ($set === 'saml20-idp-hosted') {
} return $baseurl.'saml2/idp/metadata.php';
} } elseif ($set === 'shib13-idp-hosted') {
return $baseurl.'shib13/idp/metadata.php';
} elseif ($set === 'wsfed-sp-hosted') {
return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
} elseif ($set === 'adfs-idp-hosted') {
return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
} else {
throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
}
}
} }
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