thibthibaut
3/4/2019 - 10:54 AM

Two Factor Authentication on command line

Two Factor Authentication on command line

#!/usr/bin/env bash
# !! requires oathtool and gpg !!
# very very inspired by 
# https://www.sendthemtomir.com/blog/cli-2-factor-authentication
# $HOME/.2fa should look like this :
#    service_name=code
#    service_name2=code2
# and should be encrypted with gpg this way :
# gpg --cipher-algo AES256 -c ~/.2fakeys

if [ -z $1 ]; then
  echo
  echo "Usage:"
  echo "   2fa protonmail"
  echo
  echo "Configuration: $HOME/.2fakeys.gpg"
  echo "Format: name=key"
  exit
fi
DECRYPTED="$(gpg -d $HOME/.2fakeys.gpg)"
OTPKEY="$(echo "$DECRYPTED" | sed -n "s/${1}=//p")"
if [ -z "$OTPKEY" ]; then
  echo "$(basename $0): Unknown Service Name '$1'"
  $0
  exit
fi
# Genrate the 2FA code
oathtool --totp -b "$OTPKEY"