Skip to content
Snippets Groups Projects
Select Git revision
  • 457bd7ad9c038c59796c45a60f7d49b5d57bab5d
  • master default protected
  • cesnet_simplesamlphp-1.19.8
  • elixir_simplesamlphp-1.19.8
  • simplesamlphp-1.19.8
  • cesnet_simplesamlphp-1.19.5
  • simplesamlphp-2.0
  • feature/assets
  • feature/rac-source-selector
  • cleanup/remove-base64-attributes
  • simplesamlphp-1.19
  • elixir_simplesamlphp-1.19.5
  • aarc_idp_hinting
  • feature/validate-authstate-before-processing
  • feature/build-two-tarballs
  • dependabot/composer/twig/twig-3.4.3
  • tvdijen-patch-1
  • unchanged-acs-url-no-www-script
  • feature/translation-improvements
  • symfony6
  • move_tests
  • v1.19.9
  • v2.1.3
  • v2.0.10
  • v2.1.2
  • v2.0.9
  • v2.1.1
  • v2.0.8
  • v2.1.0
  • v2.0.7
  • v2.1.0-rc1
  • v2.0.6
  • v2.0.5
  • 2.0.4-alpha.1
  • v2.0.4-alpha.1
  • v2.0.4
  • v2.0.3
  • v2.0.2
  • v2.0.1-alpha.1
  • v2.0.1
  • v1.19.8
41 results

phpcs.xml

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    SessionHandlerCookie.php 3.26 KiB
    <?php
    
    /**
     * This file is part of SimpleSAMLphp. See the file COPYING in the
     * root of the distribution for licence information.
     *
     * This file defines a base class for session handlers that need to store
     * the session id in a cookie. It takes care of storing and retrieving the
     * session id.
     *
     * @author Olav Morken, UNINETT AS. <andreas.solberg@uninett.no>
     * @package simpleSAMLphp
     * @abstract
     * @version $Id$
     */
    abstract class SimpleSAML_SessionHandlerCookie
    extends SimpleSAML_SessionHandler {
    
    	/* This variable contains the current session id. */
    	protected $session_id = NULL;
    
    
    
    	/* 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.
    	 */
    	protected function __construct() {
    		/* Call the constructor in the base class in case it should
    		 * become necessary in the future.
    		 */
    		parent::__construct();
    
    		/* Attempt to retrieve the session id from the cookie. */
    		if(array_key_exists('SimpleSAMLSessionID', $_COOKIE)) {
    			$this->session_id = $_COOKIE['SimpleSAMLSessionID'];
    		}
    
    		/* Check if we have a valid session id. */
    		if(self::isValidSessionID($this->session_id)) {
    			/* We are done now if it was valid. */
    			return;
    		}
    
    		/* We don't have a valid session. Create a new session id. */
    		$this->session_id = self::createSessionID();
    		setcookie('SimpleSAMLSessionID', $this->session_id, 0, '/',
    			NULL, self::secureCookie());
    	}
    
    
    	/**
    	 * This function checks if we should set a secure cookie.
    	 *
    	 * @return TRUE if the cookie should be secure, FALSE otherwise.
    	 */
    	private static function secureCookie() {
    
    		if(!array_key_exists('HTTPS', $_SERVER)) {
    			/* Not a https-request. */
    			return FALSE;
    		}
    
    		if($_SERVER['HTTPS'] === 'off') {
    			/* IIS with HTTPS off. */
    			return FALSE;
    		}
    
    		/* Otherwise, HTTPS will be a non-empty string. */
    		return $_SERVER['HTTPS'] !== '';
    	}
    
    
    	/* This function retrieves the session id of the current session.
    	 *
    	 * Returns:
    	 *  The session id of the current session.
    	 */
    	public function getSessionId() {
    		return $this->session_id;
    	}
    
    
    	/* This static function creates a session id. A session id consists
    	 * of 32 random hexadecimal characters.
    	 *
    	 * Returns:
    	 *  A random session id.
    	 */
    	private static function createSessionID() {
    		return SimpleSAML_Utilities::stringToHex(SimpleSAML_Utilities::generateRandomBytes(16));
    	}
    
    
    	/* This static function validates a session id. A session id is valid
    	 * if it only consists of characters which are allowed in a session id
    	 * and it is the correct length.
    	 *
    	 * Parameters:
    	 *  $session_id  The session id we should validate.
    	 *
    	 * Returns:
    	 *  TRUE if this session id is valid, FALSE if not.
    	 */
    	private static function isValidSessionID($session_id) {
    		if(!is_string($session_id)) {
    			return FALSE;
    		}
    
    		if(strlen($session_id) != 32) {
    			return FALSE;
    		}
    
    		if(preg_match('/[^0-9a-f]/', $session_id)) {
    			return FALSE;
    		}
    
    		return TRUE;
    	}
    
    
    	/**
    	 * Check whether the session cookie is set.
    	 *
    	 * This function will only return FALSE if is is certain that the cookie isn't set.
    	 *
    	 * @return bool  TRUE if it was set, FALSE if not.
    	 */
    	public function hasSessionCookie() {
    
    		return array_key_exists('SimpleSAMLSessionID', $_COOKIE);
    	}
    
    }
    
    ?>