feat: refactor password handling in ConnectHandler to reuse stdin input

--passwd-on-stdin reads again on gateway failure
Fixes #546
This commit is contained in:
Kevin Yue committed 2025-10-12 22:04:15 +08:00
1 parent e1eb191def
commit 2bd7371c7a
1 file changed
+36 -14
+36 -14
View File
@@ -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<u32>,
#[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<Option<String>>,
password_from_stdin: RefCell<Option<String>>,
}
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<String> {
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<Credential> {