summaryrefslogtreecommitdiffstats
path: root/lib/SimpleSAML/SessionHandlerStore.php
blob: 5b15cf8f48661b0ad92404b2c5fdaf5a76b9c67d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

/**
 * Session storage in the datastore.
 *
 * @package simpleSAMLphp
 */
class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie {

	/**
	 * The datastore we save the session to.
	 */
	private $store;

	/**
	 * Initialize the session handlerstore.
	 */
	protected function __construct(SimpleSAML_Store $store) {
		parent::__construct();

		$this->store = $store;
	}


	/**
	 * Load the session from the datastore.
	 *
	 * @param string|NULL $sessionId  The ID of the session we should load, or NULL to use the default.
	 * @return SimpleSAML_Session|NULL  The session object, or NULL if it doesn't exist.
	 */
	public function loadSession($sessionId = NULL) {
		assert('is_string($sessionId) || is_null($sessionId)');

		if ($sessionId === NULL) {
			$sessionId = $this->getCookieSessionId();
		}

		$session = $this->store->get('session', $sessionId);
		if ($session !== NULL) {
			assert('$session instanceof SimpleSAML_Session');
			return $session;
		}

		if (!($this->store instanceof SimpleSAML_Store_Memcache)) {
			return NULL;
		}

		/* For backwards compatibility, check the MemcacheStore object. */
		$store = SimpleSAML_MemcacheStore::find($sessionId);
		if ($store === NULL) {
			return NULL;
		}

		$session = $store->get('SimpleSAMLphp_SESSION');
		if ($session === NULL) {
			return NULL;
		}

		assert('is_string($session)');

		$session = unserialize($session);
		assert('$session instanceof SimpleSAML_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) {

		$sessionId = $session->getSessionId();

		$config = SimpleSAML_Configuration::getInstance();
		$sessionDuration = $config->getInteger('session.duration', 8*60*60);
		$expire = time() + $sessionDuration;

		$this->store->set('session', $sessionId, $session, $expire);
	}

}