From 31a888c0e4f3d8635bb646bbbde1326c839ab8bd Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Wed, 4 Aug 2021 21:26:14 -0400 Subject: [PATCH] add NetworkFailure to AccountLinkError --- src/main.rs | 5 +-- {src => steamguard/src}/accountlinker.rs | 55 ++++++++++++++++++++++-- steamguard/src/lib.rs | 2 + 3 files changed, 55 insertions(+), 7 deletions(-) rename {src => steamguard/src}/accountlinker.rs (64%) diff --git a/src/main.rs b/src/main.rs index 2bb1860..f749142 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::{ sync::{Arc, Mutex}, }; use steamguard::{ - steamapi, Confirmation, ConfirmationType, LoginError, SteamGuardAccount, UserLogin, + steamapi, AccountLinker, Confirmation, ConfirmationType, LoginError, SteamGuardAccount, UserLogin, }; use termion::{ event::{Event, Key}, @@ -22,7 +22,6 @@ use termion::{ extern crate lazy_static; #[macro_use] extern crate anyhow; -mod accountlinker; mod accountmanager; lazy_static! { @@ -124,7 +123,7 @@ fn main() { if matches.is_present("setup") { info!("setup"); - let mut linker = accountlinker::AccountLinker::new(); + let mut linker = AccountLinker::new(); // do_login(&mut linker.account); // linker.link(linker.account.session.expect("no login session")); return; diff --git a/src/accountlinker.rs b/steamguard/src/accountlinker.rs similarity index 64% rename from src/accountlinker.rs rename to steamguard/src/accountlinker.rs index 5daf521..f09dfca 100644 --- a/src/accountlinker.rs +++ b/steamguard/src/accountlinker.rs @@ -1,15 +1,18 @@ +use crate::{steamapi::Session, SteamGuardAccount}; use log::*; use reqwest::{cookie::CookieStore, header::COOKIE, Url}; use serde::Deserialize; use serde_json::Value; use std::collections::HashMap; -use steamguard::{steamapi::Session, SteamGuardAccount}; +use std::error::Error; +use std::fmt::Display; #[derive(Debug, Clone)] pub struct AccountLinker { device_id: String, phone_number: String, pub account: SteamGuardAccount, + pub finalized: bool, client: reqwest::blocking::Client, } @@ -19,6 +22,7 @@ impl AccountLinker { device_id: generate_device_id(), phone_number: String::from(""), account: SteamGuardAccount::new(), + finalized: false, client: reqwest::blocking::ClientBuilder::new() .cookie_store(true) .build() @@ -26,15 +30,29 @@ impl AccountLinker { }; } - pub fn link(&self, session: &mut Session) { + pub fn link( + &self, + session: &mut Session, + ) -> anyhow::Result { let mut params = HashMap::new(); params.insert("access_token", session.token.clone()); params.insert("steamid", session.steam_id.to_string()); params.insert("device_identifier", self.device_id.clone()); - params.insert("authenticator_type", String::from("1")); - params.insert("sms_phone_id", String::from("1")); + params.insert("authenticator_type", "1".into()); + params.insert("sms_phone_id", "1".into()); + + let resp: AddAuthenticatorResponse = self + .client + .post("https://api.steampowered.com/ITwoFactorService/AddAuthenticator/v0001") + .form(¶ms) + .send()? + .json()?; + + return Err(AccountLinkError::Unknown); } + pub fn finalize(&self, session: &Session) {} + fn has_phone(&self, session: &Session) -> bool { return self._phoneajax(session, "has_phone", "null"); } @@ -85,3 +103,32 @@ fn generate_device_id() -> String { pub struct AddAuthenticatorResponse { 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 for AccountLinkError { + fn from(err: reqwest::Error) -> AccountLinkError { + AccountLinkError::NetworkFailure(err) + } +} diff --git a/steamguard/src/lib.rs b/steamguard/src/lib.rs index ebfbdf0..8c91d3a 100644 --- a/steamguard/src/lib.rs +++ b/steamguard/src/lib.rs @@ -1,3 +1,4 @@ +pub use accountlinker::{AccountLinkError, AccountLinker, AddAuthenticatorResponse}; use anyhow::Result; pub use confirmation::{Confirmation, ConfirmationType}; use hmacsha1::hmac_sha1; @@ -18,6 +19,7 @@ extern crate anyhow; #[macro_use] extern crate maplit; +mod accountlinker; mod confirmation; pub mod steamapi; mod userlogin;