otp.plugin.zsh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export OTP_HOME=~/.otp
  2. mkdir -p $OTP_HOME
  3. function ot () {
  4. if ! command -v oathtool > /dev/null 2>&1; then
  5. echo "Note: you need to install oathtool or oath-toolkit, depending on your OS or distribution."
  6. return 1
  7. fi
  8. if ! command -v gpg > /dev/null 2>&1; then
  9. echo "Note: you need to install gpg and create an ID using 'gpg --gen-key', unless you have one already."
  10. return 1
  11. fi
  12. if [[ `uname` == 'Darwin' ]] then # MacOS X
  13. export COPY_CMD='pbcopy'
  14. elif command -v xsel > /dev/null 2>&1; then # Any Unix with xsel installed
  15. export COPY_CMD='xsel --clipboard --input'
  16. else
  17. COPY_CMD='true'
  18. fi
  19. if [[ "x$1" == "x" ]]; then
  20. echo "usage: otpw <profile.name>"
  21. return 1
  22. elif [ ! -f $OTP_HOME/$1.otp.asc ]; then
  23. echo "missing profile $1, you might need to create it first using otp_add_device"
  24. return 1
  25. else
  26. totpkey=$(gpg --decrypt $OTP_HOME/$1.otp.asc)
  27. oathtool --totp --b $totpkey | tee /dev/stderr | `echo $COPY_CMD`
  28. if [[ $COPY_CMD == 'true' ]] then
  29. echo "Note: you might consider installing xsel for clipboard integration"
  30. fi
  31. fi
  32. }
  33. function otp_add_device () {
  34. if [[ "x$1" == "x" ]] then
  35. echo "usage: otp_add <profile.name>"
  36. return 1
  37. else
  38. echo "Enter an email address attached to your GPG private key, then paste the secret configuration key followed by ^D"
  39. rm -f $OTP_HOME/$1.otp.asc
  40. gpg --armor --encrypt --output $OTP_HOME/$1.otp.asc /dev/stdin
  41. fi
  42. }
  43. function otp_devices () {
  44. reply=($(find $OTP_HOME -name \*.otp.asc | xargs basename -s .otp.asc))
  45. }
  46. compctl -K otp_devices ot