2021-08-08 21:25:27 +02:00
|
|
|
use crate::{
|
|
|
|
steamapi::{AddAuthenticatorResponse, Session},
|
|
|
|
SteamGuardAccount,
|
|
|
|
};
|
2021-08-01 14:43:18 +02:00
|
|
|
use std::collections::HashMap;
|
2021-08-05 03:26:14 +02:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::Display;
|
2021-07-27 22:24:56 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct AccountLinker {
|
2021-08-08 18:54:46 +02:00
|
|
|
device_id: String,
|
|
|
|
phone_number: String,
|
|
|
|
pub account: SteamGuardAccount,
|
2021-08-05 03:26:14 +02:00
|
|
|
pub finalized: bool,
|
2021-08-08 18:54:46 +02:00
|
|
|
client: reqwest::blocking::Client,
|
2021-07-27 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountLinker {
|
2021-08-08 18:54:46 +02:00
|
|
|
pub fn new() -> AccountLinker {
|
|
|
|
return AccountLinker {
|
|
|
|
device_id: generate_device_id(),
|
|
|
|
phone_number: String::from(""),
|
|
|
|
account: SteamGuardAccount::new(),
|
2021-08-05 03:26:14 +02:00
|
|
|
finalized: false,
|
2021-08-08 18:54:46 +02:00
|
|
|
client: reqwest::blocking::ClientBuilder::new()
|
|
|
|
.cookie_store(true)
|
|
|
|
.build()
|
|
|
|
.unwrap(),
|
|
|
|
};
|
|
|
|
}
|
2021-07-27 22:24:56 +02:00
|
|
|
|
2021-08-05 03:26:14 +02:00
|
|
|
pub fn link(
|
|
|
|
&self,
|
|
|
|
session: &mut Session,
|
|
|
|
) -> anyhow::Result<AddAuthenticatorResponse, AccountLinkError> {
|
2021-08-08 18:54:46 +02:00
|
|
|
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());
|
2021-08-05 03:26:14 +02:00
|
|
|
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);
|
2021-08-08 18:54:46 +02:00
|
|
|
}
|
2021-07-27 22:24:56 +02:00
|
|
|
|
2021-08-05 03:26:14 +02:00
|
|
|
pub fn finalize(&self, session: &Session) {}
|
2021-07-27 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_device_id() -> String {
|
2021-08-08 18:54:46 +02:00
|
|
|
return format!("android:{}", uuid::Uuid::new_v4().to_string());
|
2021-07-27 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 03:26:14 +02:00
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
}
|