2023-06-23 13:36:23 -04:00
|
|
|
use crate::protobufs::service_twofactor::CTwoFactor_RemoveAuthenticator_Request;
|
2023-06-22 16:20:15 -04:00
|
|
|
use crate::steamapi::EResult;
|
2023-07-02 08:57:13 -04:00
|
|
|
use crate::{steamapi::twofactor::TwoFactorClient, token::TwoFactorSecret};
|
2021-08-09 18:44:42 -04:00
|
|
|
pub use accountlinker::{AccountLinkError, AccountLinker, FinalizeLinkError};
|
2023-06-27 10:20:27 -04:00
|
|
|
pub use confirmation::*;
|
2023-06-24 13:45:03 -04:00
|
|
|
pub use qrapprover::{QrApprover, QrApproverError};
|
2022-06-19 14:44:18 -04:00
|
|
|
pub use secrecy::{ExposeSecret, SecretString};
|
2021-08-01 12:43:18 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-06-27 10:20:27 -04:00
|
|
|
use std::io::Read;
|
2023-06-22 16:20:15 -04:00
|
|
|
use token::Tokens;
|
2023-07-02 08:57:13 -04:00
|
|
|
use transport::{Transport, TransportError};
|
2023-06-22 16:20:15 -04:00
|
|
|
pub use userlogin::{DeviceDetails, LoginError, UserLogin};
|
|
|
|
|
2021-04-04 17:48:44 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2021-07-27 23:49:53 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate anyhow;
|
2021-08-07 18:47:39 -04:00
|
|
|
extern crate maplit;
|
2021-03-26 13:32:37 -04:00
|
|
|
|
2023-06-22 16:20:15 -04:00
|
|
|
pub mod accountlinker;
|
2022-12-06 10:02:07 -05:00
|
|
|
mod api_responses;
|
2021-07-27 23:49:53 -04:00
|
|
|
mod confirmation;
|
2023-06-25 13:11:24 -04:00
|
|
|
pub mod phonelinker;
|
2023-06-22 16:20:15 -04:00
|
|
|
pub mod protobufs;
|
2023-06-24 13:45:03 -04:00
|
|
|
mod qrapprover;
|
2023-06-24 13:18:22 -04:00
|
|
|
pub mod refresher;
|
2022-06-19 14:44:18 -04:00
|
|
|
mod secret_string;
|
2021-08-01 12:43:18 +00:00
|
|
|
pub mod steamapi;
|
2021-08-24 21:13:16 -04:00
|
|
|
pub mod token;
|
2023-06-22 16:20:15 -04:00
|
|
|
pub mod transport;
|
|
|
|
pub mod userlogin;
|
2021-03-21 21:21:29 -04:00
|
|
|
|
|
|
|
extern crate base64;
|
2021-03-30 15:51:26 -04:00
|
|
|
extern crate cookie;
|
2021-03-21 21:21:29 -04:00
|
|
|
|
2021-03-27 12:14:34 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-03-21 21:21:29 -04:00
|
|
|
pub struct SteamGuardAccount {
|
2021-08-08 12:54:46 -04:00
|
|
|
pub account_name: String,
|
2023-06-22 16:20:15 -04:00
|
|
|
pub steam_id: u64,
|
2021-08-08 12:54:46 -04:00
|
|
|
pub serial_number: String,
|
2022-06-19 14:09:08 -04:00
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub revocation_code: SecretString,
|
2021-08-24 21:13:16 -04:00
|
|
|
pub shared_secret: TwoFactorSecret,
|
2021-08-08 12:54:46 -04:00
|
|
|
pub token_gid: String,
|
2022-06-19 14:09:08 -04:00
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub identity_secret: SecretString,
|
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub uri: SecretString,
|
2021-08-08 12:54:46 -04:00
|
|
|
pub device_id: String,
|
2022-06-19 14:09:08 -04:00
|
|
|
#[serde(with = "secret_string")]
|
|
|
|
pub secret_1: SecretString,
|
2023-06-22 16:20:15 -04:00
|
|
|
pub tokens: Option<Tokens>,
|
2021-03-21 21:21:29 -04:00
|
|
|
}
|
|
|
|
|
2023-06-23 13:36:23 -04:00
|
|
|
impl Default for SteamGuardAccount {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-08-08 12:54:46 -04:00
|
|
|
account_name: String::from(""),
|
2023-06-22 16:20:15 -04:00
|
|
|
steam_id: 0,
|
2021-08-08 12:54:46 -04:00
|
|
|
serial_number: String::from(""),
|
2022-06-19 14:09:08 -04:00
|
|
|
revocation_code: String::from("").into(),
|
2021-08-24 21:13:16 -04:00
|
|
|
shared_secret: TwoFactorSecret::new(),
|
2021-08-08 12:54:46 -04:00
|
|
|
token_gid: String::from(""),
|
2022-06-19 14:09:08 -04:00
|
|
|
identity_secret: String::from("").into(),
|
|
|
|
uri: String::from("").into(),
|
2021-08-08 12:54:46 -04:00
|
|
|
device_id: String::from(""),
|
2022-06-19 14:09:08 -04:00
|
|
|
secret_1: String::from("").into(),
|
2023-06-22 16:20:15 -04:00
|
|
|
tokens: None,
|
2023-06-23 13:36:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SteamGuardAccount {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
2021-08-08 12:54:46 -04:00
|
|
|
}
|
|
|
|
|
2022-08-13 09:26:23 -04:00
|
|
|
pub fn from_reader<T>(r: T) -> anyhow::Result<Self>
|
|
|
|
where
|
|
|
|
T: Read,
|
|
|
|
{
|
|
|
|
Ok(serde_json::from_reader(r)?)
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:20:15 -04:00
|
|
|
pub fn set_tokens(&mut self, tokens: Tokens) {
|
|
|
|
self.tokens = Some(tokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_logged_in(&self) -> bool {
|
2023-06-23 13:36:23 -04:00
|
|
|
self.tokens.is_some()
|
2022-06-19 14:42:07 -04:00
|
|
|
}
|
|
|
|
|
2022-06-20 20:05:00 -04:00
|
|
|
pub fn generate_code(&self, time: u64) -> String {
|
2023-06-23 13:36:23 -04:00
|
|
|
self.shared_secret.generate_code(time)
|
2021-08-08 12:54:46 -04:00
|
|
|
}
|
|
|
|
|
2021-08-11 19:39:29 -04: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.
|
2023-07-02 08:57:13 -04:00
|
|
|
pub fn remove_authenticator<T: Transport>(
|
2023-06-30 10:53:05 -04:00
|
|
|
&self,
|
2023-07-02 08:57:13 -04:00
|
|
|
client: &TwoFactorClient<T>,
|
2023-06-30 10:53:05 -04:00
|
|
|
revocation_code: Option<&String>,
|
|
|
|
) -> Result<(), RemoveAuthenticatorError> {
|
2023-09-10 17:22:23 -04:00
|
|
|
if revocation_code.is_none() && self.revocation_code.expose_secret().is_empty() {
|
2023-06-30 10:53:05 -04:00
|
|
|
return Err(RemoveAuthenticatorError::MissingRevocationCode);
|
|
|
|
}
|
2023-06-22 16:20:15 -04:00
|
|
|
let Some(tokens) = &self.tokens else {
|
2023-09-10 17:22:23 -04:00
|
|
|
return Err(RemoveAuthenticatorError::TransportError(
|
|
|
|
TransportError::Unauthorized,
|
|
|
|
));
|
2021-08-08 12:54:46 -04:00
|
|
|
};
|
2023-06-22 16:20:15 -04:00
|
|
|
let mut req = CTwoFactor_RemoveAuthenticator_Request::new();
|
|
|
|
req.set_revocation_code(
|
2023-06-30 10:53:05 -04:00
|
|
|
revocation_code
|
|
|
|
.unwrap_or(self.revocation_code.expose_secret())
|
|
|
|
.to_owned(),
|
2023-06-22 16:20:15 -04:00
|
|
|
);
|
|
|
|
let resp = client.remove_authenticator(req, tokens.access_token())?;
|
2023-06-30 10:53:05 -04:00
|
|
|
|
|
|
|
// returns EResult::TwoFactorCodeMismatch if the revocation code is incorrect
|
|
|
|
if resp.result != EResult::OK && resp.result != EResult::TwoFactorCodeMismatch {
|
|
|
|
return Err(resp.result.into());
|
2023-06-22 16:20:15 -04:00
|
|
|
}
|
2023-06-30 10:53:05 -04:00
|
|
|
let resp = resp.into_response_data();
|
|
|
|
if !resp.success() {
|
|
|
|
return Err(RemoveAuthenticatorError::IncorrectRevocationCode {
|
|
|
|
attempts_remaining: resp.revocation_attempts_remaining(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum RemoveAuthenticatorError {
|
|
|
|
#[error("Missing revocation code")]
|
|
|
|
MissingRevocationCode,
|
|
|
|
#[error("Incorrect revocation code, {attempts_remaining} attempts remaining")]
|
|
|
|
IncorrectRevocationCode { attempts_remaining: u32 },
|
|
|
|
#[error("Transport error: {0}")]
|
|
|
|
TransportError(#[from] TransportError),
|
|
|
|
#[error("Steam returned an enexpected result: {0:?}")]
|
|
|
|
UnknownEResult(EResult),
|
|
|
|
#[error("Unexpected error: {0}")]
|
|
|
|
Unknown(#[from] anyhow::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EResult> for RemoveAuthenticatorError {
|
|
|
|
fn from(e: EResult) -> Self {
|
|
|
|
Self::UnknownEResult(e)
|
2021-08-08 12:54:46 -04:00
|
|
|
}
|
2021-07-31 16:57:51 -04:00
|
|
|
}
|