move phoneajax methods to steamapi module
This commit is contained in:
parent
31a888c0e4
commit
e4d7ea4475
3 changed files with 53 additions and 45 deletions
|
@ -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},
|
||||
|
|
|
@ -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::<Url>().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 {
|
||||
|
|
|
@ -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<bool> {
|
||||
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<bool> {
|
||||
return self.phoneajax("has_phone", "null");
|
||||
}
|
||||
|
||||
pub fn check_sms_code(&self, sms_code: String) -> anyhow::Result<bool> {
|
||||
return self.phoneajax("check_sms_code", sms_code.as_str());
|
||||
}
|
||||
|
||||
pub fn check_email_confirmation(&self) -> anyhow::Result<bool> {
|
||||
return self.phoneajax("email_confirmation", "");
|
||||
}
|
||||
|
||||
pub fn add_phone_number(&self, phone_number: String) -> anyhow::Result<bool> {
|
||||
return self.phoneajax("add_phone_number", phone_number.as_str());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue