summarylogtreecommitdiffstats
path: root/pass-autotype
blob: a9ac698d94f3edf328d299cb9530e6153704c01e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash

# pass-autotype This script is based on the fzf-pass script by ReekyMarko
# https://git.reekynet.com/ReekyMarko/fzf-pass/ but using fuzzel instead of fzf,
# which works on wayland without additional configuration. It uses wtype to
# type the information.

PROGNAME=$0

if ! command -v wtype > /dev/null; then
    echo "Please install wtype."
    exit 1
fi
if ! command -v fuzzel > /dev/null; then
    echo "Please install fuzzel."
    exit 1
fi

function type() {
    $(command -v wtype) -s 100 "$@"
}
function menu() {
    $(command -v fuzzel) --dmenu "$@"
}
function notify() {
    $(command -v notify-send) $PROGNAME "$@"
}

cache_file=${XDG_CACHE_HOME:-$HOME/.cache}/pass-autotype
[[ -f "$cache_file" ]] && last_used="$(tail -n 1 $cache_file)\n"

password_store_dir=${PASSWORD_STORE_DIR:-$HOME/.password-store}
pass_files=$(find "$password_store_dir" -type f -name '*.gpg' |
    sed -e "s|$password_store_dir/||g" -e 's|.gpg$||g')

selected_file=$(echo -e "$last_used$pass_files" | menu)

if [[ -z $selected_file ]]; then
    exit 0
fi

echo $selected_file > $cache_file

file_data="$(PASSWORD_STORE_DIR=$password_store_dir pass "$selected_file")"

declare -a KEYS
declare -A OPTS
KEYS+=("Password")
OPTS["Password"]="$(echo "$file_data" | head -n 1)"
file_data="$(echo "$file_data" | tail -n +2)"

# Type the password if it's the only line in the file
if [[ -z $file_data ]]; then
    type "${OPTS["Password"]}"
    notify "Inserted password for $selected_file"
    exit 0
fi

# Iterate through the file and add all the options to the hash table OPTS.  If
# user|username exists, add also the Autotype option at the begining of the KEYS
# array, to use it as default option.
pattern_option="^([^:]+):\s*(.+)"
pattern_user="^[Uu]ser(name)?"
while IFS= read -r line; do
    if [[ "$line" =~ $pattern_option ]]; then
        key="${BASH_REMATCH[1]^}"
        value="${BASH_REMATCH[2]}"
        if [[ "$key" =~ $pattern_user ]]; then
            key="Username"
            KEYS=("$key" "${KEYS[@]}")
            KEYS=("Autotype" "${KEYS[@]}")
        else
            KEYS+=("$key")
        fi
        OPTS["$key"]="$value"
    fi
done <<<"$file_data"

RESP=$(
    IFS=$'\n'
    echo "${KEYS[*]}" | menu
)

if [[ -z $RESP ]]; then
    exit 0
fi

case "$RESP" in
Autotype)
    type "${OPTS["Username"]}" && type -k Tab && type "${OPTS["Password"]}" && type -k "Enter"
    notify "Inserted username and password for $selected_file"
    ;;
Otpauth)
    type "$(pass otp "$selected_file")"
    notify "Inserted otp for $selected_file"
    ;;
*)
    type "${OPTS["$RESP"]}"
    notify "Inserted $RESP for $selected_file"
    ;;
esac