change LoginResult to Result<Session, LoginError>

This commit is contained in:
Carson McManus 2021-07-31 10:55:55 -04:00
parent cc48bc9537
commit e0a668fa1e
2 changed files with 19 additions and 19 deletions

View file

@ -345,18 +345,18 @@ fn do_login(account: &mut SteamGuardAccount) {
let mut loops = 0; let mut loops = 0;
loop { loop {
match login.login() { match login.login() {
steamapi::LoginResult::Ok(s) => { Ok(s) => {
account.session = Option::Some(s); account.session = Option::Some(s);
break; break;
} }
steamapi::LoginResult::Need2FA => { Err(steamapi::LoginError::Need2FA) => {
let server_time = steamapi::get_server_time(); let server_time = steamapi::get_server_time();
login.twofactor_code = account.generate_code(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); 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."); println!("You should have received an email with a code.");
print!("Enter code"); print!("Enter code");
login.email_code = prompt(); login.email_code = prompt();

View file

@ -48,8 +48,7 @@ struct RsaResponse {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum LoginResult { pub enum LoginError {
Ok(Session),
BadRSA, BadRSA,
BadCredentials, BadCredentials,
NeedCaptcha{ captcha_gid: String }, 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) { fn update_session(&self) {
trace!("UserLogin::update_session"); trace!("UserLogin::update_session");
let url = "https://steamcommunity.com".parse::<Url>().unwrap(); let url = "https://steamcommunity.com".parse::<Url>().unwrap();
@ -116,10 +116,10 @@ impl UserLogin {
trace!("cookies: {:?}", self.cookies); trace!("cookies: {:?}", self.cookies);
} }
pub fn login(&mut self) -> LoginResult { pub fn login(&mut self) -> anyhow::Result<Session, LoginError> {
trace!("UserLogin::login"); trace!("UserLogin::login");
if self.captcha_required && self.captcha_text.len() == 0 { 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::<Url>().unwrap(); let url = "https://steamcommunity.com".parse::<Url>().unwrap();
@ -145,7 +145,7 @@ impl UserLogin {
} }
Err(error) => { Err(error) => {
error!("rsa error: {:?}", error); error!("rsa error: {:?}", error);
return LoginResult::BadRSA return Err(LoginError::BadRSA);
} }
} }
@ -183,40 +183,40 @@ impl UserLogin {
Err(error) => { Err(error) => {
debug!("login response did not have normal schema"); debug!("login response did not have normal schema");
error!("login parse error: {:?}", error); error!("login parse error: {:?}", error);
return LoginResult::OtherFailure; return Err(LoginError::OtherFailure);
} }
} }
} }
Err(error) => { Err(error) => {
error!("login request error: {:?}", error); error!("login request error: {:?}", error);
return LoginResult::OtherFailure; return Err(LoginError::OtherFailure);
} }
} }
if login_resp.message.contains("too many login") { if login_resp.message.contains("too many login") {
return LoginResult::TooManyAttempts; return Err(LoginError::TooManyAttempts);
} }
if login_resp.message.contains("Incorrect login") { if login_resp.message.contains("Incorrect login") {
return LoginResult::BadCredentials; return Err(LoginError::BadCredentials);
} }
if login_resp.captcha_needed { if login_resp.captcha_needed {
self.captcha_gid = login_resp.captcha_gid.clone(); 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 { if login_resp.emailauth_needed {
self.steam_id = login_resp.emailsteamid.clone(); self.steam_id = login_resp.emailsteamid.clone();
return LoginResult::NeedEmail; return Err(LoginError::NeedEmail);
} }
if login_resp.requires_twofactor { if login_resp.requires_twofactor {
return LoginResult::Need2FA; return Err(LoginError::Need2FA);
} }
if !login_resp.login_complete { 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"); 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); trace!("cookies {:?}", cookies);
let session = self.build_session(oauth, session_id); 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 { fn build_session(&self, data: OAuthData, session_id: String) -> Session {