Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement search functionality #149

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 136 additions & 4 deletions src/bin/rbw/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,20 @@ impl DecryptedCipher {
username: Option<&str>,
folder: Option<&str>,
try_match_folder: bool,
case_insensitive: bool,
) -> bool {
if !self.name.contains(name) {
return false;
if case_insensitive {
let notes_value = self.notes.as_deref().unwrap_or("");
if !self.name.to_lowercase().contains(&name.to_lowercase())
&& !notes_value.to_lowercase().contains(&name.to_lowercase())
{
return false;
}
} else {
let notes_value = self.notes.as_deref().unwrap_or("");
if !self.name.contains(name) && !notes_value.contains(name) {
return false;
}
}

if let Some(given_username) = username {
Expand Down Expand Up @@ -886,6 +897,52 @@ pub fn get(
Ok(())
}

pub fn search(
term: &str,
user: Option<&str>,
folder: Option<&str>,
full: bool,
raw: bool,
) -> anyhow::Result<()> {
unlock()?;

let db = load_db()?;

let desc = format!(
"{}{}",
user.map_or_else(String::new, |s| format!("{s}@")),
term
);

let mut found_entries = find_entries(&db, term, user, folder)
.with_context(|| format!("No entries found for '{desc}'"))?;

if !full {
let just_names: Vec<String> = found_entries
.iter()
.cloned()
.map(|DecryptedCipher| DecryptedCipher.name)
.collect();
if raw {
println!("{}", serde_json::to_string(&just_names)?);
} else {
for name in just_names {
val_display_or_store(false, &name);
}
}
} else {
if raw {
println!("{}", serde_json::to_string(&found_entries)?);
} else {
for entry in found_entries {
entry.display_long(&term, false);
}
}
}

Ok(())
}

pub fn code(
name: &str,
user: Option<&str>,
Expand Down Expand Up @@ -1381,6 +1438,79 @@ fn find_entry(
}
}

fn find_entries(
db: &rbw::db::Db,
name: &str,
username: Option<&str>,
folder: Option<&str>,
) -> anyhow::Result<Vec<DecryptedCipher>> {
let entries: Vec<(rbw::db::Entry, DecryptedCipher)> = db
.entries
.iter()
.cloned()
.map(|entry| {
decrypt_cipher(&entry).map(|decrypted| (entry, decrypted))
})
.collect::<anyhow::Result<_>>()?;
let mut matches: Vec<DecryptedCipher> = entries
.iter()
.cloned()
.filter(|(_, decrypted_cipher)| {
decrypted_cipher.exact_match(name, username, folder, true)
})
.map(|(_, DecryptedCipher)| DecryptedCipher)
.collect();

if matches.len() >= 1 {
return Ok(matches.clone());
}

if folder.is_none() {
matches = entries
.iter()
.cloned()
.filter(|(_, decrypted_cipher)| {
decrypted_cipher.exact_match(name, username, folder, false)
})
.map(|(_, DecryptedCipher)| DecryptedCipher)
.collect();

if matches.len() >= 1 {
return Ok(matches.clone());
}
}

matches = entries
.iter()
.cloned()
.filter(|(_, decrypted_cipher)| {
decrypted_cipher.partial_match(name, username, folder, true, true)
})
.map(|(_, DecryptedCipher)| DecryptedCipher)
.collect();

if matches.len() >= 1 {
return Ok(matches.clone());
}

if folder.is_none() {
matches = entries
.iter()
.cloned()
.filter(|(_, decrypted_cipher)| {
decrypted_cipher
.partial_match(name, username, folder, false, true)
})
.map(|(_, DecryptedCipher)| DecryptedCipher)
.collect();
if matches.len() >= 1 {
return Ok(matches.clone());
}
}

Err(anyhow::anyhow!("no entries found"))
}

fn find_entry_raw(
entries: &[(rbw::db::Entry, DecryptedCipher)],
name: &str,
Expand Down Expand Up @@ -1417,7 +1547,8 @@ fn find_entry_raw(
.iter()
.cloned()
.filter(|(_, decrypted_cipher)| {
decrypted_cipher.partial_match(name, username, folder, true)
decrypted_cipher
.partial_match(name, username, folder, true, false)
})
.collect();

Expand All @@ -1430,7 +1561,8 @@ fn find_entry_raw(
.iter()
.cloned()
.filter(|(_, decrypted_cipher)| {
decrypted_cipher.partial_match(name, username, folder, false)
decrypted_cipher
.partial_match(name, username, folder, false, false)
})
.collect();
if matches.len() == 1 {
Expand Down
28 changes: 28 additions & 0 deletions src/bin/rbw/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ enum Opt {
clipboard: bool,
},

#[command(about = "Search for entries")]
Search {
#[arg(help = "Search term to locate entries")]
term: String,
#[arg(help = "Username of the entry to display")]
user: Option<String>,
#[arg(long, help = "Folder name to search in")]
folder: Option<String>,
#[arg(long, help = "Display the full entry in addition to the name")]
full: bool,
#[structopt(long, help = "Display output as JSON")]
raw: bool,
},

#[command(about = "Display the authenticator code for a given entry")]
Code {
#[arg(help = "Name or UUID of the entry to display")]
Expand Down Expand Up @@ -244,6 +258,7 @@ impl Opt {
Self::Sync => "sync".to_string(),
Self::List { .. } => "list".to_string(),
Self::Get { .. } => "get".to_string(),
Self::Search { .. } => "search".to_string(),
Self::Code { .. } => "code".to_string(),
Self::Add { .. } => "add".to_string(),
Self::Generate { .. } => "generate".to_string(),
Expand Down Expand Up @@ -334,6 +349,19 @@ fn main() {
*raw,
*clipboard,
),
Opt::Search {
term,
user,
folder,
full,
raw,
} => commands::search(
term,
user.as_deref(),
folder.as_deref(),
*full,
*raw,
),
Opt::Code { name, user, folder } => {
commands::code(name, user.as_deref(), folder.as_deref())
}
Expand Down
Loading