#!/usr/bin/env bash # # Original inspiration from https://github.com/carnager/rofi-pass/ # DEFAULT OPTIONS # typing tool used, xdotool compatible syntax should work (e.g. ydotool) # BACKEND=xdotool # the complete typing chain autoentry uses # possible fields: # :tab | :space | :return | 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_AUTOFILL="Return" # Automatically enter username only # KEY_FILL_USER="Alt+u" # Add username to clipboard # KEY_CLIP_USER="Ctrl+Alt+u" # Automatically enter password only # KEY_FILL_PASS="Alt+p" # Add password to clipboard # KEY_CLIP_PASS="Ctrl+Alt+p" # For the individual gopass entry: # Automatically fill selected field # KEY_ENTRYMENU_FILL="Return" # Add field to clipboard # KEY_ENTRYMENU_CLIP="Alt+Return" # Return to main menu # KEY_ENTRYMENU_QUIT="Alt+BackSpace" # 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 "$@" -p "Entry" } # 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 list_passwords() { gopass list --flat } # return password for argument passed show_password() { gopass show -f --password "$1" } # send password to clipboard 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 clip="$3" for key in $gp_field; do # return on first successfully returned key if [ -n "$clip" ]; then gopass show -c "$gp_entry" "$key" && break else gopass show -f "$gp_entry" "$key" && break fi done } # return username for argument passed show_username() { _gp_get_field "$1" "${GOPASS_USERNAME_FIELD:-"username user login"}" } clip_username() { _gp_get_field "$1" "${GOPASS_USERNAME_FIELD:-"username user login"}" "-c" } show_field() { _gp_get_field "$1" "$2" } clip_field() { _gp_get_field "$1" "$2" "-c" } list_fields() { gopass show -f "$1" | tail -n+2 } # invoke the dotool to type inputs _type() { local tool="${BACKEND:-xdotool}" local toolmode="$1" local key="$2" "$tool" "$toolmode" --delay "${AUTOENTRY_DELAY:-30}" "$key" } # automatically enter entry chain, set via AUTOENTRY_CHAIN # transform special chain entries into valid dotool commands autofill() { local selected="${1}" local autoentry_chain="${2}" for part in $autoentry_chain; do case "$part" in ":tab") _type key Tab ;; ":return") _type key Return ;; ":space") _type key space ;; "username") _type type "$(show_username "$selected")" ;; "password") _type type "$(show_password "$selected")" ;; ":direct") _type type "$selected" ;; *) printf '%s' "$selected" ;; esac done } # opens a menu for the specified gopass entry, containing its individual fields entrymenu() { local entry="$1" local k_entrymenu_fill="${KEY_ENTRYMENU_FILL:-Return}" local k_entrymenu_clip="${KEY_ENTRYMENU_CLIP:-Alt+Return}" local k_entrymenu_quit="${KEY_ENTRYMENU_QUIT:-Alt+BackSpace}" local pass_obfuscation="(hidden)" local field field=$( printf "password: %s\n%s" "$pass_obfuscation" "$(list_fields "$entry")" | _rofi \ -kb-accept-entry "" \ -kb-custom-1 "$k_entrymenu_fill" \ -kb-custom-2 "$k_entrymenu_clip" \ -kb-custom-3 "$k_entrymenu_quit" \ -mesg " ᐊ $k_entrymenu_quit ᐊ | $k_entrymenu_fill: fill selection | $k_entrymenu_clip: clip selection |" ) exit_value=$? exit_check "$exit_value" # get field name field=${field%%:*} case "$exit_value" in "10") if [ "$field" = "password" ]; then autofill "$entry" "password" else autofill "$(show_field "$entry" "$field")" ":direct" fi exit 0 ;; "11") if [ "$field" = "password" ]; then clip_password "$entry" else clip_field "$entry" "$field" fi exit 0 ;; "12") main ;; esac } main() { local autoentry_chain="${AUTOENTRY_CHAIN:-username :tab password}" local k_autofill="${KEY_AUTOFILL:-Return}" local k_fill_user="${KEY_FILL_USER:-Alt+u}" local k_clip_user="${KEY_CLIP_USER:-Ctrl+Alt+u}" local k_fill_pass="${KEY_FILL_PASS:-Alt+p}" local k_clip_pass="${KEY_CLIP_PASS:-Ctrl+Alt+p}" local k_submenu="${KEY_CLIP_PASS:-Alt+Return}" entry="$( list_passwords | _rofi -kb-accept-entry "" \ -kb-custom-1 "$k_autofill" \ -kb-custom-2 "$k_clip_user" \ -kb-custom-3 "$k_clip_pass" \ -kb-custom-4 "$k_fill_user" \ -kb-custom-5 "$k_fill_pass" \ -kb-custom-6 "$k_submenu" \ -mesg "| $k_autofill: fill credentials | $k_submenu: open entry | $k_fill_user: fill username | $k_fill_pass: fill password | $k_clip_user: clip username | $k_clip_pass: clip password |" )" exit_value=$? exit_check "$exit_value" case "$exit_value" in "10") autofill "$entry" "$autoentry_chain" exit 0 ;; "11") clip_username "$entry" exit 0 ;; "12") clip_password "$entry" exit ;; "13") autofill "$entry" "username" exit ;; "14") autofill "$entry" "password" exit ;; "15") entrymenu "$entry" exit ;; esac } main