diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index 036581f..32c6b15 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -10,7 +10,7 @@ use gpapi::{ error::PortalError, gateway::{gateway_login, GatewayLogin}, gp_params::{ClientOs, GpParams}, - portal::{prelogin, retrieve_config, Prelogin}, + portal::{prelogin, retrieve_config, Prelogin, StandardPrelogin}, process::{ auth_launcher::SamlAuthLauncher, users::{get_non_root_user, get_user_by_name}, @@ -96,7 +96,10 @@ pub(crate) struct ConnectArgs { #[arg(long, help = "Disable DTLS and ESP")] no_dtls: bool, - #[arg(long = "force-dpd", help = "Same as the '--force-dpd' option in the openconnect command")] + #[arg( + long = "force-dpd", + help = "Same as the '--force-dpd' option in the openconnect command" + )] dpd_interval: Option, #[cfg(feature = "webview-auth")] @@ -144,6 +147,7 @@ pub(crate) struct ConnectHandler<'a> { args: &'a ConnectArgs, shared_args: &'a SharedArgs<'a>, latest_key_password: RefCell>, + password_from_stdin: RefCell>, } impl<'a> ConnectHandler<'a> { @@ -152,6 +156,7 @@ impl<'a> ConnectHandler<'a> { args, shared_args, latest_key_password: Default::default(), + password_from_stdin: Default::default(), } } @@ -405,24 +410,41 @@ impl<'a> ConnectHandler<'a> { |user| Ok(user.to_owned()), )?; - let password = if self.args.passwd_on_stdin { - info!("Reading password from standard input"); - let mut input = String::new(); - std::io::stdin().read_line(&mut input)?; - input.trim_end().to_owned() - } else { - Password::new(&format!("{}:", prelogin.label_password())) - .without_confirmation() - .with_display_mode(PasswordDisplayMode::Masked) - .prompt()? - }; - + let password = self.obtain_password(prelogin)?; let password_cred = PasswordCredential::new(&user, &password); Ok(password_cred.into()) } } } + + fn obtain_password(&self, prelogin: &StandardPrelogin) -> anyhow::Result { + let password = if self.args.passwd_on_stdin { + // If the password has been read from stdin, use it directly + if let Some(password) = self.password_from_stdin.borrow().as_ref() { + info!("Reusing the password read from standard input"); + return Ok(password.clone()); + } + + info!("Reading password from standard input"); + let mut input = String::new(); + std::io::stdin().read_line(&mut input)?; + let password = input.trim_end().to_owned(); + // Considering that the gateway connection may fail even though the password read + // from stdin is correct. It may retry the gateway connection, so we need to + // save the password read from stdin to avoid reading stdin again. + self.password_from_stdin.replace(Some(password.clone())); + + password + } else { + Password::new(&format!("{}:", prelogin.label_password())) + .without_confirmation() + .with_display_mode(PasswordDisplayMode::Masked) + .prompt()? + }; + + Ok(password) + } } fn read_cookie_from_stdin() -> anyhow::Result {