Skip to content

Instantly share code, notes, and snippets.

@vjt
Last active May 12, 2023 11:28
Show Gist options
  • Save vjt/a6641c707402cc8f28c7c9d003f3c03f to your computer and use it in GitHub Desktop.
Save vjt/a6641c707402cc8f28c7c9d003f3c03f to your computer and use it in GitHub Desktop.
2FA from the mac command line using oathtool-toolkit without much fuss
#!/bin/bash
# -*- 2fa Command Line -*-
#
# Uses oath-toolkit http://nongnu.org/oath-toolkit/
# to generate 2FA OTPs and copies them in the Mac's
# clipboard using pbcopy(1). For other UNIX systems
# you can just strip the pbcopy at the bottom.
#
# In the dir below you should put TOTP token seeds,
# one per file, naming it after the service you are
# authenticating against.
#
# Each service uses different approaches to let you
# know (or to try to prevent you from knowing) your
# TOTP seed: it's up to you to find it out. Anyway,
# for TOTP to work, you *need* the seed, so it will
# be hidden, somewhere, for you to find out.
#
# - vjt@openssl.it Thu Oct 12 22:53:11 CEST 2017
#
# WTFPL license (http://www.wtfpl.net/)
#
BASEDIR=~/Documents/2fa
service=$1
if [ -z "$service" ]; then
echo "Usage: $0 <service>"
exit 1
fi
if ! echo "$service" | grep -Eq '[a-zA-Z0-9]+'; then
echo "Invalid service name: $service"
exit 2
fi
file="$BASEDIR/$service"
if [ ! -f "$file" ]; then
echo "Unknown service: $service"
exit 3;
fi
code=$(cat $file)
oathtool --totp -b "$code" | pbcopy
echo "Copied to the clipboard"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment