1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2025-04-04 00:17:06 +00:00

Refactor: move code for obtaining totp tokens to tests/lib/totp.sh

This commit is contained in:
downtownallday 2020-09-10 18:19:29 -04:00
parent c0431474c9
commit 752da93a37
3 changed files with 24 additions and 2 deletions

View File

@ -16,4 +16,5 @@
. "$1/populate.sh" || exit 7
. "$1/installed-state.sh" || exit 8
. "$1/totp.sh" || exit 9

21
tests/lib/totp.sh Normal file
View File

@ -0,0 +1,21 @@
#
# requires:
# mailinabox's python installed with pyotp module at
# /usr/local/lib/mailinabox/env
#
totp_current_token() {
# given a secret, get the current token
# token written to stdout
# error message to stderr
# return 0 if successful
# non-zero if an error occured
local secret="$1"
/usr/local/lib/mailinabox/env/bin/python -c "import pyotp; totp=pyotp.TOTP(r'$secret'); print(totp.now());"
if [ $? -ne 0 ]; then
return 1
else
return 0
fi
}

View File

@ -195,9 +195,9 @@ mgmt_get_totp_token() {
local count=0
while [ -z "$TOTP_TOKEN" -a $count -lt 10 ]; do
TOTP_TOKEN="$(/usr/local/lib/mailinabox/env/bin/python -c "import pyotp; totp=pyotp.TOTP(r'$secret'); print(totp.now());" 2>>"$TEST_OF")"
TOTP_TOKEN="$(totp_current_token "$secret" 2>>"$TEST_OF")"
if [ $? -ne 0 ]; then
record "Failed: Could not generate a TOTP token !"
record "Failed: Could not get the TOTP token !"
return 1
fi