Skip to content
Snippets Groups Projects
Commit a7e38a70 authored by Olav Morken's avatar Olav Morken
Browse files

Implement datastore API, for session storage and other data.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2490 44740490-163a-0410-bde0-09ae8108e29a
parent 1bd0844b
No related branches found
No related tags found
No related merge requests found
...@@ -381,16 +381,16 @@ $config = array ( ...@@ -381,16 +381,16 @@ $config = array (
/* /*
* This configuration option allows you to select which session handler * Configure the datastore for simpleSAMLphp.
* SimpleSAMLPHP should use to store the session information. Currently
* we have two session handlers:
* - 'phpsession': The default PHP session handler.
* - 'memcache': Stores the session information in one or more
* memcache servers by using the MemcacheStore class.
* *
* The default session handler is 'phpsession'. * - 'phpsession': Limited datastore, which uses the PHP session.
* - 'memcache': Key-value datastore, based on memcache.
*
* The default datastore is 'phpsession'.
*
* (This option replaces the old 'session.handler'-option.)
*/ */
'session.handler' => 'phpsession', 'store.type' => 'phpsession',
/* /*
......
...@@ -81,28 +81,12 @@ abstract class SimpleSAML_SessionHandler { ...@@ -81,28 +81,12 @@ abstract class SimpleSAML_SessionHandler {
*/ */
private static function createSessionHandler() { private static function createSessionHandler() {
/* Get the configuration. */ $store = SimpleSAML_Store::getInstance();
$config = SimpleSAML_Configuration::getInstance(); if ($store === FALSE) {
assert($config instanceof SimpleSAML_Configuration); self::$sessionHandler = new SimpleSAML_SessionHandlerPHP();
} else {
/* Get the session handler option from the configuration. */ self::$sessionHandler = new SimpleSAML_SessionHandlerStore($store);
$handler = $config->getString('session.handler', 'phpsession');
$handler = strtolower($handler);
switch ($handler) {
case 'phpsession':
$sh = new SimpleSAML_SessionHandlerPHP();
break;
case 'memcache':
$sh = new SimpleSAML_SessionHandlerMemcache();
break;
default:
throw new SimpleSAML_Error_Exception(
'Invalid session handler specified in the \'session.handler\'-option.');
} }
/* Set the session handler. */
self::$sessionHandler = $sh;
} }
......
<?php <?php
/** /**
* This file is part of SimpleSAMLphp. See the file COPYING in the * Session storage in the datastore.
* root of the distribution for licence information.
* *
* This file defines a session handler which uses the MemcacheStore
* class to store data in memcache servers.
*
* @author Olav Morken, UNINETT AS. <andreas.solberg@uninett.no>
* @package simpleSAMLphp * @package simpleSAMLphp
* @version $Id$ * @version $Id$
*/ */
class SimpleSAML_SessionHandlerMemcache class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie {
extends SimpleSAML_SessionHandlerCookie {
/* Initialize the memcache session handling. This constructor is /**
* protected because it should only be called from * The datastore we save the session to.
* SimpleSAML_SessionHandler::createSessionHandler(...).
*/ */
protected function __construct() { private $store;
/* Call parent constructor to allow it to configure the session
* id.
*/
parent::__construct();
}
/** /**
* Save the current session to memcache. * Initialize the session handlerstore.
*
* @param SimpleSAML_Session $session The session object we should save.
*/ */
public function saveSession(SimpleSAML_Session $session) { protected function __construct(SimpleSAML_Store $store) {
parent::__construct();
SimpleSAML_Memcache::set('simpleSAMLphp.session.' . $this->session_id, $session); $this->store = $store;
} }
/** /**
* Load the session from memcache. * Load the session from the datastore.
* *
* @return SimpleSAML_Session|NULL The session object, or NULL if it doesn't exist. * @return SimpleSAML_Session|NULL The session object, or NULL if it doesn't exist.
*/ */
public function loadSession() { public function loadSession() {
$session = SimpleSAML_Memcache::get('simpleSAMLphp.session.' . $this->session_id); $session = $this->store->get('session', $this->session_id);
if ($session !== NULL) { if ($session !== NULL) {
assert('$session instanceof SimpleSAML_Session'); assert('$session instanceof SimpleSAML_Session');
return $session; return $session;
} }
if (!($this->store instanceof SimpleSAML_Store_Memcache)) {
return NULL;
}
/* For backwards compatibility, check the MemcacheStore object. */ /* For backwards compatibility, check the MemcacheStore object. */
$store = SimpleSAML_MemcacheStore::find($this->session_id); $store = SimpleSAML_MemcacheStore::find($this->session_id);
if ($store === NULL) { if ($store === NULL) {
...@@ -70,4 +59,19 @@ extends SimpleSAML_SessionHandlerCookie { ...@@ -70,4 +59,19 @@ extends SimpleSAML_SessionHandlerCookie {
return $session; return $session;
} }
/**
* Save the current session to the datastore.
*
* @param SimpleSAML_Session $session The session object we should save.
*/
public function saveSession(SimpleSAML_Session $session) {
$config = SimpleSAML_Configuration::getInstance();
$sessionDuration = $config->getInteger('session.duration', 8*60*60);
$expire = time() + $sessionDuration;
$this->store->set('session', $this->session_id, $session, $expire);
}
} }
<?php
/**
* Base class for datastores.
*
* @package simpleSAMLphp
* @version $Id$
*/
abstract class SimpleSAML_Store {
/**
* Our singleton instance.
*
* This is FALSE if the datastore isn't enabled, and NULL
* if we haven't attempted to initialize it.
*
* @var SimpleSAML_Store|FALSE|NULL
*/
private static $instance;
/**
* Retrieve our singleton instance.
*
* @return SimpleSAML_Store|FALSE The datastore, or FALSE if it isn't enabled.
*/
public static function getInstance() {
if (self::$instance !== NULL) {
return self::$instance;
}
$config = SimpleSAML_Configuration::getInstance();
$storeType = $config->getString('store.type', NULL);
if ($storeType === NULL) {
$storeType = $config->getString('session.handler', 'phpsession');
}
switch ($storeType) {
case 'phpsession':
/* We cannot support advanced features with the PHP session store. */
self::$instance = FALSE;
break;
case 'memcache':
self::$instance = new SimpleSAML_Store_Memcache();
break;
default:
throw new SimpleSAML_Error_Exception('Unknown datastore type: ' . var_export($storeType, TRUE));
}
return self::$instance;
}
/**
* Retrieve a value from the datastore.
*
* @param string $type The datatype.
* @param string $key The key.
* @return mixed|NULL The value.
*/
abstract public function get($type, $key);
/**
* Save a value to the datastore.
*
* @param string $type The datatype.
* @param string $key The key.
* @param mixed $value The value.
* @param int|NULL $expire The expiration time (unix timestamp), or NULL if it never expires.
*/
abstract public function set($type, $key, $value, $expire = NULL);
/**
* Delete a value from the datastore.
*
* @param string $type The datatype.
* @param string $key The key.
*/
abstract public function delete($type, $key);
}
<?php
/**
* A memcache based datastore.
*
* @package simpleSAMLphp
* @version $Id$
*/
class SimpleSAML_Store_Memcache extends SimpleSAML_Store {
/**
* Initialize the memcache datastore.
*/
protected function __construct() {
}
/**
* Retrieve a value from the datastore.
*
* @param string $type The datatype.
* @param string $key The key.
* @return mixed|NULL The value.
*/
public function get($type, $key) {
assert('is_string($type)');
assert('is_string($key)');
return SimpleSAML_Memcache::get('simpleSAMLphp.' . $type . '.' . $key);
}
/**
* Save a value to the datastore.
*
* @param string $type The datatype.
* @param string $key The key.
* @param mixed $value The value.
* @param int|NULL $expire The expiration time (unix timestamp), or NULL if it never expires.
*/
public function set($type, $key, $value, $expire = NULL) {
assert('is_string($type)');
assert('is_string($key)');
assert('is_null($expire) || (is_int($expire) && $expire > 2592000)');
SimpleSAML_Memcache::set('simpleSAMLphp.' . $type . '.' . $key, $value, $expire);
}
/**
* Delete a value from the datastore.
*
* @param string $type The datatype.
* @param string $key The key.
*/
public function delete($type, $key) {
assert('is_string($type)');
assert('is_string($key)');
SimpleSAML_Memcache::delete('simpleSAMLphp.' . $type . '.' . $key, $value, $expire);
}
}
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