add NetworkFailure to AccountLinkError

This commit is contained in:
Carson McManus 2021-08-04 21:26:14 -04:00
parent 8a3408b405
commit 31a888c0e4
3 changed files with 55 additions and 7 deletions

View file

@ -9,7 +9,7 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use steamguard::{ use steamguard::{
steamapi, Confirmation, ConfirmationType, LoginError, SteamGuardAccount, UserLogin, steamapi, AccountLinker, Confirmation, ConfirmationType, LoginError, SteamGuardAccount, UserLogin,
}; };
use termion::{ use termion::{
event::{Event, Key}, event::{Event, Key},
@ -22,7 +22,6 @@ use termion::{
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate anyhow; extern crate anyhow;
mod accountlinker;
mod accountmanager; mod accountmanager;
lazy_static! { lazy_static! {
@ -124,7 +123,7 @@ fn main() {
if matches.is_present("setup") { if matches.is_present("setup") {
info!("setup"); info!("setup");
let mut linker = accountlinker::AccountLinker::new(); let mut linker = AccountLinker::new();
// do_login(&mut linker.account); // do_login(&mut linker.account);
// linker.link(linker.account.session.expect("no login session")); // linker.link(linker.account.session.expect("no login session"));
return; return;

View file

@ -1,15 +1,18 @@
use crate::{steamapi::Session, SteamGuardAccount};
use log::*; use log::*;
use reqwest::{cookie::CookieStore, header::COOKIE, Url}; use reqwest::{cookie::CookieStore, header::COOKIE, Url};
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
use steamguard::{steamapi::Session, SteamGuardAccount}; use std::error::Error;
use std::fmt::Display;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AccountLinker { pub struct AccountLinker {
device_id: String, device_id: String,
phone_number: String, phone_number: String,
pub account: SteamGuardAccount, pub account: SteamGuardAccount,
pub finalized: bool,
client: reqwest::blocking::Client, client: reqwest::blocking::Client,
} }
@ -19,6 +22,7 @@ impl AccountLinker {
device_id: generate_device_id(), device_id: generate_device_id(),
phone_number: String::from(""), phone_number: String::from(""),
account: SteamGuardAccount::new(), account: SteamGuardAccount::new(),
finalized: false,
client: reqwest::blocking::ClientBuilder::new() client: reqwest::blocking::ClientBuilder::new()
.cookie_store(true) .cookie_store(true)
.build() .build()
@ -26,15 +30,29 @@ impl AccountLinker {
}; };
} }
pub fn link(&self, session: &mut Session) { pub fn link(
&self,
session: &mut Session,
) -> anyhow::Result<AddAuthenticatorResponse, AccountLinkError> {
let mut params = HashMap::new(); let mut params = HashMap::new();
params.insert("access_token", session.token.clone()); params.insert("access_token", session.token.clone());
params.insert("steamid", session.steam_id.to_string()); params.insert("steamid", session.steam_id.to_string());
params.insert("device_identifier", self.device_id.clone()); params.insert("device_identifier", self.device_id.clone());
params.insert("authenticator_type", String::from("1")); params.insert("authenticator_type", "1".into());
params.insert("sms_phone_id", String::from("1")); params.insert("sms_phone_id", "1".into());
let resp: AddAuthenticatorResponse = self
.client
.post("https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v0001")
.form(&params)
.send()?
.json()?;
return Err(AccountLinkError::Unknown);
} }
pub fn finalize(&self, session: &Session) {}
fn has_phone(&self, session: &Session) -> bool { fn has_phone(&self, session: &Session) -> bool {
return self._phoneajax(session, "has_phone", "null"); return self._phoneajax(session, "has_phone", "null");
} }
@ -85,3 +103,32 @@ fn generate_device_id() -> String {
pub struct AddAuthenticatorResponse { pub struct AddAuthenticatorResponse {
pub response: SteamGuardAccount, pub response: SteamGuardAccount,
} }
#[derive(Debug)]
pub enum AccountLinkError {
/// No phone number on the account
MustProvidePhoneNumber,
/// A phone number is already on the account
MustRemovePhoneNumber,
/// User need to click link from confirmation email
MustConfirmEmail,
/// Must provide an SMS code
AwaitingFinalization,
AuthenticatorPresent,
NetworkFailure(reqwest::Error),
Unknown,
}
impl Display for AccountLinkError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}
impl Error for AccountLinkError {}
impl From<reqwest::Error> for AccountLinkError {
fn from(err: reqwest::Error) -> AccountLinkError {
AccountLinkError::NetworkFailure(err)
}
}

View file

@ -1,3 +1,4 @@
pub use accountlinker::{AccountLinkError, AccountLinker, AddAuthenticatorResponse};
use anyhow::Result; use anyhow::Result;
pub use confirmation::{Confirmation, ConfirmationType}; pub use confirmation::{Confirmation, ConfirmationType};
use hmacsha1::hmac_sha1; use hmacsha1::hmac_sha1;
@ -18,6 +19,7 @@ extern crate anyhow;
#[macro_use] #[macro_use]
extern crate maplit; extern crate maplit;
mod accountlinker;
mod confirmation; mod confirmation;
pub mod steamapi; pub mod steamapi;
mod userlogin; mod userlogin;