From 21092a15e4a9e0eba3f25aa764ef962c7337696d Mon Sep 17 00:00:00 2001 From: Awiteb Date: Wed, 15 May 2024 18:46:04 +0300 Subject: [PATCH] feat: Support changing master password --- src/cli/change_master_password_command.rs | 39 +++++++++++++++++++++++ src/cli/mod.rs | 6 +++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/cli/change_master_password_command.rs diff --git a/src/cli/change_master_password_command.rs b/src/cli/change_master_password_command.rs new file mode 100644 index 0000000..8fd249d --- /dev/null +++ b/src/cli/change_master_password_command.rs @@ -0,0 +1,39 @@ +// Lprs - A local CLI vaults manager. For human and machine use +// Copyright (C) 2024 Awiteb +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use clap::Args; +use sha2::{Digest, Sha256}; + +use crate::{utils, vault::Vaults, LprsCommand, LprsResult}; + +#[derive(Debug, Args)] +/// Change master password, reencrypt the vaults with new password +pub struct ChangeMasterPassword { + /// The new master password, if there is no value for it you will prompt it + #[allow(clippy::option_option)] + new_password: Option, +} + +impl LprsCommand for ChangeMasterPassword { + fn run(self, mut vault_manager: Vaults) -> LprsResult<()> { + vault_manager.master_password = + utils::user_secret(Some(self.new_password), "New master password:", true)? + .map(|s| Sha256::digest(s).into()) + .expect("We wrap it in `Some`, so is will return a secret"); + vault_manager.try_export()?; + Ok(()) + } +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 3cfb68f..0eee915 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -23,6 +23,8 @@ use crate::{impl_commands, utils, vault::Vaults, LprsCommand, LprsResult}; /// Add command, used to add new vault to the vaults file pub mod add_command; +/// Change master password, reencrypt the vaults with new password +pub mod change_master_password_command; /// Clean command, used to clean the vaults file (remove all vaults) pub mod clean_command; /// Generate shell completion @@ -67,11 +69,13 @@ pub enum Commands { Export(export_command::Export), /// Import vaults Import(import_command::Import), + /// Change master password, reencrypt the vaults with new password + ChangeMasterPassword(change_master_password_command::ChangeMasterPassword), /// Generate shell completion Completion(completion_command::Completion), } -impl_commands!(Commands, Add Remove List Clean Edit Gen Get Export Import Completion); +impl_commands!(Commands, Add Remove List Clean Edit Gen Get Export Import ChangeMasterPassword Completion); #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)]