diff --git a/mellon_authentication.php b/mellon_authentication.php new file mode 100644 --- /dev/null +++ b/mellon_authentication.php @@ -0,0 +1,101 @@ +add_hook('startup', array($this, 'startup')); + $this->add_hook('authenticate', array($this, 'authenticate')); + $this->add_hook('logout_after', array($this, 'logout')); + $this->add_hook('login_after', array($this, 'login')); + } + + function startup($args) + { + if (!empty($_SERVER['MELLON_SAML_RESPONSE'])) { + $rcmail = rcmail::get_instance(); + $rcmail->add_shutdown_function(array('mellon_authentication', 'shutdown')); + + // handle login action + if (empty($_SESSION['user_id'])) { + $args['action'] = 'login'; + $this->redirect_query = $_SERVER['QUERY_STRING']; + } + // Set user password in session (see shutdown() method for more info) + else if (!empty($_SESSION['user_id']) && empty($_SESSION['password'])) { + $bindata = base64_decode($_SERVER['MELLON_SAML_RESPONSE']); + $keydata = base64_encode(gzcompress($bindata)); + $_SESSION['password'] = $rcmail->encrypt($keydata); + } + } + + return $args; + } + + function authenticate($args) + { + // Allow entering other user data in login form, + // e.g. after log out (#1487953) + if (!empty($args['user'])) { + return $args; + } + + if (!empty($_SERVER['MELLON_SAML_RESPONSE'])) { + $args['user'] = $_SERVER['MELLON_email']; + $bindata = base64_decode($_SERVER['MELLON_SAML_RESPONSE']); + $keydata = base64_encode(gzcompress($bindata)); + $args['pass'] = $keydata; + } + + $args['cookiecheck'] = false; + $args['valid'] = true; + + return $args; + } + + function logout($args) + { + if(!empty($_SERVER['MELLON_SAML_RESPONSE'])) { + $req = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST]; + $url = $req."/saml/logout?ReturnTo=".$req; + header("Location: $url", true, 307); + } + } + + function shutdown() + { + // There's no need to store password (even if encrypted) in session + // We'll set it back on startup (#1486553) + rcmail::get_instance()->session->remove('password'); + } + + function login($args) + { + // Redirect to the previous QUERY_STRING + if($this->redirect_query){ + header('Location: ./?' . $this->redirect_query); + exit; + } + return $args; + } +} +