From e4d7ea4475ff88bd6fb951f2b68e3e0e7f741ba3 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 8 Aug 2021 13:37:18 -0400 Subject: [PATCH] move phoneajax methods to steamapi module --- src/main.rs | 3 +- steamguard/src/accountlinker.rs | 44 ---------------------------- steamguard/src/steamapi.rs | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index f749142..cb5b979 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,8 @@ use std::{ sync::{Arc, Mutex}, }; use steamguard::{ - steamapi, AccountLinker, Confirmation, ConfirmationType, LoginError, SteamGuardAccount, UserLogin, + steamapi, AccountLinker, Confirmation, ConfirmationType, LoginError, SteamGuardAccount, + UserLogin, }; use termion::{ event::{Event, Key}, diff --git a/steamguard/src/accountlinker.rs b/steamguard/src/accountlinker.rs index f09dfca..e9da246 100644 --- a/steamguard/src/accountlinker.rs +++ b/steamguard/src/accountlinker.rs @@ -1,8 +1,5 @@ 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 std::error::Error; use std::fmt::Display; @@ -52,47 +49,6 @@ impl AccountLinker { } pub fn finalize(&self, session: &Session) {} - - fn has_phone(&self, session: &Session) -> bool { - return self._phoneajax(session, "has_phone", "null"); - } - - fn _phoneajax(&self, session: &Session, op: &str, arg: &str) -> bool { - trace!("_phoneajax: op={}", op); - let url = "https://steamcommunity.com".parse::().unwrap(); - let cookies = reqwest::cookie::Jar::default(); - cookies.add_cookie_str("mobileClientVersion=0 (2.1.3)", &url); - cookies.add_cookie_str("mobileClient=android", &url); - cookies.add_cookie_str("Steam_Language=english", &url); - - let mut params = HashMap::new(); - params.insert("op", op); - params.insert("arg", arg); - params.insert("sessionid", session.session_id.as_str()); - if op == "check_sms_code" { - params.insert("checkfortos", "0"); - params.insert("skipvoip", "1"); - } - - let resp = self - .client - .post("https://steamcommunity.com/steamguard/phoneajax") - .header(COOKIE, cookies.cookies(&url).unwrap()) - .send() - .unwrap(); - - let result: Value = resp.json().unwrap(); - if result["has_phone"] != Value::Null { - trace!("found has_phone field"); - return result["has_phone"].as_bool().unwrap(); - } else if result["success"] != Value::Null { - trace!("found success field"); - return result["success"].as_bool().unwrap(); - } else { - trace!("did not find any expected field"); - return false; - } - } } fn generate_device_id() -> String { diff --git a/steamguard/src/steamapi.rs b/steamguard/src/steamapi.rs index 209cfeb..0fda929 100644 --- a/steamguard/src/steamapi.rs +++ b/steamguard/src/steamapi.rs @@ -7,6 +7,7 @@ use reqwest::{ Url, }; use serde::{Deserialize, Deserializer, Serialize}; +use serde_json::Value; use std::iter::FromIterator; use std::str::FromStr; use std::time::{SystemTime, UNIX_EPOCH}; @@ -289,6 +290,56 @@ impl SteamApiClient { } } } + + /// Endpoint: POST /steamguard/phoneajax + fn phoneajax(&self, op: &str, arg: &str) -> anyhow::Result { + let mut params = hashmap! { + "op" => op, + "arg" => arg, + "sessionid" => self.session.as_ref().unwrap().session_id.as_str(), + }; + if op == "check_sms_code" { + params.insert("checkfortos", "0"); + params.insert("skipvoip", "1"); + } + + let resp = self + .post("https://steamcommunity.com/steamguard/phoneajax") + .form(¶ms) + .send()?; + + let result: Value = resp.json()?; + if result["has_phone"] != Value::Null { + trace!("found has_phone field"); + return result["has_phone"] + .as_bool() + .ok_or(anyhow!("failed to parse has_phone field into boolean")); + } else if result["success"] != Value::Null { + trace!("found success field"); + return result["success"] + .as_bool() + .ok_or(anyhow!("failed to parse success field into boolean")); + } else { + trace!("did not find any expected field"); + return Ok(false); + } + } + + pub fn has_phone(&self) -> anyhow::Result { + return self.phoneajax("has_phone", "null"); + } + + pub fn check_sms_code(&self, sms_code: String) -> anyhow::Result { + return self.phoneajax("check_sms_code", sms_code.as_str()); + } + + pub fn check_email_confirmation(&self) -> anyhow::Result { + return self.phoneajax("email_confirmation", ""); + } + + pub fn add_phone_number(&self, phone_number: String) -> anyhow::Result { + return self.phoneajax("add_phone_number", phone_number.as_str()); + } } #[test]