Skip to content
Snippets Groups Projects
Commit bd7bb208 authored by Sergio Gómez's avatar Sergio Gómez
Browse files

SimpleSAML_SessionHandler* classes refactorized to PSR-4

parent 9ffc119f
Branches
Tags
No related merge requests found
......@@ -152,7 +152,7 @@ class SimpleSAML_Session implements Serializable
}
if ($transient) { // transient session
$sh = SimpleSAML_SessionHandler::getSessionHandler();
$sh = \SimpleSAML\SessionHandler::getSessionHandler();
$this->trackid = 'TR'.bin2hex(openssl_random_pseudo_bytes(4));
SimpleSAML\Logger::setTrackId($this->trackid);
$this->transient = true;
......@@ -166,7 +166,7 @@ class SimpleSAML_Session implements Serializable
$this->sessionId = $sh->newSessionId();
}
} else { // regular session
$sh = SimpleSAML_SessionHandler::getSessionHandler();
$sh = \SimpleSAML\SessionHandler::getSessionHandler();
$this->sessionId = $sh->newSessionId();
$sh->setCookie($sh->getSessionCookieName(), $this->sessionId, $sh->getCookieParams());
......@@ -318,7 +318,7 @@ class SimpleSAML_Session implements Serializable
{
assert('is_string($sessionId) || is_null($sessionId)');
$sh = SimpleSAML_SessionHandler::getSessionHandler();
$sh = \SimpleSAML\SessionHandler::getSessionHandler();
if ($sessionId === null) {
$checkToken = true;
......@@ -439,7 +439,7 @@ class SimpleSAML_Session implements Serializable
$this->dirty = false;
$this->callback_registered = false;
$sh = SimpleSAML_SessionHandler::getSessionHandler();
$sh = \SimpleSAML\SessionHandler::getSessionHandler();
try {
$sh->saveSession($this);
......@@ -462,8 +462,8 @@ class SimpleSAML_Session implements Serializable
public function cleanup()
{
$this->save();
$sh = SimpleSAML_SessionHandler::getSessionHandler();
if ($sh instanceof SimpleSAML_SessionHandlerPHP) {
$sh = \SimpleSAML\SessionHandler::getSessionHandler();
if ($sh instanceof \SimpleSAML\SessionHandlerPHP) {
$sh->restorePrevious();
}
}
......@@ -633,7 +633,7 @@ class SimpleSAML_Session implements Serializable
$this->authData[$authority] = $data;
$this->authToken = SimpleSAML\Utils\Random::generateID();
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$sessionHandler = \SimpleSAML\SessionHandler::getSessionHandler();
if (!$this->transient && (!empty($data['RememberMe']) || $this->rememberMeExpire) &&
$globalConfig->getBoolean('session.rememberme.enable', false)
......@@ -760,7 +760,7 @@ class SimpleSAML_Session implements Serializable
*/
public function updateSessionCookies($params = null)
{
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$sessionHandler = \SimpleSAML\SessionHandler::getSessionHandler();
if ($this->sessionId !== null) {
$sessionHandler->setCookie($sessionHandler->getSessionCookieName(), $this->sessionId, $params);
......@@ -1040,7 +1040,7 @@ class SimpleSAML_Session implements Serializable
*/
public function hasSessionCookie()
{
$sh = SimpleSAML_SessionHandler::getSessionHandler();
$sh = \SimpleSAML\SessionHandler::getSessionHandler();
return $sh->hasSessionCookie();
}
......
<?php
/**
* This file is part of SimpleSAMLphp. See the file COPYING in the
* root of the distribution for licence information.
......@@ -12,7 +11,10 @@
* @author Olav Morken, UNINETT AS. <andreas.solberg@uninett.no>
* @package SimpleSAMLphp
*/
abstract class SimpleSAML_SessionHandler
namespace SimpleSAML;
abstract class SessionHandler
{
......@@ -21,7 +23,7 @@ abstract class SimpleSAML_SessionHandler
* instance of the session handler. This variable will be NULL if
* we haven't instantiated a session handler yet.
*
* @var SimpleSAML_SessionHandler
* @var \SimpleSAML\SessionHandler
*/
protected static $sessionHandler = null;
......@@ -31,7 +33,7 @@ abstract class SimpleSAML_SessionHandler
* The session handler will be instantiated if this is the first call
* to this function.
*
* @return SimpleSAML_SessionHandler The current session handler.
* @return \SimpleSAML\SessionHandler The current session handler.
*/
public static function getSessionHandler()
{
......@@ -80,17 +82,17 @@ abstract class SimpleSAML_SessionHandler
/**
* Save the session.
*
* @param SimpleSAML_Session $session The session object we should save.
* @param \SimpleSAML_Session $session The session object we should save.
*/
abstract public function saveSession(SimpleSAML_Session $session);
abstract public function saveSession(\SimpleSAML_Session $session);
/**
* Load the session.
*
* @param string|NULL $sessionId The ID of the session we should load, or null to use the default.
* @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.
* @return \SimpleSAML_Session|null The session object, or null if it doesn't exist.
*/
abstract public function loadSession($sessionId = null);
......@@ -117,13 +119,12 @@ abstract class SimpleSAML_SessionHandler
*/
private static function createSessionHandler()
{
$store = \SimpleSAML\Store::getInstance();
if ($store === false) {
self::$sessionHandler = new SimpleSAML_SessionHandlerPHP();
self::$sessionHandler = new SessionHandlerPHP();
} else {
/** @var \SimpleSAML\Store $store At this point, $store can only be an object */
self::$sessionHandler = new SimpleSAML_SessionHandlerStore($store);
self::$sessionHandler = new SessionHandlerStore($store);
}
}
......@@ -149,7 +150,7 @@ abstract class SimpleSAML_SessionHandler
*/
public function getCookieParams()
{
$config = SimpleSAML_Configuration::getInstance();
$config = \SimpleSAML_Configuration::getInstance();
return array(
'lifetime' => $config->getInteger('session.cookie.lifetime', 0),
......
......@@ -11,7 +11,12 @@
* @package SimpleSAMLphp
* @abstract
*/
abstract class SimpleSAML_SessionHandlerCookie extends SimpleSAML_SessionHandler
namespace SimpleSAML;
use SimpleSAML\Utils\HTTP;
abstract class SessionHandlerCookie extends SessionHandler
{
/**
......@@ -39,7 +44,7 @@ abstract class SimpleSAML_SessionHandlerCookie extends SimpleSAML_SessionHandler
// call the constructor in the base class in case it should become necessary in the future
parent::__construct();
$config = SimpleSAML_Configuration::getInstance();
$config = \SimpleSAML_Configuration::getInstance();
$this->cookie_name = $config->getString('session.cookie.name', 'SimpleSAMLSessionID');
}
......@@ -52,7 +57,7 @@ abstract class SimpleSAML_SessionHandlerCookie extends SimpleSAML_SessionHandler
public function newSessionId()
{
$this->session_id = self::createSessionID();
SimpleSAML_Session::createSession($this->session_id);
\SimpleSAML_Session::createSession($this->session_id);
return $this->session_id;
}
......@@ -163,6 +168,6 @@ abstract class SimpleSAML_SessionHandlerCookie extends SimpleSAML_SessionHandler
$params = $this->getCookieParams();
}
\SimpleSAML\Utils\HTTP::setCookie($sessionName, $sessionID, $params, true);
HTTP::setCookie($sessionName, $sessionID, $params, true);
}
}
<?php
/**
* This file is part of SimpleSAMLphp. See the file COPYING in the root of the distribution for licence information.
*
......@@ -9,7 +8,13 @@
* @author Olav Morken, UNINETT AS. <andreas.solberg@uninett.no>
* @package SimpleSAMLphp
*/
class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
namespace SimpleSAML;
use SimpleSAML\Error\CannotSetCookie;
use SimpleSAML\Utils\HTTP;
class SessionHandlerPHP extends SessionHandler
{
/**
......@@ -34,14 +39,14 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
/**
* Initialize the PHP session handling. This constructor is protected because it should only be called from
* SimpleSAML_SessionHandler::createSessionHandler(...).
* \SimpleSAML\SessionHandler::createSessionHandler(...).
*/
protected function __construct()
{
// call the parent constructor in case it should become necessary in the future
parent::__construct();
$config = SimpleSAML_Configuration::getInstance();
$config = \SimpleSAML_Configuration::getInstance();
$this->cookie_name = $config->getString('session.phpsession.cookiename', null);
if (function_exists('session_status') && defined('PHP_SESSION_ACTIVE')) { // PHP >= 5.4
......@@ -52,7 +57,7 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
if ($previous_session) {
if (session_name() === $this->cookie_name || $this->cookie_name === null) {
SimpleSAML\Logger::warning(
Logger::warning(
'There is already a PHP session with the same name as SimpleSAMLphp\'s session, or the '.
"'session.phpsession.cookiename' configuration option is not set. Make sure to set ".
"SimpleSAMLphp's cookie name with a value not used by any other applications."
......@@ -167,7 +172,7 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
{
// generate new (secure) session id
$sessionId = bin2hex(openssl_random_pseudo_bytes(16));
SimpleSAML_Session::createSession($sessionId);
\SimpleSAML_Session::createSession($sessionId);
return $sessionId;
}
......@@ -178,7 +183,7 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
*
* @return string|null The session id saved in the cookie or null if no session cookie was set.
*
* @throws SimpleSAML_Error_Exception If the cookie is marked as secure but we are not using HTTPS.
* @throws \SimpleSAML_Error_Exception If the cookie is marked as secure but we are not using HTTPS.
*/
public function getCookieSessionId()
{
......@@ -191,8 +196,8 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
$session_cookie_params = session_get_cookie_params();
if ($session_cookie_params['secure'] && !\SimpleSAML\Utils\HTTP::isHTTPS()) {
throw new SimpleSAML_Error_Exception('Session start with secure cookie not allowed on http.');
if ($session_cookie_params['secure'] && !HTTP::isHTTPS()) {
throw new \SimpleSAML_Error_Exception('Session start with secure cookie not allowed on http.');
}
$this->sessionStart();
......@@ -214,9 +219,9 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
/**
* Save the current session to the PHP session array.
*
* @param SimpleSAML_Session $session The session object we should save.
* @param \SimpleSAML_Session $session The session object we should save.
*/
public function saveSession(SimpleSAML_Session $session)
public function saveSession(\SimpleSAML_Session $session)
{
$_SESSION['SimpleSAMLphp_SESSION'] = serialize($session);
}
......@@ -227,9 +232,9 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
*
* @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.
* @return \SimpleSAML_Session|null The session object, or null if it doesn't exist.
*
* @throws SimpleSAML_Error_Exception If it wasn't possible to disable session cookies or we are trying to load a
* @throws \SimpleSAML_Error_Exception If it wasn't possible to disable session cookies or we are trying to load a
* PHP session with a specific identifier and it doesn't match with the current session identifier.
*/
public function loadSession($sessionId = null)
......@@ -241,13 +246,13 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
// session not initiated with getCookieSessionId(), start session without setting cookie
$ret = ini_set('session.use_cookies', '0');
if ($ret === false) {
throw new SimpleSAML_Error_Exception('Disabling PHP option session.use_cookies failed.');
throw new \SimpleSAML_Error_Exception('Disabling PHP option session.use_cookies failed.');
}
session_id($sessionId);
$this->sessionStart();
} elseif ($sessionId !== session_id()) {
throw new SimpleSAML_Error_Exception('Cannot load PHP session with a specific ID.');
throw new \SimpleSAML_Error_Exception('Cannot load PHP session with a specific ID.');
}
} elseif (session_id() === '') {
self::getCookieSessionId();
......@@ -288,17 +293,17 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
* @return array The cookie parameters for our sessions.
* @link http://www.php.net/manual/en/function.session-get-cookie-params.php
*
* @throws SimpleSAML_Error_Exception If both 'session.phpsession.limitedpath' and 'session.cookie.path' options
* @throws \SimpleSAML_Error_Exception If both 'session.phpsession.limitedpath' and 'session.cookie.path' options
* are set at the same time in the configuration.
*/
public function getCookieParams()
{
$config = SimpleSAML_Configuration::getInstance();
$config = \SimpleSAML_Configuration::getInstance();
$ret = parent::getCookieParams();
if ($config->hasValue('session.phpsession.limitedpath') && $config->hasValue('session.cookie.path')) {
throw new SimpleSAML_Error_Exception(
throw new \SimpleSAML_Error_Exception(
'You cannot set both the session.phpsession.limitedpath and session.cookie.path options.'
);
} elseif ($config->hasValue('session.phpsession.limitedpath')) {
......@@ -329,17 +334,17 @@ class SimpleSAML_SessionHandlerPHP extends SimpleSAML_SessionHandler
$cookieParams = session_get_cookie_params();
}
if ($cookieParams['secure'] && !\SimpleSAML\Utils\HTTP::isHTTPS()) {
throw new \SimpleSAML\Error\CannotSetCookie(
if ($cookieParams['secure'] && !HTTP::isHTTPS()) {
throw new CannotSetCookie(
'Setting secure cookie on plain HTTP is not allowed.',
\SimpleSAML\Error\CannotSetCookie::SECURE_COOKIE
CannotSetCookie::SECURE_COOKIE
);
}
if (headers_sent()) {
throw new \SimpleSAML\Error\CannotSetCookie(
throw new CannotSetCookie(
'Headers already sent.',
\SimpleSAML\Error\CannotSetCookie::HEADERS_SENT
CannotSetCookie::HEADERS_SENT
);
}
......
......@@ -6,7 +6,10 @@
*
* @package SimpleSAMLphp
*/
class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie
namespace SimpleSAML;
class SessionHandlerStore extends SessionHandlerCookie
{
/**
......@@ -22,7 +25,7 @@ class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie
*
* @param \SimpleSAML\Store $store The store to use.
*/
protected function __construct(\SimpleSAML\Store $store)
protected function __construct(Store $store)
{
parent::__construct();
......@@ -35,7 +38,7 @@ class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie
*
* @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.
* @return \SimpleSAML_Session|null The session object, or null if it doesn't exist.
*/
public function loadSession($sessionId = null)
{
......@@ -62,18 +65,16 @@ class SimpleSAML_SessionHandlerStore extends SimpleSAML_SessionHandlerCookie
/**
* Save a session to the data store.
*
* @param SimpleSAML_Session $session The session object we should save.
* @param \SimpleSAML_Session $session The session object we should save.
*/
public function saveSession(SimpleSAML_Session $session)
public function saveSession(\SimpleSAML_Session $session)
{
$sessionId = $session->getSessionId();
$config = SimpleSAML_Configuration::getInstance();
$config = \SimpleSAML_Configuration::getInstance();
$sessionDuration = $config->getInteger('session.duration', 8 * 60 * 60);
$expire = time() + $sessionDuration;
$this->store->set('session', $sessionId, $session, $expire);
}
}
......@@ -37,7 +37,7 @@ class sspmod_core_Auth_Process_ExtendIdPSession extends SimpleSAML_Auth_Processi
}
/* Or if session lifetime is more than zero */
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$sessionHandler = \SimpleSAML\SessionHandler::getSessionHandler();
$cookieParams = $sessionHandler->getCookieParams();
if ($cookieParams['lifetime'] > 0) {
$session->updateSessionCookies();
......
......@@ -49,7 +49,7 @@ if (!empty($_REQUEST['username']) || !empty($password)) {
}
if ($source->getRememberUsernameEnabled()) {
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$sessionHandler = \SimpleSAML\SessionHandler::getSessionHandler();
$params = $sessionHandler->getCookieParams();
$params['expire'] = time();
$params['expire'] += (isset($_REQUEST['remember_username']) && $_REQUEST['remember_username'] == 'Yes' ? 31536000 : -300);
......
......@@ -53,7 +53,7 @@ if ($organizations === NULL || !empty($organization)) {
if (!empty($username) && !empty($password)) {
if ($source->getRememberUsernameEnabled()) {
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$sessionHandler = \SimpleSAML\SessionHandler::getSessionHandler();
$params = $sessionHandler->getCookieParams();
$params['expire'] = time();
$params['expire'] += (isset($_REQUEST['remember_username']) && $_REQUEST['remember_username'] == 'Yes' ? 31536000 : -300);
......
......@@ -255,7 +255,7 @@ class sspmod_saml_SP_LogoutStore {
$sessionIndexes = array_keys($sessions);
}
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$sessionHandler = \SimpleSAML\SessionHandler::getSessionHandler();
$numLoggedOut = 0;
foreach ($sessionIndexes as $sessionIndex) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment