Wrapper for bitwardenCli in bash

Hello,

I wrote a bash script to help with the bitwarden cli which for me just does not cut it. I will post it here please try it out and give feedback. It has as a dependencies jq for json parsing and the bitwarden cli. Both are in the official repositories.

Thanks,

frog


#!/bin/bash

vaultUnlock(){
   sessiontoken=$(bw unlock)
   if [ -z "$sessiontoken" ]; then
       echo "Master Password Incorrect"
       exit
   else
       sessiontoken=$(grep -E -m 1 -o '"(.*)"' <<< "$sessiontoken")
   fi
}

vaultClose(){
   bw sync
   bw lock
}

editItem(){
   read -p 'Item ID: ' itemID
   echo 'Enter u to change Username and p to change Password'
   read -p 'Element to change: ' element
   if [ "$element" == 'u' ] ; then 
       read -p 'New Username: ' newusername
       newusername=\"$newusername\"
       bw get item $itemID | jq '.login.username='$newusername'' | bw encode | bw edit item $itemID > /dev/null
   elif [ "$element" == 'p' ] ; then
       echo 'Enter g for Bitwarden generated password or m for manual entry'
       read -p 'Method: ' method
       if [ "$method" == 'g' ]; then
           read -p 'Password Length: ' length
           newpassword=$(bw generate -uln --length $length)
       elif [ "$method" == 'm' ]; then
           read -p 'New Password: ' newpassword
       fi
       newpassword=\"$newpassword\"
       echo $newpassword
       bw get item $itemID | jq '.login.password='$newpassword'' | bw encode | bw edit item $itemID > /dev/null
   else
       echo 'Element to change was spelled incorrectly'
   fi
}

createItem(){
   read -p 'Item Name: ' itemname
   read -p 'Username: ' username
   read -p 'Password: ' password
   itemname=\"$itemname\"
   username=\"$username\"
   password=\"$password\"
   bw get template item | jq ".name=$itemname | .login=$(bw get template item.login | jq '.username='$username' | .password='$password'')" | bw encode | bw create item
}

deleteItem(){
   read -p 'Id of item to delete' deleteid
   bw delete item $deleteid 
}

listItems(){
   items=$(bw list items)
   names=$(jq '.[].name' <<< "$items") 
   id=$(jq '.[].id' <<< "$items")
   paste <(printf "%s\n" "${names[@]}") <(printf "%s\n" "${id[@]}")
}

getItem(){
   read -p 'Id of item to get: ' getitemid
   rawitem=$(bw get item $getitemid)
   jq . <<< $rawitem
}

echo '###### Bitwarden-Cli Wrapper in Bash ######'
echo
echo 'Avaible Options'
echo '0) Exit'
echo '1) Edit Item'
echo '2) Create Item'
echo '3) Delete Item'
echo '4) List Items'
echo '5) Get Item'
echo

read -p 'Select Action from above list: ' action

vaultUnlock
export BW_SESSION=$sessiontoken

while [[ $action -ne 0 ]]; do
   case "$action" in
       1) editItem
           ;;
       2) createItem
           ;;
       3) deleteItem
          ;;
       4) listItems
          ;;
       5) getItem
          ;;
   esac
   echo
   echo 'Avaible Options'
   echo '0) Exit'
   echo '1) Edit Item'
   echo '2) Create Item'
   echo '3) Delete Item'
   echo '4) List Items'
   echo '5) Get Item'
   read -p 'Select Next Action: ' action
done
vaultClose

Hello @theleapingfrog,

Thanks for your Wrapper.

Do you know how to create identity item?

Below function does NOT works.

createIdentity(){
  read -p 'Item Name: ' itemname
  itemname=\"$itemname\"
  bw get template item.identity

  bw get template item | jq ".name=$itemname | .identity=$(bw get template item.identity | jq '.title="Mr" | .firstName="Boyang"')" | bw encode | bw create item
}

I opened a issue in https://github.com/bitwarden/clients/issues/5842