chore: Add confirmation parameter to user_secret

This parameter will ask the user to confirmation the secret
This commit is contained in:
Awiteb 2024-05-15 17:58:20 +03:00
parent 4b5928a5a9
commit 74a42d9813
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F
5 changed files with 15 additions and 11 deletions

View file

@ -55,8 +55,9 @@ impl LprsCommand for Add {
fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> {
if !self.vault_info.is_empty() {
self.vault_info.name = self.vault_info.name.trim().to_string();
self.vault_info.password = utils::user_secret(self.password, "Vault password:")?;
self.vault_info.totp_secret = utils::user_secret(self.totp_secret, "TOTP Secret:")?;
self.vault_info.password = utils::user_secret(self.password, "Vault password:", false)?;
self.vault_info.totp_secret =
utils::user_secret(self.totp_secret, "TOTP Secret:", false)?;
self.vault_info.custom_fields = self.custom_fields.into_iter().collect();
vault_manager.add_vault(self.vault_info);
vault_manager.try_export()?;

View file

@ -77,10 +77,10 @@ impl LprsCommand for Edit {
vault.name = new_name;
}
if self.password.is_some() {
vault.password = utils::user_secret(self.password, "New vault password:")?;
vault.password = utils::user_secret(self.password, "New vault password:", false)?;
}
if self.totp_secret.is_some() {
vault.totp_secret = utils::user_secret(self.totp_secret, "TOTP Secret:")?;
vault.totp_secret = utils::user_secret(self.totp_secret, "TOTP Secret:", false)?;
}
if let Some(new_username) = self.username {
vault.username = Some(new_username);

View file

@ -55,7 +55,7 @@ impl LprsCommand for Export {
);
let encryption_key: Option<[u8; 32]> =
utils::user_secret(self.encryption_password, "Encryption Password:")?
utils::user_secret(self.encryption_password, "Encryption Password:", false)?
.map(|p| sha2::Sha256::digest(p).into());
let exported_data = match self.format {

View file

@ -60,7 +60,7 @@ impl LprsCommand for Import {
);
let decryption_key: Option<[u8; 32]> =
utils::user_secret(self.decryption_password, "Decryption password:")?
utils::user_secret(self.decryption_password, "Decryption password:", false)?
.map(|p| sha2::Sha256::digest(p).into());
let imported_passwords_len = match self.format {

View file

@ -57,6 +57,7 @@ pub fn local_project_file(filename: &str) -> LprsResult<PathBuf> {
pub fn user_secret(
secret: Option<Option<String>>,
prompt_message: &str,
confirmation: bool,
) -> LprsResult<Option<String>> {
Ok(match secret {
None => None,
@ -64,11 +65,13 @@ pub fn user_secret(
Some(None) => {
log::debug!("User didn't provide a secret, prompting it");
Some(
Password::new(prompt_message)
.without_confirmation()
.with_formatter(&|p| "*".repeat(p.chars().count()))
.with_display_mode(PasswordDisplayMode::Masked)
.prompt()?,
Password {
enable_confirmation: confirmation,
..Password::new(prompt_message)
.with_formatter(&|p| "*".repeat(p.chars().count()))
.with_display_mode(PasswordDisplayMode::Masked)
}
.prompt()?,
)
}
})