Skip to content

Commit a7e38a7

Browse files
committed
Implement datastore API, for session storage and other data.
git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2490 44740490-163a-0410-bde0-09ae8108e29a
1 parent 1bd0844 commit a7e38a7

6 files changed

Lines changed: 237 additions & 102 deletions

File tree

config-templates/config.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,16 @@
381381

382382

383383
/*
384-
* This configuration option allows you to select which session handler
385-
* SimpleSAMLPHP should use to store the session information. Currently
386-
* we have two session handlers:
387-
* - 'phpsession': The default PHP session handler.
388-
* - 'memcache': Stores the session information in one or more
389-
* memcache servers by using the MemcacheStore class.
384+
* Configure the datastore for simpleSAMLphp.
390385
*
391-
* The default session handler is 'phpsession'.
386+
* - 'phpsession': Limited datastore, which uses the PHP session.
387+
* - 'memcache': Key-value datastore, based on memcache.
388+
*
389+
* The default datastore is 'phpsession'.
390+
*
391+
* (This option replaces the old 'session.handler'-option.)
392392
*/
393-
'session.handler' => 'phpsession',
393+
'store.type' => 'phpsession',
394394

395395

396396
/*

lib/SimpleSAML/SessionHandler.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,12 @@ abstract public function loadSession();
8181
*/
8282
private static function createSessionHandler() {
8383

84-
/* Get the configuration. */
85-
$config = SimpleSAML_Configuration::getInstance();
86-
assert($config instanceof SimpleSAML_Configuration);
87-
88-
/* Get the session handler option from the configuration. */
89-
$handler = $config->getString('session.handler', 'phpsession');
90-
$handler = strtolower($handler);
91-
92-
switch ($handler) {
93-
case 'phpsession':
94-
$sh = new SimpleSAML_SessionHandlerPHP();
95-
break;
96-
case 'memcache':
97-
$sh = new SimpleSAML_SessionHandlerMemcache();
98-
break;
99-
default:
100-
throw new SimpleSAML_Error_Exception(
101-
'Invalid session handler specified in the \'session.handler\'-option.');
84+
$store = SimpleSAML_Store::getInstance();
85+
if ($store === FALSE) {
86+
self::$sessionHandler = new SimpleSAML_SessionHandlerPHP();
87+
} else {
88+
self::$sessionHandler = new SimpleSAML_SessionHandlerStore($store);
10289
}
103-
104-
/* Set the session handler. */
105-
self::$sessionHandler = $sh;
10690
}
10791

10892

lib/SimpleSAML/SessionHandlerMemcache.php

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* Session storage in the datastore.
5+
*
6+
* @package simpleSAMLphp
7+
* @version $Id$
8+
*/
9+
class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie {
10+
11+
/**
12+
* The datastore we save the session to.
13+
*/
14+
private $store;
15+
16+
/**
17+
* Initialize the session handlerstore.
18+
*/
19+
protected function __construct(SimpleSAML_Store $store) {
20+
parent::__construct();
21+
22+
$this->store = $store;
23+
}
24+
25+
26+
/**
27+
* Load the session from the datastore.
28+
*
29+
* @return SimpleSAML_Session|NULL The session object, or NULL if it doesn't exist.
30+
*/
31+
public function loadSession() {
32+
33+
$session = $this->store->get('session', $this->session_id);
34+
if ($session !== NULL) {
35+
assert('$session instanceof SimpleSAML_Session');
36+
return $session;
37+
}
38+
39+
if (!($this->store instanceof SimpleSAML_Store_Memcache)) {
40+
return NULL;
41+
}
42+
43+
/* For backwards compatibility, check the MemcacheStore object. */
44+
$store = SimpleSAML_MemcacheStore::find($this->session_id);
45+
if ($store === NULL) {
46+
return NULL;
47+
}
48+
49+
$session = $store->get('SimpleSAMLphp_SESSION');
50+
if ($session === NULL) {
51+
return NULL;
52+
}
53+
54+
assert('is_string($session)');
55+
56+
$session = unserialize($session);
57+
assert('$session instanceof SimpleSAML_Session');
58+
59+
return $session;
60+
}
61+
62+
63+
/**
64+
* Save the current session to the datastore.
65+
*
66+
* @param SimpleSAML_Session $session The session object we should save.
67+
*/
68+
public function saveSession(SimpleSAML_Session $session) {
69+
70+
$config = SimpleSAML_Configuration::getInstance();
71+
$sessionDuration = $config->getInteger('session.duration', 8*60*60);
72+
$expire = time() + $sessionDuration;
73+
74+
$this->store->set('session', $this->session_id, $session, $expire);
75+
}
76+
77+
}

lib/SimpleSAML/Store.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* Base class for datastores.
5+
*
6+
* @package simpleSAMLphp
7+
* @version $Id$
8+
*/
9+
abstract class SimpleSAML_Store {
10+
11+
/**
12+
* Our singleton instance.
13+
*
14+
* This is FALSE if the datastore isn't enabled, and NULL
15+
* if we haven't attempted to initialize it.
16+
*
17+
* @var SimpleSAML_Store|FALSE|NULL
18+
*/
19+
private static $instance;
20+
21+
22+
/**
23+
* Retrieve our singleton instance.
24+
*
25+
* @return SimpleSAML_Store|FALSE The datastore, or FALSE if it isn't enabled.
26+
*/
27+
public static function getInstance() {
28+
29+
if (self::$instance !== NULL) {
30+
return self::$instance;
31+
}
32+
33+
$config = SimpleSAML_Configuration::getInstance();
34+
$storeType = $config->getString('store.type', NULL);
35+
if ($storeType === NULL) {
36+
$storeType = $config->getString('session.handler', 'phpsession');
37+
}
38+
39+
switch ($storeType) {
40+
case 'phpsession':
41+
/* We cannot support advanced features with the PHP session store. */
42+
self::$instance = FALSE;
43+
break;
44+
case 'memcache':
45+
self::$instance = new SimpleSAML_Store_Memcache();
46+
break;
47+
default:
48+
throw new SimpleSAML_Error_Exception('Unknown datastore type: ' . var_export($storeType, TRUE));
49+
}
50+
51+
return self::$instance;
52+
}
53+
54+
55+
/**
56+
* Retrieve a value from the datastore.
57+
*
58+
* @param string $type The datatype.
59+
* @param string $key The key.
60+
* @return mixed|NULL The value.
61+
*/
62+
abstract public function get($type, $key);
63+
64+
65+
/**
66+
* Save a value to the datastore.
67+
*
68+
* @param string $type The datatype.
69+
* @param string $key The key.
70+
* @param mixed $value The value.
71+
* @param int|NULL $expire The expiration time (unix timestamp), or NULL if it never expires.
72+
*/
73+
abstract public function set($type, $key, $value, $expire = NULL);
74+
75+
76+
/**
77+
* Delete a value from the datastore.
78+
*
79+
* @param string $type The datatype.
80+
* @param string $key The key.
81+
*/
82+
abstract public function delete($type, $key);
83+
84+
}

lib/SimpleSAML/Store/Memcache.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* A memcache based datastore.
5+
*
6+
* @package simpleSAMLphp
7+
* @version $Id$
8+
*/
9+
class SimpleSAML_Store_Memcache extends SimpleSAML_Store {
10+
11+
/**
12+
* Initialize the memcache datastore.
13+
*/
14+
protected function __construct() {
15+
}
16+
17+
18+
/**
19+
* Retrieve a value from the datastore.
20+
*
21+
* @param string $type The datatype.
22+
* @param string $key The key.
23+
* @return mixed|NULL The value.
24+
*/
25+
public function get($type, $key) {
26+
assert('is_string($type)');
27+
assert('is_string($key)');
28+
29+
return SimpleSAML_Memcache::get('simpleSAMLphp.' . $type . '.' . $key);
30+
}
31+
32+
33+
/**
34+
* Save a value to the datastore.
35+
*
36+
* @param string $type The datatype.
37+
* @param string $key The key.
38+
* @param mixed $value The value.
39+
* @param int|NULL $expire The expiration time (unix timestamp), or NULL if it never expires.
40+
*/
41+
public function set($type, $key, $value, $expire = NULL) {
42+
assert('is_string($type)');
43+
assert('is_string($key)');
44+
assert('is_null($expire) || (is_int($expire) && $expire > 2592000)');
45+
46+
SimpleSAML_Memcache::set('simpleSAMLphp.' . $type . '.' . $key, $value, $expire);
47+
}
48+
49+
50+
/**
51+
* Delete a value from the datastore.
52+
*
53+
* @param string $type The datatype.
54+
* @param string $key The key.
55+
*/
56+
public function delete($type, $key) {
57+
assert('is_string($type)');
58+
assert('is_string($key)');
59+
60+
SimpleSAML_Memcache::delete('simpleSAMLphp.' . $type . '.' . $key, $value, $expire);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)