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
|
use std::borrow::Cow::{self, Borrowed, Owned};
use rustyline::config::Configurer;
use rustyline::highlight::{CmdKind, Highlighter};
use rustyline::{ColorMode, Editor, Result};
use rustyline::{Completer, Helper, Hinter, Validator};
#[derive(Completer, Helper, Hinter, Validator)]
struct MaskingHighlighter {
masking: bool,
}
impl Highlighter for MaskingHighlighter {
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
use unicode_width::UnicodeWidthStr;
if self.masking {
Owned(" ".repeat(line.width()))
} else {
Borrowed(line)
}
}
fn highlight_char(&self, _line: &str, _pos: usize, kind: CmdKind) -> bool {
match kind {
CmdKind::MoveCursor => false,
_ => self.masking,
}
}
}
fn main() -> Result<()> {
env_logger::init();
println!("This is just a hack. Reading passwords securely requires more than that.");
let h = MaskingHighlighter { masking: false };
let mut rl = Editor::new()?;
rl.set_helper(Some(h));
let username = rl.readline("Username:")?;
println!("Username: {username}");
rl.helper_mut().expect("No helper").masking = true;
rl.set_color_mode(ColorMode::Forced); // force masking
rl.set_auto_add_history(false); // make sure password is not added to history
let mut guard = rl.set_cursor_visibility(false)?;
let passwd = rl.readline("Password:")?;
guard.take();
println!("Secret: {passwd}");
Ok(())
}
|