diff --git a/cli/src/cmd_auth.rs b/cli/src/cmd_auth.rs index 2384e73d..e6a94314 100644 --- a/cli/src/cmd_auth.rs +++ b/cli/src/cmd_auth.rs @@ -4,7 +4,11 @@ // Copyright 2023 Oxide Computer Company -use std::{collections::HashMap, fs::File, io::Read}; +use std::{ + collections::HashMap, + fs::File, + io::{self, stdin, BufRead, BufReader, Read, Write}, +}; use anyhow::{anyhow, bail, Result}; use async_trait::async_trait; @@ -243,21 +247,28 @@ impl CmdAuthLogin { let uri = details.verification_uri().to_string(); + println!( + "Copy your one-time code:\n {}", + details.user_code().secret() + ); + let opened = match (&self.browser, self.no_browser) { - (None, false) => open::that(&uri).is_ok(), - (Some(app), false) => open::with(&uri, app).is_ok(), + (None, false) => { + wait_for_enter(&uri)?; + open::that(&uri).is_ok() + } + (Some(app), false) => { + wait_for_enter(&uri)?; + open::with(&uri, app).is_ok() + } (None, true) => false, (Some(_), true) => unreachable!(), }; - if opened { - println!("Opened this URL in your browser:\n {}", uri); - } else { + if !opened { println!("Open this URL in your browser:\n {}", uri); } - println!("\nEnter the code: {}\n", details.user_code().secret()); - auth_client .exchange_device_access_token(&details) .request_async(async_http_client_custom, tokio::time::sleep, None) @@ -329,6 +340,14 @@ impl CmdAuthLogin { } } +/// Wait for the user to press the enter key to continue. +fn wait_for_enter(uri: &str) -> Result { + print!("Press ENTER to open {} in your browser...", &uri); + io::stdout().flush()?; + let mut buf = String::new(); + Ok(BufReader::new(stdin()).read_line(&mut buf)?) +} + /// Removes saved authentication information. /// /// This command does not invalidate any tokens from the hosts.