-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmytotp.rc
78 lines (67 loc) · 2.15 KB
/
mytotp.rc
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
71
72
73
74
75
76
77
78
# this code is a fork of https://github.com/ThingCrimson/mytotp
# put this file in path and source it in .bashrc or .zshrc
# add next line to .bashrc or .zshrc
# source mytotp.sh
# create a directory for the keys ~/.config/mytotp
# usage, mannually creating SERVID.gpg file:
# Put TOTP key for service SERVID to GPG file crypted for '\''My TOTP'\''
# gpg -e -r '\''My TOTP'\'' > ~/.config/mytotp/SERVID.gpg
# usage, getting TOTP for service SERVID:
# mytotp SERVID
# usage, adding new SERVID:
# mytotpadd SERVID
# usage, listing all SERVIDs:
# mytotplist
function mytotp() {
if ! command -v oathtool &> /dev/null
then
echo "oathtool could not be found"
echo "Please install it with: brew install oath-toolkit"
return 1
fi
KEYDIR=~/.config/mytotp
KEYEXT=.gpg
SERVID=$1
if [ -z "${SERVID}" ] ; then
echo -e "Usage: $0 SERVID\n\tSERVID is a service ID, abbreviated, w/o ext:"
find ${KEYDIR}/*${KEYEXT} | sed -e 's/\/home.*\// /; s/\.gpg//'
return 2
fi
if [ ! -f "${KEYDIR}/${SERVID}${KEYEXT}" ] ; then
echo "No key for ${KEYDIR}/${SERVID}${KEYEXT}"
return 1
fi
SKEY=$(gpg -d --quiet "${KEYDIR}/${SERVID}${KEYEXT}")
NOWS=$(date +'%S')
WAIT=$((60 - NOWS))
if [ ${WAIT} -gt 30 ]; then
WAIT=$((WAIT - 30))
fi
echo -n "Seconds :${NOWS} (we need to wait ${WAIT}) ... "
sleep ${WAIT}
TOTP=$(echo "${SKEY}" | oathtool -b --totp - )
echo "${TOTP}"
SKEY="none"
return 0
}
# add new SERVID to GPG file in ~/.config/mytotp/SERVID.gpg
# paste the key in the prompt and press enter, then $SERVID.gpg will be created
function mytotpadd() {
# if no $1 supplied, exit
if [ -z "$1" ] ; then
echo -e "Usage: $0 SERVID\n\tSERVID is a service ID, abbreviated, w/o ext:"
return 1
fi
KEYDIR=~/.config/mytotp
KEYEXT=.gpg
SERVID=$1
# print user instruction about press control-D to stop gpg"
echo "Paste the key in the prompt, press enter, and then press control-D to stop gpg"
gpg -e -r "My TOTP" >~/.config/mytotp/$SERVID.gpg
}
# function to list all SERVIDs in ~/.config/mytotp
function mytotplist() {
KEYDIR=~/.config/mytotp
KEYEXT=.gpg
find ${KEYDIR}/*${KEYEXT} | sed -e 's/\/home.*\// /; s/\.gpg//'
}