login: make the 2fa and email code prompts less easy to mess up (#352)

This commit is contained in:
Carson McManus 2023-12-08 11:48:14 -05:00 committed by GitHub
parent 68dcedaeb7
commit 4ea44151b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,6 +8,7 @@ use steamguard::{
steamapi::{self, AuthenticationClient}, steamapi::{self, AuthenticationClient},
token::Tokens, token::Tokens,
transport::Transport, transport::Transport,
userlogin::UpdateAuthSessionError,
DeviceDetails, LoginError, SteamGuardAccount, UserLogin, DeviceDetails, LoginError, SteamGuardAccount, UserLogin,
}; };
@ -141,21 +142,48 @@ fn do_login_impl<T: Transport + Clone>(
eprintln!("Press enter when you have confirmed."); eprintln!("Press enter when you have confirmed.");
tui::pause(); tui::pause();
} }
EAuthSessionGuardType::k_EAuthSessionGuardType_DeviceCode => { EAuthSessionGuardType::k_EAuthSessionGuardType_DeviceCode
| EAuthSessionGuardType::k_EAuthSessionGuardType_EmailCode => {
let prompt = if method.confirmation_type
== EAuthSessionGuardType::k_EAuthSessionGuardType_DeviceCode
{
"Enter the 2fa code from your device: "
} else {
"Enter the 2fa code sent to your email: "
};
let mut attempts = 0;
loop {
let code = if let Some(account) = account { let code = if let Some(account) = account {
debug!("Generating 2fa code..."); debug!("Generating 2fa code...");
let time = steamapi::get_server_time(transport)?.server_time(); let time = steamapi::get_server_time(transport.clone())?.server_time();
account.generate_code(time) account.generate_code(time)
} else { } else {
eprint!("Enter the 2fa code from your device: "); tui::prompt_non_empty(prompt).trim().to_owned()
tui::prompt().trim().to_owned()
}; };
login.submit_steam_guard_code(method.confirmation_type, code)?;
match login.submit_steam_guard_code(method.confirmation_type, code) {
Ok(_) => break,
Err(err) => {
error!("Failed to submit code: {}", err);
match err {
UpdateAuthSessionError::TooManyAttempts
| UpdateAuthSessionError::SessionExpired
| UpdateAuthSessionError::InvalidGuardType => {
error!("Error is unrecoverable. Aborting.");
return Err(err.into());
}
_ => {}
}
attempts += 1;
debug!("Attempts: {}/3", attempts);
if attempts >= 3 {
error!("Too many failed attempts. Aborting.");
return Err(err.into());
}
}
}
} }
EAuthSessionGuardType::k_EAuthSessionGuardType_EmailCode => {
eprint!("Enter the 2fa code sent to your email: ");
let code = tui::prompt().trim().to_owned();
login.submit_steam_guard_code(method.confirmation_type, code)?;
} }
EAuthSessionGuardType::k_EAuthSessionGuardType_None => { EAuthSessionGuardType::k_EAuthSessionGuardType_None => {
debug!("No login confirmation required. Proceeding with login."); debug!("No login confirmation required. Proceeding with login.");