diff --git a/README.md b/README.md index 83fa829..fa0bc0b 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,21 @@ POSIX-compliant console, with BASH, GNU utils, GNU Privacy Guard and oathtool av Just place mytotp.sh somewhere within your $PATH, check is +x attribute present. +Then add `source mytotp.sh` to your .bashrc or .zshrc to have all functions available, this way you get `mytotp` for generating, `mytotpadd` for adding and `mytotplist` for listing. + Create a direcrory for TOTP keys `mkdir -p ~/.config/mytotp` If you have no suitable GPG key for encrypting of TOTP keys, create it -`gpg --yes --batch --passphrase 'Some-words' --quick-generate-key "My TOTP"` +`gpg --yes --batch --passphrase-fd 0 --quick-generate-key 'My TOTP'` +enter your passphrase hit Enter, check keys with +`gpg --list-secret-keys` ## Usage -When you activate 2FA on some service, get TOTP key string, copy it into buffer, then `gpg -e -r "My TOTP" >~/.config/mytotp/SERVID.gpg`, paste string, press Enter and Crtl+D. +When you activate 2FA on some service, get TOTP key string, copy it into buffer, then, assuming SERVID is a name you want to give to service `mytotpadd SERVID`, paste string, press Enter and Crtl+D . For getting TOTP code for this service do `mytotp.sh SERVID`, unlock your GPG key with above passphrase, then wait for 6-digit code (for your convenience script will wait for next 30 second interval before generating, so you will have a maximum of 30 seconds time for use it). ## Afterword -I wrote this script for my own usage, trying to keep a balance between simplicity and usability. So anyone can try to use it, or modify for it's own needs. - +I wrote this script for my own usage, trying to keep a balance between simplicity and usability. So anyone can try to use it, or modify for it's own needs. \ No newline at end of file diff --git a/mytotp.sh b/mytotp.sh index 514ffb9..88c4454 100755 --- a/mytotp.sh +++ b/mytotp.sh @@ -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//' +}