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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<?php
/**
* Authenticate using PAPI protocol.
*
* @author Jaime Perez, RedIRIS
* @package simpleSAMLphp
*/
include("poa2/PoA.php");
class sspmod_papi_Auth_Source_PAPI extends SimpleSAML_Auth_Source {
/**
* The string used to identify our states.
*/
const STAGE_INIT = 'sspmod_papi_Auth_Source_PAPI.state';
/**
* The key of the AuthId field in the state.
*/
const AUTHID = 'sspmod_papi_Auth_Source_PAPI.AuthId';
/**
* @var the PoA to use.
*/
private $_poa;
/**
* @var the home locator interface to use.
*/
private $_hli;
/**
* @var the PAPIOPOA to use.
*/
private $_papiopoa;
/**
* @var the attributes of the user.
*/
private $_attrs;
/**
* @var the state ID to retrieve the original request later.
*/
private $_stateId;
/**
* Constructor for this authentication source.
*
* @param array $info Information about this authentication source.
* @param array $config Configuration.
*/
public function __construct($info, $config) {
assert('is_array($info)');
assert('is_array($config)');
/* Call the parent constructor first, as required by the interface. */
parent::__construct($info, $config);
if (!array_key_exists('site', $config)) {
throw new Exception('PAPI authentication source is not properly configured: missing [site]');
}
$this->_poa = new PoA($config['site']);
if (array_key_exists('hli', $config)) {
$this->_hli = $config['hli'];
}
}
/**
* Hook that will set Home Locator Identifier, PAPIOPOA and/or State ID.
*
* @param The PAPI request parameters that will be modified/extended.
*/
public function modifyParams(&$params) {
if (!empty($this->_hli)) {
$params['PAPIHLI'] = $this->_hli;
}
if (!empty($this->_papiopoa)) {
$params['PAPIOPOA'] = $this->_papiopoa;
}
$params['URL'] = $params['URL'].urlencode("&SSPStateID=".$this->_stateId);
return false;
}
/**
* Parse the attribute array in a format suitable for SSP.
*
* @param the original attribute array.
*/
protected function parseAttributes($attrs) {
assert('is_array($attrs)');
foreach ($attrs as $name => $value) {
if (!is_array($value)) {
$attrs[$name] = array($value);
}
}
return $attrs;
}
/**
* Log-in using PAPI
*
* @param array &$state Information about the current authentication.
*/
public function authenticate(&$state) {
assert('is_array($state)');
$this->_papiopoa = $state['SPMetadata']['entityid'];
// check if we are returning back from PAPI authentication
if (isset($_REQUEST['SSPStateID'])) {
// yes! restore original request
$this->_stateId = (string)$_REQUEST['SSPStateID'];
// sanitize the input
$sid = SimpleSAML_Utilities::parseStateID($this->_stateId);
if (!is_null($sid['url'])) {
SimpleSAML_Utilities::checkURLAllowed($sid['url']);
}
$state = SimpleSAML_Auth_State::loadState($this->_stateId, self::STAGE_INIT);
} else if (!$this->_poa->isAuthenticated()) {
// no! we have to save the request
/* We are will need the authId in order to retrieve this authentication source later. */
$state[self::AUTHID] = $this->authId;
$this->_stateId = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
$this->_poa->addHook("PAPI_REDIRECT_URL_FINISH", new Hook(array($this, "modifyParams")));
}
$this->_poa->authenticate();
$this->_attrs = $this->_poa->getAttributes();
$state['Attributes'] = $this->parseAttributes($this->_attrs);
self::completeAuth($state);
}
/**
* Log out from this authentication source.
*
* This function should be overridden if the authentication source requires special
* steps to complete a logout operation.
*
* If the logout process requires a redirect, the state should be saved. Once the
* logout operation is completed, the state should be restored, and completeLogout
* should be called with the state. If this operation can be completed without
* showing the user a page, or redirecting, this function should return.
*
* @param array &$state Information about the current logout operation.
*/
public function logout(&$state) {
assert('is_array($state)');
// check first if we have a valid session
if ($this->_poa->isAuthenticated()) {
/* We are will need the authId in order to retrieve this authentication source later. */
$state[self::AUTHID] = $this->authId;
$this->_stateId = SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
// TODO: pending on phpPoA adding PAPI_SLO_REDIRECT_URL_FINISH hook
$this->_poa->addHook("PAPI_SLO_REDIRECT_URL_FINISH", new Hook(array($this, "modifyParams")));
// perform single logout, this won't return
$this->_poa->logout(true);
} else if (isset($_REQUEST['SSPStateID'])) {
$this->_stateId = (string)$_REQUEST['SSPStateID'];
// sanitize the input
$sid = SimpleSAML_Utilities::parseStateID($this->_stateId);
if (!is_null($sid['url'])) {
SimpleSAML_Utilities::checkURLAllowed($sid['url']);
}
$state = SimpleSAML_Auth_State::loadState($this->_stateId, self::STAGE_INIT);
} else {
return;
}
self::completeLogout($state);
}
}
?>
|