'', ecUnknownUser, MAPI_E_LOGON_FAILED, MAPI_E_UNCONFIGURED => _('Logon failed. Please verify your credentials and try again.'), MAPI_E_NETWORK_ERROR => _('Cannot connect to Gromox.'), MAPI_E_INVALID_WORKSTATION_ACCOUNT => _('Login did not work due to a duplicate session. The issue was automatically resolved, please log in again.'), MAPI_E_END_OF_SESSION => '', default => _('Unknown MAPI Error') . ': ' . get_mapi_error_name(WebAppAuthentication::getErrorCode()), }; } /** * Returns the MAPISession instance. */ public static function getMAPISession(): MAPISession { [, $mapiSession] = WebAppAuthentication::initializeSessions(); return $mapiSession; } /** * Set the underlying MAPI session handle. * * @param false|resource $session the MAPI session resource to set, or false to clear it */ public static function setMAPISession($session) { WebAppAuthentication::getMAPISession()->setSession($session); } /** * Tries to authenticate the user. First it will check if the * user is using the login-form. And finally if not of above * methods apply, it will try to find credentials in the * php session. */ public static function authenticate() { if (!WebAppAuthentication::regenerate_access_token()) { return; } if (isset($_GET['code']) && (!defined('DISABLE_KEYCLOAK') || !DISABLE_KEYCLOAK)) { WebAppAuthentication::authenticateWithAccessToken($_GET['code'], $_GET['state'] ?? null); } elseif (WebAppAuthentication::isUsingLoginForm()) { WebAppAuthentication::authenticateWithPostedCredentials(); } // At last check if we have credentials in the session // and if found, try to login with those else { WebAppAuthentication::_authenticateWithSession(); } } /** * Checks if keycloak features are enabled and regenerates * the access token before expiration. * * @return bool false when the request must stop and reauthenticate */ public static function regenerate_access_token() { if (isset($_SESSION['_keycloak_auth'])) { $_keycloak_auth = $_SESSION['_keycloak_auth']; if (time() - $_keycloak_auth->get_last_refresh_time() > 280) { if (!$_keycloak_auth->refresh_grant_req() && !$_keycloak_auth->validate_grant()) { $reopenedSession = false; $sessionActive = session_status() === PHP_SESSION_ACTIVE; if (!$sessionActive) { $reopenedSession = session_start(); $sessionActive = $reopenedSession; } $keycloak = null; if ($sessionActive) { $keycloak = $_keycloak_auth instanceof WebAppKeyCloak ? $_keycloak_auth : WebAppKeyCloak::getInstance(); } if ($keycloak !== null) { header('Location:' . $keycloak->login_url($keycloak->redirect_url)); } // Do not let a later authentication path reuse either the invalid // Keycloak object or its encrypted access-token credentials. Keep // the newly generated OAuth state in this same session. unset($_SESSION['_keycloak_auth']); $_SESSION[EncryptionStore::_SESSION_KEY] = []; if ($reopenedSession) { session_write_close(); } WebAppAuthentication::$_errorCode = MAPI_E_END_OF_SESSION; return false; } $token = $_keycloak_auth->access_token->get_payload(); $user = $_keycloak_auth->access_token->get_claims('email'); WebAppAuthentication::_storeCredentialsInSession($user, $token); $_keycloak_auth->set_last_refresh_time(time()); // Persist the updated Keycloak auth to the session. // After read_and_close the session is not active, so // briefly reopen it for writing. if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } $_SESSION['_keycloak_auth'] = $_keycloak_auth; session_write_close(); } } return true; } /** * Returns true if a user is authenticated, or false otherwise. * * @return bool */ public static function isAuthenticated() { return WebAppAuthentication::$_authenticated; } /** * Tries to logon to Gromox with the given username and password/token. Returns * the error code that was given back. * * @param string $username The username * @param string $pass The password/token * * @return int */ public static function login($username, $pass) { // Release the PHP session file lock before any blocking MAPI // calls so concurrent requests from the same session are not // serialized behind this one. EncryptionStore re-opens the // session briefly when it needs to read or write credentials. if (session_status() === PHP_SESSION_ACTIVE) { session_write_close(); } $mapiSession = WebAppAuthentication::getMAPISession(); if (!WebAppAuthentication::_restoreMAPISession()) { // TODO: move logon from MAPISession to here WebAppAuthentication::$_errorCode = isset($_SESSION['_keycloak_auth']) ? $mapiSession->logon_token($username, $pass) : $mapiSession->logon($username, $pass, DEFAULT_SERVER); // Include external login plugins to be loaded if (file_exists(BASE_PATH . 'extlogin.php')) { include BASE_PATH . 'extlogin.php'; } if (WebAppAuthentication::$_errorCode === NOERROR) { WebAppAuthentication::$_authenticated = true; WebAppAuthentication::_storeMAPISession($mapiSession->getSession()); $tmp = explode('@', $username); if (count($tmp) == 2) { setcookie('domainname', $tmp[1], ['expires' => time() + 31536000, 'path' => '/', 'domain' => '', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']); } $wa_title = (string) $mapiSession->getFullName(); $companyname = (string) $mapiSession->getCompanyName(); if ($companyname !== '') { $wa_title .= " ({$companyname})"; } if ($wa_title !== '') { setcookie('webapp_title', $wa_title, ['expires' => time() + 31536000, 'path' => '/', 'domain' => '', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']); } } elseif (WebAppAuthentication::$_errorCode == MAPI_E_LOGON_FAILED || WebAppAuthentication::$_errorCode == MAPI_E_UNCONFIGURED) { error_log('grommunio Web user: ' . $username . ': authentication failure at MAPI'); } } return WebAppAuthentication::$_errorCode; } /** * Store a serialized MAPI Session, which can be used by _restoreMAPISession to re-create * a MAPISession, which saves a login call. * * @param resource $session the MAPI session handle to serialize and save */ private static function _storeMAPISession($session) { if (!WebAppAuthentication::$_sessionSaveSupport) { return; } $encryptionStore = EncryptionStore::getInstance(); $data = ''; if (kc_session_save($session, $data) === NOERROR) { $encryptionStore->add('savedsession', bin2hex((string) $data)); } } /** * Restore a MAPISession from the serialized with kc_session_restore. * * @return bool true if session has been restored successfully */ private static function _restoreMAPISession() { $encryptionStore = EncryptionStore::getInstance(); if (!WebAppAuthentication::$_sessionSaveSupport || $encryptionStore->get('savedsession') === null) { return false; } if (kc_session_restore(hex2bin((string) $encryptionStore->get('savedsession')), $session) === NOERROR) { WebAppAuthentication::$_errorCode = NOERROR; WebAppAuthentication::$_authenticated = true; WebAppAuthentication::setMAPISession($session); return true; } return false; } /** * Stores the given username and password in the session using the encryptionstore. * * @param string The username * @param string The password * @param mixed $username * @param mixed $password */ private static function _storeCredentialsInSession($username, $password) { $encryptionStore = EncryptionStore::getInstance(); $encryptionStore->add('username', $username); $encryptionStore->add('password', $password); } /** * Checks if a user tries to log in by submitting the login form. * * @return bool */ public static function isUsingLoginForm() { // Login form is only found on index.php // If we don't check it, then posting to grommunio.php would // also make authenticating possible. if (basename((string) $_SERVER['SCRIPT_NAME']) !== 'index.php') { return false; } return isset($_POST) && isset($_POST['username'], $_POST['password']); } /** * Tries to authenticate the user with credentials that were posted. * Returns the error code from the logon attempt. * * @return int */ public static function authenticateWithPostedCredentials() { $email = appendDefaultDomain($_POST['username']); if (empty($email) || empty($_POST['password'])) { WebAppAuthentication::$_errorCode = MAPI_E_LOGON_FAILED; return WebAppAuthentication::getErrorCode(); } // Check if a session is already running and if the credentials match $encryptionStore = EncryptionStore::getInstance(); $username = $encryptionStore->get('username'); $password = $encryptionStore->get('password'); if (!is_null($username) && !is_null($password)) { if ($username != $email || $password != $_POST['password']) { WebAppAuthentication::$_errorCode = MAPI_E_INVALID_WORKSTATION_ACCOUNT; [$phpSession] = WebAppAuthentication::initializeSessions(); $phpSession->destroy(); return WebAppAuthentication::getErrorCode(); } } else { // If no session is currently running, then store a fingerprint of the requester // in the session. $_SESSION['fingerprint'] = BrowserFingerprint::getFingerprint(); } // Give the session a new id session_regenerate_id(); WebAppAuthentication::login($email, $_POST['password']); // Store the credentials in the session if logging in was successful if (WebAppAuthentication::$_errorCode === NOERROR) { WebAppAuthentication::_storeCredentialsInSession($email, $_POST['password']); return WebAppAuthentication::getErrorCode(); } return WebAppAuthentication::getErrorCode(); } /** * Login with Oauth2.0 keycloak access token. * User selects login with keycloak, then gets redirected to keycloak server. * If login is successful, the user is redirected with code grant back to gromox server. * gromox requests access token with the received grant. * keycloak server verifies grant, and sends access token. * access token is used to authenticate user. * * @param mixed $code authorization code * @param mixed $state OAuth state returned by Keycloak */ public static function authenticateWithAccessToken($code, $state = null) { $keycloak = WebAppKeyCloak::getInstance(); if (!is_null($keycloak)) { if (is_string($code) && $keycloak->consumeState($state) && $keycloak->client_credential_grant_req($code) && $keycloak->validate_grant()) { $keycloak->set_last_refresh_time(time()); $_SESSION['_keycloak_auth'] = $keycloak; if (isset($_SESSION['_keycloak_auth'])) { $email = appendDefaultDomain($_SESSION['_keycloak_auth']->access_token->get_claims('email')); $token = $_SESSION['_keycloak_auth']->access_token->get_payload(); // Check if a session is already running and if the credentials match $encryptionStore = EncryptionStore::getInstance(); $username = $encryptionStore->get('username'); $password = $encryptionStore->get('password'); if (!is_null($username) && !is_null($password)) { if ($username != $email || $password != $token) { WebAppAuthentication::$_errorCode = MAPI_E_INVALID_WORKSTATION_ACCOUNT; [$phpSession] = WebAppAuthentication::initializeSessions(); $phpSession->destroy(); return WebAppAuthentication::getErrorCode(); } } else { // If no session is currently running, then store a fingerprint of the requester // in the session. $_SESSION['fingerprint'] = BrowserFingerprint::getFingerprint(); } // Give the session a new id session_regenerate_id(); WebAppAuthentication::login($email, $token); // Store the credentials in the session if logging in was successful if (WebAppAuthentication::$_errorCode === NOERROR) { WebAppAuthentication::_storeCredentialsInSession($email, $token); return WebAppAuthentication::getErrorCode(); } } } header('Location:' . $keycloak->login_url($keycloak->redirect_url)); } return WebAppAuthentication::getErrorCode(); } /** * Logs the user in with a given username and token in $_POST and logs * in with the special flag for token authentication enabled. If $new * is true it's assumed that a session does not exists and there will * be a new one generated and fingerprint stored in session which is * later compared after logon. After successful logon the session is stored. * * @param bool $new true if user has no session yet * * @return int|void */ public static function authenticateWithToken($new = true) { if (empty($_POST['token'])) { WebAppAuthentication::$_errorCode = MAPI_E_LOGON_FAILED; return WebAppAuthentication::getErrorCode(); } if ($new) { // If no session is currently running, then store a fingerprint of the requester // in the session. $_SESSION['fingerprint'] = BrowserFingerprint::getFingerprint(); // Give the session a new id session_regenerate_id(); } // Release the session lock before the blocking MAPI logon. if (session_status() === PHP_SESSION_ACTIVE) { session_write_close(); } $mapiSession = WebAppAuthentication::getMAPISession(); WebAppAuthentication::$_errorCode = $mapiSession->logon( $_POST['username'], $_POST['token'], DEFAULT_SERVER, null, null, 0 ); // Store the credentials in the session if logging in was successful if (WebAppAuthentication::$_errorCode === NOERROR) { WebAppAuthentication::_storeCredentialsInSession($_POST['username'], $_POST['token']); WebAppAuthentication::_storeMAPISession($mapiSession->getSession()); } return WebAppAuthentication::getErrorCode(); } /** * Tries to authenticate the user with credentials from the session. When credentials * are found in the session it will return the error code from the logon attempt with * those credentials, otherwise it will return void. * * Before trying to logon, it will compare the requesters fingerprint with the * fingerprint stored in the session. If they are not the same, the session will be * destroyed and the script will be killed. * * @return int|void */ private static function _authenticateWithSession() { [$phpSession] = WebAppAuthentication::initializeSessions(); // Check if the session hasn't timed out if ($phpSession->hasTimedOut()) { // Using a MAPI error code here, while it is not really a MAPI session timeout // However to the user this should make no difference, so the MAPI error will do. WebAppAuthentication::$_errorCode = MAPI_E_END_OF_SESSION; return WebAppAuthentication::getErrorCode(); } // Now check if we stored credentials in the session (in the encryption store) $encryptionStore = EncryptionStore::getInstance(); $username = $encryptionStore->get('username'); $password = $encryptionStore->get('password'); if (is_null($username) || is_null($password)) { return; } // Check if the browser fingerprint is the same as that of the browser that was // used to login in the first place. if (!DISABLE_FINGERPRINT_CHECK && $_SESSION['fingerprint'] !== BrowserFingerprint::getFingerprint()) { // Another browser presents this session cookie; end the session the way a // timeout does, every entry point answers that with its login or 401 path $phpSession->destroy(); WebAppAuthentication::$_errorCode = MAPI_E_END_OF_SESSION; return WebAppAuthentication::getErrorCode(); } // The helper strips every character that could inject another header. /** @scrutinizer ignore-call */ header('X-grommunio-Authuser:' . removeHTTPControlCharacters($username)); return WebAppAuthentication::login($username, $password); } /** * Returns the username that is stored in the session. * * @return string */ public static function getUserName() { $encryptionStore = EncryptionStore::getInstance(); return $encryptionStore->get('username'); } } // Instantiate the class WebAppAuthentication::getInstance();