Ask password from prompt

This commit is contained in:
ChristianVisintin
2020-11-22 20:36:53 +01:00
parent d692cdf28d
commit 589adf294f
3 changed files with 30 additions and 3 deletions

11
Cargo.lock generated
View File

@@ -352,6 +352,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "rpassword"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d755237fc0f99d98641540e66abac8bc46a0652f19148ac9e21de2da06b326c9"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
@@ -428,6 +438,7 @@ version = "0.1.0"
dependencies = [
"crossterm 0.18.2",
"getopts",
"rpassword",
"ssh2",
"tempfile",
"tui",

View File

@@ -21,6 +21,7 @@ ssh2 = "0.8.2"
tui = { version = "0.12.0", features = ["crossterm"], default-features = false }
users = "0.11.0"
whoami = "0.9.0"
rpassword = "5.0.0"
[dev-dependencies]
tempfile = "3"

View File

@@ -24,6 +24,7 @@ const TERMSCP_AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");
// Crates
extern crate getopts;
extern crate rpassword;
// External libs
use getopts::Options;
@@ -41,7 +42,7 @@ mod utils;
// namespaces
use activity_manager::{ActivityManager, NextActivity};
use filetransfer::{FileTransfer, sftp_transfer::SftpFileTransfer};
use filetransfer::{sftp_transfer::SftpFileTransfer, FileTransfer};
use ui::activities::auth_activity::ScpProtocol;
/// ### print_usage
@@ -132,7 +133,7 @@ fn main() {
port = portn;
protocol = proto;
username = user;
},
}
Err(err) => {
eprintln!("Bad address option: {}", err);
print_usage(opts);
@@ -152,7 +153,7 @@ fn main() {
// Get working directory
let wrkdir: PathBuf = match env::current_dir() {
Ok(dir) => dir,
Err(_) => PathBuf::from("/")
Err(_) => PathBuf::from("/"),
};
// Create activity manager
let mut manager: ActivityManager = match ActivityManager::new(file_transfer, &wrkdir) {
@@ -165,6 +166,20 @@ fn main() {
// Initialize client if necessary
let mut start_activity: NextActivity = NextActivity::Authentication;
if let Some(address) = address {
// Ask password
password = match rpassword::read_password_from_tty(Some("Password: ")) {
Ok(p) => {
if p.len() > 0 {
Some(p)
} else {
None
}
}
Err(_) => {
eprintln!("Could not read password from prompt");
std::process::exit(255);
}
};
// In this case the first activity will be FileTransfer
start_activity = NextActivity::FileTransfer;
manager.set_filetransfer_params(address, port, protocol, username, password);