mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-05-20 07:26:58 -04:00
feat: support default browser for CLI (#345)
This commit is contained in:
@@ -18,6 +18,7 @@ pub struct SamlAuthLauncher<'a> {
|
||||
fix_openssl: bool,
|
||||
ignore_tls_errors: bool,
|
||||
clean: bool,
|
||||
default_browser: bool,
|
||||
}
|
||||
|
||||
impl<'a> SamlAuthLauncher<'a> {
|
||||
@@ -33,6 +34,7 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
fix_openssl: false,
|
||||
ignore_tls_errors: false,
|
||||
clean: false,
|
||||
default_browser: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +83,13 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn default_browser(mut self, default_browser: bool) -> Self {
|
||||
self.default_browser = default_browser;
|
||||
self
|
||||
}
|
||||
|
||||
/// Launch the authenticator binary as the current user or SUDO_USER if available.
|
||||
pub async fn launch(self) -> anyhow::Result<Credential> {
|
||||
pub async fn launch(self) -> anyhow::Result<Option<Credential>> {
|
||||
let mut auth_cmd = Command::new(GP_AUTH_BINARY);
|
||||
auth_cmd.arg(self.server);
|
||||
|
||||
@@ -122,6 +129,10 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
auth_cmd.arg("--clean");
|
||||
}
|
||||
|
||||
if self.default_browser {
|
||||
auth_cmd.arg("--default-browser");
|
||||
}
|
||||
|
||||
let mut non_root_cmd = auth_cmd.into_non_root()?;
|
||||
let output = non_root_cmd
|
||||
.kill_on_drop(true)
|
||||
@@ -130,12 +141,16 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
.wait_with_output()
|
||||
.await?;
|
||||
|
||||
if self.default_browser {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let Ok(auth_result) = serde_json::from_slice::<SamlAuthResult>(&output.stdout) else {
|
||||
bail!("Failed to parse auth data")
|
||||
};
|
||||
|
||||
match auth_result {
|
||||
SamlAuthResult::Success(auth_data) => Ok(Credential::from(auth_data)),
|
||||
SamlAuthResult::Success(auth_data) => Ok(Some(Credential::from(auth_data))),
|
||||
SamlAuthResult::Failure(msg) => bail!(msg),
|
||||
}
|
||||
}
|
||||
|
@@ -1,34 +0,0 @@
|
||||
use std::{env::temp_dir, io::Write};
|
||||
|
||||
pub struct BrowserAuthenticator<'a> {
|
||||
auth_request: &'a str,
|
||||
}
|
||||
|
||||
impl BrowserAuthenticator<'_> {
|
||||
pub fn new(auth_request: &str) -> BrowserAuthenticator {
|
||||
BrowserAuthenticator { auth_request }
|
||||
}
|
||||
|
||||
pub fn authenticate(&self) -> anyhow::Result<()> {
|
||||
if self.auth_request.starts_with("http") {
|
||||
open::that_detached(self.auth_request)?;
|
||||
} else {
|
||||
let html_file = temp_dir().join("gpauth.html");
|
||||
let mut file = std::fs::File::create(&html_file)?;
|
||||
|
||||
file.write_all(self.auth_request.as_bytes())?;
|
||||
|
||||
open::that_detached(html_file)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BrowserAuthenticator<'_> {
|
||||
fn drop(&mut self) {
|
||||
// Cleanup the temporary file
|
||||
let html_file = temp_dir().join("gpauth.html");
|
||||
let _ = std::fs::remove_file(html_file);
|
||||
}
|
||||
}
|
@@ -2,8 +2,6 @@ pub(crate) mod command_traits;
|
||||
pub(crate) mod gui_helper_launcher;
|
||||
|
||||
pub mod auth_launcher;
|
||||
#[cfg(feature = "browser-auth")]
|
||||
pub mod browser_authenticator;
|
||||
pub mod gui_launcher;
|
||||
pub mod hip_launcher;
|
||||
pub mod service_launcher;
|
||||
|
Reference in New Issue
Block a user