aboutsummaryrefslogtreecommitdiff
path: root/base/otp
blob: e8c11f933c0456b23a3867da5a7cc52751718e9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash

shopt -s nullglob globstar

PREFIX=${PASSWORD_STORE_DIR-~/.password-store}
PREFIX="$PREFIX/otp"
if [[ ! -d "$PREFIX" ]]; then
  echo "Pass OTP does not exist" >&1
  exit 1
fi

if [[ "$1" ]]; then
  if [[ ! -f "$PREFIX/$1.gpg" ]]; then
    echo "otp/$1 does not exist" >&1
    exit 1
  fi
  SITE="$1"
else
  OTPFILES=( "$PREFIX"/**/*.gpg )
  OTPFILES=( "${OTPFILES[@]#"$PREFIX"/}" )
  OTPFILES=( "${OTPFILES[@]%.gpg}" )
  SITE="$(printf '%s\n' "${OTPFILES[@]}" |
    fzf --height 8 --reverse --no-multi --prompt="Select Site: ")"
  if [[ ! "$SITE" ]]; then exit; fi
fi

URL="$(pass "otp/$SITE" 2>/dev/null | head -n1)"
if [[ "$?" != "0" ]]; then echo "Authentication failed" >&2; exit 1; fi
PERIOD="$(echo "$URL" | sed 's/^.*period=\([0-9]\+\).*$/\1/')"
#BLOCKS=(" " "▏" "▎" "▍" "▌" "▋" "▊" "▉")
BLOCKS=(" " "▎" "▌" "▊")
TOTAL=$(( $PERIOD / 4 ))
echo

function finish {
  echo -ne "\r"
  tput el
  tput cuu1
  tput cnorm
  exit 0
}

trap finish SIGINT SIGTERM SIGQUIT
tput civis

while :; do
  CODE="$(pass otp "otp/$SITE" 2>/dev/null)"
  WAIT=$(( $PERIOD - ( $(date +%s) % $PERIOD ) ))
  FULL=$(( $WAIT / 4 ))
  PARTIAL=$(( $WAIT % 4 ))
  BLANK=$(( $TOTAL - $FULL))

  tput bold;     tput setab 8;  echo -ne "\r "
  tput setaf 8;  tput setab 18; echo -n " "
  tput setaf 6;                 echo -n "$SITE"
  tput setaf 8;                 echo -n "  "
  tput sgr0
  tput setaf 16; tput setab 18; echo -n "$CODE"
  tput setaf 8;                 echo -n " "
  for i in `seq $FULL`; do      echo -n "█"; done
  echo -n "${BLOCKS[$PARTIAL]}"
  for i in `seq $BLANK`; do     echo -n " "; done
  tput sgr0;     tput setaf 18; echo -n ""
  tput sgr0

  #sleep 0.1
  read -rsn1 -t.1 INP
  if [[ "$INP" = "q" || "$INP" = "Q" ||
    "$INP" = "$(echo -ne "\e")" ]]; then finish; fi
done