1
0
mirror of https://github.com/frebib/dotfiles.git synced 2024-06-14 12:57:23 +00:00
dotfiles/scripts/otp
Joe Groocock 2cc93a1649
scripts/otp: Refactor & ensure notifications are dismissed
Modernise the script and make it more sane.
Ensure lingering notifications are removed when the script exits to
prevent collecting many notifications if the script is called in a loop.

Signed-off-by: Joe Groocock <me@frebib.net>
2023-03-29 12:43:29 +00:00

46 lines
847 B
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
NOTIF_PERSIST=30000
NOTIF_INFORM=5000
stdout=
caller=
notif=
while getopts "on:" o; do
case "${o}" in
o)
stdout=1;;
n)
caller="${OPTARG}"
;;
*)
exec ykman oath accounts list
;;
esac
done
shift $((OPTIND-1))
if [ $# -lt 1 ]; then
exec ykman oath accounts list
fi
if [ -n "$caller" ]; then
notif="$(notify-send -p -t $NOTIF_PERSIST "Touch your Yubikey" "$caller is requesting otp for $1")"
trap 'notify-send -r $notif -t 1 "Touch your Yubikey" "$caller is requesting otp for $1"' EXIT
fi
code=$(ykman oath accounts code -s "$1")
if [ -z "$stdout" ]; then
echo "$code" | tee >(xclip -i)
xdotool type "$code"
else
echo "$code"
fi
if [ -n "$code" ] && [ -n "$notif" ]; then
trap : EXIT
notify-send -r "$notif" -t $NOTIF_INFORM "Yubikey Touched" "otp generated for $caller"
fi