From 073311f5d3819876b75887e030ed2536e74fb95f Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Fri, 2 Aug 2024 11:11:42 -0400 Subject: [PATCH] status: improve formatting of printed info, add `--raw` arg --- src/commands/status.rs | 108 +++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/src/commands/status.rs b/src/commands/status.rs index c9d1a386..8af0312b 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -1,7 +1,7 @@ use clap::Parser; use log::*; use steamguard::{ - protobufs::service_twofactor::CTwoFactor_Status_Request, + protobufs::service_twofactor::{CTwoFactor_Status_Request, CTwoFactor_Status_Response}, steamapi::TwoFactorClient, transport::{Transport, TransportError}, SteamGuardAccount, @@ -11,7 +11,10 @@ use super::AccountCommand; #[derive(Debug, Clone, Parser)] #[clap(about = "Query and print the 2FA status of an account.")] -pub struct StatusCommand; +pub struct StatusCommand { + #[clap(long, help = "Print raw protobuf response.")] + pub raw: bool, +} impl AccountCommand for StatusCommand where @@ -28,7 +31,7 @@ where for account in accounts { let mut account = account.lock().unwrap(); - match print_account_status(&mut account, &transport, args, &client) { + match self.print_account_status(&mut account, &transport, args, &client) { Ok(_) => {} Err(e) => { error!( @@ -45,41 +48,74 @@ where } } -fn print_account_status( - account: &mut SteamGuardAccount, - transport: &T, - args: &super::GlobalArgs, - client: &TwoFactorClient, -) -> 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..."); +impl StatusCommand { + fn print_account_status( + &self, + account: &mut SteamGuardAccount, + transport: &T, + args: &super::GlobalArgs, + client: &TwoFactorClient, + ) -> anyhow::Result<()> + where + T: Transport + Clone, + { + if account.tokens.is_none() { 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 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); + if self.raw { + println!("{:#?}", data); + return Ok(()); + } else { + self.pretty_print_status(data); } - }; - let data = resp.into_response_data(); + Ok(()) + } - println!("Account: {}", account.account_name); - println!("Status: {:#?}", data); - Ok(()) + fn pretty_print_status(&self, data: CTwoFactor_Status_Response) { + println!( + "Steamguard scheme: {}", + match data.steamguard_scheme() { + 0 => "None".into(), + 1 => "Email".into(), + 2 => "Mobile app".into(), + s => format!("Unknown ({})", s), + } + ); + println!("Email validated? {}", data.email_validated()); + println!("Is 2FA set up? {}", data.state() == 1); + if data.state() == 1 { + println!( + "Revocation attempts remaining: {}", + data.revocation_attempts_remaining() + ); + println!("Version: {}", data.version()); + println!("Time Created: {}", data.time_created()); + println!("Time Transferred: {}", data.time_transferred()); + println!("Device ID: {}", data.device_identifier()); + println!("Authenticator Type: {}", data.authenticator_type()); + } + } }