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

add --password argument and STEAMGUARD_CLI_STEAM_PASSWORD environment variable #326

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ pub(crate) trait ManifestCommand<T>
where
T: Transport,
{
fn execute(&self, transport: T, manager: &mut AccountManager) -> anyhow::Result<()>;
fn execute(
&self,
transport: T,
manager: &mut AccountManager,
args: &GlobalArgs,
) -> anyhow::Result<()>;
}

/// A command that operates on individual accounts.
Expand All @@ -57,6 +62,7 @@ where
transport: T,
manager: &mut AccountManager,
accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
args: &GlobalArgs,
) -> anyhow::Result<()>;
}

Expand Down Expand Up @@ -92,6 +98,13 @@ pub(crate) struct GlobalArgs {
long_help = "Select the account you want by steam username. Case-sensitive. By default, the first account in the manifest is selected."
)]
pub username: Option<String>,
#[clap(
long,
conflicts_with = "all",
help = "Steam account password. You really shouldn't use this if you can avoid it.",
env = "STEAMGUARD_CLI_STEAM_PASSWORD"
)]
pub password: Option<SecretString>,
#[clap(
short,
long,
Expand Down
1 change: 1 addition & 0 deletions src/commands/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ where
transport: T,
_manager: &mut AccountManager,
accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
_args: &GlobalArgs,
) -> anyhow::Result<()> {
let server_time = if self.offline {
SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs()
Expand Down
7 changes: 6 additions & 1 deletion src/commands/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ impl<T> ManifestCommand<T> for DecryptCommand
where
T: Transport,
{
fn execute(&self, _transport: T, manager: &mut AccountManager) -> anyhow::Result<()> {
fn execute(
&self,
_transport: T,
manager: &mut AccountManager,
_args: &GlobalArgs,
) -> anyhow::Result<()> {
load_accounts_with_prompts(manager)?;

#[cfg(feature = "keyring")]
Expand Down
7 changes: 6 additions & 1 deletion src/commands/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ impl<T> ManifestCommand<T> for EncryptCommand
where
T: Transport,
{
fn execute(&self, _transport: T, manager: &mut AccountManager) -> anyhow::Result<()> {
fn execute(
&self,
_transport: T,
manager: &mut AccountManager,
_args: &GlobalArgs,
) -> anyhow::Result<()> {
if !manager.has_passkey() {
let passkey: Option<SecretString>;
loop {
Expand Down
7 changes: 6 additions & 1 deletion src/commands/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ impl<T> ManifestCommand<T> for ImportCommand
where
T: Transport,
{
fn execute(&self, _transport: T, manager: &mut AccountManager) -> anyhow::Result<()> {
fn execute(
&self,
_transport: T,
manager: &mut AccountManager,
_args: &GlobalArgs,
) -> anyhow::Result<()> {
for file_path in self.files.iter() {
debug!("loading entry: {:?}", file_path);
match manager.import_account(file_path) {
Expand Down
1 change: 1 addition & 0 deletions src/commands/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ where
_transport: T,
_manager: &mut AccountManager,
accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
_args: &GlobalArgs,
) -> anyhow::Result<()> {
use anyhow::Context;

Expand Down
5 changes: 3 additions & 2 deletions src/commands/qr_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ where
transport: T,
_manager: &mut AccountManager,
accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
args: &GlobalArgs,
) -> anyhow::Result<()> {
ensure!(
accounts.len() == 1,
Expand All @@ -37,7 +38,7 @@ where
info!("Approving login to {}", account.account_name);

if account.tokens.is_none() {
crate::do_login(transport.clone(), &mut account)?;
crate::do_login(transport.clone(), &mut account, args.password.clone())?;
}

loop {
Expand All @@ -57,7 +58,7 @@ where
}
Err(QrApproverError::Unauthorized) => {
warn!("tokens are invalid. Attempting to log in again.");
crate::do_login(transport.clone(), &mut account)?;
crate::do_login(transport.clone(), &mut account, args.password.clone())?;
}
Err(e) => {
error!("Failed to approve login: {}", e);
Expand Down
3 changes: 2 additions & 1 deletion src/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ where
transport: T,
manager: &mut AccountManager,
accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
args: &GlobalArgs,
) -> anyhow::Result<()> {
eprintln!(
"This will remove the mobile authenticator from {} accounts: {}",
Expand Down Expand Up @@ -54,7 +55,7 @@ where
}
Err(RemoveAuthenticatorError::TransportError(TransportError::Unauthorized)) => {
error!("Account {} is not logged in", account.account_name);
crate::do_login(transport.clone(), &mut account)?;
crate::do_login(transport.clone(), &mut account, args.password.clone())?;
continue;
}
Err(RemoveAuthenticatorError::IncorrectRevocationCode {
Expand Down
9 changes: 7 additions & 2 deletions src/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ impl<T> ManifestCommand<T> for SetupCommand
where
T: Transport + Clone,
{
fn execute(&self, transport: T, manager: &mut AccountManager) -> anyhow::Result<()> {
fn execute(
&self,
transport: T,
manager: &mut AccountManager,
args: &GlobalArgs,
) -> anyhow::Result<()> {
eprintln!("Log in to the account that you want to link to steamguard-cli");
eprint!("Username: ");
let username = tui::prompt().to_lowercase();
Expand All @@ -30,7 +35,7 @@ where
);
}
info!("Logging in to {}", username);
let tokens = crate::do_login_raw(transport.clone(), username)
let tokens = crate::do_login_raw(transport.clone(), username, args.password.clone())
.expect("Failed to log in. Account has not been linked.");

info!("Adding authenticator...");
Expand Down
5 changes: 3 additions & 2 deletions src/commands/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ where
transport: T,
manager: &mut AccountManager,
accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
args: &GlobalArgs,
) -> anyhow::Result<()> {
for a in accounts {
let mut account = a.lock().unwrap();

if !account.is_logged_in() {
info!("Account does not have tokens, logging in");
crate::do_login(transport.clone(), &mut account)?;
crate::do_login(transport.clone(), &mut account, args.password.clone())?;
}

info!("{}: Checking for trade confirmations", account.account_name);
Expand All @@ -55,7 +56,7 @@ where
}
Err(ConfirmerError::InvalidTokens) => {
info!("obtaining new tokens");
crate::do_login(transport.clone(), &mut account)?;
crate::do_login(transport.clone(), &mut account, args.password.clone())?;
}
Err(err) => {
error!("Failed to get trade confirmations: {}", err);
Expand Down
14 changes: 12 additions & 2 deletions src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::tui;
pub fn do_login<T: Transport + Clone>(
transport: T,
account: &mut SteamGuardAccount,
password: Option<SecretString>,
) -> anyhow::Result<()> {
if let Some(tokens) = account.tokens.as_mut() {
info!("Refreshing access token...");
Expand Down Expand Up @@ -44,7 +45,11 @@ pub fn do_login<T: Transport + Clone>(
account.account_name = tui::prompt();
}
let _ = std::io::stdout().flush();
let password = tui::prompt_password()?;
let password = if let Some(p) = password {
p
} else {
tui::prompt_password()?
};
if !password.expose_secret().is_empty() {
debug!("password is present");
} else {
Expand All @@ -65,9 +70,14 @@ pub fn do_login<T: Transport + Clone>(
pub fn do_login_raw<T: Transport + Clone>(
transport: T,
username: String,
password: Option<SecretString>,
) -> anyhow::Result<Tokens> {
let _ = std::io::stdout().flush();
let password = tui::prompt_password()?;
let password = if let Some(p) = password {
p
} else {
tui::prompt_password()?
};
if !password.expose_secret().is_empty() {
debug!("password is present");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn run(args: commands::Args) -> anyhow::Result<()> {
let transport = WebApiTransport::new(http_client);

if let CommandType::Manifest(cmd) = cmd {
cmd.execute(transport, &mut manager)?;
cmd.execute(transport, &mut manager, &globalargs)?;
return Ok(());
}

Expand Down Expand Up @@ -269,7 +269,7 @@ fn run(args: commands::Args) -> anyhow::Result<()> {
);

if let CommandType::Account(cmd) = cmd {
return cmd.execute(transport, &mut manager, selected_accounts);
return cmd.execute(transport, &mut manager, selected_accounts, &globalargs);
}

Ok(())
Expand Down