-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from mfairchild365/slo
Implement Single Log Out (discussion)
- Loading branch information
Showing
6 changed files
with
258 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
/** | ||
* A class that implments Single Log out (SLO) and maintains a mapping between php session IDs and CAS tickets. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Authentication | ||
* @package SimpleCAS | ||
* @author Michael Fairchild <mfairchild365@gmail.com> | ||
* @copyright 2014 Regents of the University of Nebraska | ||
* @license http://www1.unl.edu/wdn/wiki/Software_License BSD License | ||
* @link http://code.google.com/p/simplecas/ | ||
*/ | ||
class SimpleCAS_SLOMap extends SimpleCAS_SLOMapInterface | ||
{ | ||
protected $pool = false; | ||
|
||
public function __construct($cache_driver = NULL) | ||
{ | ||
if (!$cache_driver) { | ||
// Create Driver with default options | ||
$cache_driver = new \Stash\Driver\FileSystem(); | ||
|
||
$cache_driver->setOptions(array( | ||
//Scope the cache to the current application only. | ||
'path' => sys_get_temp_dir() . '/simpleCAS_map_' . md5($this->getApplicationID()) | ||
)); | ||
} | ||
|
||
// Inject the driver into a new Pool object. | ||
$this->pool = new \Stash\Pool($cache_driver); | ||
} | ||
|
||
/** | ||
* Generate a unique ID for the current application. This is based on the session cookie's domain/path. | ||
* | ||
* @return string | ||
*/ | ||
public function getApplicationID() | ||
{ | ||
$cookie_params = session_get_cookie_params(); | ||
|
||
if (empty($cookie_params['domain'])) { | ||
//By default, the domain will be empty, so if it is empty, lets use the current server_name. | ||
$cookie_params['domain'] = $_SERVER['SERVER_NAME']; | ||
} | ||
return $cookie_params['domain'] . '-' . $cookie_params['path']; | ||
} | ||
|
||
/** | ||
* get the session id by a cas ticket | ||
* | ||
* @param $cas_ticket | ||
* @return bool | ||
*/ | ||
public function get($cas_ticket) | ||
{ | ||
$item = $this->pool->getItem($cas_ticket); | ||
|
||
if ($item->isMiss()) { | ||
return false; | ||
} | ||
|
||
return $item->get(); | ||
} | ||
|
||
/** | ||
* Set the session id for a cas ticket | ||
* | ||
* @param $cas_ticket | ||
* @param $session_id | ||
* @return bool | ||
*/ | ||
public function set($cas_ticket, $session_id) | ||
{ | ||
$item = $this->pool->getItem($cas_ticket); | ||
return $item->set($session_id); | ||
} | ||
|
||
/** | ||
* Remove a CAS ticket from the registry | ||
* | ||
* @param $cas_ticket | ||
* @return mixed|void | ||
*/ | ||
public function remove($cas_ticket) | ||
{ | ||
$item = $this->pool->getItem($cas_ticket); | ||
|
||
if ($item->isMiss()) { | ||
return false; | ||
} | ||
|
||
return $item->clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* A class that implments Single Log out (SLO) and maintains a mapping between php session IDs and CAS tickets. | ||
* | ||
* PHP version 5 | ||
* | ||
* @category Authentication | ||
* @package SimpleCAS | ||
* @author Michael Fairchild <mfairchild365@gmail.com> | ||
* @copyright 2014 Regents of the University of Nebraska | ||
* @license http://www1.unl.edu/wdn/wiki/Software_License BSD License | ||
* @link http://code.google.com/p/simplecas/ | ||
*/ | ||
abstract class SimpleCAS_SLOMapInterface implements SimpleCAS_SingleSignOut | ||
{ | ||
/** | ||
* Determines if the posted request is a valid single sign out request. | ||
* | ||
* @param mixed $post $_POST data sent to the service. | ||
* | ||
* @return bool | ||
*/ | ||
public function validateLogoutRequest($post) | ||
{ | ||
if (isset($_POST['logoutRequest']) && ($ticket = $this->parseLogoutRequest($_POST['logoutRequest']))) { | ||
return $ticket; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Log out a session by the single log out ticket. | ||
* | ||
* @param $cas_ticket | ||
* @return bool | ||
*/ | ||
public function logout($cas_ticket) | ||
{ | ||
if (!$session_id = $this->get($cas_ticket)) { | ||
return false; | ||
} | ||
|
||
if (session_id()) { | ||
//If a current session exists, save it and close it. | ||
session_commit(); | ||
} | ||
|
||
//Start the session for this ticket. | ||
session_id($session_id); | ||
session_start(); | ||
$result = session_destroy(); | ||
|
||
$this->loadMapFile(); | ||
unset($this->data[$cas_ticket]); | ||
$this->saveMapFile(); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param $xml - the XML from the single sign out request | ||
* @return bool|string - the CAS ticket to sign out, false if no ticket was found. | ||
*/ | ||
protected function parseLogoutRequest($xml) | ||
{ | ||
$xml = new \SimpleXMLElement($xml); | ||
$element = $xml->xpath('//samlp:SessionIndex'); | ||
|
||
if (empty($element)) { | ||
return false; | ||
} | ||
|
||
return (string)$element[0]; | ||
} | ||
|
||
/** | ||
* Get a session id for a given CAS ticket | ||
* | ||
* @param string $cas_ticket | ||
* @return string mixed | ||
*/ | ||
abstract public function get($cas_ticket); | ||
|
||
/** | ||
* Save a mapping between a cas ticket and session id | ||
* | ||
* @param $cas_ticket | ||
* @param $session_id | ||
* @return mixed | ||
*/ | ||
abstract public function set($cas_ticket, $session_id); | ||
|
||
/** | ||
* Remove a CAS ticket | ||
* | ||
* @param $cas_ticket | ||
* @return mixed | ||
*/ | ||
abstract public function remove($cas_ticket); | ||
} |