dotfiles/gopass/.local/bin/rofi-gopass

150 lines
3.8 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Code originally inspired by https://github.com/carnager/rofi-pass/
# DEFAULT OPTIONS
# typing tool used, choice of:
# xdotool |
# BACKEND=xdotool
# the complete typing chain autoentry uses
# possible fields:
# :tab | :space | :enter | username | password
#
# Note that spaces have to be passed explicitly with :space,
# putting a literal ' ' in the chain will not work
#
# By default it enters the username, tabs down, and enters the password
# It does not press return at the end for safety, though you can add
# :return to make it do so.
# AUTOENTRY_CHAIN="username :tab password"
# wait time before entering each input
# in milliseconds
# AUTOENTRY_DELAY=20
# default key bindings
# Automatically enter username and password
# KEY_AUTOENTRY="Return"
# The field name containing your gopass username
# can take multiple names, separated by space
# Will go through names in descending precedence
# GOPASS_USERNAME_FIELD="username user login"
# rofi wrapper. Add custom settings here.
_rofi() {
# TODO add check for dmenu
exist rofi critical "rofi-pass" || exit 0
rofi -dmenu -no-auto-select -i "$@" -theme /themes/dmenu
}
# exit on escape pressed
# rofi returns exit code 1 on esc
exit_check() {
[ "$1" -eq 1 ] && exit
}
# simply return a list of all passwords in gopass store
# TODO choose password store
# TODO only show website names (+ folder names), and account names for multiple accounts on one site
_gp_list_passwords() {
gopass list --flat
}
# return password for argument passed
_gp_show_password() {
gopass show -f --password "$1"
}
# send password to clipboard
_gp_clip_password() {
gopass show -c "$1"
}
# attempt to return the field specified
# attempts all (space separated) fields until the
# first one successfully returned
_gp_get_field() {
local gp_entry="$1"
local gp_field="$2"
local gp_opt="$3"
for key in $gp_field; do
# return on first successfully returned key
if [ -n "$gp_opt" ]; then
gopass show "$gp_opt" -f "$gp_entry" "$key" && break
else
gopass show -f "$gp_entry" "$key" && break
fi
done
}
# return username for argument passed
_gp_show_username() {
_gp_get_field "$1" "${GOPASS_USERNAME_FIELD:-"username user login"}"
}
_gp_clip_username() {
_gp_get_field "$1" "${GOPASS_USERNAME_FIELD:-"username user login"}" "-c"
}
# invoke the dotool to type inputs
_type() {
local tool
local toolmode
local key
tool="${BACKEND:-xdotool}"
toolmode="$1"
key="$2"
"$tool" "$toolmode" --delay "${AUTOENTRY_DELAY:-20}" "$key"
}
# automatically enter entry chain, set via AUTOENTRY_CHAIN
# transform special chain entries into valid dotool commands
autoentry() {
local selected
local autoentry_chain
selected="${1}"
autoentry_chain="${AUTOENTRY_CHAIN:-username :tab password}"
for part in $autoentry_chain; do
case "$part" in
":tab") _type key Tab ;;
":return") _type key Return ;;
":space") _type key space ;;
"username") _type type "$(_gp_show_username "$selected")" ;;
"password") _type type "$(_gp_show_password "$selected")" ;;
*) printf '%s' "$part" ;;
esac
done
}
main() {
# entry="$(list_passwords | _rofi -kb-accept-entry "" -kb-custom-1 "${key_autotype}" -kb-custom-2 "${key_usertype}" -kb-custom-3 "${key_passtype}" -kb-custom-4 "${key_actions}" -mesg "${key_autotype}: Autotype | ${key_usertype}: Type User | ${key_passtype}: Type Pass | ${key_actions}: More Actions")"
entry="$(_gp_list_passwords | _rofi -kb-accept-entry "" -kb-custom-1 "${KEY_AUTOENTRY:-Return}")"
exit_value=$?
exit_check "${exit_value}"
case "${exit_value}" in
# autoentry selected
"10")
autoentry "${entry}"
exit
;;
"11")
printf '%s' "$(gopass show "${entry}" username)" | _dotool type
exit
;;
"12")
printf '%s' "$(gopass show --password "${entry}")" | _dotool type
exit
;;
esac
do_menu "${entry}"
}
main