From 01f2ef98c865d92433d5834c56513aff3b763e9b Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Tue, 1 Jan 2019 21:46:40 +0100 Subject: [PATCH 1/5] Extract an exit_error function --- bwmenu | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/bwmenu b/bwmenu index b011917..d7c4c50 100755 --- a/bwmenu +++ b/bwmenu @@ -15,15 +15,22 @@ load_items() { if [ $? -ne 0 ]; then mpw=`rofi -dmenu -p "Master Password" -password` || exit $?; - bw unlock "$mpw" | grep -e 'export.*=="' -o > $BW_HASH_FILE || { - rofi -e "Failed to unlock BitWarden. -Please try again."; - exit 1; + bw unlock "$mpw" | grep -e 'export.*=="' -o > $BW_HASH_FILE || { + exit_error 1 "Failed to unlock BitWarden. +Please try again." } load_items; fi } +exit_error() { + local code="$1" + local message="$2" + + rofi -e "$message" + exit "$code" +} + # Show the Rofi menu with options # Reads items from stdin rofi_menu() { @@ -33,7 +40,7 @@ rofi_menu() { -kb-custom-1 alt+r \ -kb-custom-2 alt+n \ -kb-custom-3 alt+u \ - -kb-custom-4 alt+c + -kb-custom-4 alt+c } @@ -90,10 +97,7 @@ show_folders() { # re-sync the BitWarden items with the server sync_bitwarden() { - bw sync &>/dev/null || { - rofi -e "Failed to sync bitwarden" - exit 1; - } + bw sync &>/dev/null || exit_error 1 "Failed to sync bitwarden" load_items show_items @@ -101,7 +105,7 @@ sync_bitwarden() { # Evaluate the rofi exit codes on_rofi_exit() { - case "$1" in + case "$1" in 10) sync_bitwarden;; 11) load_items; show_items;; 12) show_urls;; @@ -118,7 +122,7 @@ show_password() { # send a notification for 5 seconds (-t 5000) # not sure if icon will be present everywhere, /usr/share/icons is default icon location - notify-send "`echo $1 | jq -r '.name'` copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png + notify-send "`echo $1 | jq -r '.name'` copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png echo $pass | xclip -selection clipboard -r sleep 5; From 69d5a48163bf4c1e94652a46e118b81508d997f8 Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Tue, 1 Jan 2019 21:47:27 +0100 Subject: [PATCH 2/5] Abstract clipboard management --- bwmenu | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/bwmenu b/bwmenu index d7c4c50..eeb5e4e 100755 --- a/bwmenu +++ b/bwmenu @@ -7,6 +7,9 @@ BW_HASH_FILE=~/.bwhash # Holds the available items in memory ITEMS= +# Stores which command will be used to deal with clipboards +CLIPBOARD_MODE= + # source the hash file to gain access to the BitWarden CLI # Pre fetch all the items load_items() { @@ -114,6 +117,39 @@ on_rofi_exit() { esac } +# Set $CLIPBOARD_MODE to a command that will put stdin into the clipboard. +select_copy_command() { + if hash xclip 2>/dev/null; then + CLIPBOARD_MODE=xclip + else + exit_error 1 "No clipboard command found. Please install xclip." + fi +} + +clipboard-set() { + clipboard-${CLIPBOARD_MODE}-set +} + +clipboard-get() { + clipboard-${CLIPBOARD_MODE}-get +} + +clipboard-clear() { + clipboard-${CLIPBOARD_MODE}-clear +} + +clipboard-xclip-set() { + xclip -selection clipboard -r +} + +clipboard-xclip-get() { + xclip -selection clipboard -o +} + +clipboard-xclip-clear() { + echo "" | xclip -selection clipboard -r +} + # Output the password # copy to clipboard and give the user feedback that the password is copied # $1: json item @@ -122,12 +158,12 @@ show_password() { # send a notification for 5 seconds (-t 5000) # not sure if icon will be present everywhere, /usr/share/icons is default icon location - notify-send "`echo $1 | jq -r '.name'` copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png - echo $pass | xclip -selection clipboard -r + notify-send "$(echo "$1" | jq -r '.name') copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png + echo "$pass" | clipboard-set - sleep 5; - if [ "`xclip -selection clipboard -o`" = "$pass" ]; then - echo '' | xclip -selection clipboard -r + sleep 5 + if [[ "$(clipboard-get)" == "$pass" ]]; then + clipboard-clear fi } @@ -136,5 +172,6 @@ if ! [ -f $BW_HASH_FILE ]; then chmod 600 $BW_HASH_FILE; fi -load_items; -show_items; +select_copy_command +load_items +show_items From 838c7896e1ab9c1224035ab226549e05a4c08bc6 Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Tue, 1 Jan 2019 21:47:58 +0100 Subject: [PATCH 3/5] Add support for xsel for clipboard management --- bwmenu | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/bwmenu b/bwmenu index eeb5e4e..182168a 100755 --- a/bwmenu +++ b/bwmenu @@ -121,8 +121,10 @@ on_rofi_exit() { select_copy_command() { if hash xclip 2>/dev/null; then CLIPBOARD_MODE=xclip + elif hash xsel 2>/dev/null; then + CLIPBOARD_MODE=xsel else - exit_error 1 "No clipboard command found. Please install xclip." + exit_error 1 "No clipboard command found. Please install either xclip or xsel." fi } @@ -150,6 +152,18 @@ clipboard-xclip-clear() { echo "" | xclip -selection clipboard -r } +clipboard-xsel-set() { + xsel --clipboard --input +} + +clipboard-xsel-get() { + xsel --clipboard +} + +clipboard-xsel-clear() { + xsel --clipboard --delete +} + # Output the password # copy to clipboard and give the user feedback that the password is copied # $1: json item From 49efecc7be6865eae49b598a03aa150ebd9230e9 Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Tue, 1 Jan 2019 22:56:18 +0100 Subject: [PATCH 4/5] Mention xsel as an option in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6d996e1..6762e9a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # BitWarden Rofi Menu -This is a work in progress to get the BitWarden cli functionality in an easy Rofi menu. +This is a work in progress to get the BitWarden cli functionality in an easy Rofi menu. On selecting an entry, the password is copied to your clipboard for 5 seconds. During those 5 seconds, a notification is shown indicating which password you are copying at that time. ![bitwarden-rofi](img/screenshot1.png) @@ -24,4 +24,4 @@ You can either execute the script from a terminal or by binding it to a key comb - bitwarden-cli - jq -- xclip +- xclip or xsel From f007694b87de6ad9073018d8fd227db530bd9517 Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Tue, 1 Jan 2019 23:27:22 +0100 Subject: [PATCH 5/5] Don't include newline in copied password --- bwmenu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bwmenu b/bwmenu index 182168a..cb631b4 100755 --- a/bwmenu +++ b/bwmenu @@ -149,7 +149,7 @@ clipboard-xclip-get() { } clipboard-xclip-clear() { - echo "" | xclip -selection clipboard -r + echo -n "" | xclip -selection clipboard -r } clipboard-xsel-set() { @@ -173,7 +173,7 @@ show_password() { # send a notification for 5 seconds (-t 5000) # not sure if icon will be present everywhere, /usr/share/icons is default icon location notify-send "$(echo "$1" | jq -r '.name') copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png - echo "$pass" | clipboard-set + echo -n "$pass" | clipboard-set sleep 5 if [[ "$(clipboard-get)" == "$pass" ]]; then