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

Reformat code.

parent e2edba3c
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* Class for handling metadata files stored in a database. * Class for handling metadata files stored in a database.
* *
* This class has been based off a previous version written by * This class has been based off a previous version written by
* mooknarf@gmail.com and patched to work with the latest version * mooknarf@gmail.com and patched to work with the latest version
* of simpleSAMLphp * of SimpleSAMLphp
* *
* @author Tyler Antonio, University of Alberta <tantonio@ualberta.ca> * @author Tyler Antonio, University of Alberta <tantonio@ualberta.ca>
* @package simpleSAMLphp * @package SimpleSAMLphp
*/ */
class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_MetaDataStorageSource
class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_MetaDataStorageSource{ {
/** /**
* The PDO object * The PDO object
*/ */
private $db; private $db;
/** /**
* Prefix to apply to the metadata table * Prefix to apply to the metadata table
*/ */
private $tablePrefix; private $tablePrefix;
/** /**
* This is an associative array which stores the different metadata sets we have loaded. * This is an associative array which stores the different metadata sets we have loaded.
*/ */
private $cachedMetadata = array(); private $cachedMetadata = array();
/** /**
* All the metadata sets supported by this MetaDataStorageHandler * All the metadata sets supported by this MetaDataStorageHandler
*/ */
public $supportedSets = array( public $supportedSets = array(
'adfs-idp-hosted', 'adfs-idp-hosted',
'adfs-sp-remote', 'adfs-sp-remote',
'saml20-idp-hosted', 'saml20-idp-hosted',
'saml20-idp-remote', 'saml20-idp-remote',
'saml20-sp-remote', 'saml20-sp-remote',
'shib13-idp-hosted', 'shib13-idp-hosted',
'shib13-idp-remote', 'shib13-idp-remote',
'shib13-sp-hosted', 'shib13-sp-hosted',
'shib13-sp-remote', 'shib13-sp-remote',
'wsfed-idp-remote', 'wsfed-idp-remote',
'wsfed-sp-hosted' 'wsfed-sp-hosted'
); );
/** /**
* This constructor initializes the PDO metadata storage handler with the specified * This constructor initializes the PDO metadata storage handler with the specified
* configuration. The configuration is an associative array with the following * configuration. The configuration is an associative array with the following
* possible elements (set in config.php): * possible elements (set in config.php):
* - 'usePersistentConnection': TRUE/FALSE if database connection should be * - 'usePersistentConnection': TRUE/FALSE if database connection should be
* persistent. * persistent.
* *
* - 'dsn': The database connection string. * - 'dsn': The database connection string.
* *
* - 'username': Database user name * - 'username': Database user name
* *
* - 'password': Password for the database user. * - 'password': Password for the database user.
* *
* @param array $config An associtive array with the configuration for this handler. * @param array $config An associtive array with the configuration for this handler.
*/ */
public function __construct($config) { public function __construct($config)
assert('is_array($config)'); {
assert('is_array($config)');
$this->db = SimpleSAML\Database::getInstance();
} $this->db = SimpleSAML\Database::getInstance();
}
/**
* This function loads the given set of metadata from a file to a configured database. /**
* This function returns NULL if it is unable to locate the given set in the metadata directory. * This function loads the given set of metadata from a file to a configured database.
* * 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 $metadata Associative array with the metadata, or NULL if we are unable to load metadata from the given file. * @param string $set The set of metadata we are loading.
*/ *
private function load($set) { * @return array $metadata Associative array with the metadata, or NULL if we are unable to load metadata from the
assert('is_string($set)'); * given file.
*
$tableName = $this->getTableName($set); * @throws Exception If a database error occurs.
*/
if (!in_array($set, $this->supportedSets)) { private function load($set)
return NULL; {
} assert('is_string($set)');
$stmt = $this->db->read("SELECT entity_id, entity_data FROM $tableName"); $tableName = $this->getTableName($set);
if ($stmt->execute()) {
$metadata = array(); if (!in_array($set, $this->supportedSets)) {
return null;
while ($d = $stmt->fetch()) { }
$metadata[$d['entity_id']] = json_decode($d['entity_data'], TRUE);
} $stmt = $this->db->read("SELECT entity_id, entity_data FROM $tableName");
if ($stmt->execute()) {
return $metadata; $metadata = array();
} else {
throw new Exception('PDO metadata handler: Database error: ' . var_export($this->db->getLastError(), TRUE)); while ($d = $stmt->fetch()) {
} $metadata[$d['entity_id']] = json_decode($d['entity_data'], true);
} }
return $metadata;
/** } else {
* Retrieve a list of all available metadata for a given set. throw new Exception('PDO metadata handler: Database error: '.var_export($this->db->getLastError(), true));
* }
* @param string $set The set we are looking for metadata in. }
* @return array $metadata An associative array with all the metadata for the given set.
*/
public function getMetadataSet($set) { /**
assert('is_string($set)'); * Retrieve a list of all available metadata for a given set.
*
if (array_key_exists($set, $this->cachedMetadata)) { * @param string $set The set we are looking for metadata in.
return $this->cachedMetadata[$set]; *
} * @return array $metadata An associative array with all the metadata for the given set.
*/
$metadataSet = $this->load($set); public function getMetadataSet($set)
if ($metadataSet === NULL) { {
$metadataSet = array(); assert('is_string($set)');
}
if (array_key_exists($set, $this->cachedMetadata)) {
foreach ($metadataSet AS $entityId => &$entry) { return $this->cachedMetadata[$set];
if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) { }
$entry['entityid'] = $this->generateDynamicHostedEntityID($set);
} else { $metadataSet = $this->load($set);
$entry['entityid'] = $entityId; if ($metadataSet === null) {
} $metadataSet = array();
} }
$this->cachedMetadata[$set] = $metadataSet; foreach ($metadataSet as $entityId => &$entry) {
return $metadataSet; if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
} $entry['entityid'] = $this->generateDynamicHostedEntityID($set);
} else {
private function generateDynamicHostedEntityID($set) { $entry['entityid'] = $entityId;
assert('is_string($set)'); }
}
/* Get the configuration. */
$baseurl = \SimpleSAML\Utils\HTTP::getBaseURL(); $this->cachedMetadata[$set] = $metadataSet;
return $metadataSet;
if ($set === 'saml20-idp-hosted') { }
return $baseurl . 'saml2/idp/metadata.php';
} elseif($set === 'saml20-sp-hosted') {
return $baseurl . 'saml2/sp/metadata.php'; private function generateDynamicHostedEntityID($set)
} elseif($set === 'shib13-idp-hosted') { {
return $baseurl . 'shib13/idp/metadata.php'; assert('is_string($set)');
} elseif($set === 'shib13-sp-hosted') {
return $baseurl . 'shib13/sp/metadata.php'; // get the configuration
} elseif($set === 'wsfed-sp-hosted') { $baseurl = \SimpleSAML\Utils\HTTP::getBaseURL();
return 'urn:federation:' . \SimpleSAML\Utils\HTTP::getSelfHost();
} elseif($set === 'adfs-idp-hosted') { if ($set === 'saml20-idp-hosted') {
return 'urn:federation:' . \SimpleSAML\Utils\HTTP::getSelfHost() . ':idp'; return $baseurl.'saml2/idp/metadata.php';
} else { } elseif ($set === 'saml20-sp-hosted') {
throw new Exception('Can not generate dynamic EntityID for metadata of this type: [' . $set . ']'); return $baseurl.'saml2/sp/metadata.php';
} } elseif ($set === 'shib13-idp-hosted') {
} return $baseurl.'shib13/idp/metadata.php';
} elseif ($set === 'shib13-sp-hosted') {
/** return $baseurl.'shib13/sp/metadata.php';
* Add metadata to the configured database } elseif ($set === 'wsfed-sp-hosted') {
* return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
* @param string $index Entity ID } elseif ($set === 'adfs-idp-hosted') {
* @param string $set The set to add the metadata to return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
* @param array $entityData Metadata } else {
* @return bool True/False if entry was sucessfully added throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
*/ }
public function addEntry($index, $set, $entityData) { }
assert('is_string($index)');
assert('is_string($set)');
assert('is_array($entityData)'); /**
* Add metadata to the configured database
if (!in_array($set, $this->supportedSets)) { *
return FALSE; * @param string $index Entity ID
} * @param string $set The set to add the metadata to
* @param array $entityData Metadata
$tableName = $this->getTableName($set); *
* @return bool True/False if entry was successfully added
$metadata = $this->db->read("SELECT entity_id, entity_data FROM $tableName WHERE entity_id = :entity_id", array( */
'entity_id' => $index, public function addEntry($index, $set, $entityData)
)); {
assert('is_string($index)');
$retrivedEntityIDs = $metadata->fetch(); assert('is_string($set)');
assert('is_array($entityData)');
$params = array(
'entity_id' => $index, if (!in_array($set, $this->supportedSets)) {
'entity_data' => json_encode($entityData), return false;
); }
if($retrivedEntityIDs !== FALSE && count($retrivedEntityIDs) > 0){ $tableName = $this->getTableName($set);
$stmt = $this->db->write("UPDATE $tableName SET entity_data = :entity_data WHERE entity_id = :entity_id", $params);
} else { $metadata = $this->db->read(
$stmt = $this->db->write("INSERT INTO $tableName (entity_id, entity_data) VALUES (:entity_id, :entity_data)", $params); "SELECT entity_id, entity_data FROM $tableName WHERE entity_id = :entity_id",
} array(
'entity_id' => $index,
return 1 === $stmt->rowCount(); )
} );
/** $retrivedEntityIDs = $metadata->fetch();
* Replace the -'s to an _ in table names for Metadata sets
* since SQL does not allow a - in a table name. $params = array(
* 'entity_id' => $index,
* @param string $table Table 'entity_data' => json_encode($entityData),
* @return string Replaced table name );
*/
private function getTableName($table) { if ($retrivedEntityIDs !== false && count($retrivedEntityIDs) > 0) {
assert('is_string($table)'); $stmt = $this->db->write(
"UPDATE $tableName SET entity_data = :entity_data WHERE entity_id = :entity_id",
return $this->db->applyPrefix(str_replace("-", "_", $this->tablePrefix . $table)); $params
} );
} else {
/** $stmt = $this->db->write(
* Initialize the configured database "INSERT INTO $tableName (entity_id, entity_data) VALUES (:entity_id, :entity_data)",
*/ $params
public function initDatabase() { );
foreach ($this->supportedSets as $set) { }
$tableName = $this->getTableName($set);
$this->db->write("CREATE TABLE IF NOT EXISTS $tableName (entity_id VARCHAR(255) PRIMARY KEY NOT NULL, entity_data TEXT NOT NULL)"); return 1 === $stmt->rowCount();
} }
}
/**
* Replace the -'s to an _ in table names for Metadata sets
* since SQL does not allow a - in a table name.
*
* @param string $table Table
*
* @return string Replaced table name
*/
private function getTableName($table)
{
assert('is_string($table)');
return $this->db->applyPrefix(str_replace("-", "_", $this->tablePrefix.$table));
}
/**
* Initialize the configured database
*/
public function initDatabase()
{
foreach ($this->supportedSets as $set) {
$tableName = $this->getTableName($set);
$this->db->write(
"CREATE TABLE IF NOT EXISTS $tableName (entity_id VARCHAR(255) PRIMARY KEY NOT NULL, entity_data ".
"TEXT NOT 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