Skip to content

Instantly share code, notes, and snippets.

@VTimofeenko
Created December 17, 2020 03:51
Show Gist options
  • Save VTimofeenko/ee6384843e35fafe6458b3a5e580b8a6 to your computer and use it in GitHub Desktop.
Save VTimofeenko/ee6384843e35fafe6458b3a5e580b8a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# As of Dec 16 2020 requires nitrocli built from otp cache branch:
# https://github.com/d-e-s-o/nitrocli/tree/topic/otp-cache
# Setup:
# Set the exported variables below
# Set NITROCLI_OTP_PATH to the location of nitrocli-otp-cache binary
export NITROCLI_BINARY="/usr/bin/nitrocli"
export NITROCLI_MODEL="storage"
export NITROCLI_VERBOSITY='10'
NITROCLI_OTP_PATH="${HOME}/.cargo/bin/nitrocli-otp-cache"
set -Eeuo pipefail
trap reporterror ERR
reporterror() {
dunstify --appname='TOTP' "Encountered an error"
}
KEY_NAME=$(${NITROCLI_OTP_PATH} list | awk '{print $2}' | tail -n+2 | dmenu -l 10 -p "TOTP:")
# check for user cancellation
[[ $KEY_NAME = '' ]] && exit 255
NOTIFICATION_ID=0
NOTIFICATION_ACTION=''
function get_key() {
TOTP_KEY=$($NITROCLI_OTP_PATH get "${KEY_NAME}")
echo -n "$TOTP_KEY" | xclip -selection clipboard
EPOCH=$(date +%s)
TIME_REMAINING=$(( 30 - EPOCH % 30 ))
NOTIFICATION_OUTPUT=$(dunstify -u low \
-A again,"Copy key again" \
--replace=${NOTIFICATION_ID} \
--printid \
--appname='TOTP' \
"${KEY_NAME} OTP copied"\
"Time remaining: $TIME_REMAINING seconds")
NOTIFICATION_ID=$(echo "$NOTIFICATION_OUTPUT" | head -n1)
NOTIFICATION_ACTION=$(echo "$NOTIFICATION_OUTPUT" | tail -n1)
}
get_key
while [[ $NOTIFICATION_ACTION = "again" ]]; do
get_key
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment