diff --git a/src/bin/totp.rs b/src/bin/totp.rs index e5df92d..7e63d22 100644 --- a/src/bin/totp.rs +++ b/src/bin/totp.rs @@ -20,8 +20,7 @@ struct Cli {} impl Cli { fn app() -> RusTOTPony { - let secret = Self::get_secret(); - let db = JsonDatabase::new(Self::get_database_path(), &secret); + let db = JsonDatabase::new(Self::get_database_path(), &Self::get_secret); RusTOTPony::new(db) } diff --git a/src/lib.rs b/src/lib.rs index 6d04bfd..4f9150e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,15 +130,15 @@ struct DatabaseContentSchema { pub struct JsonDatabase { file_path: PathBuf, - secret: String, + secret_fn: &'static Fn() -> String, } const IV_SIZE: usize = 16; impl JsonDatabase { - pub fn new(path: PathBuf, secret: &str) -> JsonDatabase { + pub fn new(path: PathBuf, secret_fn: &'static Fn() -> String) -> JsonDatabase { JsonDatabase { file_path: path, - secret: String::from(secret), + secret_fn: secret_fn, } } @@ -148,7 +148,7 @@ impl JsonDatabase { Err(ref err) if err.kind() == ErrorKind::NotFound => return Self::get_empty_schema(), Err(err) => panic!("There was a problem opening file: {:?}", err), }; - let decrypted_data = Self::decrypt_data(&data, self.secret.as_str()); + let decrypted_data = Self::decrypt_data(&data, (self.secret_fn)().as_str()); serde_json::from_str(decrypted_data.as_str()) .expect("Couldn't parse JSON from database file") } @@ -184,7 +184,7 @@ impl JsonDatabase { Err(err) => panic!("Couldn't open database file: {:?}", err), }; let data = serde_json::to_string(&content).expect("Couldn't serialize data to JSON"); - let encrypted_data = Self::encrypt_data(&data, &self.secret); + let encrypted_data = Self::encrypt_data(&data, &(self.secret_fn)()); file.write_all(&encrypted_data) .expect("Couldn't write data to database file"); }