function SSO_ValidateUser() { global $sso_rng, $sso_db, $sso_db_user_sessions, $sso_db_temp_sessions, $sso_session_info, $sso_apirow, $sso_sessionrow, $sso_sessionrow2, $sso_ipaddr, $sso_settings; try { // Browser gets a token representing the new session in the temporary session. $sso_session_info["new_id2"] = $sso_rng->GenerateString(); $sso_db->Query("UPDATE", array($sso_db_temp_sessions, array("info" => serialize($sso_session_info)), "WHERE" => "id = ?"), $sso_sessionrow->id); // Validate the session. $sso_db->Query("UPDATE", array($sso_db_user_sessions, array("updated" => CSDB::ConvertToDBTime(time()), "info" => serialize(array("validated" => true, "ipaddr" => $sso_ipaddr["ipv6"]))), "WHERE" => "id = ?"), $sso_sessionrow2->id); // Build the redirect. $redirect = str_replace(array("\r", "\n"), "", base64_decode($sso_session_info["url"])); $redirect .= (strpos($redirect, "?") === false ? "?" : "&") . "from_sso_server=1&sso_id=" . urlencode($sso_session_info["new_id2"]) . "&sso_id2=" . urlencode($_REQUEST["sso_id"]); // Set the namespace cookie. if (isset($sso_settings[""]["namespacekey2"])) { $namespaces = SSO_LoadNamespaces(true); $namespaces[$sso_apirow->namespace] = $_COOKIE["sso_server_id2"]; $data = serialize($namespaces); $data = base64_encode(Blowfish::CreateDataPacket($data, pack("H*", $sso_settings[""]["namespacekey"]), array("prefix" => $sso_rng->GenerateString(), "mode" => "CBC", "iv" => pack("H*", $sso_settings[""]["namespaceiv"]), "key2" => pack("H*", $sso_settings[""]["namespacekey2"]), "iv2" => pack("H*", $sso_settings[""]["namespaceiv2"]), "lightweight" => true))); SetCookieFixDomain("sso_server_ns", $data, 0, "", "", SSO_IsSSLRequest(), true); } // Set the exposed namespace cookie if the option is enabled. if (isset($sso_settings[""]["expose_namespaces"]) && $sso_settings[""]["expose_namespaces"] && isset($sso_settings[""]["namespacekey4"])) { $namespaces = SSO_LoadNamespaces(false); $namespaces[$sso_apirow->namespace] = $sso_sessionrow2->id; $data = serialize($namespaces); $data = base64_encode(Blowfish::CreateDataPacket($data, pack("H*", $sso_settings[""]["namespacekey3"]), array("prefix" => $sso_rng->GenerateString(), "mode" => "CBC", "iv" => pack("H*", $sso_settings[""]["namespaceiv3"]), "key2" => pack("H*", $sso_settings[""]["namespacekey4"]), "iv2" => pack("H*", $sso_settings[""]["namespaceiv4"]), "lightweight" => true))); $host = str_replace(array("http://", "https://"), "", BB_GetRequestHost()); SetCookieFixDomain("sso_server_ns2", $data, 0, "/", $host, false, true); } // Redirect back to the client. SSO_ExternalRedirect($redirect, true); } catch (Exception $e) { // Don't do anything here. Just catch the database exception and let the code fall through. // It should be nearly impossible to get here in the first place. } return false; }
// Process the action. if ($sso_data["action"] == "test") { $result = array("success" => true); SSO_EndpointOutput($result); } else { if ($sso_data["action"] == "canautologin") { if ($sso_apikey_info["type"] != "normal") { SSO_EndpointError("Invalid API key type."); } if (!isset($sso_data["ns"]) || $sso_data["ns"] == "") { SSO_EndpointError("Namespace information not sent or not specified."); } if (!isset($sso_settings[""]["expose_namespaces"]) || $sso_settings[""]["expose_namespaces"] < 1 || !isset($sso_settings[""]["namespacekey4"])) { SSO_EndpointError("Namespace exposure support is disabled at the server level."); } $namespaces = SSO_LoadNamespaces(false, $sso_data["ns"]); if (!isset($namespaces[$sso_apirow->namespace])) { return false; } $session_id = $namespaces[$sso_apirow->namespace]; $sessionrow = $sso_db->GetRow("SELECT", array("*", "FROM" => "?", "WHERE" => "id = ? AND updated > ?"), $sso_db_user_sessions, $session_id, CSDB::ConvertToDBTime(time() - 5 * 60)); if ($sessionrow === false) { SSO_EndpointError("Namespace referenced session is invalid or has expired."); } // Namespace sign in is only allowed if the session is fully validated and the user is from the same IP address. $session_info = unserialize($sessionrow->info); if (!isset($session_info["validated"])) { SSO_EndpointError("Namespace referenced session is not validated."); } if (!isset($session_info["ipaddr"]) || !isset($_REQUEST["ipaddr"]) || $session_info["ipaddr"] != $_REQUEST["ipaddr"]) { SSO_EndpointError("Namespace referenced session is from an unspecified or different IP address.");