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;
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();

View file

@ -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::<Url>().unwrap();
@ -116,10 +116,10 @@ impl UserLogin {
trace!("cookies: {:?}", self.cookies);
}
pub fn login(&mut self) -> LoginResult {
pub fn login(&mut self) -> anyhow::Result<Session, LoginError> {
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::<Url>().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 {