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

Reformat SimpleSAML_SessionHandlerCookie.

parent 5d3e4cce
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* This file is part of SimpleSAMLphp. See the file COPYING in the * This file is part of SimpleSAMLphp. See the file COPYING in the root of the distribution for licence information.
* root of the distribution for licence information.
* *
* This file defines a base class for session handlers that need to store * This file defines a base class for session handlers that need to store the session id in a cookie. It takes care of
* the session id in a cookie. It takes care of storing and retrieving the * storing and retrieving the session id.
* session id.
* *
* @author Olav Morken, UNINETT AS. <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS. <andreas.solberg@uninett.no>
* @package SimpleSAMLphp * @package SimpleSAMLphp
* @abstract * @abstract
*/ */
abstract class SimpleSAML_SessionHandlerCookie abstract class SimpleSAML_SessionHandlerCookie extends SimpleSAML_SessionHandler
extends SimpleSAML_SessionHandler { {
/** /**
* This variable contains the current session id. * This variable contains the current session id.
* *
* @var string|null * @var string|null
*/ */
private $session_id = NULL; private $session_id = null;
/** /**
...@@ -35,10 +34,9 @@ extends SimpleSAML_SessionHandler { ...@@ -35,10 +34,9 @@ extends SimpleSAML_SessionHandler {
* This constructor initializes the session id based on what we receive in a cookie. We create a new session id and * This constructor initializes the session id based on what we receive in a cookie. We create a new session id and
* set a cookie with this id if we don't have a session id. * set a cookie with this id if we don't have a session id.
*/ */
protected function __construct() { protected function __construct()
/* Call the constructor in the base class in case it should {
* become necessary in the future. // call the constructor in the base class in case it should become necessary in the future
*/
parent::__construct(); parent::__construct();
$config = SimpleSAML_Configuration::getInstance(); $config = SimpleSAML_Configuration::getInstance();
...@@ -51,7 +49,8 @@ extends SimpleSAML_SessionHandler { ...@@ -51,7 +49,8 @@ extends SimpleSAML_SessionHandler {
* *
* @return string The new session id. * @return string The new session id.
*/ */
public function newSessionId() { public function newSessionId()
{
$this->session_id = self::createSessionID(); $this->session_id = self::createSessionID();
SimpleSAML_Session::createSession($this->session_id); SimpleSAML_Session::createSession($this->session_id);
$this->setCookie($this->cookie_name, $this->session_id); $this->setCookie($this->cookie_name, $this->session_id);
...@@ -65,16 +64,17 @@ extends SimpleSAML_SessionHandler { ...@@ -65,16 +64,17 @@ extends SimpleSAML_SessionHandler {
* *
* @return string The session id saved in the cookie. * @return string The session id saved in the cookie.
*/ */
public function getCookieSessionId() { public function getCookieSessionId()
if ($this->session_id === NULL) { {
if ($this->session_id === null) {
if (self::hasSessionCookie()) { if (self::hasSessionCookie()) {
/* Attempt to retrieve the session id from the cookie. */ // attempt to retrieve the session id from the cookie
$this->session_id = $_COOKIE[$this->cookie_name]; $this->session_id = $_COOKIE[$this->cookie_name];
} }
/* Check if we have a valid session id. */ // check if we have a valid session id
if (!self::isValidSessionID($this->session_id)) { if (!self::isValidSessionID($this->session_id)) {
/* We don't have a valid session. Create a new session id. */ // we don't have a valid session. Create a new session id
return self::newSessionId(); return self::newSessionId();
} }
} }
...@@ -88,8 +88,8 @@ extends SimpleSAML_SessionHandler { ...@@ -88,8 +88,8 @@ extends SimpleSAML_SessionHandler {
* *
* @return string The session cookie name. * @return string The session cookie name.
*/ */
public function getSessionCookieName() { public function getSessionCookieName()
{
return $this->cookie_name; return $this->cookie_name;
} }
...@@ -99,7 +99,8 @@ extends SimpleSAML_SessionHandler { ...@@ -99,7 +99,8 @@ extends SimpleSAML_SessionHandler {
* *
* @return string A random session id. * @return string A random session id.
*/ */
private static function createSessionID() { private static function createSessionID()
{
return bin2hex(openssl_random_pseudo_bytes(16)); return bin2hex(openssl_random_pseudo_bytes(16));
} }
...@@ -112,20 +113,21 @@ extends SimpleSAML_SessionHandler { ...@@ -112,20 +113,21 @@ extends SimpleSAML_SessionHandler {
* *
* @return boolean True if this session ID is valid, false otherwise. * @return boolean True if this session ID is valid, false otherwise.
*/ */
private static function isValidSessionID($session_id) { private static function isValidSessionID($session_id)
{
if (!is_string($session_id)) { if (!is_string($session_id)) {
return FALSE; return false;
} }
if (strlen($session_id) != 32) { if (strlen($session_id) != 32) {
return FALSE; return false;
} }
if (preg_match('/[^0-9a-f]/', $session_id)) { if (preg_match('/[^0-9a-f]/', $session_id)) {
return FALSE; return false;
} }
return TRUE; return true;
} }
...@@ -136,9 +138,8 @@ extends SimpleSAML_SessionHandler { ...@@ -136,9 +138,8 @@ extends SimpleSAML_SessionHandler {
* *
* @return boolean True if it was set, false otherwise. * @return boolean True if it was set, false otherwise.
*/ */
public function hasSessionCookie() { public function hasSessionCookie()
{
return array_key_exists($this->cookie_name, $_COOKIE); return array_key_exists($this->cookie_name, $_COOKIE);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment