Browse Source

password continue

db-refactoring
Coin de Gamma 3 months ago
parent
commit
807c40ee18
  1. 1
      Cargo.toml
  2. 77
      src/main.rs

1
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"

77
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<PROMPT> {
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<String> {
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<PROMPT> {
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()?;
}
}
}

Loading…
Cancel
Save