-
Notifications
You must be signed in to change notification settings - Fork 0
/
passmenu
executable file
·204 lines (165 loc) · 4.43 KB
/
passmenu
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
shopt -s nullglob globstar
# TODO)):
# - [X] remove `fzf` deps
# - [ ] remove hex-colors, use names: red, blue...
PROG="${0##*/}"
DEPS=(notify-send xdotool pass)
PASS=$(which pass)
# vars
# shellcheck disable=SC2034
EDITOR="nvim -u NONE"
TERMINAL=${TERMINAL:-st}
PASSWORD_STORE_CLIP_TIME=${PASSWORD_STORE_CLIP_TIME:-15}
# menu
MENU="dmenu"
declare -a MENU_ARGS=(-l 10)
# colors
BLUE="#458588"
MAGENTA="#D3869B"
RED="#FB4934"
YELLOW="#D79921"
function usage {
cat <<-EOF
Usage: $PROG [options]
options:
-m, --menu Select password with menu
-e, --edit Edit selected password
-l, --list List all passwords
-o, --otp Copy one-time-password
-t, --type Password is typed into the terminal
-h, --help Show this help message and exit
EOF
exit 0
}
function log_err {
printf "%s: %s\n" "$PROG" "$*" >&2
exit 1
}
function send_notification {
local mesg="$1"
local cmd=notify-send
declare -a args=(-i "dialog-password-symbolic")
args+=(-h "string:x-dunst-stack-tag:pass")
args+=(-r 888)
"$cmd" "${args[@]}" "$PROG" "$mesg"
}
function get_password_paths {
local prefix=${PASSWORD_STORE_DIR-~/.password-store}
local pwd_files
pwd_files=("$prefix"/**/*.gpg)
pwd_files=("${pwd_files[@]#"$prefix"/}")
pwd_files=("${pwd_files[@]%.gpg}")
printf '%s\n' "${pwd_files[@]}"
}
function get_pass_with_menu {
local prompt passwords
declare -a passwords=()
prompt="${1:-"Pass>"}"
MENU_ARGS+=(-p "$prompt")
shift
if [[ -n "$*" && "$MENU" == "dmenu" ]]; then
# shellcheck disable=SC2128
MENU_ARGS+=("$@")
fi
mapfile -t passwords < <(get_password_paths)
password=$(printf "%s\n" "${passwords[@]}" | "$MENU" "${MENU_ARGS[@]}")
echo "$password"
}
function edit_pwd {
shift
local pwd="$1"
if [[ -z "$pwd" ]]; then
pwd=$(get_pass_with_menu "PassEdit> " "-sb" "$RED")
fi
[[ -z $pwd ]] && exit
$TERMINAL -e "$PASS" edit "$pwd"
exit 0
}
function menu {
local icon=""
local password mesg
password=$(get_pass_with_menu "${icon} Password> " "-sb" "$BLUE")
[[ -z "${password}" ]] && exit
"$PASS" show -c "$password" 2>/dev/null
retcode=$?
if [[ "$retcode" -ne 0 ]]; then
send_notification "No password found for <b>${password}</b>"
log_err "no password found for '${password}'"
fi
mesg="Copied $icon <b>${password}</b> to clipboard.\n\n"
mesg+="<i>Will clear in <b>${PASSWORD_STORE_CLIP_TIME}</b> seconds.</i>"
send_notification "$mesg"
exit 0
}
function list_path {
local pwd_path
pwd_path=$(get_pass_with_menu "PassList> " "-sb" "$YELLOW")
[[ -z "${pwd_path}" ]] && exit
echo "$pwd_path"
exit 0
}
function otp_code {
local password mesg
local icon=""
password=$(get_pass_with_menu "${icon} PassOTP> " "-sb" "$YELLOW")
[[ -z "${password}" ]] && exit
"$PASS" otp -c "${password}"
retcode=$?
if [[ "$retcode" -gt 0 ]]; then
send_notification "No OTP found for <b>${password}</b>"
log_err "no OTP found for ${password}"
fi
mesg="Copied $icon <b>OTP</b> code for <b>${password}</b> to clipboard.\n\n"
mesg+="<i>Will clear in <b>${PASSWORD_STORE_CLIP_TIME}</b> seconds.</i>"
send_notification "$mesg"
exit 0
}
function type_pwd {
shift
local password="$1"
if [[ -z "${password}" ]]; then
password=$(get_pass_with_menu "PassType>" "-sb" "$MAGENTA")
fi
[[ -z "${password}" ]] && exit
"$PASS" show "$password" | {
IFS= read -r pass
printf %s "$pass"
} | $XDOTOOL
exit 0
}
if [[ -n $WAYLAND_DISPLAY ]]; then
MENU="dmenu-wl"
XDOTOOL="ydotool type --file -"
elif [[ -n $DISPLAY ]]; then
XDOTOOL="xdotool type --clearmodifiers --file -"
else
log_err "no x11/wayland display detected"
fi
function check_pass {
local retcode
"$PASS" >/dev/null
retcode="$?"
return "$retcode"
}
function main {
for cmd in "${DEPS[@]}"; do
if ! command -v "$cmd" >/dev/null; then
log_err "'$cmd' not found"
fi
done
if ! check_pass; then
err_msg="'pass' returned a <b>non-zero</b> code."
send_notification "$err_msg"
log_err "$err_msg"
fi
case "$1" in
-m | --menu) menu ;;
-e | --edit) edit_pwd "$@" ;;
-l | --list) list_path ;;
-o | --otp) otp_code ;;
-t | --type) type_pwd "$@" ;;
*) usage ;;
esac
}
main "$@"