Skip to content

Commit

Permalink
add status subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jul 21, 2024
1 parent ee76d3a commit fdeafff
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::{Arc, Mutex};
use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use secrecy::SecretString;
use status::StatusCommand;
use std::str::FromStr;
use steamguard::{transport::Transport, SteamGuardAccount};

Expand All @@ -20,6 +21,7 @@ pub mod qr;
pub mod qr_login;
pub mod remove;
pub mod setup;
pub mod status;

pub use code::CodeCommand;
pub use completions::CompletionsCommand;
Expand Down Expand Up @@ -175,6 +177,7 @@ pub(crate) enum Subcommands {
#[cfg(feature = "qr")]
Qr(QrCommand),
QrLogin(QrLoginCommand),
Status(StatusCommand),
}

#[derive(Debug, Clone, Copy, ValueEnum)]
Expand Down
85 changes: 85 additions & 0 deletions src/commands/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use clap::Parser;
use log::*;
use steamguard::{
protobufs::service_twofactor::CTwoFactor_Status_Request,
steamapi::TwoFactorClient,
transport::{Transport, TransportError},
SteamGuardAccount,
};

use super::AccountCommand;

#[derive(Debug, Clone, Parser)]
#[clap(about = "Query and print the 2FA status of an account.")]
pub struct StatusCommand;

impl<T> AccountCommand<T> for StatusCommand
where
T: Transport + Clone,
{
fn execute(
&self,
transport: T,
manager: &mut crate::AccountManager,
accounts: Vec<std::sync::Arc<std::sync::Mutex<steamguard::SteamGuardAccount>>>,
args: &super::GlobalArgs,
) -> anyhow::Result<()> {
let client = TwoFactorClient::new(transport.clone());

for account in accounts {
let mut account = account.lock().unwrap();
match print_account_status(&mut account, &transport, args, &client) {
Ok(_) => {}
Err(e) => {
error!(
"Failed to print status for account {}: {}",
account.account_name, e
);
}
}
}

manager.save()?;

Ok(())
}
}

fn print_account_status<T>(
account: &mut SteamGuardAccount,
transport: &T,
args: &super::GlobalArgs,
client: &TwoFactorClient<T>,
) -> anyhow::Result<()>
where
T: Transport + Clone,
{
if account.tokens.is_none() {
crate::do_login(transport.clone(), account, args.password.clone())?;
}
let Some(tokens) = account.tokens.as_ref() else {
bail!(
"No tokens found for {}. Can't query status if we aren't logged in ourselves.",
account.account_name
);
};
let mut req = CTwoFactor_Status_Request::new();
req.set_steamid(account.steam_id);
let resp = match client.query_status(req.clone(), tokens.access_token()) {
Ok(resp) => resp,
Err(TransportError::Unauthorized) => {
info!("Access token expired, re-logging in...");
crate::do_login(transport.clone(), account, args.password.clone())?;
let tokens = account.tokens.as_ref().unwrap();
client.query_status(req, tokens.access_token())?
}
Err(e) => {
return Err(e.into());
}
};
let data = resp.into_response_data();

println!("Account: {}", account.account_name);
println!("Status: {:#?}", data);
Ok(())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn run(args: commands::Args) -> anyhow::Result<()> {
#[cfg(feature = "qr")]
Subcommands::Qr(args) => CommandType::Account(Box::new(args)),
Subcommands::QrLogin(args) => CommandType::Account(Box::new(args)),
Subcommands::Status(args) => CommandType::Account(Box::new(args)),
};

if let CommandType::Const(cmd) = cmd {
Expand Down
4 changes: 2 additions & 2 deletions steamguard/src/steamapi/twofactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ where
&self,
req: CTwoFactor_Status_Request,
access_token: &Jwt,
) -> anyhow::Result<ApiResponse<CTwoFactor_Status_Response>> {
) -> Result<ApiResponse<CTwoFactor_Status_Response>, TransportError> {
let req =
ApiRequest::new(SERVICE_NAME, "QueryStatus", 1, req).with_access_token(access_token);
let resp = self
Expand All @@ -121,7 +121,7 @@ where
Ok(resp)
}

pub fn query_time(&self) -> anyhow::Result<ApiResponse<CTwoFactor_Time_Response>> {
pub fn query_time(&self) -> Result<ApiResponse<CTwoFactor_Time_Response>, TransportError> {
let req = ApiRequest::new(SERVICE_NAME, "QueryTime", 1, CTwoFactor_Time_Request::new());
let resp = self
.transport
Expand Down

0 comments on commit fdeafff

Please sign in to comment.