Skip to content

Instantly share code, notes, and snippets.

@imme-emosol
Created July 10, 2018 13:04
Show Gist options
  • Save imme-emosol/e0475031ac62092fecf63fdd5abb1a30 to your computer and use it in GitHub Desktop.
Save imme-emosol/e0475031ac62092fecf63fdd5abb1a30 to your computer and use it in GitHub Desktop.
oathtool wrapper
#!/usr/bin/env sh
BASEDIR="${HOME}"
SECRET_FILE="${BASEDIR}/.totp_secret"
if test ! -e "${SECRET_FILE}"
then
touch -a "${SECRET_FILE}"
fi
if test ! -w "${SECRET_FILE}"
then
echo "Storage is unwritable, stopping execution."
exit 1
fi
TOTP_SHARED_SECRET=$( cat "${SECRET_FILE}" )
if test "q${TOTP_SHARED_SECRET}" = "q"
then
echo "Generate time based password for what secret?"
else
echo "* Leave blank to generate password for current secret, i.e.: '${TOTP_SHARED_SECRET}'."
echo "* Type d followed by Enter to remove the current secret."
fi
read -r NEW_TOTP_SECRET
if test "q${NEW_TOTP_SECRET}" = "qd"
then
rm "${SECRET_FILE}"
echo "removed secret."
exit 0
fi
if test "q${NEW_TOTP_SECRET}" != "q"
then
TOTP_SHARED_SECRET=${NEW_TOTP_SECRET}
fi
if test "q${TOTP_SHARED_SECRET}" = "q"
then
echo "Funny, the secret has not been set .. aborting."
exit 1
fi
echo "Generating time based password for the provided secret."
oathtool --totp --base32 "${TOTP_SHARED_SECRET}"
echo "Storing the provided secret."
echo "${TOTP_SHARED_SECRET}" > "${SECRET_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment