forked from ThingCrimson/mytotp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytotp.rc
186 lines (167 loc) · 6.25 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# this code is a fork of https://github.com/ThingCrimson/mytotp
# there are prerequisites to use this code:
# 1. gpg
# 2. oathtool
# 3. xclip (optional, for Linux only) or pbpaste (it is included in MacOS)
# 4. that for MacOS only bash 5.2.26, if you have older version that is default case for MacOS, please upgrade it with brew or use previous version of script,
# see this repo releases for MacOS standard bash
# put this file in ~ path and source it in .bashrc or .zshrc
# if [ -f $HOME/mytotp.rc ]; then
# . $HOME/mytotp.rc
# fi
# or add next line to .bashrc or .zshrc
# source /path/to/somewhere/mytotp.rc
# 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
KEYDIR=~/.config/mytotp
KEYEXT=.gpg
if [ "$(uname)" == "Darwin" ]; then
PASTECOMMAND="pbpaste"
# make check of bash version, if it is even to 3.2.57 then show warning and instructions how to upgrade shell to bash 5.2.26 with brew, then exit with code 1
if [ "$(echo $BASH_VERSION | awk -F. '{print $1}')" -eq 3 ] && [ "$(echo $BASH_VERSION | awk -F. '{print $2}')" -eq 2 ] && [ "$(echo $BASH_VERSION | awk -F. '{print $3}')" -eq 57 ]; then
echo "Your MacOS bash version is too old, please upgrade it to 5.2.26 with brew, following these steps"
echo "brew install bash"
echo "sudo ln -s /opt/homebrew/bin/bash /usr/local/bin/bash "
echo "sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'"
echo "optional step to change default zsh to bash, not really needed: chsh -s /usr/local/bin/bash"
return 1
fi
fi
if [ "$(uname)" == "Linux" ]; then
if ! command -v xclip &>/dev/null; then
echo "xclip could not be found. It is an optional tool for reading the initial key from the clipboard."
echo "If you want to use this optional feature, please install it with: sudo apt-get install xclip"
read -p "Do you want to continue without xclip? (y/n) " yn
case $yn in
[Yy]*) ;;
[Nn]*) return 1 ;;
*) echo "Please answer yes or no." ;;
esac
fi
PASTECOMMAND="xclip -o"
fi
function mytotp() {
SERVID=$1
if ! command -v oathtool &>/dev/null; then
echo "oathtool could not be found"
echo "Please install it with: brew install oath-toolkit"
echo "or check further https://launchpad.net/oath-toolkit/+packages && https://www.nongnu.org/oath-toolkit/"
return 1
fi
if [ -z "$1" ]; then
echo "mytotp version 1.0.1.rc"
echo "Usage: mytotp SERVID"
echo "SERVID is a service ID, abbreviated, that you provided for mytotpadd before, check all with mytotplist command"
return 1
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() {
SERVID=$1
# Check if "My TOTP" GPG key exists
if ! gpg --list-keys "My TOTP" >/dev/null 2>&1; then
echo "GPG key 'My TOTP' does not exist. Please create it first."
# ask user if they want to create the key
read -p "Do you want to create the key 'My TOTP' now ? (y/n) " yn
case $yn in
[Yy]*)
echo "Write and remember the password for 'My TOTP' gpg key in the next line:"
gpg --yes --batch --passphrase-fd 0 --quick-generate-key 'My TOTP'
;;
[Nn]*) return 1 ;;
*) echo "Please answer yes or no." ;;
esac
echo "get back with further usage: mytotpadd <SERVID>"
return
fi
# 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
# 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
# if you want paste again the key, this way it would be stored in ~/.config/mytotp/SERVIDS.keys.lst file
# read from clipboard with xclip
echo "Do you want to store the initial service key in .key.asc? Warn: it is unsafe (y/n) "
read -p "y/n " byn
case $byn in
[Yy]*) $PASTECOMMAND >>~/.config/mytotp/${SERVID}.key.asc ;;
[Nn]*) return ;;
*) echo "Please answer yes or no." ;;
esac
}
function mytotpdel() {
SERVID=$1
if [ -z "$1" ]; then
echo -e "Usage: $0 SERVID\n\tSERVID is a service ID, abbreviated, w/o ext:"
return 1
fi
if [ -f "${KEYDIR}/${SERVID}${KEYEXT}" ]; then
rm -f "${KEYDIR}/${SERVID}${KEYEXT}"
echo "Key for ${SERVID} deleted."
else
echo "No key for ${SERVID}"
fi
}
# function to list all SERVIDs in ~/.config/mytotp
function mytotplist() {
if [ ! -d "${KEYDIR}" ]; then
read -p "Directory ${KEYDIR} does not exist. Do you want to create it? (y/n) " yn
case $yn in
[Yy]*) mkdir -p "${KEYDIR}" ;;
[Nn]*) return ;;
*) echo "Please answer yes or no." ;;
esac
fi
ENTRIES=$(find ${KEYDIR}/*${KEYEXT} 2>/dev/null | sed -e 's/\/home.*\// /; s/\.gpg//')
if [ -z "$ENTRIES" ]; then
echo "Warning: No SERVID entries found."
else
echo "$ENTRIES"
fi
}
# Function to get the list of .gpg files in $KEYDIR
get_gpg_files() {
find "$KEYDIR" -name "*$KEYEXT" -type f 2>/dev/null | sed -e "s|$KEYDIR/||" -e "s|$KEYEXT||"
}
# Register argument completer for mytotpdel function
_mytotpdel_completions() {
local cur_word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(get_gpg_files)" -- "$cur_word") )
}
# Register argument completer for mytotp function
_mytotp_completions() {
local cur_word="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(get_gpg_files)" -- "$cur_word") )
}
# Register the completion functions
complete -F _mytotpdel_completions mytotpdel
complete -F _mytotp_completions mytotp