Change password struct to use name as String instead of Rc<String>, fix gen command.
This commit is contained in:
+23
-13
@@ -1,13 +1,12 @@
|
||||
use rand::{thread_rng, Rng};
|
||||
use regex::Regex;
|
||||
use rpassword::prompt_password;
|
||||
use sha1::{Digest, Sha1};
|
||||
use std::cmp::min;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::io::{BufWriter, Write};
|
||||
use std::rc::Rc;
|
||||
use std::cmp::min;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
use crate::parser::command_parser;
|
||||
use crate::password::fix_password_recursion;
|
||||
@@ -18,9 +17,9 @@ use crate::utils::{call_cmd_with_input, get_cmd_args_from_command, get_copy_comm
|
||||
|
||||
impl<'a> LKEval<'a> {
|
||||
pub fn get_password(&self, name: &String) -> Option<PasswordRef> {
|
||||
match self.state.borrow().db.get(name) {
|
||||
match self.state.borrow().ls.get(name) {
|
||||
Some(pwd) => Some(pwd.clone()),
|
||||
None => match self.state.borrow().ls.get(name) {
|
||||
None => match self.state.borrow().db.get(name) {
|
||||
Some(pwd) => Some(pwd.clone()),
|
||||
None => None,
|
||||
},
|
||||
@@ -129,7 +128,7 @@ impl<'a> LKEval<'a> {
|
||||
match self.get_password(name) {
|
||||
Some(p) => {
|
||||
let pwd = (self.read_password)(format!("Password for {}: ", p.borrow().name)).unwrap();
|
||||
self.cmd_correct(&out, &p.borrow().name.as_ref(), true, Some(pwd.clone()));
|
||||
self.cmd_correct(&out, &p.borrow().name, true, Some(pwd.clone()));
|
||||
self.state.borrow_mut().secrets.insert(p.borrow().name.to_string(), pwd);
|
||||
}
|
||||
None => {
|
||||
@@ -428,14 +427,14 @@ impl<'a> LKEval<'a> {
|
||||
let name = pwd.name.trim_end_matches('G');
|
||||
for num in 1..10_u32.pow(gen.len().try_into().unwrap()) {
|
||||
let npwd = Password::from_password(&pwd);
|
||||
npwd.borrow_mut().name = Rc::new(format!("{}{}", name, num).to_string());
|
||||
npwd.borrow_mut().name = format!("{}{}", name, num).to_string();
|
||||
genpwds.push(npwd);
|
||||
}
|
||||
} else {
|
||||
let name = pwd.name.trim_end_matches('X');
|
||||
let num = rng.gen_range(1..10_u32.pow(gen.len().try_into().unwrap()));
|
||||
let npwd = Password::from_password(&pwd);
|
||||
npwd.borrow_mut().name = Rc::new(format!("{}{}", name, num).to_string());
|
||||
npwd.borrow_mut().name = format!("{}{}", name, num).to_string();
|
||||
genpwds.push(npwd);
|
||||
}
|
||||
}
|
||||
@@ -455,21 +454,32 @@ impl<'a> LKEval<'a> {
|
||||
lspwds.push((pwd, key));
|
||||
}
|
||||
self.state.borrow().fix_hierarchy();
|
||||
let mut err = match &out.err { Some(e) => Some(e.clone()), None => None };
|
||||
let mut err = match &out.err {
|
||||
Some(e) => Some(e.clone()),
|
||||
None => None,
|
||||
};
|
||||
let mut encpwds: Vec<(PasswordRef, String)> = Vec::new();
|
||||
for (pwd, key) in lspwds {
|
||||
let pass = match self.cmd_enc(&LKOut::from_lkout(None, err), &key) {
|
||||
Some((_, pass)) => pass,
|
||||
None => { out.e(format!("error: failed to encrypt password")); return; },
|
||||
Some((name, pass)) => {
|
||||
if name != pwd.borrow().name {
|
||||
panic!("INTERNAL_ERROR: wrong name found: {} != {}", name, pwd.borrow().name);
|
||||
};
|
||||
pass
|
||||
}
|
||||
None => {
|
||||
out.e(format!("error: failed to encrypt password"));
|
||||
return;
|
||||
}
|
||||
};
|
||||
err = None;
|
||||
encpwds.push((pwd.clone(), pass));
|
||||
}
|
||||
encpwds.sort_by(|a,b| b.1.len().cmp(&a.1.len()));
|
||||
encpwds.sort_by(|a, b| b.1.len().cmp(&a.1.len()));
|
||||
self.state.borrow_mut().ls.clear();
|
||||
let mut counter = 1;
|
||||
out.o(format!("{:>3} {:>36} {:>4} {}", "", "Password", "Len", "Name"));
|
||||
for num in (encpwds.len()-min(genpwds.len(), num))..encpwds.len() {
|
||||
for num in (encpwds.len() - min(genpwds.len(), num))..encpwds.len() {
|
||||
let (pwd, pass) = (encpwds[num].0.clone(), encpwds[num].1.to_string());
|
||||
let key = Radix::new(counter, 36).unwrap().to_string();
|
||||
counter += 1;
|
||||
|
||||
+23
-23
@@ -122,7 +122,7 @@ add t3 C 99 2022-12-14"###
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t1".to_string()),
|
||||
name: "t1".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -132,7 +132,7 @@ add t3 C 99 2022-12-14"###
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t2".to_string()),
|
||||
name: "t2".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -142,7 +142,7 @@ add t3 C 99 2022-12-14"###
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t3".to_string()),
|
||||
name: "t3".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -162,7 +162,7 @@ add t3 C 99 2022-12-14
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t1".to_string()),
|
||||
name: "t1".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -172,7 +172,7 @@ add t3 C 99 2022-12-14
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t2".to_string()),
|
||||
name: "t2".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -182,7 +182,7 @@ add t3 C 99 2022-12-14
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t3".to_string()),
|
||||
name: "t3".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -204,7 +204,7 @@ add t3 C 99 2022-12-14
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t1".to_string()),
|
||||
name: "t1".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -214,7 +214,7 @@ add t3 C 99 2022-12-14
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t2".to_string()),
|
||||
name: "t2".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -224,7 +224,7 @@ add t3 C 99 2022-12-14
|
||||
Command::Add(Rc::new(RefCell::new(Password {
|
||||
parent: None,
|
||||
prefix: None,
|
||||
name: Rc::new("t3".to_string()),
|
||||
name: "t3".to_string(),
|
||||
length: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
seq: 99,
|
||||
@@ -242,7 +242,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 R 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::Regular,
|
||||
@@ -255,7 +255,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 U 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::RegularUpcase,
|
||||
@@ -268,7 +268,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 U 2020-12-09"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::RegularUpcase,
|
||||
@@ -281,7 +281,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 R 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Regular,
|
||||
@@ -294,7 +294,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 N 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::NoSpace,
|
||||
@@ -307,7 +307,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 UN 99 2020-12-09 xx.ableton@domain.info https://www.ableton.com"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::NoSpaceUpcase,
|
||||
@@ -320,7 +320,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20R 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Regular,
|
||||
@@ -333,7 +333,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20UR 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::RegularUpcase,
|
||||
@@ -346,7 +346,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20UH 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::HexUpcase,
|
||||
@@ -359,7 +359,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20UB 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Base64Upcase,
|
||||
@@ -372,7 +372,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("#W9 ableton89 20D 99 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: Some("#W9".to_string()),
|
||||
mode: Mode::Decimal,
|
||||
@@ -385,7 +385,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 20D 98 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::Decimal,
|
||||
@@ -398,7 +398,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 20C 98 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::NoSpaceCamel,
|
||||
@@ -411,7 +411,7 @@ add t3 C 99 2022-12-14
|
||||
assert_eq!(
|
||||
command_parser::name("ableton89 20D 2020-12-09 a b c"),
|
||||
Ok(Password {
|
||||
name: Rc::new("ableton89".to_string()),
|
||||
name: "ableton89".to_string(),
|
||||
parent: None,
|
||||
prefix: None,
|
||||
mode: Mode::Decimal,
|
||||
|
||||
+2
-3
@@ -4,7 +4,6 @@ use chrono::naive::NaiveDate;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
pub type Name = String;
|
||||
pub type NameRef = Rc<Name>;
|
||||
pub type Prefix = Option<String>;
|
||||
pub type Comment = Option<String>;
|
||||
pub type PasswordRef = Rc<RefCell<Password>>;
|
||||
@@ -17,7 +16,7 @@ pub type Date = NaiveDate;
|
||||
pub struct Password {
|
||||
pub parent: Parent,
|
||||
pub prefix: Prefix,
|
||||
pub name: NameRef,
|
||||
pub name: Name,
|
||||
pub length: Length,
|
||||
pub mode: Mode,
|
||||
pub seq: Seq,
|
||||
@@ -37,7 +36,7 @@ impl Password {
|
||||
) -> Password {
|
||||
Password {
|
||||
prefix,
|
||||
name: Rc::new(name),
|
||||
name: name,
|
||||
length,
|
||||
mode,
|
||||
date,
|
||||
|
||||
+3
-3
@@ -101,7 +101,7 @@ impl<'a> LKEval<'a> {
|
||||
Command::Comment(name, comment) => self.cmd_comment(&out, &name, &comment),
|
||||
Command::Rm(name) => match self.get_password(name) {
|
||||
Some(pwd) => {
|
||||
self.state.borrow_mut().db.remove(pwd.borrow().name.as_ref());
|
||||
self.state.borrow_mut().db.remove(&pwd.borrow().name);
|
||||
out.o(format!("removed {}", pwd.borrow().name));
|
||||
}
|
||||
None => out.e(format!("error: password {} not found", name)),
|
||||
@@ -174,7 +174,7 @@ mod tests {
|
||||
LKPrint::new(LKOut::from_vecs(vec![], vec![]), false, lk.clone())
|
||||
);
|
||||
let pwd1 = Rc::new(RefCell::new(Password {
|
||||
name: Rc::new("t1".to_string()),
|
||||
name: "t1".to_string(),
|
||||
prefix: None,
|
||||
length: None,
|
||||
mode: Mode::Regular,
|
||||
@@ -201,7 +201,7 @@ mod tests {
|
||||
LKPrint::new(LKOut::from_vecs(vec![], vec!["Bye!".to_string()]), true, lk.clone())
|
||||
);
|
||||
let pwd2 = Rc::new(RefCell::new(Password {
|
||||
name: Rc::new("t2".to_string()),
|
||||
name: "t2".to_string(),
|
||||
prefix: None,
|
||||
length: None,
|
||||
mode: Mode::Regular,
|
||||
|
||||
Reference in New Issue
Block a user