diff --git a/bin/get-otp b/bin/get-otp new file mode 120000 index 0000000..3905c78 --- /dev/null +++ b/bin/get-otp @@ -0,0 +1 @@ +../get-otp/get-otp.sh \ No newline at end of file diff --git a/get-otp/README.md b/get-otp/README.md new file mode 100644 index 0000000..b8eea48 --- /dev/null +++ b/get-otp/README.md @@ -0,0 +1,8 @@ +# get-otp + +## About +A simple shell wrapper for go-andotp and oathtool for generating TOTP with andotp backups files. + +## Requirements +- [go-andotp](https://github.com/RijulGulati/go-andotp) +- [oathtool](https://www.nongnu.org/oath-toolkit/) diff --git a/get-otp/get-otp.sh b/get-otp/get-otp.sh new file mode 100755 index 0000000..54f991a --- /dev/null +++ b/get-otp/get-otp.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +accounts_file=$1 + +# Check if file path was provided +if [ -z "$accounts_file" ]; then + echo "File not provided." + exit 1 +fi + +printf "Password: " +stty -echo +read -r password +stty echo +printf "\n" + +# Decrypt the andotp generated file +accounts_json=$(go-andotp -d -i "$accounts_file" -p "$password") +if [ "$(echo $accounts_json | awk '{print $1;}')" = "error:" ]; then + echo "Could not decrypt the file" + exit 1; +fi + +# Get Issuers +issuers=$(echo "$accounts_json" | jq -r '.[] | .issuer') +issuers_count=$(echo "$accounts_json" | jq -r '. | length') + +# Get issuer +printf "\nWhat issuer do you want?\n" "$issuers" + +# Print issuers with index as prefix +for i in $(seq 1 "$issuers_count"); do + issuer=$(echo "$issuers" | awk "NR==$i") + echo "$i: $issuer" +done + +printf "\nIssuer index: " +read -r issuer_i + +# Check valid input (needs to be a number and less or equal to issuers_count) +if ! ([ "$issuer_i" -eq "$issuer_i" ] && [ "$issuer_i" -le "$issuers_count" ] && [ "$issuer_i" -ge "1" ]); then + echo "Invalid index!" + exit 1; +fi + +issuer_selected=$(echo "$issuers" | awk "NR==$issuer_i") + +# Get secret +secret=$(echo "$accounts_json" | jq -r ".[] | select(.issuer == \"$issuer_selected\") | .secret") + +# Get otp-code +code=$(echo "$secret" | xargs oathtool --totp -b) +echo "\nOTP CODE: $code"