Skip to content

Instantly share code, notes, and snippets.

@loeschzwerg
Created April 8, 2023 14:46
Show Gist options
  • Save loeschzwerg/c2b9d0b50f712a026aa6454af3b58598 to your computer and use it in GitHub Desktop.
Save loeschzwerg/c2b9d0b50f712a026aa6454af3b58598 to your computer and use it in GitHub Desktop.
Bitwarden CLI: Interactive Clipboard Selection with fzf

Interactive Bitwarden CLI Clipboard Selection

The Bitwarden CLI bw provides JSON Output. Because I don't like to select Strings from the stdout JSON output:

  • jq redirects the output into selectable snippets
  • fzf selects the entry I need
  • xclip copies the selected entry into your clipboard (e.g. password)

I adjusted the Bash script by Christian Drumm:

  • pbcopy -> xclip, because xclip is more common
  • sanity check for searchterm
  • increased log messages (because of slow bw calls)
  • Reuse of BW_SESSION
  • Output of entry ID for immediate acquisition in shell scripts

You may add this as ~/.local/bin/bwc:

#!/bin/zsh
set -ef
copy_data () {
  local id=$1
  local login=$2
  local sessionkey=$3
  local totp
  echo "Name: $(jq -r ".name" <<< $login), ID: $(jq -r ".id" <<< $login)"
  # Copy the username to the clipboard
  echo "> Copying Username"
  jq -r ".login.username" <<< $login | xclip -sel clip
  # Wait for user input before coping the password
  echo "> Press Enter to copy password..."
  read
  # Copy the password to the clipboard
  echo "> Copying Password"
  jq -r ".login.password" <<< $login | xclip -sel clip
  # Copy a TOTP Token if available
  totp=$(jq -r ".login.totp" <<< $login)
  if [[ $totp != "null" ]]; then
    # Wait for user input before coping the totp token
    echo "> Press any key to copy TOTP Token..."
    read
    echo "> Copying TOTP Token"
    bw get totp $id --session $sessionkey | xclip -sel clip
  fi
}
main() {
  local searchterm=$1
  local sessionkey logins login id
  # Use an existing BW_SESSION or obtain a new session key
  if [[ -z $BW_SESSION ]] ; then
    echo "Creating new sessionkey..."
    sessionkey=$(bw unlock --raw)
  else
    echo "Using existing sessionkey..."
    sessionkey=$BW_SESSION
  fi
  # Search for passwords using the search term
  if [[ -z $searchterm ]] ; then
    echo "Searchterm can't be empty"
    exit 1
  fi 
  echo "Searching for '$searchterm'..."
  logins=$(bw list items --search $searchterm --session $sessionkey)
  id=$(jq -r '.[] | "\(.name)\t\(.login.username)\t\(.id)"' <<< $logins \
    | fzf --reverse --with-nth=1,2 --delimiter="\t" --select-1 --exit-0 \
    | awk -F"\t" '{print $3}'
  )
  if [[ -n $id ]]; then
    login="$(jq ".[] | select(.id == \"$id\")" <<< $logins)"
    copy_data $id $login $sessionkey
  else
    echo "No entry found..."
  fi
}

main "$@"

Beware: The Bitwarden CLI is slow, so it may take some time until the output is redirected to the fzf selection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment