diff options
-rw-r--r-- | src/bin/sso/main.rs | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/bin/sso/main.rs b/src/bin/sso/main.rs index f447f10..0356f3d 100644 --- a/src/bin/sso/main.rs +++ b/src/bin/sso/main.rs @@ -47,14 +47,19 @@ struct Args { #[clap(short, long, parse(from_occurrences))] verbose: usize, + /// Do not attempt to open a browser window + #[clap(long)] + no_browser: bool, + #[clap(subcommand)] command: Option<Commands>, } #[derive(Subcommand, PartialEq)] enum Commands { - /// does testing things + /// Request or refresh an access token (default command). Login, + /// Send a curl request with an authorization header. Curl { args: Vec<String>, }, @@ -97,7 +102,7 @@ impl Profile { self.refresh_token.is_some() } - pub fn authorize(&mut self) -> Result<(), Box<dyn Error>> { + pub fn authorize(&mut self, use_browser: bool) -> Result<(), Box<dyn Error>> { let client = BasicClient::new(client_id(), None, self.auth_url(), Some(self.token_url())) .set_auth_type(AuthType::RequestBody) .set_device_authorization_url(self.device_url()); @@ -115,11 +120,23 @@ impl Profile { .add_scope(scope) .request(http_client)?; - println!( - "Open this URL in your browser:\n{}\nand enter the code: {}", - details.verification_uri().to_string(), - details.user_code().secret().to_string() - ); + let mut quiet = false; + + if use_browser { + if let Some(uri) = details.verification_uri_complete() { + if webbrowser::open(uri.secret()).is_ok() { + quiet = true; + } + } + } + + if !quiet { + println!( + "Open this URL in your browser:\n{}\nand enter the code: {}", + details.verification_uri().as_str(), + details.user_code().secret() + ); + } let token_result = client.exchange_device_access_token(&details).request( http_client, @@ -292,7 +309,7 @@ fn main() -> Result<(), Box<dyn Error>> { if !profile.valid_access_token() { // Acquire access token - profile.authorize()?; + profile.authorize(!args.no_browser)?; } } |