From 81d13deaab1c65acc94c09c7247dee41dafb9e0c Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sat, 9 May 2026 09:24:26 +0800 Subject: [PATCH] refactor: simplify SAML browser selection --- apps/gpclient/src/connect.rs | 51 ++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index 7c7a70e..0a6c233 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -15,7 +15,7 @@ use gpapi::{ error::PortalError, gateway::{GatewayLogin, SessionExtensionAuth, gateway_login}, gp_params::{ClientOs, GpParams}, - portal::{Prelogin, StandardPrelogin, prelogin, retrieve_config}, + portal::{Prelogin, SamlPrelogin, StandardPrelogin, prelogin, retrieve_config}, process::{ auth_launcher::SamlAuthLauncher, users::{get_non_root_user, get_user_by_name}, @@ -185,6 +185,36 @@ impl ConnectArgs { Os::Mac => host_utils::get_macos_os_string(), } } + + fn saml_auth_browser<'a>(&'a self, prelogin: &SamlPrelogin) -> anyhow::Result> { + if prelogin.support_default_browser() { + return Ok(self.default_browser_saml_auth_browser()); + } + + self.embedded_saml_auth_browser() + } + + #[cfg(feature = "webview-auth")] + fn default_browser_saml_auth_browser(&self) -> Option<&str> { + self.browser.as_deref() + } + + #[cfg(not(feature = "webview-auth"))] + fn default_browser_saml_auth_browser(&self) -> Option<&str> { + None + } + + #[cfg(feature = "webview-auth")] + fn embedded_saml_auth_browser(&self) -> anyhow::Result> { + Ok(None) + } + + #[cfg(not(feature = "webview-auth"))] + fn embedded_saml_auth_browser(&self) -> anyhow::Result> { + bail!( + "The server does not support authentication via the default browser and the gpclient is not built with the `webview-auth` feature" + ); + } } pub(crate) struct ConnectHandler<'a> { @@ -539,24 +569,7 @@ impl<'a> ConnectHandler<'a> { match prelogin { Prelogin::Saml(prelogin) => { - let browser = if prelogin.support_default_browser() { - #[cfg(feature = "webview-auth")] - { - self.args.browser.as_deref() - } - - #[cfg(not(feature = "webview-auth"))] - { - None - } - } else if !cfg!(feature = "webview-auth") { - bail!( - "The server does not support authentication via the default browser and the gpclient is not built with the `webview-auth` feature" - ); - } else { - None - }; - + let browser = self.args.saml_auth_browser(prelogin)?; let os_version = self.args.os_version(); let verbose = self.shared_args.verbose.to_verbose_arg(); let user_agent = self.user_agent();