Skip to content

Commit 5345936

Browse files
author
replydev
committed
Now users can change their databases password.
1 parent 749817c commit 5345936

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/argument_functions.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn help(){
1616
println!(" -j,--json | Print results in json format");
1717
println!(" -s,--single | Print OTP codes in single mode");
1818
println!(" -in,--info [ID] | Print info of choosen OTP code");
19+
println!(" -chpw,--change-password | Change the database password");
1920
println!(" -h,--help | Print this help");
2021
}
2122

@@ -177,6 +178,28 @@ pub fn info(args: Vec<String>){
177178
}
178179
}
179180
else{
180-
println!("Invalid arguments, type cotp --info");
181+
println!("Invalid arguments, type cotp --info [ID]");
181182
}
182-
}
183+
}
184+
185+
pub fn change_password(args: Vec<String>) {
186+
if args.len() == 2{
187+
let old_password = &cryptograpy::prompt_for_passwords("Old password: ", 8, false);
188+
let decrypted_text = database_loader::read_decrypted_text(old_password);
189+
match decrypted_text{
190+
Ok(s) => {
191+
let new_password = &cryptograpy::prompt_for_passwords("New password: ", 8, true);
192+
match database_loader::overwrite_database_json(&s, new_password) {
193+
Ok(()) => println!("Password changed"),
194+
Err(e) => eprintln!("An error has occurred: {}",e),
195+
}
196+
},
197+
Err(e) => {
198+
eprintln!("An error has occurred: {}",e);
199+
}
200+
}
201+
}
202+
else{
203+
println!("Invalid arguments, type cotp --change-password");
204+
}
205+
}

src/database_loader.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ use utils::{get_db_path,check_elements};
66
use crate::cryptograpy;
77
use crate::otp::otp_element::OTPElement;
88

9-
10-
pub fn read_from_file(password: &str) -> Result<Vec<OTPElement>,String>{
9+
pub fn read_decrypted_text(password: &str) -> Result<String,String>{
1110
let encrypted_contents = read_to_string(&get_db_path()).unwrap();
1211
//rust close files at the end of the function
13-
let contents = cryptograpy::decrypt_string(&encrypted_contents, password);
14-
match contents {
12+
cryptograpy::decrypt_string(&encrypted_contents, password)
13+
}
14+
15+
pub fn read_from_file(password: &str) -> Result<Vec<OTPElement>,String>{
16+
match read_decrypted_text(password) {
1517
Ok(contents) => {
1618
let vector: Vec<OTPElement> = serde_json::from_str(&contents).unwrap();
1719
return Ok(vector);

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ fn args_parser(args: Vec<String>) -> bool {
121121
"-j" | "--json" => argument_functions::json(args),
122122
"-s" | "--single" => argument_functions::single(args),
123123
"-in" | "--info" => argument_functions::info(args),
124+
"-chpw" | "--change-password" => argument_functions::change_password(args),
124125
_=>{
125126
println!("Invalid argument: {}, type cotp -h to get command options", args[1]);
126127
return true;

0 commit comments

Comments
 (0)