diff --git a/auth/saml/auth.php b/auth/saml/auth.php index 1fe0b83..2769f9b 100755 --- a/auth/saml/auth.php +++ b/auth/saml/auth.php @@ -26,12 +26,39 @@ **/ class auth_plugin_saml extends auth_plugin_base { + /** + * The SP. + */ + protected static $sp; + + /** + * Are we currently forcing log-out? + */ + protected static $forcingLogout = FALSE; + /** * Constructor. */ function auth_plugin_saml() { - $this->authtype = 'saml'; - $this->config = get_config('auth/saml'); + $this->authtype = 'saml'; + $this->config = get_config('auth/saml'); + // Include the autoloader since Moodle isn't a composer project itself. + require_once($this->config->samllib . '/_autoload.php'); + if (empty(static::$sp)) { + static::$sp = new SimpleSAML_Auth_Simple($this->config->sp_source); + } + // Additionally determine if the user is still logged in to the SP. + global $USER; + global $SESSION; + if ($USER->loggedin && $USER->auth == "saml") { + if (!static::$forcingLogout + && (!static::$sp->isAuthenticated() + || (static::$sp->getAuthData('saml:sp:NameID') != $SESSION->auth_saml['NameID']))) { + // The user is logged in with the saml module but doesn't share a session on the SP. + static::$forcingLogout = TRUE; + require_logout(); + } + } } /** diff --git a/auth/saml/index.php b/auth/saml/index.php index 99166c7..fd0b155 100755 --- a/auth/saml/index.php +++ b/auth/saml/index.php @@ -207,7 +207,7 @@ $USER = complete_user_login($user); - if (function_exists('saml_hook_post_user_created')) { + if (function_exists('saml_hook_post_user_created') && !$user_exists && $USER->id) { saml_hook_post_user_created($USER); } @@ -218,6 +218,7 @@ $USER->loggedin = true; $USER->site = $CFG->wwwroot; set_moodle_cookie($USER->username); + $SESSION->auth_saml['NameID'] = $as->getAuthData('saml:sp:NameID'); if(isset($err) && !empty($err)) { auth_saml_error($err, $urltogo, $pluginconfig->samllogfile);