-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbw-fzf.sh
executable file
·348 lines (301 loc) · 9.58 KB
/
bw-fzf.sh
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/bash
trap 'exit_handler' INT TERM
trap 'cleanup' EXIT
ITEMS=
TIMEOUT=60s
SEARCH_TERM=
SESSION_FILE="$HOME/.bw-fzf-session"
TIMEOUT_PID=
TIMESTAMP_FILE="/tmp/bw-fzf-active.timestamp"
TEMP_ITEMS_FILE=
NO_PREVIEW=0
# Determine the clipboard command based on the session type
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
CLIP_COMMAND="wl-copy"
CLIP_ARGS=""
else
CLIP_COMMAND="xclip"
CLIP_ARGS="-selection clipboard"
fi
function exit_handler() {
trap - INT TERM
cleanup
exit 1
}
function cleanup() {
if [[ -n "$TIMEOUT_PID" ]]; then
kill "$TIMEOUT_PID" 2>/dev/null || true
wait "$TIMEOUT_PID" 2>/dev/null || true
fi
rm -f "$TIMESTAMP_FILE" 2>/dev/null
rm -f "$TEMP_ITEMS_FILE" 2>/dev/null
pkill -P $$ 2>/dev/null || true
wait 2>/dev/null || true
}
function monitor_inactivity() {
rm -f "$TIMESTAMP_FILE"
touch "$TIMESTAMP_FILE"
while true; do
sleep 1
if [[ -f "$TIMESTAMP_FILE" ]]; then
if [[ $(expr "$(date +%s)" - "$(stat -c %Y "$TIMESTAMP_FILE")") -ge ${TIMEOUT%s} ]]; then
echo -e "\nSession timed out after ${TIMEOUT}"
cleanup
exit 1
fi
fi
done &
TIMEOUT_PID=$!
}
function check_session() {
if [[ -n "${BW_SESSION}" ]]; then
if bw unlock --check --quiet 2>/dev/null; then
echo "Using existing session from environment"
return 0
fi
fi
if [[ -f "$SESSION_FILE" ]]; then
export BW_SESSION=$(cat "$SESSION_FILE")
if bw unlock --check --quiet 2>/dev/null; then
echo "Using existing session from file"
return 0
fi
fi
return 1
}
function handle_session_save() {
read -p "Do you want to save this session? [y/N] " save_session
local session
session=$(echo "$password" | bw unlock --raw 2>/dev/null)
if [[ -z "$session" ]]; then
echo "Could not unlock vault"
exit 1
fi
case $save_session in
[Yy]*)
echo "Saving session..."
echo "$session" >"$SESSION_FILE"
chmod 600 "$SESSION_FILE"
echo "Session saved to $SESSION_FILE"
;;
*)
echo "Session will not be saved"
;;
esac
export BW_SESSION="$session"
echo "Unlocked!"
}
function ask_password() {
local password
if check_session; then
return 0
fi
if command -v systemd-ask-password &>/dev/null; then
password=$(systemd-ask-password "Password: ")
else
read -s -p "Password [hidden]: " password
echo
fi
handle_session_save
}
function load_items() {
local search_term="$1"
echo "Loading items..."
if [[ -n "$search_term" ]]; then
if ! ITEMS=$(bw list items --search "$search_term" --session "$BW_SESSION" 2>/dev/null); then
echo "Could not load items or operation timed out"
exit 1
fi
else
if ! ITEMS=$(bw list items --session "$BW_SESSION" 2>/dev/null); then
echo "Could not load items or operation timed out"
exit 1
fi
fi
echo "Items loaded successfully."
}
export -f monitor_inactivity
function bw_list() {
local prompt
TEMP_ITEMS_FILE=$(mktemp)
echo "$ITEMS" >"$TEMP_ITEMS_FILE"
chmod 600 "$TEMP_ITEMS_FILE"
if [ -n "$SEARCH_TERM" ]; then
prompt="bw-fzf (filter: $SEARCH_TERM) ➜ "
else
prompt="bw-fzf ➜ "
fi
monitor_inactivity
# Define help text as a variable
local HELP_TEXT="
Keyboard Shortcuts:
------------------
ctrl-h Show this help window
ctrl-u Copy username to clipboard
ctrl-p Copy password to clipboard
ctrl-o Copy TOTP code to clipboard
Navigation:
----------
↑/↓ Select item
Enter Select item
Page Up/Down Scroll preview window
/ Filter items
ESC Clear filter/Exit
Tips:
-----
• Type to filter entries
• All copied items go to system clipboard
• Session times out after ${TIMEOUT} of inactivity
"
local fzf_args=(
--cycle
--inline-info
--ansi
--no-mouse
--layout=reverse
--prompt="$prompt"
--bind="change:execute-silent(touch $TIMESTAMP_FILE)"
--bind="focus:execute-silent(touch $TIMESTAMP_FILE)"
--bind="ctrl-u:execute(item_id=\$(echo {} | sed -n 's/.*(\(.*\)).*/\1/p'); username=\$(jq -r --arg id \"\$item_id\" '.[] | select(.id == \$id) | .login.username' \"$TEMP_ITEMS_FILE\"); echo -n \"\$username\" | $CLIP_COMMAND $CLIP_ARGS)+execute-silent(touch $TIMESTAMP_FILE)"
--bind="ctrl-p:execute(item_id=\$(echo {} | sed -n 's/.*(\(.*\)).*/\1/p'); password=\$(jq -r --arg id \"\$item_id\" '.[] | select(.id == \$id) | .login.password' \"$TEMP_ITEMS_FILE\"); echo -n \"\$password\" | $CLIP_COMMAND $CLIP_ARGS)+execute-silent(touch $TIMESTAMP_FILE)"
--bind="ctrl-o:execute(item_id=\$(echo {} | sed -n 's/.*(\(.*\)).*/\1/p'); totp_secret=\$(jq -r --arg id \"\$item_id\" '.[] | select(.id == \$id) | .login.totp' \"$TEMP_ITEMS_FILE\"); if [[ \"\$totp_secret\" != \"null\" ]]; then if command -v oathtool &> /dev/null; then totp=\$(oathtool --totp -b \"\$totp_secret\"); else totp=\$(bw get totp \"\$item_id\"); fi; echo -n \"\$totp\" | $CLIP_COMMAND $CLIP_ARGS; else echo \"No TOTP available for this item\"; fi)+execute-silent(touch $TIMESTAMP_FILE)"
)
# If preview is disabled, set a blank preview and hide the preview window.
# Otherwise, use the standard item preview.
if [ "$NO_PREVIEW" -eq 1 ]; then
fzf_args+=(
--header="Press ctrl-h for help"
--preview=""
--preview-window=hidden
)
else
fzf_args+=(
--header="Press ctrl-h for help"
--preview-window='right:50%'
--preview='
if [[ "{}" == "HELP" ]]; then
echo "'"$HELP_TEXT"'"
else
item_id=$(echo {} | sed -n "s/.*(\(.*\)).*/\1/p")
touch '"$TIMESTAMP_FILE"'
item=$(jq -r --arg id "$item_id" ".[] | select(.id == \$id)" "'"$TEMP_ITEMS_FILE"'")
username=$(jq -r ".login.username" <<< $item)
password=$(jq -r ".login.password" <<< $item)
notes=$(jq -r ".notes // empty" <<< $item)
creationDate=$(jq -r ".creationDate" <<< $item)
revisionDate=$(jq -r ".revisionDate" <<< $item)
uris=$(jq -r ".login.uris[]?.uri // empty" <<< "$item" | sed "s/^/ • /")
totp_available=$(jq -r ".login.totp != null" <<< $item)
if [ "$totp_available" = "true" ]; then
totp_secret=$(jq -r ".login.totp" <<< $item)
if command -v oathtool &> /dev/null; then
totp=$(oathtool --totp -b "$totp_secret")
else
totp=$(bw get totp "$item_id")
fi
else
totp="No TOTP available"
fi
bold=$(tput bold)
normal=$(tput sgr0)
cyan=$(tput setaf 6)
red=$(tput setaf 1)
underline=$(tput smul)
nounderline=$(tput rmul)
padding=" "
printf "\n${padding}${bold}${underline}Login Details${nounderline}${normal}\n\n"
printf "${padding}${bold}${cyan}Username${normal}\n${padding} %s\n\n" "$username"
printf "${padding}${bold}${cyan}Password${normal}\n${padding} ${red}%s${normal}\n\n" "$password"
printf "${padding}${bold}${cyan}TOTP${normal}\n${padding} %s\n\n" "$totp"
printf "${padding}${bold}${cyan}Notes${normal}\n${padding} %s\n\n" "$notes"
printf "${padding}${bold}${cyan}URIs${normal}\n%s\n\n" "$uris"
printf "${padding}${bold}${cyan}Created${normal}\n${padding} %s\n" "$creationDate"
printf "${padding}${bold}${cyan}Modified${normal}\n${padding} %s\n" "$revisionDate"
fi
'
)
fi
fzf_args+=(--bind="ctrl-h:preview(echo \"$HELP_TEXT\")")
jq -r '.[] | "\(.name) (\(.id)) \(.login.username)"' "$TEMP_ITEMS_FILE" |
FZF_PREVIEW_FILE="$TEMP_ITEMS_FILE" fzf "${fzf_args[@]}"
}
function install_script() {
local install_path="/usr/local/bin/bw-fzf"
if [[ "$EUID" -ne 0 ]]; then
echo "This script must be run as root. Try using sudo."
exit 1
fi
if cp "$0" "$install_path" && chmod +x "$install_path"; then
echo "Successfully installed to $install_path"
else
echo "Failed to install. Check your permissions."
exit 1
fi
}
function help() {
echo "bw-fzf - A Bitwarden cli wrapper with fzf"
echo "Project url: https://github.com/radityaharya/bw-fzf"
echo "Author: Raditya Harya <contact@radityaharya.com>"
echo
echo "Usage: bw-fzf [OPTIONS]"
echo
echo "Options:"
echo " -i, --install Install the script to /usr/local/bin"
echo " -h, --help Show this help message"
echo " -t, --timeout Set custom timeout (e.g., 30s, 1m). Default is 1 minute."
echo " -s, --search Search term to filter items"
echo " -n, --no-preview Disable preview window"
echo
}
function main() {
while [[ "$1" != "" ]]; do
case $1 in
-i | --install)
install_script
exit 0
;;
-h | --help)
help
exit 0
;;
-t | --timeout)
shift
TIMEOUT="$1"
;;
-s | --search)
shift
SEARCH_TERM="$1"
;;
-n | --no-preview)
NO_PREVIEW=1
;;
*)
echo "Invalid option: $1"
help
exit 1
;;
esac
shift
done
if ! command -v bw >/dev/null; then
echo "Bitwarden cli is missing. Exiting"
exit 1
fi
if ! command -v jq >/dev/null; then
echo "jq is missing. Exiting"
exit 1
fi
if ! command -v fzf >/dev/null; then
echo "fzf is missing. Exiting"
exit 1
fi
# Check for clipboard command availability
if ! command -v $CLIP_COMMAND >/dev/null; then
echo "WARNING: $CLIP_COMMAND is missing. Copy functionality will be unavailable"
fi
monitor_inactivity
ask_password
load_items "$SEARCH_TERM"
bw_list
}
main "$@"