Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions crates/openshell-cli/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,37 @@ pub async fn browser_auth_flow(gateway_endpoint: &str) -> Result<String> {
gateway_endpoint.to_string(),
));

// Allow suppressing the browser popup via environment variable (useful for
// CI, e2e tests, and headless environments).
let no_browser = std::env::var("OPENSHELL_NO_BROWSER")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);

// Prompt the user before opening the browser.
eprintln!(" Confirmation code: {code}");
eprintln!(" Verify this code matches your browser before clicking Connect.");
eprintln!();
eprint!("Press Enter to open the browser for authentication...");
std::io::stderr().flush().ok();
let mut _input = String::new();
std::io::stdin().read_line(&mut _input).ok();

if let Err(e) = open_browser(&auth_url) {
debug!(error = %e, "failed to open browser");
eprintln!("Could not open browser automatically.");

if no_browser {
eprintln!("Browser opening suppressed (OPENSHELL_NO_BROWSER is set).");
eprintln!("Open this URL in your browser:");
eprintln!(" {auth_url}");
eprintln!();
} else {
eprintln!("Browser opened.");
eprint!("Press Enter to open the browser for authentication...");
std::io::stderr().flush().ok();
let mut _input = String::new();
std::io::stdin().read_line(&mut _input).ok();

if let Err(e) = open_browser(&auth_url) {
debug!(error = %e, "failed to open browser");
eprintln!("Could not open browser automatically.");
eprintln!("Open this URL in your browser:");
eprintln!(" {auth_url}");
eprintln!();
} else {
eprintln!("Browser opened.");
}
}

// Wait for the callback or timeout.
Expand Down
10 changes: 6 additions & 4 deletions e2e/rust/tests/cf_auth_smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ async fn run_isolated(args: &[&str]) -> (String, i32) {
.env("XDG_CONFIG_HOME", tmpdir.path())
.env("HOME", tmpdir.path())
.env_remove("OPENSHELL_GATEWAY")
// `gateway add` may enter the browser auth flow, which prompts on stdin.
// Use a closed stdin so auth is skipped instead of hanging the test.
// Suppress browser popup during auth flow.
.env("OPENSHELL_NO_BROWSER", "1")
// Use a closed stdin so auth prompts don't hang the test.
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
Expand All @@ -43,8 +44,9 @@ async fn run_with_config(tmpdir: &std::path::Path, args: &[&str]) -> (String, i3
.env("XDG_CONFIG_HOME", tmpdir)
.env("HOME", tmpdir)
.env_remove("OPENSHELL_GATEWAY")
// `gateway add` may enter the browser auth flow, which prompts on stdin.
// Use a closed stdin so auth is skipped instead of hanging the test.
// Suppress browser popup during auth flow.
.env("OPENSHELL_NO_BROWSER", "1")
// Use a closed stdin so auth prompts don't hang the test.
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
Expand Down
Loading