forked from ThingCrimson/mytotp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement mytotp as bash funcs to manage SERVIDS
- Loading branch information
Showing
2 changed files
with
77 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,78 @@ | ||
#!/bin/bash | ||
# | ||
# Put TOTP key for service SERVID to GPG file crypted for 'My TOTP' | ||
# gpg -e -r 'My TOTP' > ~/.config/mytotp/SERVID.gpg | ||
# this code is a fork of https://github.com/ThingCrimson/mytotp | ||
# put this file in path and source it in .bashrc or .zshrc | ||
# add next line to .bashrc or .zshrc | ||
# source mytotp.sh | ||
# 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 | ||
function mytotp() { | ||
if ! command -v oathtool &> /dev/null | ||
then | ||
echo "oathtool could not be found" | ||
echo "Please install it with: brew install oath-toolkit" | ||
return 1 | ||
fi | ||
|
||
KEYDIR=~/.config/mytotp | ||
KEYEXT=.gpg | ||
SERVID=$1 | ||
KEYDIR=~/.config/mytotp | ||
KEYEXT=.gpg | ||
SERVID=$1 | ||
|
||
if [ -z "${SERVID}" ] ; then | ||
echo -e "Usage: $0 SERVID\n\tSERVID is a service ID, abbreviated, w/o ext:" | ||
find ${KEYDIR}/*${KEYEXT} | sed -e 's/\/home.*\// /; s/\.gpg//' | ||
return 2 | ||
fi | ||
|
||
if [ ! -f "${KEYDIR}/${SERVID}${KEYEXT}" ] ; then | ||
echo "No key for ${KEYDIR}/${SERVID}${KEYEXT}" | ||
return 1 | ||
fi | ||
|
||
if [ -z "${SERVID}" ] ; then | ||
echo -e "Usage: $0 SERVID\n\tSERVID is a service ID, abbreviated, w/o ext:" | ||
find ${KEYDIR}/*${KEYEXT} | sed -e 's/\/home.*\// /; s/\.gpg//' | ||
exit 2 | ||
fi | ||
|
||
if [ ! -f "${KEYDIR}/${SERVID}${KEYEXT}" ] ; then | ||
echo "No key for ${KEYDIR}/${SERVID}${KEYEXT}" | ||
exit 1 | ||
fi | ||
|
||
SKEY=$(gpg -d --quiet "${KEYDIR}/${SERVID}${KEYEXT}") | ||
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} (wait ${WAIT}) ... " | ||
sleep ${WAIT} | ||
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 - ) | ||
TOTP=$(echo "${SKEY}" | oathtool -b --totp - ) | ||
|
||
echo "${TOTP}" | ||
SKEY="none" | ||
echo "${TOTP}" | ||
SKEY="none" | ||
return 0 | ||
} | ||
|
||
exit 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() { | ||
# 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 | ||
KEYDIR=~/.config/mytotp | ||
KEYEXT=.gpg | ||
SERVID=$1 | ||
# 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 | ||
} | ||
|
||
# function to list all SERVIDs in ~/.config/mytotp | ||
function mytotplist() { | ||
KEYDIR=~/.config/mytotp | ||
KEYEXT=.gpg | ||
find ${KEYDIR}/*${KEYEXT} | sed -e 's/\/home.*\// /; s/\.gpg//' | ||
} |
fae182d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.