2017-12-05 22:39:10 -08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
#
|
|
|
|
# Prompts for a pass entry, then for a specific field. Similar to passmenu, but
|
2018-03-23 12:29:50 -07:00
|
|
|
# uses sk, and can resume from a previous session.
|
2017-12-05 22:39:10 -08:00
|
|
|
#
|
2018-03-23 12:29:50 -07:00
|
|
|
# Example: sk-pass [RESUME]
|
2017-12-05 22:39:10 -08:00
|
|
|
#
|
|
|
|
|
|
|
|
set -e
|
|
|
|
shopt -s nullglob globstar
|
|
|
|
|
2018-05-03 11:23:45 -07:00
|
|
|
for prog in xsel notify-send pass; do
|
|
|
|
if ! command -v "$prog" >/dev/null; then
|
|
|
|
>&2 echo "$prog not installed, aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
if [[ $1 == -r ]]; then
|
|
|
|
resume=1; shift
|
|
|
|
fi
|
|
|
|
url=$@
|
|
|
|
|
|
|
|
LAST_ENTRY_FILE=/tmp/_upm
|
2017-12-31 22:33:49 -08:00
|
|
|
if [[ -n $resume && -f $LAST_ENTRY_FILE ]]; then
|
2017-12-05 22:39:10 -08:00
|
|
|
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}" )
|
2018-03-23 12:29:50 -07:00
|
|
|
entry=$(printf "%s\n" "${password_files[@]}" | sk --query="$url" --prompt='pass > ')
|
2017-12-05 22:39:10 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $entry ]]; then
|
|
|
|
notify-send --urgency=critical "Failed to select $entry"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$entry" >/tmp/_upm
|
|
|
|
secrets="$(pass show "$entry")"
|
2017-12-31 22:33:49 -08:00
|
|
|
if [[ -n $secrets ]]; then
|
2017-12-05 22:39:10 -08:00
|
|
|
fields=$(awk -F': ' 'NR>1 && !/^otpauth:/ {print $1}' <<<"$secrets")
|
2018-03-23 12:29:50 -07:00
|
|
|
field=$(printf "password\n%s\notp\n" "${fields[@]}" | sk --prompt="pass ($entry) > ")
|
2017-12-23 16:55:09 -08:00
|
|
|
case $field in
|
|
|
|
password) printf "%s" "$(head -n1 <<<"$secrets")" ;;
|
|
|
|
otp) printf "%s" "$(pass otp "$entry")" ;;
|
|
|
|
*) printf "%s" "$(awk -v "key=$field:" '$1 == key {print $2}' <<<"$secrets")" ;;
|
|
|
|
esac | xsel -i -b -t 15000
|
2017-12-05 22:39:10 -08:00
|
|
|
notify-send --urgency=low "<b>$entry -> $field</b>" "Copied to clipboard for 15s"
|
|
|
|
else
|
|
|
|
notify-send --urgency=critical "Failed to retrieve $entry"
|
|
|
|
exit 2
|
|
|
|
fi
|