feat: Support custom fields #31
4 changed files with 111 additions and 49 deletions
|
@ -17,6 +17,7 @@
|
|||
use clap::Args;
|
||||
|
||||
use crate::{
|
||||
clap_parsers,
|
||||
utils,
|
||||
vault::{Vault, Vaults},
|
||||
LprsCommand,
|
||||
|
@ -34,11 +35,16 @@ pub struct Add {
|
|||
#[arg(short, long)]
|
||||
#[allow(clippy::option_option)]
|
||||
password: Option<Option<String>>,
|
||||
/// Add a custom field to the vault
|
||||
#[arg(name = "KEY=VALUE", short = 'c', long = "custom")]
|
||||
#[arg(value_parser = clap_parsers::kv_parser)]
|
||||
custom_fields: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl LprsCommand for Add {
|
||||
fn run(mut self, mut vault_manager: Vaults) -> LprsResult<()> {
|
||||
self.vault_info.password = utils::user_password(self.password, "Vault password:")?;
|
||||
self.vault_info.custom_fields = self.custom_fields.into_iter().collect();
|
||||
vault_manager.add_vault(self.vault_info);
|
||||
vault_manager.try_export()
|
||||
}
|
||||
|
@ -48,9 +54,17 @@ impl LprsCommand for Add {
|
|||
&& self.password.is_none()
|
||||
&& self.vault_info.service.is_none()
|
||||
&& self.vault_info.note.is_none()
|
||||
&& self.custom_fields.is_empty()
|
||||
{
|
||||
return Err(LprsError::Other("You can't add empty vault".to_owned()));
|
||||
}
|
||||
|
||||
if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) {
|
||||
return Err(LprsError::Other(format!(
|
||||
"Duplication error: The custom key `{duplicated_key}` is duplicate"
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,19 +18,13 @@ use std::num::NonZeroU64;
|
|||
|
||||
use clap::Args;
|
||||
|
||||
use crate::{
|
||||
utils,
|
||||
vault::{Vault, Vaults},
|
||||
LprsCommand,
|
||||
LprsError,
|
||||
LprsResult,
|
||||
};
|
||||
use crate::{clap_parsers, utils, vault::Vaults, LprsCommand, LprsError, LprsResult};
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
/// Edit command, used to edit the vault content
|
||||
pub struct Edit {
|
||||
/// The password index. Check it from list command
|
||||
/// The password index. You can get it from the list command
|
||||
index: NonZeroU64,
|
||||
|
||||
#[arg(short, long)]
|
||||
|
@ -49,6 +43,12 @@ pub struct Edit {
|
|||
#[arg(short = 'o', long)]
|
||||
/// The new vault note
|
||||
note: Option<String>,
|
||||
/// The custom field, make its value empty to delete it
|
||||
///
|
||||
/// If the custom field not exist will created it, if it's will update it
|
||||
#[arg(name = "KEY=VALUE", short = 'c', long = "custom")]
|
||||
#[arg(value_parser = clap_parsers::kv_parser)]
|
||||
pub custom_fields: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
impl LprsCommand for Edit {
|
||||
|
@ -65,15 +65,23 @@ impl LprsCommand for Edit {
|
|||
};
|
||||
|
||||
log::info!("Applying the new values to the vault");
|
||||
*vault = Vault::new(
|
||||
self.name.as_ref().unwrap_or(&vault.name),
|
||||
self.username.as_ref().or(vault.username.as_ref()),
|
||||
utils::user_password(self.password, "New vault password:")?
|
||||
.as_ref()
|
||||
.or(vault.password.as_ref()),
|
||||
self.service.as_ref().or(vault.service.as_ref()),
|
||||
self.note.as_ref().or(vault.note.as_ref()),
|
||||
);
|
||||
if let Some(new_name) = self.name {
|
||||
vault.name = new_name;
|
||||
}
|
||||
if self.password.is_some() {
|
||||
vault.password = utils::user_password(self.password, "New vault password:")?;
|
||||
}
|
||||
if let Some(new_username) = self.username {
|
||||
vault.username = Some(new_username);
|
||||
}
|
||||
if let Some(new_service) = self.service {
|
||||
vault.service = Some(new_service);
|
||||
}
|
||||
if let Some(new_note) = self.note {
|
||||
vault.note = Some(new_note);
|
||||
}
|
||||
utils::apply_custom_fields(&mut vault.custom_fields, self.custom_fields);
|
||||
|
||||
vault_manager.try_export()
|
||||
}
|
||||
|
||||
|
@ -83,11 +91,18 @@ impl LprsCommand for Edit {
|
|||
&& self.password.is_none()
|
||||
&& self.service.is_none()
|
||||
&& self.note.is_none()
|
||||
&& self.custom_fields.is_empty()
|
||||
{
|
||||
return Err(LprsError::Other(
|
||||
"You must edit one option at least".to_owned(),
|
||||
));
|
||||
}
|
||||
if let Some(duplicated_key) = utils::get_duplicated_field(&self.custom_fields) {
|
||||
return Err(LprsError::Other(format!(
|
||||
"Duplication error: The custom key `{duplicated_key}` is duplicate"
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,12 @@ pub struct BitWardenFolder {
|
|||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct BitWardenNameValue {
|
||||
pub name: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct BitWardenPassword {
|
||||
#[serde(rename = "type")]
|
||||
|
@ -49,6 +55,8 @@ pub struct BitWardenPassword {
|
|||
pub name: String,
|
||||
pub login: Option<BitWardenLoginData>,
|
||||
pub notes: Option<String>,
|
||||
#[serde(default)]
|
||||
pub fields: Vec<BitWardenNameValue>,
|
||||
}
|
||||
|
||||
/// The bitwarden password struct
|
||||
|
@ -71,6 +79,11 @@ impl From<BitWardenPassword> for Vault {
|
|||
.and_then(|p| p.first().map(|u| u.uri.clone()))
|
||||
}),
|
||||
value.notes,
|
||||
value
|
||||
.fields
|
||||
.into_iter()
|
||||
.map(|nv| (nv.name, nv.value))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -88,6 +101,11 @@ impl From<Vault> for BitWardenPassword {
|
|||
.map(|s| vec![BitWardenUri { mt: None, uri: s }]),
|
||||
}),
|
||||
notes: value.note,
|
||||
fields: value
|
||||
.custom_fields
|
||||
.into_iter()
|
||||
.map(|(name, value)| BitWardenNameValue { name, value })
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
|
||||
|
||||
use std::{fmt, fs, path::PathBuf};
|
||||
use std::{collections::BTreeMap, fmt, fs, path::PathBuf};
|
||||
|
||||
use base64::Engine;
|
||||
use clap::{Parser, ValueEnum};
|
||||
|
@ -58,6 +58,9 @@ pub struct Vault {
|
|||
/// Add a note to the vault
|
||||
#[arg(short, long)]
|
||||
pub note: Option<String>,
|
||||
/// The vault custom fields
|
||||
#[arg(skip)]
|
||||
pub custom_fields: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
/// The vaults manager
|
||||
|
@ -79,6 +82,7 @@ impl Vault {
|
|||
password: Option<impl Into<String>>,
|
||||
service: Option<impl Into<String>>,
|
||||
note: Option<impl Into<String>>,
|
||||
custom_fields: BTreeMap<String, String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
|
@ -86,6 +90,7 @@ impl Vault {
|
|||
password: password.map(Into::into),
|
||||
service: service.map(Into::into),
|
||||
note: note.map(Into::into),
|
||||
custom_fields,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,9 +136,8 @@ impl Vaults {
|
|||
///
|
||||
/// Note: The returned string is `Vec<Vault>`
|
||||
pub fn json_export(&self, encryption_key: &[u8; 32]) -> LprsResult<String> {
|
||||
let encrypt = |val: &str| {
|
||||
LprsResult::Ok(crate::BASE64.encode(cipher::encrypt(encryption_key, val.as_ref())))
|
||||
};
|
||||
let encrypt =
|
||||
|val: &str| crate::BASE64.encode(cipher::encrypt(encryption_key, val.as_ref()));
|
||||
|
||||
serde_json::to_string(
|
||||
&self
|
||||
|
@ -141,11 +145,15 @@ impl Vaults {
|
|||
.iter()
|
||||
.map(|v| {
|
||||
LprsResult::Ok(Vault::new(
|
||||
encrypt(&v.name)?,
|
||||
v.username.as_ref().and_then(|u| encrypt(u).ok()),
|
||||
v.password.as_ref().and_then(|p| encrypt(p).ok()),
|
||||
v.service.as_ref().and_then(|s| encrypt(s).ok()),
|
||||
v.note.as_ref().and_then(|n| encrypt(n).ok()),
|
||||
encrypt(&v.name),
|
||||
v.username.as_ref().map(|u| encrypt(u)),
|
||||
v.password.as_ref().map(|p| encrypt(p)),
|
||||
v.service.as_ref().map(|s| encrypt(s)),
|
||||
v.note.as_ref().map(|n| encrypt(n)),
|
||||
v.custom_fields
|
||||
.iter()
|
||||
.map(|(key, value)| (encrypt(key), encrypt(value)))
|
||||
.collect(),
|
||||
))
|
||||
})
|
||||
.collect::<LprsResult<Vec<_>>>()?,
|
||||
|
@ -178,6 +186,10 @@ impl Vaults {
|
|||
v.password.as_ref().and_then(|p| decrypt(p).ok()),
|
||||
v.service.as_ref().and_then(|s| decrypt(s).ok()),
|
||||
v.note.as_ref().and_then(|n| decrypt(n).ok()),
|
||||
v.custom_fields
|
||||
.into_iter()
|
||||
.map(|(key, value)| LprsResult::Ok((decrypt(&key)?, decrypt(&value)?)))
|
||||
.collect::<LprsResult<_>>()?,
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
|
@ -245,6 +257,9 @@ impl fmt::Display for Vault {
|
|||
if let Some(ref note) = self.note {
|
||||
write!(f, "\nNote:\n{note}")?;
|
||||
}
|
||||
for (key, value) in &self.custom_fields {
|
||||
write!(f, "\n{key}: {value}")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue