From 3146d4824c702c3b7640cd3251e72cdb8a5cc1ee Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sun, 2 Jul 2023 07:17:09 -0400 Subject: [PATCH] refactor to make adding proxy support easier later (#263) --- src/commands/qr_login.rs | 4 ++-- src/commands/setup.rs | 4 ++-- src/main.rs | 4 ++-- steamguard/src/accountlinker.rs | 18 ++++++++++----- steamguard/src/lib.rs | 2 +- steamguard/src/phonelinker.rs | 17 ++++++++++----- steamguard/src/qrapprover.rs | 18 ++++++++++----- steamguard/src/refresher.rs | 16 +++++++++----- steamguard/src/steamapi.rs | 2 +- steamguard/src/transport/webapi.rs | 23 +++++++++++--------- steamguard/src/userlogin.rs | 35 ++++++++++++++++-------------- 11 files changed, 86 insertions(+), 57 deletions(-) diff --git a/src/commands/qr_login.rs b/src/commands/qr_login.rs index 9e7f673..b6fddb6 100644 --- a/src/commands/qr_login.rs +++ b/src/commands/qr_login.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Mutex}; use log::*; -use steamguard::{QrApprover, QrApproverError}; +use steamguard::{transport::WebApiTransport, QrApprover, QrApproverError}; use crate::AccountManager; @@ -42,7 +42,7 @@ impl AccountCommand for QrLoginCommand { return Err(anyhow!("No tokens found for {}", account.account_name)); }; - let mut approver = QrApprover::new(tokens); + let mut approver = QrApprover::new(WebApiTransport::default(), tokens); match approver.approve(&account, &self.url) { Ok(_) => { info!("Login approved."); diff --git a/src/commands/setup.rs b/src/commands/setup.rs index 6bdcbfb..f1a2b01 100644 --- a/src/commands/setup.rs +++ b/src/commands/setup.rs @@ -31,7 +31,7 @@ impl ManifestCommand for SetupCommand { crate::do_login_raw(username).expect("Failed to log in. Account has not been linked."); info!("Adding authenticator..."); - let mut linker = AccountLinker::new(tokens); + let mut linker = AccountLinker::new(WebApiTransport::default(), tokens); let link: AccountLinkSuccess; loop { match linker.link() { @@ -130,7 +130,7 @@ impl ManifestCommand for SetupCommand { } pub fn do_add_phone_number(tokens: &Tokens) -> anyhow::Result<()> { - let client = PhoneClient::new(WebApiTransport::new()); + let client = PhoneClient::new(WebApiTransport::default()); let linker = PhoneLinker::new(client, tokens.clone()); diff --git a/src/main.rs b/src/main.rs index ed07296..70706d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -274,7 +274,7 @@ fn get_selected_accounts( fn do_login(account: &mut SteamGuardAccount) -> anyhow::Result<()> { if let Some(tokens) = account.tokens.as_mut() { info!("Refreshing access token..."); - let client = AuthenticationClient::new(WebApiTransport::new()); + let client = AuthenticationClient::new(WebApiTransport::default()); let mut refresher = TokenRefresher::new(client); match refresher.refresh(account.steam_id, tokens) { Ok(token) => { @@ -327,7 +327,7 @@ fn do_login_impl( password: String, account: Option<&SteamGuardAccount>, ) -> anyhow::Result { - let mut login = UserLogin::new(build_device_details()); + let mut login = UserLogin::new(WebApiTransport::default(), build_device_details()); let mut password = password; let confirmation_methods; diff --git a/steamguard/src/accountlinker.rs b/steamguard/src/accountlinker.rs index b4e381b..c10ade4 100644 --- a/steamguard/src/accountlinker.rs +++ b/steamguard/src/accountlinker.rs @@ -3,28 +3,34 @@ use crate::protobufs::service_twofactor::{ }; use crate::steamapi::twofactor::TwoFactorClient; use crate::token::TwoFactorSecret; -use crate::transport::WebApiTransport; +use crate::transport::Transport; use crate::{steamapi::EResult, token::Tokens, SteamGuardAccount}; use log::*; use thiserror::Error; #[derive(Debug)] -pub struct AccountLinker { +pub struct AccountLinker +where + T: Transport, +{ device_id: String, pub account: Option, pub finalized: bool, tokens: Tokens, - client: TwoFactorClient, + client: TwoFactorClient, } -impl AccountLinker { - pub fn new(tokens: Tokens) -> AccountLinker { +impl AccountLinker +where + T: Transport, +{ + pub fn new(transport: T, tokens: Tokens) -> Self { Self { device_id: generate_device_id(), account: None, finalized: false, tokens, - client: TwoFactorClient::new(WebApiTransport::new()), + client: TwoFactorClient::new(transport), } } diff --git a/steamguard/src/lib.rs b/steamguard/src/lib.rs index 4c8c5c4..6c3fbce 100644 --- a/steamguard/src/lib.rs +++ b/steamguard/src/lib.rs @@ -109,7 +109,7 @@ impl SteamGuardAccount { let Some(tokens) = &self.tokens else { return Err(RemoveAuthenticatorError::TransportError(TransportError::Unauthorized)); }; - let mut client = TwoFactorClient::new(WebApiTransport::new()); + let mut client = TwoFactorClient::new(WebApiTransport::default()); let mut req = CTwoFactor_RemoveAuthenticator_Request::new(); req.set_revocation_code( revocation_code diff --git a/steamguard/src/phonelinker.rs b/steamguard/src/phonelinker.rs index 02115ce..8b8801d 100644 --- a/steamguard/src/phonelinker.rs +++ b/steamguard/src/phonelinker.rs @@ -1,20 +1,25 @@ use crate::protobufs::service_phone::*; -use crate::transport::TransportError; +use crate::transport::{Transport, TransportError}; use crate::{ steamapi::{EResult, PhoneClient}, token::Tokens, - transport::WebApiTransport, }; pub use phonenumber::PhoneNumber; -pub struct PhoneLinker { - client: PhoneClient, +pub struct PhoneLinker +where + T: Transport, +{ + client: PhoneClient, tokens: Tokens, } -impl PhoneLinker { - pub fn new(client: PhoneClient, tokens: Tokens) -> Self { +impl PhoneLinker +where + T: Transport, +{ + pub fn new(client: PhoneClient, tokens: Tokens) -> Self { Self { client, tokens } } diff --git a/steamguard/src/qrapprover.rs b/steamguard/src/qrapprover.rs index 9cda119..00f8f0e 100644 --- a/steamguard/src/qrapprover.rs +++ b/steamguard/src/qrapprover.rs @@ -5,21 +5,27 @@ use crate::{ protobufs::steammessages_auth_steamclient::CAuthentication_UpdateAuthSessionWithMobileConfirmation_Request, steamapi::{AuthenticationClient, EResult}, token::{Tokens, TwoFactorSecret}, - transport::WebApiTransport, + transport::Transport, SteamGuardAccount, }; /// QR code login approver /// /// This can be used to approve a login request from another device that is displaying a QR code. -pub struct QrApprover<'a> { +pub struct QrApprover<'a, T> +where + T: Transport, +{ tokens: &'a Tokens, - client: AuthenticationClient, + client: AuthenticationClient, } -impl<'a> QrApprover<'a> { - pub fn new(tokens: &'a Tokens) -> Self { - let client = AuthenticationClient::new(WebApiTransport::new()); +impl<'a, T> QrApprover<'a, T> +where + T: Transport, +{ + pub fn new(transport: T, tokens: &'a Tokens) -> Self { + let client = AuthenticationClient::new(transport); Self { tokens, client } } diff --git a/steamguard/src/refresher.rs b/steamguard/src/refresher.rs index aad5205..dbfe869 100644 --- a/steamguard/src/refresher.rs +++ b/steamguard/src/refresher.rs @@ -2,15 +2,21 @@ use crate::{ protobufs::steammessages_auth_steamclient::CAuthentication_AccessToken_GenerateForApp_Request, steamapi::{AuthenticationClient, EResult}, token::{Jwt, Tokens}, - transport::WebApiTransport, + transport::Transport, }; -pub struct TokenRefresher { - client: AuthenticationClient, +pub struct TokenRefresher +where + T: Transport, +{ + client: AuthenticationClient, } -impl TokenRefresher { - pub fn new(client: AuthenticationClient) -> Self { +impl TokenRefresher +where + T: Transport, +{ + pub fn new(client: AuthenticationClient) -> Self { Self { client } } diff --git a/steamguard/src/steamapi.rs b/steamguard/src/steamapi.rs index cea585f..154b6c9 100644 --- a/steamguard/src/steamapi.rs +++ b/steamguard/src/steamapi.rs @@ -21,7 +21,7 @@ lazy_static! { /// /// Endpoint: `/ITwoFactorService/QueryTime/v0001` pub fn get_server_time() -> anyhow::Result { - let mut client = TwoFactorClient::new(WebApiTransport::new()); + let mut client = TwoFactorClient::new(WebApiTransport::default()); let resp = client.query_time()?; if resp.result != EResult::OK { return Err(anyhow::anyhow!("QueryTime failed: {:?}", resp)); diff --git a/steamguard/src/transport/webapi.rs b/steamguard/src/transport/webapi.rs index 63d5446..ccd14aa 100644 --- a/steamguard/src/transport/webapi.rs +++ b/steamguard/src/transport/webapi.rs @@ -17,21 +17,24 @@ pub struct WebApiTransport { impl Default for WebApiTransport { fn default() -> Self { - Self::new() + Self::new(reqwest::blocking::Client::new()) } } impl WebApiTransport { - pub fn new() -> WebApiTransport { - Self { - client: reqwest::blocking::Client::new(), - // client: reqwest::blocking::Client::builder() - // .danger_accept_invalid_certs(true) - // .proxy(reqwest::Proxy::all("http://localhost:8080").unwrap()) - // .build() - // .unwrap(), - } + pub fn new(client: reqwest::blocking::Client) -> Self { + Self { client } } + + // pub fn new_with_proxy(proxy: &str) -> Self { + // Self { + // client: reqwest::blocking::Client::builder() + // // .danger_accept_invalid_certs(true) + // .proxy(reqwest::Proxy::all(proxy).unwrap()) + // .build() + // .unwrap(), + // } + // } } impl Transport for WebApiTransport { diff --git a/steamguard/src/userlogin.rs b/steamguard/src/userlogin.rs index 49c1997..85bd087 100644 --- a/steamguard/src/userlogin.rs +++ b/steamguard/src/userlogin.rs @@ -6,20 +6,17 @@ use crate::protobufs::steammessages_auth_steamclient::{ CAuthentication_PollAuthSessionStatus_Request, CAuthentication_PollAuthSessionStatus_Response, EAuthSessionGuardType, }; +use crate::protobufs::steammessages_auth_steamclient::{ + CAuthentication_BeginAuthSessionViaCredentials_Response, + CAuthentication_BeginAuthSessionViaQR_Request, CAuthentication_BeginAuthSessionViaQR_Response, + CAuthentication_GetPasswordRSAPublicKey_Response, + CAuthentication_UpdateAuthSessionWithSteamGuardCode_Request, + CAuthentication_UpdateAuthSessionWithSteamGuardCode_Response, EAuthTokenPlatformType, +}; use crate::steamapi::authentication::AuthenticationClient; use crate::steamapi::EResult; use crate::token::Tokens; -use crate::{ - protobufs::steammessages_auth_steamclient::{ - CAuthentication_BeginAuthSessionViaCredentials_Response, - CAuthentication_BeginAuthSessionViaQR_Request, - CAuthentication_BeginAuthSessionViaQR_Response, - CAuthentication_GetPasswordRSAPublicKey_Response, - CAuthentication_UpdateAuthSessionWithSteamGuardCode_Request, - CAuthentication_UpdateAuthSessionWithSteamGuardCode_Response, EAuthTokenPlatformType, - }, - transport::WebApiTransport, -}; +use crate::transport::Transport; use log::*; use rsa::{PublicKey, RsaPublicKey}; use std::time::Duration; @@ -82,17 +79,23 @@ impl BeginQrLoginResponse { /// Handles the user login flow. #[derive(Debug)] -pub struct UserLogin { - client: AuthenticationClient, +pub struct UserLogin +where + T: Transport, +{ + client: AuthenticationClient, device_details: DeviceDetails, started_auth: Option, } -impl UserLogin { - pub fn new(device_details: DeviceDetails) -> Self { +impl UserLogin +where + T: Transport, +{ + pub fn new(transport: T, device_details: DeviceDetails) -> Self { Self { - client: AuthenticationClient::new(WebApiTransport::new()), + client: AuthenticationClient::new(transport), device_details, started_auth: None, }