1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.

// Parity Ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.

// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use cfxkey::Password;
use rpassword::read_password;
use std::{
    fs::File,
    io::{self, BufRead, BufReader, Write},
};

const PASSWORD_STDIN_ERROR: &str =
    "Unable to ask for password on non-interactive terminal.";

/// Flush output buffer.
pub fn flush_stdout() {
    io::stdout().flush().expect("stdout is flushable; qed");
}

/// Prompts user asking for password.
pub fn password_prompt() -> Result<Password, String> {
    println!("Please note that password is NOT RECOVERABLE.");
    print!("Type password: ");
    flush_stdout();

    let password = read_password()
        .map_err(|_| PASSWORD_STDIN_ERROR.to_owned())?
        .into();

    print!("Repeat password: ");
    flush_stdout();

    let password_repeat = read_password()
        .map_err(|_| PASSWORD_STDIN_ERROR.to_owned())?
        .into();

    if password != password_repeat {
        return Err("Passwords do not match!".into());
    }

    Ok(password)
}

pub fn input_password() -> Result<Password, String> {
    print!("Type password: ");
    flush_stdout();

    let password = read_password()
        .map_err(|_| PASSWORD_STDIN_ERROR.to_owned())?
        .into();

    Ok(password)
}

/// Read a password from password file.
pub fn password_from_file(path: String) -> Result<Password, String> {
    let passwords = passwords_from_files(&[path])?;
    // use only first password from the file
    passwords
        .get(0)
        .map(Password::clone)
        .ok_or_else(|| "Password file seems to be empty.".to_owned())
}

/// Reads passwords from files. Treats each line as a separate password.
pub fn passwords_from_files(files: &[String]) -> Result<Vec<Password>, String> {
    let passwords = files.iter().map(|filename| {
		let file = File::open(filename).map_err(|_| format!("{} Unable to read password file. Ensure it exists and permissions are correct.", filename))?;
		let reader = BufReader::new(&file);
		let lines = reader.lines()
			.filter_map(|l| l.ok())
			.map(|pwd| pwd.trim().to_owned().into())
			.collect::<Vec<Password>>();
		Ok(lines)
	}).collect::<Result<Vec<Vec<Password>>, String>>();
    Ok(passwords?.into_iter().flatten().collect())
}