From a3397e81a7b61c6328fc11fed7dd15cf2b8b982c Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 9 Aug 2026 16:35:33 +0800 Subject: [PATCH] fix: report missing LXQt browser SSO support --- crates/auth/src/callback.rs | 66 ++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/crates/auth/src/callback.rs b/crates/auth/src/callback.rs index 829f82a..3cda767 100644 --- a/crates/auth/src/callback.rs +++ b/crates/auth/src/callback.rs @@ -14,6 +14,13 @@ use tokio::{ time::timeout, }; +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +use log::warn; +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +use std::ffi::OsStr; +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +use std::process::{Command, Output}; + const CALLBACK_SCHEME: &str = "globalprotectcallback"; const PROTOCOL_MAGIC: &[u8; 4] = b"GPAC"; const PROTOCOL_VERSION: u8 = 1; @@ -99,16 +106,19 @@ pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { const MIME_TYPE: &str = "x-scheme-handler/globalprotectcallback"; const HANDLER: &str = "gpgui.desktop"; + ensure_lxqt_mime_tool()?; + let current_handler = query_callback_handler(MIME_TYPE)?; if !should_register_handler(¤t_handler) { return Ok(()); } - let status = std::process::Command::new("xdg-mime") + let output = Command::new("xdg-mime") .args(["default", HANDLER, MIME_TYPE]) - .status() + .output() .context("xdg-mime is required for external-browser authentication")?; - if !status.success() { + if !output.status.success() { + log_command_failure("register callback handler", &output); bail!("Failed to register the globalprotectcallback URL handler"); } if query_callback_handler(MIME_TYPE)? != HANDLER { @@ -120,16 +130,55 @@ pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] fn query_callback_handler(mime_type: &str) -> anyhow::Result { - let output = std::process::Command::new("xdg-mime") + let output = Command::new("xdg-mime") .args(["query", "default", mime_type]) .output() .context("xdg-mime is required for external-browser authentication")?; if !output.status.success() { + log_command_failure("query callback handler", &output); bail!("Failed to query the globalprotectcallback URL handler"); } Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) } +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +fn ensure_lxqt_mime_tool() -> anyhow::Result<()> { + if !is_lxqt_desktop(std::env::var_os("XDG_CURRENT_DESKTOP").as_deref()) { + return Ok(()); + } + + let result = Command::new("qtxdg-mat").args(["defapp", "--help"]).output(); + match result { + Ok(output) if output.status.success() => Ok(()), + Ok(output) => { + let stderr = String::from_utf8_lossy(&output.stderr); + warn!( + "qtxdg-mat readiness check failed with {}: {}", + output.status, + stderr.trim() + ); + bail!("Install qtxdg-tools for browser SSO.") + } + Err(err) => { + warn!("Failed to run qtxdg-mat readiness check: {err}"); + bail!("Install qtxdg-tools for browser SSO.") + } + } +} + +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))] +fn is_lxqt_desktop(current_desktop: Option<&OsStr>) -> bool { + current_desktop + .and_then(OsStr::to_str) + .is_some_and(|desktop| desktop.split(':').any(|name| name.eq_ignore_ascii_case("lxqt"))) +} + +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +fn log_command_failure(action: &str, output: &Output) { + let stderr = String::from_utf8_lossy(&output.stderr); + warn!("xdg-mime failed to {action} with {}: {}", output.status, stderr.trim()); +} + #[cfg(target_os = "macos")] pub fn ensure_auth_callback_handler() -> anyhow::Result<()> { macos::ensure_auth_callback_handler() @@ -340,6 +389,15 @@ mod tests { assert!(should_register_handler("other-vpn.desktop")); } + #[test] + fn detects_lxqt_desktop_session() { + assert!(is_lxqt_desktop(Some(OsStr::new("LXQt")))); + assert!(is_lxqt_desktop(Some(OsStr::new("LXQt:KDE")))); + assert!(is_lxqt_desktop(Some(OsStr::new("lxqt")))); + assert!(!is_lxqt_desktop(Some(OsStr::new("LXDE")))); + assert!(!is_lxqt_desktop(None)); + } + #[tokio::test] async fn forwards_callback_to_waiting_receiver() { let directory = tempdir().unwrap();