Skip to content

Commit 6d33025

Browse files
committed
Fix warning on logout.
Before registering a logout handler check that session is still authenticated. If logout initiated from Moodle, the session will be destroyed earlier, so no need to register a handler (we logged out user in Moodle already anyway). When logout is initiated from IdP, the SP session will be active, so we register handler that will perform logout in Moodle when session is destroyed by SP. Closes #520
1 parent d2ea4b4 commit 6d33025

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

TESTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ http://moodle.local/auth/saml2/login.php?wants&idp=c4b9265e38e3107bee1ccdf9d6475
1818
http://moodle.local/login/logout.php?sesskey=ihwmEywPxu
1919

2020

21-
4) Test Single logout starting from the IdP
21+
4) Test Single logout starting from the IdP. Notice that `ReturnTo` URL domain should be in `trusted.url.domains` in IdP config.
22+
If that is not the case, try using `ReturnTo=http://idp.local/simplesaml` which should work as SimpleSAMLphp trusts self hostname by default.
2223

2324
http://idp.local/simplesaml/saml2/idp/SingleLogoutService.php?ReturnTo=http://moodle.local/
2425

classes/api.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@
2929
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3030
*/
3131
class api {
32+
3233
/**
33-
* Called from SimpleSamlphp after a LogoutResponse from the IdP
34+
* IdP logout callback. Called only when logout is initiated from IdP.
35+
* {@see saml2-logout.php}
3436
*/
3537
public static function logout_from_idp_front_channel(): void {
36-
// The SP session will be cleaned up but we need to remove the
37-
// Moodle session here.
38-
\core\session\manager::terminate_current();
38+
// The SP session will be cleaned up. Log user out of Moodle.
39+
require_logout();
3940
}
4041

4142
/**
42-
* Called from SimpleSamlphp after a LogoutRequest from the SP
43+
* SP logout callback. Called in case of normal Moodle logout.
44+
* {@see auth::logoutpage_hook}
4345
*
4446
* @param array $state Information about the current logout operation.
4547
*/

sp/saml2-logout.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
/*
3434
* There are 4 methods of logging out:
3535
*
36-
* 1) Initiated from moodles logout, in which case we first logout of
36+
* 1) Initiated from moodle logout, in which case we first logout of
3737
* moodle and then log out of the middle SP and then optionally
3838
* redirect to the IdP to do full Single Logout. This is the way
39-
* a majority of users logout and is fully supported.
39+
* a majority of users logout and is fully supported. Notice that in this
40+
* case SAML session is not authenticated by the time we reach this point.
4041
*
4142
* 2) If doing SLO via IdP via the HTTP-Redirect binding
4243
*
@@ -48,7 +49,12 @@
4849
*/
4950
try {
5051
$session = \SimpleSAML\Session::getSessionFromRequest();
51-
$session->registerLogoutHandler($saml2auth->spname, '\auth_saml2\api', 'logout_from_idp_front_channel');
52+
// When logout is initiated from IdP (we land here from SingleLogoutService call),
53+
// session is still authenticated, so we can register the handler that will log
54+
// user out in Moodle.
55+
if (!is_null($session->getAuthState($saml2auth->spname))) {
56+
$session->registerLogoutHandler($saml2auth->spname, '\auth_saml2\api', 'logout_from_idp_front_channel');
57+
}
5258

5359
require('../.extlib/simplesamlphp/modules/saml/www/sp/saml2-logout.php');
5460
} catch (Exception $e) {

0 commit comments

Comments
 (0)