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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\multiauth\Auth\Source;
use Exception;
use SAML2\Constants;
use SimpleSAML\Auth;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Module;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\Module\saml\Error\NoAuthnContext;
/**
* Authentication source which let the user chooses among a list of
* other authentication sources
*
* @author Lorenzo Gil, Yaco Sistemas S.L.
* @package SimpleSAMLphp
*/
class MultiAuth extends \SimpleSAML\Auth\Source
{
/**
* The key of the AuthId field in the state.
*/
public const AUTHID = '\SimpleSAML\Module\multiauth\Auth\Source\MultiAuth.AuthId';
/**
* The string used to identify our states.
*/
public const STAGEID = '\SimpleSAML\Module\multiauth\Auth\Source\MultiAuth.StageId';
/**
* The key where the sources is saved in the state.
*/
public const SOURCESID = '\SimpleSAML\Module\multiauth\Auth\Source\MultiAuth.SourceId';
/**
* The key where the selected source is saved in the session.
*/
public const SESSION_SOURCE = 'multiauth:selectedSource';
/**
* Array of sources we let the user chooses among.
* @var array
*/
private $sources;
/**
* @var string|null preselect source in filter module configuration
*/
private $preselect;
/**
* 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('sources', $config)) {
throw new Exception('The required "sources" config option was not found');
}
if (array_key_exists('preselect', $config) && is_string($config['preselect'])) {
if (!array_key_exists($config['preselect'], $config['sources'])) {
throw new Exception('The optional "preselect" config option must be present in "sources"');
}
$this->preselect = $config['preselect'];
}
$globalConfiguration = Configuration::getInstance();
$defaultLanguage = $globalConfiguration->getString('language.default', 'en');
$authsources = Configuration::getConfig('authsources.php');
$this->sources = [];
/** @psalm-var array $sources */
$sources = $config['sources'];
foreach ($sources as $source => $info) {
if (is_int($source)) {
// Backwards compatibility
$source = $info;
$info = [];
}
if (array_key_exists('text', $info)) {
$text = $info['text'];
} else {
$text = [$defaultLanguage => $source];
}
if (array_key_exists('help', $info)) {
$help = $info['help'];
} else {
$help = null;
}
if (array_key_exists('css-class', $info)) {
$css_class = $info['css-class'];
} else {
// Use the authtype as the css class
$authconfig = $authsources->getArray($source, null);
if (!array_key_exists(0, $authconfig) || !is_string($authconfig[0])) {
$css_class = "";
} else {
$css_class = str_replace(":", "-", $authconfig[0]);
}
}
$class_ref = [];
if (array_key_exists('AuthnContextClassRef', $info)) {
$ref = $info['AuthnContextClassRef'];
if (is_string($ref)) {
$class_ref = [$ref];
} else {
$class_ref = $ref;
}
}
$this->sources[] = [
'source' => $source,
'text' => $text,
'help' => $help,
'css_class' => $css_class,
'AuthnContextClassRef' => $class_ref,
];
}
}
/**
* Prompt the user with a list of authentication sources.
*
* This method saves the information about the configured sources,
* and redirects to a page where the user must select one of these
* authentication sources.
*
* This method never return. The authentication process is finished
* in the delegateAuthentication method.
*
* @param array &$state Information about the current authentication.
* @return void
*/
public function authenticate(&$state)
{
assert(is_array($state));
$state[self::AUTHID] = $this->authId;
$state[self::SOURCESID] = $this->sources;
if (!array_key_exists('multiauth:preselect', $state) && is_string($this->preselect)) {
$state['multiauth:preselect'] = $this->preselect;
}
if (
!is_null($state['saml:RequestedAuthnContext'])
&& array_key_exists('AuthnContextClassRef', $state['saml:RequestedAuthnContext'])
) {
$refs = array_values($state['saml:RequestedAuthnContext']['AuthnContextClassRef']);
$new_sources = [];
foreach ($this->sources as $source) {
if (count(array_intersect($source['AuthnContextClassRef'], $refs)) >= 1) {
$new_sources[] = $source;
}
}
$state[self::SOURCESID] = $new_sources;
$number_of_sources = count($new_sources);
if ($number_of_sources === 0) {
throw new NoAuthnContext(
Constants::STATUS_RESPONDER,
'No authentication sources exist for the requested AuthnContextClassRefs: ' . implode(', ', $refs)
);
} else if ($number_of_sources === 1) {
MultiAuth::delegateAuthentication($new_sources[0]['source'], $state);
}
}
// Save the $state array, so that we can restore if after a redirect
$id = Auth\State::saveState($state, self::STAGEID);
/* Redirect to the select source page. We include the identifier of the
* saved state array as a parameter to the login form
*/
$url = Module::getModuleURL('multiauth/selectsource.php');
$params = ['AuthState' => $id];
// Allows the user to specify the auth source to be used
if (isset($_GET['source'])) {
$params['source'] = $_GET['source'];
}
Utils\HTTP::redirectTrustedURL($url, $params);
// The previous function never returns, so this code is never executed
assert(false);
}
/**
* Delegate authentication.
*
* This method is called once the user has choosen one authentication
* source. It saves the selected authentication source in the session
* to be able to logout properly. Then it calls the authenticate method
* on such selected authentication source.
*
* @param string $authId Selected authentication source
* @param array $state Information about the current authentication.
* @return void
* @throws \Exception
*/
public static function delegateAuthentication($authId, $state)
{
assert(is_string($authId));
assert(is_array($state));
$as = Auth\Source::getById($authId);
$valid_sources = array_map(
/**
* @param array $src
* @return string
*/
function ($src) {
return $src['source'];
},
$state[self::SOURCESID]
);
if ($as === null || !in_array($authId, $valid_sources, true)) {
throw new Exception('Invalid authentication source: ' . $authId);
}
// Save the selected authentication source for the logout process.
$session = Session::getSessionFromRequest();
$session->setData(
self::SESSION_SOURCE,
$state[self::AUTHID],
$authId,
Session::DATA_TIMEOUT_SESSION_END
);
try {
$as->authenticate($state);
} catch (Error\Exception $e) {
Auth\State::throwException($state, $e);
} catch (Exception $e) {
$e = new Error\UnserializableException($e);
Auth\State::throwException($state, $e);
}
Auth\Source::completeAuth($state);
}
/**
* Log out from this authentication source.
*
* This method retrieves the authentication source used for this
* session and then call the logout method on it.
*
* @param array &$state Information about the current logout operation.
* @return void
*/
public function logout(&$state)
{
assert(is_array($state));
// Get the source that was used to authenticate
$session = Session::getSessionFromRequest();
$authId = $session->getData(self::SESSION_SOURCE, $this->authId);
$source = Auth\Source::getById($authId);
if ($source === null) {
throw new Exception('Invalid authentication source during logout: ' . $authId);
}
// Then, do the logout on it
$source->logout($state);
}
/**
* Set the previous authentication source.
*
* This method remembers the authentication source that the user selected
* by storing its name in a cookie.
*
* @param string $source Name of the authentication source the user selected.
* @return void
*/
public function setPreviousSource($source)
{
assert(is_string($source));
$cookieName = 'multiauth_source_' . $this->authId;
$config = Configuration::getInstance();
$params = [
// We save the cookies for 90 days
'lifetime' => 7776000, //60*60*24*90
// The base path for cookies. This should be the installation directory for SimpleSAMLphp.
'path' => $config->getBasePath(),
'httponly' => false,
];
Utils\HTTP::setCookie($cookieName, $source, $params, false);
}
/**
* Get the previous authentication source.
*
* This method retrieves the authentication source that the user selected
* last time or NULL if this is the first time or remembering is disabled.
* @return string|null
*/
public function getPreviousSource()
{
$cookieName = 'multiauth_source_' . $this->authId;
if (array_key_exists($cookieName, $_COOKIE)) {
return $_COOKIE[$cookieName];
} else {
return null;
}
}
}
|