mirror of
https://github.com/frebib/dotfiles.git
synced 2024-06-14 12:57:23 +00:00
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>
This commit is contained in:
parent
787008fdb7
commit
2cc93a1649
31
scripts/otp
31
scripts/otp
@ -1,4 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
NOTIF_PERSIST=30000
|
||||
NOTIF_INFORM=5000
|
||||
|
||||
stdout=
|
||||
caller=
|
||||
notif=
|
||||
|
||||
while getopts "on:" o; do
|
||||
case "${o}" in
|
||||
@ -19,24 +27,19 @@ if [ $# -lt 1 ]; then
|
||||
fi
|
||||
|
||||
if [ -n "$caller" ]; then
|
||||
notif="$(notify-send -p "Touch your Yubikey" "$caller is requesting otp for $1")"
|
||||
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
|
||||
|
||||
ykman oath accounts code -s "$1" 2>&1 |
|
||||
while read -r x; do
|
||||
if grep -Pqw '[0-9]{6}' <<< "$x"; then
|
||||
code=$(ykman oath accounts code -s "$1")
|
||||
if [ -z "$stdout" ]; then
|
||||
echo "$x" | tee >(xclip -i)
|
||||
xdotool type "$x"
|
||||
echo "$code" | tee >(xclip -i)
|
||||
xdotool type "$code"
|
||||
else
|
||||
echo "$x"
|
||||
echo "$code"
|
||||
fi
|
||||
fi
|
||||
if [ -z "$stdout" ]; then
|
||||
notify-send "$x";
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -n "$notif" ]; then
|
||||
notify-send -r "$notif" -t 1000 "Touch your Yubikey" "otp generated for $caller"
|
||||
if [ -n "$code" ] && [ -n "$notif" ]; then
|
||||
trap : EXIT
|
||||
notify-send -r "$notif" -t $NOTIF_INFORM "Yubikey Touched" "otp generated for $caller"
|
||||
fi
|
||||
|
Loading…
Reference in New Issue
Block a user