2023-06-23 19:36:23 +02:00
|
|
|
use crate::protobufs::service_twofactor::CTwoFactor_RemoveAuthenticator_Request;
|
2023-06-22 22:20:15 +02:00
|
|
|
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};
|
2023-06-27 16:20:27 +02:00
|
|
|
pub use confirmation::*;
|
2023-06-24 19:45:03 +02:00
|
|
|
pub use qrapprover::{QrApprover, QrApproverError};
|
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};
|
2023-06-27 16:20:27 +02:00
|
|
|
use std::io::Read;
|
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
|
|
|
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-25 19:11:24 +02:00
|
|
|
pub mod phonelinker;
|
2023-06-22 22:20:15 +02:00
|
|
|
pub mod protobufs;
|
2023-06-24 19:45:03 +02:00
|
|
|
mod qrapprover;
|
2023-06-24 19:18:22 +02:00
|
|
|
pub mod refresher;
|
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
|
|
|
}
|
|
|
|
|
2023-06-23 19:36:23 +02:00
|
|
|
impl Default for SteamGuardAccount {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-08-08 18:54:46 +02:00
|
|
|
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,
|
2023-06-23 19:36:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SteamGuardAccount {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
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 {
|
2023-06-23 19:36:23 +02:00
|
|
|
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 {
|
2023-06-23 19:36:23 +02:00
|
|
|
self.shared_secret.generate_code(time)
|
2021-08-08 18:54:46 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|