diff --git a/config-templates/config.php b/config-templates/config.php index 43fd71ceb74c835527debf906be44f43fa9dc7df..42f18fe173a2a1a5ca90f7b6802bbea018a589a0 100644 --- a/config-templates/config.php +++ b/config-templates/config.php @@ -807,11 +807,11 @@ $config = array( * - 'file': Path to the XML file with the metadata. * - 'url': The URL to fetch metadata from. THIS IS ONLY FOR DEBUGGING - THERE IS NO CACHING OF THE RESPONSE. * - * MDX metadata handler: - * This metadata handler looks up for the metadata of an entity at the given MDX server. - * The MDX metadata handler defines the following options: - * - 'type': This is always 'mdx'. - * - 'server': Base URL of the MDX server. Mandatory. + * MDQ metadata handler: + * This metadata handler looks up for the metadata of an entity at the given MDQ server. + * The MDQ metadata handler defines the following options: + * - 'type': This is always 'mdq'. + * - 'server': Base URL of the MDQ server. Mandatory. * - 'validateFingerprint': The fingerprint of the certificate used to sign the metadata. You don't need this * option if you don't want to validate the signature on the metadata. Optional. * - 'cachedir': Directory where metadata can be cached. Optional. @@ -843,12 +843,12 @@ $config = array( * array('type' => 'xml', 'file' => 'idp.example.org-idpMeta.xml'), * ), * - * This example defines an mdx source. + * This example defines an mdq source. * 'metadata.sources' => array( * array( - * 'type' => 'mdx', - * 'server' => 'http://mdx.server.com:8080', - * 'cachedir' => '/var/simplesamlphp/mdx-cache', + * 'type' => 'mdq', + * 'server' => 'http://mdq.server.com:8080', + * 'cachedir' => '/var/simplesamlphp/mdq-cache', * 'cachelength' => 86400 * ) * ), diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php index 9d677cda5ee9ace272e1df5324b012640f9b2354..d4f480848ed9d0ff08baa8b3ea313ad585f615be 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php @@ -75,7 +75,8 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource case 'serialize': return new SimpleSAML_Metadata_MetaDataStorageHandlerSerialize($sourceConfig); case 'mdx': - return new SimpleSAML_Metadata_MetaDataStorageHandlerMDX($sourceConfig); + case 'mdq': + return new \SimpleSAML\Metadata\Sources\MDQ($sourceConfig); case 'pdo': return new SimpleSAML_Metadata_MetaDataStorageHandlerPdo($sourceConfig); default: diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php b/lib/SimpleSAML/Metadata/Sources/MDQ.php similarity index 73% rename from lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php rename to lib/SimpleSAML/Metadata/Sources/MDQ.php index 4800d74ab990ae002437ba192f8899574d173b94..5bd58847e410e6da76aab17b44d424524af4abac 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerMDX.php +++ b/lib/SimpleSAML/Metadata/Sources/MDQ.php @@ -1,19 +1,23 @@ <?php +namespace SimpleSAML\Metadata\Sources; + +use SimpleSAML\Logger; +use SimpleSAML\Utils\HTTP; /** - * This class implements SAML Metadata Exchange Protocol + * This class implements SAML Metadata Query Protocol * * @author Andreas Ă…kre Solberg, UNINETT AS. * @author Olav Morken, UNINETT AS. * @author Tamas Frank, NIIFI * @package SimpleSAMLphp */ -class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_MetaDataStorageSource +class MDQ extends \SimpleSAML_Metadata_MetaDataStorageSource { /** - * The URL of MDX server (url:port) + * The URL of MDQ server (url:port) * * @var string */ @@ -47,7 +51,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ * This function initializes the dynamic XML metadata source. * * Options: - * - 'server': URL of the MDX server (url:port). Mandatory. + * - 'server': URL of the MDQ server (url:port). Mandatory. * - 'validateFingerprint': The fingerprint of the certificate used to sign the metadata. * You don't need this option if you don't want to validate the signature on the metadata. * Optional. @@ -57,14 +61,14 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ * * @param array $config The configuration for this instance of the XML metadata source. * - * @throws Exception If no server option can be found in the configuration. + * @throws \Exception If no server option can be found in the configuration. */ protected function __construct($config) { assert('is_array($config)'); if (!array_key_exists('server', $config)) { - throw new Exception("The 'server' configuration option is not set."); + throw new \Exception(__CLASS__.": the 'server' configuration option is not set."); } else { $this->server = $config['server']; } @@ -76,7 +80,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ } if (array_key_exists('cachedir', $config)) { - $globalConfig = SimpleSAML_Configuration::getInstance(); + $globalConfig = \SimpleSAML_Configuration::getInstance(); $this->cacheDir = $globalConfig->resolvePath($config['cachedir']); } else { $this->cacheDir = null; @@ -130,7 +134,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ * * @return array|NULL The associative array with the metadata for this entity, or NULL * if the entity could not be found. - * @throws Exception If an error occurs while loading metadata from cache. + * @throws \Exception If an error occurs while loading metadata from cache. */ private function getFromCache($set, $entityId) { @@ -146,9 +150,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ return null; } if (!is_readable($cachefilename)) { - throw new Exception('Could not read cache file for entity ['.$cachefilename.']'); + throw new \Exception(__CLASS__.': could not read cache file for entity ['.$cachefilename.']'); } - SimpleSAML\Logger::debug('MetaData - Handler.MDX: Reading cache ['.$entityId.'] => ['.$cachefilename.']'); + Logger::debug(__CLASS__.': reading cache ['.$entityId.'] => ['.$cachefilename.']'); /* Ensure that this metadata isn't older that the cachelength option allows. This * must be verified based on the file, since this option may be changed after the @@ -156,25 +160,25 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ */ $stat = stat($cachefilename); if ($stat['mtime'] + $this->cacheLength <= time()) { - SimpleSAML\Logger::debug('MetaData - Handler.MDX: Cache file older that the cachelength option allows.'); + Logger::debug(__CLASS__.': cache file older that the cachelength option allows.'); return null; } $rawData = file_get_contents($cachefilename); if (empty($rawData)) { $error = error_get_last(); - throw new Exception( - 'Error reading metadata from cache file "'.$cachefilename.'": '.$error['message'] + throw new \Exception( + __CLASS__.': error reading metadata from cache file "'.$cachefilename.'": '.$error['message'] ); } $data = unserialize($rawData); if ($data === false) { - throw new Exception('Error unserializing cached data from file "'.$cachefilename.'".'); + throw new \Exception(__CLASS__.': error unserializing cached data from file "'.$cachefilename.'".'); } if (!is_array($data)) { - throw new Exception('Cached metadata from "'.$cachefilename.'" wasn\'t an array.'); + throw new \Exception(__CLASS__.': Cached metadata from "'.$cachefilename.'" wasn\'t an array.'); } return $data; @@ -188,7 +192,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ * @param string $entityId The entity id of this entity. * @param array $data The associative array with the metadata for this entity. * - * @throws Exception If metadata cannot be written to cache. + * @throws \Exception If metadata cannot be written to cache. */ private function writeToCache($set, $entityId, $data) { @@ -202,9 +206,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ $cachefilename = $this->getCacheFilename($set, $entityId); if (!is_writable(dirname($cachefilename))) { - throw new Exception('Could not write cache file for entity ['.$cachefilename.']'); + throw new \Exception(__CLASS__.': could not write cache file for entity ['.$cachefilename.']'); } - SimpleSAML\Logger::debug('MetaData - Handler.MDX: Writing cache ['.$entityId.'] => ['.$cachefilename.']'); + Logger::debug(__CLASS__.': Writing cache ['.$entityId.'] => ['.$cachefilename.']'); file_put_contents($cachefilename, serialize($data)); } @@ -212,13 +216,13 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ /** * Retrieve metadata for the correct set from a SAML2Parser. * - * @param SimpleSAML_Metadata_SAMLParser $entity A SAML2Parser representing an entity. + * @param \SimpleSAML_Metadata_SAMLParser $entity A SAML2Parser representing an entity. * @param string $set The metadata set we are looking for. * * @return array|NULL The associative array with the metadata, or NULL if no metadata for * the given set was found. */ - private static function getParsedSet(SimpleSAML_Metadata_SAMLParser $entity, $set) + private static function getParsedSet(\SimpleSAML_Metadata_SAMLParser $entity, $set) { assert('is_string($set)'); @@ -236,7 +240,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ return $ret[0]; default: - SimpleSAML\Logger::warning('MetaData - Handler.MDX: Unknown metadata set: '.$set); + Logger::warning(__CLASS__.': unknown metadata set: \''.$set.'\'.'); } return null; @@ -258,14 +262,14 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ * * @return array An associative array with metadata for the given entity, or NULL if we are unable to * locate the entity. - * @throws Exception If an error occurs while downloading metadata, validating the signature or writing to cache. + * @throws \Exception If an error occurs while downloading metadata, validating the signature or writing to cache. */ public function getMetaData($index, $set) { assert('is_string($index)'); assert('is_string($set)'); - SimpleSAML\Logger::info('MetaData - Handler.MDX: Loading metadata entity ['.$index.'] from ['.$set.']'); + Logger::info(__CLASS__.': loading metadata entity ['.$index.'] from ['.$set.']'); // read from cache if possible $data = $this->getFromCache($set, $index); @@ -277,45 +281,44 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerMDX extends SimpleSAML_Metadata_ if (isset($data)) { // metadata found in cache and not expired - SimpleSAML\Logger::debug('MetaData - Handler.MDX: Using cached metadata for: '.$index.'.'); + Logger::debug(__CLASS__.': using cached metadata for: '.$index.'.'); return $data; } // look at Metadata Query Protocol: https://github.com/iay/md-query/blob/master/draft-young-md-query.txt - $mdx_url = $this->server.'/entities/'.urlencode($index); + $mdq_url = $this->server.'/entities/'.urlencode($index); - SimpleSAML\Logger::debug('MetaData - Handler.MDX: Downloading metadata for "'.$index.'" from ['.$mdx_url.']'); + Logger::debug(__CLASS__.': downloading metadata for "'.$index.'" from ['.$mdq_url.']'); try { - $xmldata = \SimpleSAML\Utils\HTTP::fetch($mdx_url); - } catch (Exception $e) { - SimpleSAML\Logger::warning('Fetching metadata for '.$index.': '.$e->getMessage()); + $xmldata = HTTP::fetch($mdq_url); + } catch (\Exception $e) { + Logger::warning('Fetching metadata for '.$index.': '.$e->getMessage()); } if (empty($xmldata)) { $error = error_get_last(); - throw new Exception( - 'Error downloading metadata for "'.$index.'" from "'.$mdx_url.'": '.$error['message'] + throw new \Exception( + 'Error downloading metadata for "'.$index.'" from "'.$mdq_url.'": '.$error['message'] ); } /** @var string $xmldata */ - $entity = SimpleSAML_Metadata_SAMLParser::parseString($xmldata); - SimpleSAML\Logger::debug('MetaData - Handler.MDX: Completed parsing of ['.$mdx_url.']'); + $entity = \SimpleSAML_Metadata_SAMLParser::parseString($xmldata); + Logger::debug(__CLASS__.': completed parsing of ['.$mdq_url.']'); if ($this->validateFingerprint !== null) { if (!$entity->validateFingerprint($this->validateFingerprint)) { - throw new Exception('Error, could not verify signature for entity: '.$index.'".'); + throw new \Exception(__CLASS__.': error, could not verify signature for entity: '.$index.'".'); } } $data = self::getParsedSet($entity, $set); if ($data === null) { - throw new Exception('No metadata for set "'.$set.'" available from "'.$index.'".'); + throw new \Exception(__CLASS__.': no metadata for set "'.$set.'" available from "'.$index.'".'); } $this->writeToCache($set, $index, $data); return $data; } - } diff --git a/lib/_autoload_modules.php b/lib/_autoload_modules.php index cf7e051b6523d597156141437514ff111332bbad..c600388533e108848d0735efb4558c732ff5bc2c 100644 --- a/lib/_autoload_modules.php +++ b/lib/_autoload_modules.php @@ -22,6 +22,16 @@ function temporaryLoader($class) if (!strstr($class, 'SimpleSAML_')) { return; // not a valid class name for old classes } + $original = $class; + + // list of classes that have been renamed or moved + $renamed = array( + 'SimpleSAML_Metadata_MetaDataStorageHandlerMDX' => 'SimpleSAML_Metadata_Sources_MDQ', + ); + if (array_key_exists($class, $renamed)) { + // the class has been renamed, try to load it and create an alias + $class = $renamed[$class]; + } // try to load it from the corresponding file $path = explode('_', $class); @@ -39,8 +49,8 @@ function temporaryLoader($class) $new = join('\\', $path); if (class_exists($new, false) || interface_exists($new, false)) { // do not try to autoload it if it doesn't exist! It should! - class_alias($new, $class); - SimpleSAML\Logger::warning("The class or interface '$class' is now using namespaces, please use '$new'."); + class_alias($new, $original); + SimpleSAML\Logger::warning("The class or interface '$original' is now using namespaces, please use '$new'."); } }