Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

status: improve formatting of printed info, add --raw arg #407

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 72 additions & 36 deletions src/commands/status.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<T> AccountCommand<T> for StatusCommand
where
Expand All @@ -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!(
Expand All @@ -45,41 +48,74 @@ where
}
}

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...");
impl StatusCommand {
fn print_account_status<T>(
&self,
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 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());
}
}
}
Loading