Skip to content

Commit 749817c

Browse files
author
replydev
committed
Skip useless prompts
1 parent 665eeb3 commit 749817c

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

src/argument_functions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::database_loader;
1+
use crate::{cryptograpy, database_loader};
22
use crate::otp::{otp_element::OTPElement,otp_helper};
33
use crate::importers;
44
use crate::cryptograpy::prompt_for_passwords;
@@ -44,7 +44,7 @@ pub fn import(args: Vec<String>){
4444
}
4545
}
4646

47-
match database_loader::overwrite_database(elements){
47+
match database_loader::overwrite_database(elements,&cryptograpy::prompt_for_passwords("Choose a password: ", 8,true)){
4848
Ok(()) => {
4949
println!("Successfully imported database");
5050
},
@@ -75,7 +75,7 @@ pub fn add(args: Vec<String>){
7575
return;
7676
}
7777

78-
match database_loader::add_element(&prompt_for_passwords("Insert the secret: ",0),&args[2],&args[3],&args[4],digits){
78+
match database_loader::add_element(&prompt_for_passwords("Insert the secret: ",0,false),&args[2],&args[3],&args[4],digits){
7979
Ok(()) => println!("Success"),
8080
Err(e) => eprintln!("An error occurred: {}",e)
8181
}
@@ -102,7 +102,7 @@ pub fn remove(args: Vec<String>){
102102
pub fn edit(args: Vec<String>){
103103
if args.len() == 7{
104104
let id = args[2].parse::<usize>().unwrap();
105-
let secret = &prompt_for_passwords("Inser the secret (type ENTER to skip modification): ",0);
105+
let secret = &prompt_for_passwords("Inser the secret (type ENTER to skip modification): ",0,false);
106106
let issuer = &args[3];
107107
let label = &args[4];
108108
let algorithm = &args[5];

src/cryptograpy.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::convert::TryInto;
33
use sodiumoxide::crypto::pwhash;
44
use sodiumoxide::crypto::secretstream::{Stream, Tag, KEYBYTES};
55
use sodiumoxide::crypto::secretstream::xchacha20poly1305::{Header, Key};
6+
use crate::utils::clear_lines;
67

78
const SIGNATURE: [u8;4] = [0xC1, 0x0A, 0x4B, 0xED];
89

@@ -92,13 +93,26 @@ fn header_vec_to_header_array(byte_vec: Vec<u8>) -> [u8;24]{
9293
.unwrap_or_else(|v: Vec<u8>| panic!("Expected a Vec of length {} but it was {}", 24, v.len()))
9394
}
9495

95-
pub fn prompt_for_passwords(message: &str,minimum_password_length: usize) -> String{
96+
pub fn prompt_for_passwords(message: &str,minimum_password_length: usize,verify: bool) -> String{
9697
let mut password;
98+
let mut verify_password;
9799
loop{
98100
password = rpassword::prompt_password_stdout(message).unwrap();
99-
if password.len() >= minimum_password_length {
101+
if verify {
102+
verify_password = rpassword::prompt_password_stdout("Retype the same password: ").unwrap();
103+
if password != verify_password{
104+
clear_lines(2, true);
105+
println!("Passwords do not match");
106+
continue;
107+
}
108+
if password.len() >= minimum_password_length{
109+
break;
110+
}
111+
}
112+
else if password.len() >= minimum_password_length {
100113
break;
101114
}
115+
clear_lines(1, true);
102116
println!("Please insert a password with at least {} digits.",minimum_password_length);
103117
}
104118
password

src/database_loader.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use crate::cryptograpy;
77
use crate::otp::otp_element::OTPElement;
88

99

10-
pub fn read_from_file() -> Result<Vec<OTPElement>,String>{
10+
pub fn read_from_file(password: &str) -> Result<Vec<OTPElement>,String>{
1111
let encrypted_contents = read_to_string(&get_db_path()).unwrap();
1212
//rust close files at the end of the function
13-
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: ",8));
13+
let contents = cryptograpy::decrypt_string(&encrypted_contents, password);
1414
match contents {
1515
Ok(contents) => {
1616
let vector: Vec<OTPElement> = serde_json::from_str(&contents).unwrap();
@@ -36,14 +36,15 @@ pub fn add_element(secret: &String,issuer: &String,label: &String,algorithm: &st
3636
if !check_secret(&upper_secret){
3737
return Err(String::from("Bad secret"))
3838
}
39+
let pw = &cryptograpy::prompt_for_passwords("Password: ",8,false);
3940
let otp_element = OTPElement::new(upper_secret.to_string(), issuer.to_string(), label.to_string(),digits, String::from("TOTP"), String::from(algorithm).to_uppercase(),String::from("Default"),0,0,30,vec![]);
4041
let mut elements;
41-
match read_from_file(){
42+
match read_from_file(pw){
4243
Ok(result) => elements = result,
4344
Err(e) => return Err(e)
4445
}
4546
elements.push(otp_element);
46-
match overwrite_database(elements){
47+
match overwrite_database(elements,pw){
4748
Ok(()) => Ok(()),
4849
Err(e) => Err(format!("{}",e))
4950
}
@@ -57,8 +58,8 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
5758
id -= 1;
5859

5960
let mut elements: Vec<OTPElement>;
60-
61-
match read_from_file(){
61+
let pw = &cryptograpy::prompt_for_passwords("Password: ",8,false);
62+
match read_from_file(pw){
6263
Ok(result) => elements = result,
6364
Err(e) => {
6465
return Err(e);
@@ -73,7 +74,7 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
7374
break;
7475
}
7576
}
76-
match overwrite_database(elements){
77+
match overwrite_database(elements,pw){
7778
Ok(()) => Ok(()),
7879
Err(e) => Err(format!("{}",e)),
7980
}
@@ -89,7 +90,8 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str,algorit
8990
id -= 1;
9091

9192
let mut elements: Vec<OTPElement>;
92-
match read_from_file(){
93+
let pw = &cryptograpy::prompt_for_passwords("Password: ",8,false);
94+
match read_from_file(pw){
9395
Ok(result) => elements = result,
9496
Err(_e) => return Err(String::from("Cannot decrypt existing database"))
9597
}
@@ -116,7 +118,7 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str,algorit
116118
break;
117119
}
118120
}
119-
match overwrite_database(elements){
121+
match overwrite_database(elements,pw){
120122
Ok(()) => Ok(()),
121123
Err(e) => Err(format!("{}",e)),
122124
}
@@ -130,7 +132,7 @@ pub fn export_database() -> Result<String, String> {
130132
exported_path.push_str("/exported.cotp");
131133
let mut file = File::create(&exported_path).expect("Cannot create file");
132134
let encrypted_contents = read_to_string(&get_db_path()).unwrap();
133-
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: ",8));
135+
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: ",8,false));
134136
match contents {
135137
Ok(contents) => {
136138
if contents == "[]"{
@@ -145,13 +147,13 @@ pub fn export_database() -> Result<String, String> {
145147
}
146148
}
147149

148-
pub fn overwrite_database(elements: Vec<OTPElement>) -> Result<(),std::io::Error>{
150+
pub fn overwrite_database(elements: Vec<OTPElement>,password: &str) -> Result<(),std::io::Error>{
149151
let json_string: &str = &serde_json::to_string(&elements)?;
150-
overwrite_database_json(json_string)
152+
overwrite_database_json(json_string,password)
151153
}
152154

153-
pub fn overwrite_database_json(json: &str) -> Result<(),std::io::Error>{
154-
let encrypted = cryptograpy::encrypt_string(json.to_string(), &cryptograpy::prompt_for_passwords("Insert password for database encryption: ",8));
155+
pub fn overwrite_database_json(json: &str,password: &str) -> Result<(),std::io::Error>{
156+
let encrypted = cryptograpy::encrypt_string(json.to_string(), password);
155157
let mut file = File::create(utils::get_db_path())?;
156158
utils::write_to_file(&encrypted, &mut file)
157159
}

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn init_ctrlc_handler(lines: usize){
4242
}).expect("Failed to initialize ctrl-c handler");
4343
}
4444

45-
fn init() -> Result<(), String>{
45+
fn init() -> Result<bool, String>{
4646
match sodiumoxide::init(){
4747
Err(()) => {
4848
return Err(String::from("Error during sodiumoxide initialization"))
@@ -52,12 +52,13 @@ fn init() -> Result<(), String>{
5252
match utils::create_db_if_needed() {
5353
Ok(value) => {
5454
if value {
55-
match database_loader::overwrite_database_json("[]"){
56-
Ok(()) => return Ok(()),
55+
let pw = &cryptograpy::prompt_for_passwords("Choose a password: ", 8,true);
56+
match database_loader::overwrite_database_json("[]",pw){
57+
Ok(()) => return Ok(true),
5758
Err(_e) => return Err(String::from("An error occurred during database overwriting")),
5859
}
5960
}
60-
Ok(())
61+
Ok(false)
6162
},
6263
Err(()) => {
6364
return Err(String::from("An error occurred during database creation"));
@@ -69,7 +70,11 @@ fn main() {
6970
print_title();
7071
let init_result = init();
7172
match init_result {
72-
Ok(()) => {},
73+
Ok(true) => {
74+
println!("Database correctly initialized");
75+
return;
76+
},
77+
Ok(false) => {},
7378
Err(e) => {
7479
println!("{}",e);
7580
return;

src/otp/otp_helper.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::database_loader;
1+
use crate::{cryptograpy, database_loader};
22
use serde_json;
33
use serde::{Deserialize, Serialize};
44
use crate::otp::otp_element::OTPElement;
@@ -26,7 +26,7 @@ impl JsonResult {
2626
}
2727

2828
pub fn read_codes() -> Result<Vec<OTPElement>,String>{
29-
match database_loader::read_from_file(){
29+
match database_loader::read_from_file(&cryptograpy::prompt_for_passwords("Password: ", 8,false)){
3030
Ok(result) => Ok(result),
3131
Err(e) => Err(e),
3232
}
@@ -61,7 +61,7 @@ fn get_good_otp_code(element: &OTPElement) -> String {
6161
pub fn get_json_results() -> Result<String,String>{
6262
let elements: Vec<OTPElement>;
6363

64-
match database_loader::read_from_file(){
64+
match database_loader::read_from_file(&cryptograpy::prompt_for_passwords("Password: ",8,false)){
6565
Ok(result) => elements = result,
6666
Err(e) => return Err(e)
6767
}
@@ -89,7 +89,7 @@ pub fn print_json_result(mut index: usize) -> Result<(),String>{
8989

9090
let elements: Vec<OTPElement>;
9191

92-
match database_loader::read_from_file(){
92+
match database_loader::read_from_file(&cryptograpy::prompt_for_passwords("Password: ",8,false)){
9393
Ok(result) => elements = result,
9494
Err(e) => return Err(e),
9595
}

0 commit comments

Comments
 (0)