From e4cf29e852fd590e9b3a08d4fffc6f66738c01da Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Mon, 6 Sep 2021 16:51:44 -0400 Subject: [PATCH] don't allow setup or import for account_names already in the manifest, fixes #109 --- src/accountmanager.rs | 10 ++++++++++ src/main.rs | 11 +++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/accountmanager.rs b/src/accountmanager.rs index cbbe92a..e5ae3ea 100644 --- a/src/accountmanager.rs +++ b/src/accountmanager.rs @@ -116,6 +116,15 @@ impl Manifest { Ok(()) } + pub fn account_exists(&self, account_name: &String) -> bool { + for entry in &self.entries { + if &entry.account_name == account_name { + return true; + } + } + return false; + } + pub fn add_account(&mut self, account: SteamGuardAccount) { debug!("adding account to manifest: {}", account.account_name); let steamid = account.session.as_ref().map_or(0, |s| s.steam_id); @@ -136,6 +145,7 @@ impl Manifest { let file = File::open(path)?; let reader = BufReader::new(file); let account: SteamGuardAccount = serde_json::from_reader(reader)?; + ensure!(!self.account_exists(&account.account_name), "Account already exists in manifest, please remove it first."); self.add_account(account); return Ok(()); diff --git a/src/main.rs b/src/main.rs index ef61fd4..501dddd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -210,7 +210,12 @@ fn main() { if matches.is_present("setup") { println!("Log in to the account that you want to link to steamguard-cli"); - let session = do_login_raw().expect("Failed to log in. Account has not been linked."); + print!("Username: "); + let username = tui::prompt(); + if manifest.account_exists(&username) { + error!("Account {} already exists in manifest, remove it first", username); + } + let session = do_login_raw(username).expect("Failed to log in. Account has not been linked."); let mut linker = AccountLinker::new(session); let account: SteamGuardAccount; @@ -515,9 +520,7 @@ fn do_login(account: &mut SteamGuardAccount) -> anyhow::Result<()> { return Ok(()); } -fn do_login_raw() -> anyhow::Result { - print!("Username: "); - let username = tui::prompt(); +fn do_login_raw(username: String) -> anyhow::Result { let _ = std::io::stdout().flush(); let password = rpassword::prompt_password_stdout("Password: ").unwrap(); if password.len() > 0 {