2023-06-22 22:20:15 +02:00
|
|
|
use crate::api_responses::SteamApiResponse;
|
|
|
|
use crate::confirmation::{ConfirmationListResponse, SendConfirmationResponse};
|
|
|
|
use crate::protobufs::service_twofactor::{
|
|
|
|
CTwoFactor_RemoveAuthenticator_Request, CTwoFactor_RemoveAuthenticator_Response,
|
|
|
|
};
|
|
|
|
use crate::steamapi::EResult;
|
|
|
|
use crate::{
|
|
|
|
steamapi::twofactor::TwoFactorClient, token::TwoFactorSecret, transport::WebApiTransport,
|
|
|
|
};
|
2021-08-10 00:44:42 +02:00
|
|
|
pub use accountlinker::{AccountLinkError, AccountLinker, FinalizeLinkError};
|
2021-07-28 21:07:15 +02:00
|
|
|
use anyhow::Result;
|
2021-07-29 15:08:06 +02:00
|
|
|
pub use confirmation::{Confirmation, ConfirmationType};
|
2021-04-04 20:07:06 +02:00
|
|
|
use hmacsha1::hmac_sha1;
|
|
|
|
use log::*;
|
2021-08-01 14:43:18 +02:00
|
|
|
use reqwest::{
|
2021-08-08 18:54:46 +02:00
|
|
|
cookie::CookieStore,
|
|
|
|
header::{COOKIE, USER_AGENT},
|
|
|
|
Url,
|
2021-08-01 14:43:18 +02:00
|
|
|
};
|
2021-07-31 22:57:51 +02:00
|
|
|
use scraper::{Html, Selector};
|
2022-06-19 20:44:18 +02:00
|
|
|
pub use secrecy::{ExposeSecret, SecretString};
|
2021-08-01 14:43:18 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-08-13 15:26:23 +02:00
|
|
|
use std::{collections::HashMap, convert::TryInto, io::Read};
|
2021-08-12 01:39:29 +02:00
|
|
|
use steamapi::SteamApiClient;
|
2023-06-22 22:20:15 +02:00
|
|
|
use token::Tokens;
|
|
|
|
pub use userlogin::{DeviceDetails, LoginError, UserLogin};
|
|
|
|
|
2021-04-04 23:48:44 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2021-07-28 05:49:53 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate anyhow;
|
2021-08-08 00:47:39 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate maplit;
|
2021-03-26 18:32:37 +01:00
|
|
|
|
2023-06-22 22:20:15 +02:00
|
|
|
pub mod accountlinker;
|
2022-12-06 16:02:07 +01:00
|
|
|
mod api_responses;
|
2021-07-28 05:49:53 +02:00
|
|
|
mod confirmation;
|
2023-06-22 22:20:15 +02:00
|
|
|
pub mod protobufs;
|
2022-06-19 20:44:18 +02:00
|
|
|
mod secret_string;
|
2021-08-01 14:43:18 +02:00
|
|
|
pub mod steamapi;
|
2021-08-25 03:13:16 +02:00
|
|
|
pub mod token;
|
2023-06-22 22:20:15 +02:00
|
|
|
pub mod transport;
|
|
|
|
pub mod userlogin;
|
2021-03-22 02:21:29 +01:00
|
|
|
|
|
|
|
extern crate base64;
|
2021-03-30 21:51:26 +02:00
|
|
|
extern crate cookie;
|
2021-08-01 14:43:18 +02:00
|
|
|
extern crate hmacsha1;
|
2021-03-22 02:21:29 +01:00
|
|
|
|
2021-03-27 17:14:34 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-03-22 02:21:29 +01:00
|
|
|
pub struct SteamGuardAccount {
|
2021-08-08 18:54:46 +02:00
|
|
|
pub account_name: String,
|
2023-06-22 22:20:15 +02:00
|
|
|
pub steam_id: u64,
|
2021-08-08 18:54:46 +02:00
|
|
|
pub serial_number: String,
|
2022-06-19 20:09:08 +02:00
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub revocation_code: SecretString,
|
2021-08-25 03:13:16 +02:00
|
|
|
pub shared_secret: TwoFactorSecret,
|
2021-08-08 18:54:46 +02:00
|
|
|
pub token_gid: String,
|
2022-06-19 20:09:08 +02:00
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub identity_secret: SecretString,
|
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub uri: SecretString,
|
2021-08-08 18:54:46 +02:00
|
|
|
pub device_id: String,
|
2022-06-19 20:09:08 +02:00
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub secret_1: SecretString,
|
2023-06-22 22:20:15 +02:00
|
|
|
pub tokens: Option<Tokens>,
|
2021-03-22 02:21:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
fn build_time_bytes(time: u64) -> [u8; 8] {
|
2021-08-08 18:54:46 +02:00
|
|
|
return time.to_be_bytes();
|
2021-03-22 02:21:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
fn generate_confirmation_hash_for_time(time: u64, tag: &str, identity_secret: &String) -> String {
|
2021-08-08 18:54:46 +02:00
|
|
|
let decode: &[u8] = &base64::decode(&identity_secret).unwrap();
|
|
|
|
let time_bytes = build_time_bytes(time);
|
|
|
|
let tag_bytes = tag.as_bytes();
|
|
|
|
let array = [&time_bytes, tag_bytes].concat();
|
|
|
|
let hash = hmac_sha1(decode, &array);
|
|
|
|
let encoded = base64::encode(hash);
|
|
|
|
return encoded;
|
2021-04-05 05:18:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:21:29 +01:00
|
|
|
impl SteamGuardAccount {
|
2021-08-08 18:54:46 +02:00
|
|
|
pub fn new() -> Self {
|
|
|
|
return SteamGuardAccount {
|
|
|
|
account_name: String::from(""),
|
2023-06-22 22:20:15 +02:00
|
|
|
steam_id: 0,
|
2021-08-08 18:54:46 +02:00
|
|
|
serial_number: String::from(""),
|
2022-06-19 20:09:08 +02:00
|
|
|
revocation_code: String::from("").into(),
|
2021-08-25 03:13:16 +02:00
|
|
|
shared_secret: TwoFactorSecret::new(),
|
2021-08-08 18:54:46 +02:00
|
|
|
token_gid: String::from(""),
|
2022-06-19 20:09:08 +02:00
|
|
|
identity_secret: String::from("").into(),
|
|
|
|
uri: String::from("").into(),
|
2021-08-08 18:54:46 +02:00
|
|
|
device_id: String::from(""),
|
2022-06-19 20:09:08 +02:00
|
|
|
secret_1: String::from("").into(),
|
2023-06-22 22:20:15 +02:00
|
|
|
tokens: None,
|
2021-08-08 18:54:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:26:23 +02:00
|
|
|
pub fn from_reader<T>(r: T) -> anyhow::Result<Self>
|
|
|
|
where
|
|
|
|
T: Read,
|
|
|
|
{
|
|
|
|
Ok(serde_json::from_reader(r)?)
|
|
|
|
}
|
|
|
|
|
2023-06-22 22:20:15 +02:00
|
|
|
pub fn set_tokens(&mut self, tokens: Tokens) {
|
|
|
|
self.tokens = Some(tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_logged_in(&self) -> bool {
|
|
|
|
return self.tokens.is_some();
|
2022-06-19 20:42:07 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
pub fn generate_code(&self, time: u64) -> String {
|
2021-08-25 03:13:16 +02:00
|
|
|
return self.shared_secret.generate_code(time);
|
2021-08-08 18:54:46 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
fn get_confirmation_query_params(&self, tag: &str, time: u64) -> HashMap<&str, String> {
|
2021-08-08 18:54:46 +02:00
|
|
|
let mut params = HashMap::new();
|
|
|
|
params.insert("p", self.device_id.clone());
|
2023-06-22 22:20:15 +02:00
|
|
|
params.insert("a", self.steam_id.to_string());
|
2021-08-08 18:54:46 +02:00
|
|
|
params.insert(
|
|
|
|
"k",
|
2022-06-19 20:09:08 +02:00
|
|
|
generate_confirmation_hash_for_time(time, tag, &self.identity_secret.expose_secret()),
|
2021-08-08 18:54:46 +02:00
|
|
|
);
|
|
|
|
params.insert("t", time.to_string());
|
|
|
|
params.insert("m", String::from("android"));
|
|
|
|
params.insert("tag", String::from(tag));
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_cookie_jar(&self) -> reqwest::cookie::Jar {
|
|
|
|
let url = "https://steamcommunity.com".parse::<Url>().unwrap();
|
|
|
|
let cookies = reqwest::cookie::Jar::default();
|
2023-06-22 22:20:15 +02:00
|
|
|
// let session = self.session.as_ref().unwrap().expose_secret();
|
|
|
|
let tokens = self.tokens.as_ref().unwrap();
|
2021-08-08 18:54:46 +02:00
|
|
|
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);
|
|
|
|
cookies.add_cookie_str("dob=", &url);
|
2023-06-22 22:20:15 +02:00
|
|
|
// cookies.add_cookie_str(format!("sessionid={}", session.session_id).as_str(), &url);
|
|
|
|
cookies.add_cookie_str(format!("steamid={}", self.steam_id).as_str(), &url);
|
2021-08-08 18:54:46 +02:00
|
|
|
cookies.add_cookie_str(
|
2023-06-22 22:20:15 +02:00
|
|
|
format!(
|
|
|
|
"steamLoginSecure={}||{}",
|
|
|
|
self.steam_id,
|
|
|
|
tokens.access_token().expose_secret()
|
|
|
|
)
|
|
|
|
.as_str(),
|
2021-08-08 18:54:46 +02:00
|
|
|
&url,
|
|
|
|
);
|
|
|
|
return cookies;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_trade_confirmations(&self) -> Result<Vec<Confirmation>, anyhow::Error> {
|
|
|
|
// uri: "https://steamcommunity.com/mobileconf/conf"
|
|
|
|
// confirmation details:
|
|
|
|
let url = "https://steamcommunity.com".parse::<Url>().unwrap();
|
|
|
|
let cookies = self.build_cookie_jar();
|
|
|
|
let client = reqwest::blocking::ClientBuilder::new()
|
|
|
|
.cookie_store(true)
|
|
|
|
.build()?;
|
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
let time = steamapi::get_server_time()?.server_time;
|
2021-08-08 18:54:46 +02:00
|
|
|
let resp = client
|
2023-06-22 22:20:15 +02:00
|
|
|
.get("https://steamcommunity.com/mobileconf/getlist".parse::<Url>().unwrap())
|
2021-04-04 23:48:44 +02:00
|
|
|
.header("X-Requested-With", "com.valvesoftware.android.steam.community")
|
|
|
|
.header(USER_AGENT, "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
|
|
|
|
.header(COOKIE, cookies.cookies(&url).unwrap())
|
2022-06-21 02:05:00 +02:00
|
|
|
.query(&self.get_confirmation_query_params("conf", time))
|
2021-07-31 22:57:51 +02:00
|
|
|
.send()?;
|
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
trace!("{:?}", resp);
|
|
|
|
let text = resp.text().unwrap();
|
|
|
|
trace!("text: {:?}", text);
|
2023-04-20 00:07:10 +02:00
|
|
|
trace!("{}", text);
|
2023-06-22 22:20:15 +02:00
|
|
|
|
|
|
|
let body: ConfirmationListResponse = serde_json::from_str(text.as_str())?;
|
|
|
|
ensure!(body.success);
|
|
|
|
Ok(body.conf)
|
2021-08-08 18:54:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Respond to a confirmation.
|
|
|
|
///
|
|
|
|
/// Host: https://steamcommunity.com
|
|
|
|
/// Steam Endpoint: `GET /mobileconf/ajaxop`
|
|
|
|
fn send_confirmation_ajax(&self, conf: &Confirmation, operation: String) -> anyhow::Result<()> {
|
|
|
|
ensure!(operation == "allow" || operation == "cancel");
|
|
|
|
|
|
|
|
let url = "https://steamcommunity.com".parse::<Url>().unwrap();
|
|
|
|
let cookies = self.build_cookie_jar();
|
|
|
|
let client = reqwest::blocking::ClientBuilder::new()
|
|
|
|
.cookie_store(true)
|
|
|
|
.build()?;
|
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
let time = steamapi::get_server_time()?.server_time;
|
|
|
|
let mut query_params = self.get_confirmation_query_params("conf", time);
|
2021-08-08 18:54:46 +02:00
|
|
|
query_params.insert("op", operation);
|
|
|
|
query_params.insert("cid", conf.id.to_string());
|
2023-06-22 22:20:15 +02:00
|
|
|
query_params.insert("ck", conf.nonce.to_string());
|
2021-08-08 18:54:46 +02:00
|
|
|
|
2022-07-23 13:42:29 +02:00
|
|
|
let resp = client.get("https://steamcommunity.com/mobileconf/ajaxop".parse::<Url>().unwrap())
|
2021-07-28 21:07:15 +02:00
|
|
|
.header("X-Requested-With", "com.valvesoftware.android.steam.community")
|
|
|
|
.header(USER_AGENT, "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
|
|
|
|
.header(COOKIE, cookies.cookies(&url).unwrap())
|
|
|
|
.query(&query_params)
|
2022-07-23 13:42:29 +02:00
|
|
|
.send()?;
|
|
|
|
|
|
|
|
trace!("send_confirmation_ajax() response: {:?}", &resp);
|
|
|
|
debug!(
|
|
|
|
"send_confirmation_ajax() response status code: {}",
|
|
|
|
&resp.status()
|
|
|
|
);
|
|
|
|
|
|
|
|
let raw = resp.text()?;
|
|
|
|
debug!("send_confirmation_ajax() response body: {:?}", &raw);
|
|
|
|
|
|
|
|
let body: SendConfirmationResponse = serde_json::from_str(raw.as_str())?;
|
|
|
|
|
|
|
|
if !body.success {
|
|
|
|
return Err(anyhow!("Server responded with failure."));
|
|
|
|
}
|
2021-07-28 21:07:15 +02:00
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-28 21:48:06 +02:00
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
pub fn accept_confirmation(&self, conf: &Confirmation) -> anyhow::Result<()> {
|
|
|
|
self.send_confirmation_ajax(conf, "allow".into())
|
|
|
|
}
|
2021-07-28 21:48:06 +02:00
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
pub fn deny_confirmation(&self, conf: &Confirmation) -> anyhow::Result<()> {
|
|
|
|
self.send_confirmation_ajax(conf, "cancel".into())
|
|
|
|
}
|
2021-07-28 22:09:21 +02:00
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
/// Steam Endpoint: `GET /mobileconf/details/:id`
|
|
|
|
pub fn get_confirmation_details(&self, conf: &Confirmation) -> anyhow::Result<String> {
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
struct ConfirmationDetailsResponse {
|
|
|
|
pub success: bool,
|
|
|
|
pub html: String,
|
|
|
|
}
|
2021-07-28 22:09:21 +02:00
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
let url = "https://steamcommunity.com".parse::<Url>().unwrap();
|
|
|
|
let cookies = self.build_cookie_jar();
|
|
|
|
let client = reqwest::blocking::ClientBuilder::new()
|
|
|
|
.cookie_store(true)
|
|
|
|
.build()?;
|
2021-07-28 22:09:21 +02:00
|
|
|
|
2022-06-21 02:05:00 +02:00
|
|
|
let time = steamapi::get_server_time()?.server_time;
|
|
|
|
let query_params = self.get_confirmation_query_params("details", time);
|
2021-07-28 22:09:21 +02:00
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
let resp: ConfirmationDetailsResponse = client.get(format!("https://steamcommunity.com/mobileconf/details/{}", conf.id).parse::<Url>().unwrap())
|
2021-07-28 22:09:21 +02:00
|
|
|
.header("X-Requested-With", "com.valvesoftware.android.steam.community")
|
|
|
|
.header(USER_AGENT, "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
|
|
|
|
.header(COOKIE, cookies.cookies(&url).unwrap())
|
|
|
|
.query(&query_params)
|
|
|
|
.send()?
|
|
|
|
.json()?;
|
|
|
|
|
2021-08-08 18:54:46 +02:00
|
|
|
ensure!(resp.success);
|
|
|
|
Ok(resp.html)
|
|
|
|
}
|
2021-08-12 01:39:29 +02:00
|
|
|
|
|
|
|
/// Removes the mobile authenticator from the steam account. If this operation succeeds, this object can no longer be considered valid.
|
|
|
|
/// Returns whether or not the operation was successful.
|
|
|
|
pub fn remove_authenticator(&self, revocation_code: Option<String>) -> anyhow::Result<bool> {
|
|
|
|
ensure!(
|
2022-06-19 20:09:08 +02:00
|
|
|
matches!(revocation_code, Some(_)) || !self.revocation_code.expose_secret().is_empty(),
|
2021-08-12 01:39:29 +02:00
|
|
|
"Revocation code not provided."
|
|
|
|
);
|
2023-06-22 22:20:15 +02:00
|
|
|
let Some(tokens) = &self.tokens else {
|
|
|
|
return Err(anyhow!("Tokens not set, login required"));
|
2021-08-08 18:54:46 +02:00
|
|
|
};
|
2023-06-22 22:20:15 +02:00
|
|
|
let mut client = TwoFactorClient::new(WebApiTransport::new());
|
|
|
|
let mut req = CTwoFactor_RemoveAuthenticator_Request::new();
|
|
|
|
req.set_revocation_code(
|
|
|
|
revocation_code.unwrap_or(self.revocation_code.expose_secret().to_owned()),
|
|
|
|
);
|
|
|
|
let resp = client.remove_authenticator(req, tokens.access_token())?;
|
|
|
|
if resp.result != EResult::OK {
|
|
|
|
Err(anyhow!("Failed to remove authenticator: {:?}", resp.result))
|
|
|
|
} else {
|
|
|
|
Ok(true)
|
|
|
|
}
|
2021-08-08 18:54:46 +02:00
|
|
|
}
|
2021-07-31 22:57:51 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:21:29 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-08-08 18:54:46 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_confirmation_hash_for_time() {
|
|
|
|
assert_eq!(
|
|
|
|
generate_confirmation_hash_for_time(
|
|
|
|
1617591917,
|
|
|
|
"conf",
|
|
|
|
&String::from("GQP46b73Ws7gr8GmZFR0sDuau5c=")
|
|
|
|
),
|
|
|
|
String::from("NaL8EIMhfy/7vBounJ0CvpKbrPk=")
|
|
|
|
);
|
|
|
|
}
|
2021-03-22 02:21:29 +01:00
|
|
|
}
|