2019-12-03 21:00:32 -08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# Prompts for a pass entry, then for a specific field. Similar to passmenu, but
|
|
|
|
# uses rofi, and can resume from a previous session.
|
|
|
|
#
|
|
|
|
# Example: passmenu [RESUME]
|
|
|
|
|
|
|
|
ROFI_CMD='rofi -dmenu -theme theme/passmenu.rasi'
|
2020-03-28 22:45:17 -07:00
|
|
|
MSG_CMD='notify-send'
|
2019-12-03 21:00:32 -08:00
|
|
|
set -e
|
|
|
|
shopt -s nullglob globstar
|
|
|
|
|
2020-02-09 00:03:58 -08:00
|
|
|
for prog in xclip pass; do
|
2019-12-03 21:00:32 -08:00
|
|
|
if ! command -v "$prog" >/dev/null; then
|
|
|
|
>&2 echo "$prog not installed, aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $1 == -r ]]; then
|
|
|
|
resume=1; shift
|
|
|
|
fi
|
2019-12-08 14:02:41 -08:00
|
|
|
|
|
|
|
url=
|
|
|
|
# Get qutebrowser's current url
|
|
|
|
win=$(xdotool getactivewindow)
|
|
|
|
if [[ -n $win ]]; then
|
|
|
|
winname=$(xdotool getwindowname $win)
|
|
|
|
if [[ $winname =~ " - Mozilla Firefox$" ]]; then
|
|
|
|
url=${winname% - Mozilla*}
|
|
|
|
url=${url##* - }
|
|
|
|
while (( ${#url//[^.]/} > 1 )); do
|
|
|
|
url=${url#*.}
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
2019-12-03 21:00:32 -08:00
|
|
|
|
|
|
|
LAST_ENTRY_FILE=/tmp/_upm
|
|
|
|
if [[ -n $resume && -f $LAST_ENTRY_FILE ]]; then
|
|
|
|
entry="$(<$LAST_ENTRY_FILE)"
|
|
|
|
else
|
|
|
|
prefix=${PASSWORD_STORE_DIR-~/.password-store}
|
|
|
|
password_files=( "$prefix"/**/*.gpg )
|
|
|
|
password_files=( "${password_files[@]#"$prefix"/}" )
|
|
|
|
password_files=( "${password_files[@]%.gpg}" )
|
2020-02-09 00:03:58 -08:00
|
|
|
entry=$(printf "%s\n" "${password_files[@]}" | $ROFI_CMD -p "..." -filter "$url")
|
2019-12-03 21:00:32 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $entry ]]; then
|
2020-03-28 22:45:17 -07:00
|
|
|
$MSG_CMD "Error!" "Failed to select $entry"
|
2019-12-03 21:00:32 -08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$entry" >/tmp/_upm
|
|
|
|
secrets="$(pass show "$entry")"
|
|
|
|
if [[ -n $secrets ]]; then
|
|
|
|
fields=$(awk -F': ' 'NR>1 && !/^otpauth:/ {print $1}' <<<"$secrets")
|
2020-02-09 00:03:58 -08:00
|
|
|
field=$(printf "password\n%s\notp\n" "${fields[@]}" | $ROFI_CMD -p "$entry")
|
2019-12-03 21:00:32 -08:00
|
|
|
case $field in
|
|
|
|
password) pass -c "$entry" ;;
|
|
|
|
otp) printf "%s" "$(pass otp -c "$entry")" ;;
|
|
|
|
*) printf "%s" "$(awk -v "key=$field:" '$1 == key {print $2}' <<<"$secrets")" | xclip -selection clipboard -in ;;
|
|
|
|
esac
|
2020-03-28 22:45:17 -07:00
|
|
|
$MSG_CMD "Success!" "Copied <b>$entry/$field</b> to clipboard for 45s"
|
2019-12-03 21:00:32 -08:00
|
|
|
else
|
2020-03-28 22:45:17 -07:00
|
|
|
$MSG_CMD "Error!" "Failed to retrieve $entry"
|
2019-12-03 21:00:32 -08:00
|
|
|
exit 2
|
|
|
|
fi
|