feat: add browser-launcher crate and integrate with authentication flow

This commit is contained in:
Kevin Yue committed 2026-07-06 14:45:13 +08:00
1 parent ca221e3488
commit 293cd56930
6 files changed
+251 -99

No files matched your search

+26 -90
View File
@@ -1,5 +1,6 @@
use std::{env::temp_dir, fs, os::unix::fs::PermissionsExt};
use browser_launcher::Browser as LaunchBrowser;
use common::constants::GP_CALLBACK_PORT_FILENAME;
use gpapi::auth::SamlAuthData;
use log::info;
@@ -10,50 +11,14 @@ use tokio::{
use crate::browser::auth_server::AuthServer;
pub enum Browser<'a> {
Auto,
Default,
Chrome,
Firefox,
Remote,
Other(&'a str),
}
impl<'a> Browser<'a> {
pub fn from_str(browser: &'a str) -> Self {
match browser.to_lowercase().as_str() {
"auto" => Browser::Auto,
"default" => Browser::Default,
"chrome" => Browser::Chrome,
"firefox" => Browser::Firefox,
"remote" => Browser::Remote,
_ => Browser::Other(browser),
}
}
fn as_str(&self) -> &str {
match self {
Browser::Auto => "auto",
Browser::Default => "default",
Browser::Chrome => "chrome",
Browser::Firefox => "firefox",
Browser::Remote => "remote",
Browser::Other(browser) => browser,
}
}
}
pub struct BrowserAuthenticator<'a> {
auth_request: &'a str,
browser: Browser<'a>,
browser: &'a str,
}
impl<'a> BrowserAuthenticator<'a> {
pub fn new(auth_request: &'a str, browser: &'a str) -> Self {
Self {
auth_request,
browser: Browser::from_str(browser),
}
Self { auth_request, browser }
}
pub async fn authenticate(&self) -> anyhow::Result<SamlAuthData> {
@@ -66,10 +31,9 @@ impl<'a> BrowserAuthenticator<'a> {
auth_server.serve_request(&auth_request);
});
match self.browser {
Browser::Remote => {
info!(
r#"
if is_remote_browser(self.browser) {
info!(
r#"
==== Manual Authentication Required ====
@@ -82,37 +46,19 @@ After completing the authentication, please paste the authentication data back t
Note that the URL is only valid for a single use.
"#,
auth_url
);
return read_auth_data_from_stdin();
}
Browser::Default => {
info!("Launching the default browser...");
webbrowser::open(&auth_url)?;
}
Browser::Auto => {
if let Some(app) = find_auto_browser_path() {
info!("Launching browser: {}", app);
open::with_detached(&auth_url, app)?;
} else {
info!("No preferred browser found; launching the default browser...");
webbrowser::open(&auth_url)?;
}
}
_ => {
let app = find_browser_path(&self.browser);
info!("Launching browser: {}", app);
open::with_detached(auth_url, app)?;
}
auth_url
);
return read_auth_data_from_stdin();
}
browser_launcher::open_url(&auth_url, LaunchBrowser::from_name(self.browser))?;
info!("Please continue the authentication process in the default browser");
wait_auth_data().await
}
async fn determine_addr(&self) -> anyhow::Result<String> {
if matches!(self.browser, Browser::Remote) {
if is_remote_browser(self.browser) {
let local_ip = detect_local_ip().await?;
Ok(format!("{}:0", local_ip))
} else {
@@ -121,6 +67,10 @@ Note that the URL is only valid for a single use.
}
}
fn is_remote_browser(browser: &str) -> bool {
browser.eq_ignore_ascii_case("remote")
}
/// Detect the local IP address by creating a UDP socket and connecting to an external address
async fn detect_local_ip() -> anyhow::Result<String> {
let socket = UdpSocket::bind("0.0.0.0:0").await?;
@@ -134,27 +84,6 @@ async fn detect_local_ip() -> anyhow::Result<String> {
Ok(ip.to_string())
}
fn find_browser_path(browser: &Browser) -> String {
match browser {
Browser::Chrome => find_chrome_path().unwrap_or_else(|| browser.as_str().to_string()),
_ => browser.as_str().to_string(),
}
}
fn find_auto_browser_path() -> Option<String> {
find_chrome_path().or_else(|| find_program_path("firefox"))
}
fn find_chrome_path() -> Option<String> {
["google-chrome-stable", "google-chrome", "chromium"]
.iter()
.find_map(|browser_name| find_program_path(browser_name))
}
fn find_program_path(name: &str) -> Option<String> {
which::which(name).ok().map(|path| path.to_string_lossy().to_string())
}
async fn wait_auth_data() -> anyhow::Result<SamlAuthData> {
// Start a local server to receive the browser authentication data
let listener = TcpListener::bind("127.0.0.1:0").await?;
@@ -200,8 +129,15 @@ mod tests {
use super::*;
#[test]
fn browser_auto_is_distinct_from_system_default() {
assert!(matches!(Browser::from_str("auto"), Browser::Auto));
assert!(matches!(Browser::from_str("default"), Browser::Default));
fn shared_launcher_keeps_auto_distinct_from_system_default() {
assert_eq!(LaunchBrowser::from_name("auto"), LaunchBrowser::Auto);
assert_eq!(LaunchBrowser::from_name("default"), LaunchBrowser::Default);
}
#[test]
fn browser_remote_remains_auth_specific() {
assert!(is_remote_browser("remote"));
assert!(is_remote_browser("REMOTE"));
assert_ne!(LaunchBrowser::from_name("remote"), LaunchBrowser::Default);
}
}