dotfiles/.config/rofi/rofi-ykman.sh

73 lines
1.3 KiB
Bash
Raw Normal View History

2023-08-27 15:17:35 +02:00
#!/usr/bin/env bash
# rofi-ykman
# YubiKey OATH utility
#2019 nukeop
print_usage() {
echo "Usage: rofi-ykman [command]"
echo
echo "Commands:"
echo " clipboard Copy the code to the clipboard. (default)"
echo " type Type the code."
echo
}
clipboard () {
if [ $XDG_SESSION_TYPE == "wayland" ]
then
wl-copy
else
xsel --clipboard
fi
}
type () {
if [ $XDG_SESSION_TYPE == "wayland" ]
then
wtype -
else
xargs xdotool type
fi
}
# shortcuts
type="Alt+1"
clipboard="Alt+2"
main() {
accounts=$(ykman oath accounts list)
shortcuts=(
-kb-custom-1 "$type"
-kb-custom-2 "$clipboard"
)
prompt="YubiKey OATH"
account=$(echo "${accounts/, TOTP/\n}" | awk '{ print $0 "\0icon\x1fyubioath"; }' | rofi -dmenu -i -p "$prompt" ${shortcuts[@]})
case $? in
1) exit ;;
10) command=type ;;
11) command=clipboard ;;
*) command=$default_command ;;
esac
code=$(ykman oath accounts code "$account")
IFS=', ' read -r -a code <<< "$code"
case $command in
2023-12-05 15:03:14 +01:00
clipboard) echo -n "${code[-1]}" | wl-copy;;
2023-08-27 15:17:35 +02:00
type) echo -n "${code[-1]}" | type;;
esac
}
2024-05-02 13:16:29 +02:00
default_command="${1:-clipboard}"
2023-08-27 15:17:35 +02:00
case $default_command in
clipboard|type) ;;
*) print_usage; exit 1;;
esac
main