1
1
Fork 0
nixos/options/custom/scripts/bwm.sh
Myned 53c8575116
git: migrate to forgejo
Squashes 1,331 commits

Signed-off-by: Myned <dev@bjork.tech>
2024-09-12 19:41:21 -05:00

50 lines
1.3 KiB
Bash

#! /usr/bin/env bash
# Bitwarden dmenu
# TODO: Clear clipboard after timer
SESSIONFILE="$XDG_RUNTIME_DIR/bwm"
# Use current session if exists
if test -f "$SESSIONFILE"; then
BW_SESSION="$(cat "$SESSIONFILE")" && export BW_SESSION
fi
# Unlock vault if needed
if ! bw unlock --check; then
# Prompt for obfuscated password
password="$(wofi --dmenu --password --lines 3 <<< 'Vault locked. Enter master password.')"
# Save session to /tmp
BW_SESSION="$(bw unlock "$password" --raw)" && export BW_SESSION
touch "$SESSIONFILE"
chmod u=rw,g=,o= "$SESSIONFILE"
echo "$BW_SESSION" > "$SESSIONFILE"
fi
# Prompt for search term
search="$(wofi --dmenu --lines 3 <<< 'Enter item to search.')"
# Gather and parse results
items="$(bw list items --search "$search")"
usernames="$(jq -r '.[].login.username' <<< "$items")"
passwords="$(jq -r '.[].login.password' <<< "$items")"
# Prompt to select username
username="$(wofi --dmenu <<< "$usernames")"
# Find matching password line number
count=1
while IFS= read -r username; do
if [[ "$username" == "$username" ]]; then
break
else
((count++))
fi
done <<< "$usernames"
# Copy line to clipboard
tail --lines "+$count" <<< "$passwords" | head -1 | tee >(xclip -rmlastnl -selection clipboard &> /dev/null) >(wl-copy --trim-newline &> /dev/null)
notify-send '> bwm' 'Copied' --urgency low