add get-otp script

This commit is contained in:
Guilherme Rugai Freire 2023-10-21 19:20:24 -03:00
parent e5d50eb7e5
commit a9a5ae6d0c
No known key found for this signature in database
GPG Key ID: C246288AC51220BC
3 changed files with 62 additions and 0 deletions

1
bin/get-otp Symbolic link
View File

@ -0,0 +1 @@
../get-otp/get-otp.sh

8
get-otp/README.md Normal file
View File

@ -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/)

53
get-otp/get-otp.sh Executable file
View File

@ -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"