From e0a668fa1e4aaee5de92930db281731b1fe32b29 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sat, 31 Jul 2021 10:55:55 -0400 Subject: [PATCH] change LoginResult to Result --- src/main.rs | 8 ++++---- src/steamapi.rs | 30 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 98bed8f..aaa50c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -345,18 +345,18 @@ fn do_login(account: &mut SteamGuardAccount) { let mut loops = 0; loop { match login.login() { - steamapi::LoginResult::Ok(s) => { + Ok(s) => { account.session = Option::Some(s); break; } - steamapi::LoginResult::Need2FA => { + Err(steamapi::LoginError::Need2FA) => { let server_time = steamapi::get_server_time(); login.twofactor_code = account.generate_code(server_time); } - steamapi::LoginResult::NeedCaptcha{ captcha_gid } => { + Err(steamapi::LoginError::NeedCaptcha{ captcha_gid }) => { login.captcha_text = prompt_captcha_text(&captcha_gid); } - steamapi::LoginResult::NeedEmail => { + Err(steamapi::LoginError::NeedEmail) => { println!("You should have received an email with a code."); print!("Enter code"); login.email_code = prompt(); diff --git a/src/steamapi.rs b/src/steamapi.rs index 784dac5..b98152e 100644 --- a/src/steamapi.rs +++ b/src/steamapi.rs @@ -48,8 +48,7 @@ struct RsaResponse { } #[derive(Debug)] -pub enum LoginResult { - Ok(Session), +pub enum LoginError { BadRSA, BadCredentials, NeedCaptcha{ captcha_gid: String }, @@ -95,6 +94,7 @@ impl UserLogin { } } + /// Updates the cookie jar with the session cookies by pinging steam servers. fn update_session(&self) { trace!("UserLogin::update_session"); let url = "https://steamcommunity.com".parse::().unwrap(); @@ -116,10 +116,10 @@ impl UserLogin { trace!("cookies: {:?}", self.cookies); } - pub fn login(&mut self) -> LoginResult { + pub fn login(&mut self) -> anyhow::Result { trace!("UserLogin::login"); if self.captcha_required && self.captcha_text.len() == 0 { - return LoginResult::NeedCaptcha{captcha_gid: self.captcha_gid.clone()}; + return Err(LoginError::NeedCaptcha{captcha_gid: self.captcha_gid.clone()}); } let url = "https://steamcommunity.com".parse::().unwrap(); @@ -145,7 +145,7 @@ impl UserLogin { } Err(error) => { error!("rsa error: {:?}", error); - return LoginResult::BadRSA + return Err(LoginError::BadRSA); } } @@ -183,40 +183,40 @@ impl UserLogin { Err(error) => { debug!("login response did not have normal schema"); error!("login parse error: {:?}", error); - return LoginResult::OtherFailure; + return Err(LoginError::OtherFailure); } } } Err(error) => { error!("login request error: {:?}", error); - return LoginResult::OtherFailure; + return Err(LoginError::OtherFailure); } } if login_resp.message.contains("too many login") { - return LoginResult::TooManyAttempts; + return Err(LoginError::TooManyAttempts); } if login_resp.message.contains("Incorrect login") { - return LoginResult::BadCredentials; + return Err(LoginError::BadCredentials); } if login_resp.captcha_needed { self.captcha_gid = login_resp.captcha_gid.clone(); - return LoginResult::NeedCaptcha{ captcha_gid: self.captcha_gid.clone() }; + return Err(LoginError::NeedCaptcha{ captcha_gid: self.captcha_gid.clone() }); } if login_resp.emailauth_needed { self.steam_id = login_resp.emailsteamid.clone(); - return LoginResult::NeedEmail; + return Err(LoginError::NeedEmail); } if login_resp.requires_twofactor { - return LoginResult::Need2FA; + return Err(LoginError::Need2FA); } if !login_resp.login_complete { - return LoginResult::BadCredentials; + return Err(LoginError::BadCredentials); } @@ -254,7 +254,7 @@ impl UserLogin { } _ => { error!("did not receive transfer_urls and transfer_parameters"); - return LoginResult::OtherFailure; + return Err(LoginError::OtherFailure); } } @@ -271,7 +271,7 @@ impl UserLogin { trace!("cookies {:?}", cookies); let session = self.build_session(oauth, session_id); - return LoginResult::Ok(session); + return Ok(session); } fn build_session(&self, data: OAuthData, session_id: String) -> Session {