diff --git a/Cargo.toml b/Cargo.toml index b3d73f6..46ac04a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" base64 = "0.22.1" clap = { version = "4.5.16", features = ["derive"] } once_cell = "1.19.0" +rpassword = "7.3.1" tempfile = "3.12.0" diff --git a/src/main.rs b/src/main.rs index d3caf8b..8cbc89d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,10 @@ mod editor; use db::{DB, Item}; -use once_cell::sync::Lazy; - use clap::{Parser, Subcommand}; +use rpassword; + +use once_cell::sync::Lazy; use std::fs; use std::io::{self, Write}; @@ -47,6 +48,39 @@ enum Commands { } +enum PROMPT { + YES, + NO +} + +fn get_prompt(question: &str) -> io::Result { + print!("{} [Y/n] ", question); + io::stdout().flush()?; + let mut input = String::new(); + io::stdin().read_line(&mut input)?; + let input = input.trim().to_lowercase(); + match input.as_str() { + "y" => Ok(PROMPT::YES), + "yes" => Ok(PROMPT::YES), + "n" => Ok(PROMPT::NO), + "no" => Ok(PROMPT::NO), + _ => Ok(PROMPT::YES), + } + +} + +// TODO: change password functionality + +fn login() -> io::Result { + println!("Master password:"); + // TODO: check in db + // TODO: return error if db is not inited + let password = rpassword::read_password()?; + if password != "pass" { + return Err(io::Error::new(io::ErrorKind::InvalidData, "Wrong password")); + } + Ok(String::from(password)) +} fn is_inited() -> bool { let path = Path::new(STORAGE_FOLDER); @@ -54,12 +88,24 @@ fn is_inited() -> bool { } fn init() -> io::Result<()> { - // TODO: ask password two times if is_inited() { return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Reinitialization attempted")); } + println!("Enter master password"); + let password = rpassword::read_password()?; + println!("Enter master password again:"); + let password2 = rpassword::read_password()?; + + if password != password2 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords should be equal")); + } + fs::create_dir(STORAGE_FOLDER)?; println!("Storage folder created"); + //let mut db = DB::init(&*STORAGE_PATH, pass)?; + + // TODO: enter password + // TODO: dont create storate if master password hasn't entered fs::File::create(&*STORAGE_PATH)?; println!("Storage db created."); @@ -92,32 +138,11 @@ fn list() -> io:: Result<()> { let mut vec: Vec<_> = db.items.iter().collect(); vec.sort(); for item in &vec { - println!("{}", item); + println!("{}", item.id); } Ok(()) } -enum PROMPT { - YES, - NO -} - -fn get_prompt(question: &str) -> io::Result { - print!("{} [Y/n] ", question); - io::stdout().flush()?; - let mut input = String::new(); - io::stdin().read_line(&mut input)?; - let input = input.trim().to_lowercase(); - match input.as_str() { - "y" => Ok(PROMPT::YES), - "yes" => Ok(PROMPT::YES), - "n" => Ok(PROMPT::NO), - "no" => Ok(PROMPT::NO), - _ => Ok(PROMPT::YES), - } - -} - fn run_command() -> io::Result<()> { let cli = Cli::parse(); match &cli.command { @@ -142,7 +167,7 @@ fn run_command() -> io::Result<()> { }, } } else { - println!("login, not implemented yet") + login()?; } } }