It's minimal, but I'm posting things.
Bitwarden is a reputable and open-source password manager.
You save encrypted credentials in a Vault.
You can decrypt and access your Vault with a master password.
The Bitwarden tool for interacting with the CLI is pretty bare bones.
Bitwarden sells a solution to make interacting with their vault more convenient, but we don't need to use that for our purposes.
Using the Bitwarden CLI, here is what you'd be expected to do to obtain an entrie's password.
If you want to copy a password you must do the following:
bw password get [search_term]
However, if more than one result is returned, you must specify the item's id, which you can obtain by doing the following:
bw list items --search [search_term]
This returns a JSON string which contains a list of entries. Within each entry there is the item id.
To extract this id, you can use a tool such as jq
which helps interacting with json strings in the CLI.
Once you have the id
of the vault entry for which you need a password you can query it:
bw get password $id
I cannot imagine myself doing all of this manuallyl; there is a better way.
We will define a function you can place in your .zshrc
file (it probably works with .bashrc
as well) which will automatically:
search_term
input.clipboard
, you can then simply paste
it in.# xClip, FZF, and Bitwarden CLI
sudo apt install xclip fzf bw jq
function bw-get-password() {
searchTerm=$1
if [ -z "$searchTerm" ]; then echo "ValueError: SearchTerm is empty." && return 2; fi
json_url_ids=$(bw --nointeraction --cleanexit list items --search "$searchTerm" | jq '.[] | {uri: .login.uris[].uri, id: .id}')
if [ -z "$json_url_ids" ]; then
echo "No matching vault entries for '$searchTerm'"
return 1
fi
selected_uri=$(echo $json_url_ids | jq --raw-output '.uri' | fzf --sync)
selected_id=$(echo "$json_url_ids" | jq -r --arg uri "$selected_uri" '. | select(.uri == $uri) | .id')
bw --nointeraction --cleanexit get password $selected_id | xclip -selection c
echo "Successfully copied password for '$selected_uri'"
}
You'll probably want to bind the above function to a keybind.
For me, I use zsh
.
I bind the shortcut to LEFT_ALT + l
.
function bitwarden-get-password() {
zle -I
read "searchTerm?Bitwarden Service search term: " < /dev/tty
bw-get-password $searchTerm
}
zle -N bitwarden-get-password
bindkey "^[l" bitwarden-get-password