From 53812b8131b9a8dba7824d398a94eb5b6a9226fa Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Tue, 1 Aug 2023 22:47:29 +0200 Subject: [PATCH] feat: add listing only items matching valid ssh key item criteria --- src/main.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index b815b4c..e73f55f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,10 @@ struct Cli { /// Environment variable housing an existing Bitwarden session. const SESSION_ENV_KEY: &str = "BW_SESSION"; +/// Environment variable referencing the name of the field containing the SSH key filename. +const BW_FIELD_KEY_FILENAME: &str = "private"; +/// Environment variable referencing the name of the field containing the SSH key passphrase (if any). +const BW_FIELD_KEY_PASSPHRASE: &str = "passphrase"; fn main() -> Result<()> { let args: Cli = Cli::parse_args_default_or_exit(); @@ -42,7 +46,32 @@ fn main() -> Result<()> { for folder in folders { let folder_items = bwutil::exec_list_folder_items(&session_token, &folder.id)?; for item in folder_items { - println!("{:#?}", item); + // In order for this to be considered a valid SSH key item, this item needs to have the following fields: + // - private - (required) this is the filename of the attachment containing the SSH key. + // The field name is not case sensitive in order to be more user-friendly. + // - passphrase - (optional) this is the passphrase (if any) for the SSH key. + // The field name is not case sensitive in order to be more user-friendly. + // - attachment (required) an attachment with the name specified in `private`. + if let Some(fields) = &item.fields { + let mut key_filename = String::new(); + let mut key_passphrase = String::new(); + for field in fields { + if field.name.to_lowercase().eq(&String::from(BW_FIELD_KEY_FILENAME).to_lowercase()) { + key_filename.push_str(&field.value); + } + if field.name.to_lowercase().eq(&String::from(BW_FIELD_KEY_PASSPHRASE).to_lowercase()) { + key_passphrase.push_str(&field.value); + } + } + + if let Some(attachments) = &item.attachments { + for attachment in attachments { + if attachment.file_name.eq(&key_filename) { + println!("{:#?}", item); + } + } + } + } } } } -- libgit2 1.7.2