From c1925894f7823fdae50fb83559ee79fe7abbf1c1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 13 Jan 2023 20:55:00 +0100 Subject: [PATCH] hide password --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 10 +++++----- src/utils.rs | 13 ++++++++++--- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86a8891..b120538 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1138,6 +1138,27 @@ dependencies = [ "winreg", ] +[[package]] +name = "rpassword" +version = "7.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6678cf63ab3491898c0d021b493c94c9b221d91295294a2a5746eacbe5928322" +dependencies = [ + "libc", + "rtoolbox", + "winapi", +] + +[[package]] +name = "rtoolbox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "034e22c514f5c0cb8a10ff341b9b048b5ceb21591f31c8f44c43b960f9b3524a" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "rustc_version" version = "0.4.0" @@ -1638,6 +1659,7 @@ dependencies = [ "clap", "directories", "reqwest", + "rpassword", "scraper", "serde", "tokio", diff --git a/Cargo.toml b/Cargo.toml index 58a5258..efe8162 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ reqwest = { version = "0.11", features = ["cookies"] } tokio = { version = ">=1.23.1", features = ["full"] } scraper = "0.14" clap = { version = "4.0.32", features = ["derive"] } +rpassword = "7.2" diff --git a/src/main.rs b/src/main.rs index 0705b54..246dda6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,9 +45,9 @@ async fn main() { username: if args.username.is_some() { args.username.unwrap() } else { - utils::ask("Username: ") + utils::ask_username() }, - password: Some(utils::ask("Password: ")), + password: Some(utils::ask_password()), } } }, @@ -57,14 +57,14 @@ async fn main() { args.username.unwrap() } else { // Only ask if nothing has been given in args - utils::ask("Username: ") + utils::ask_username() }, - password: Some(utils::ask("Password: ")), + password: Some(utils::ask_password()), }, }; if config.password.is_none() { - config.password = Some(utils::ask("Password: ")); + config.password = Some(utils::ask_password()); } let user_agent = format!("uwm/{}", env!("CARGO_PKG_VERSION")); diff --git a/src/utils.rs b/src/utils.rs index 3d6a656..a6ed460 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,17 +1,24 @@ use std::io::{self, Write}; -/// Ask user for something -pub fn ask(text: &str) -> std::string::String { +/// Ask user for username +pub fn ask_username() -> std::string::String { let mut user_input = String::new(); let stdin = io::stdin(); - print!("{}", text); + print!("Username: "); io::stdout().flush().unwrap(); stdin.read_line(&mut user_input).unwrap(); user_input.trim_end().to_string() } +/// Ask user for password +pub fn ask_password() -> std::string::String { + print!("Password: "); + io::stdout().flush().unwrap(); + rpassword::read_password().unwrap() +} + /// Write a document to a file /// /// `html_data` may be created with something like that: