Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created November 27, 2013 03:15
Show Gist options
  • Save xaviershay/7670161 to your computer and use it in GitHub Desktop.
Save xaviershay/7670161 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby --disable-gems
# Roll your own two-factor authentication! Place this file on a USB key, make
# it executable, then run:
#
# two-factor --setup
#
# Now place each TOTP secret in its own file in the `keys/` directory. I like
# to name them after the website they apply to, but any name is fine. To
# generate a token, plug in your USB key and:
#
# two-factor github.com
#
# Remember to unplug the key afterwards! To disable the unsafe eject warning on
# OSX, run:
#
# sudo launchctl unload -w \
# /System/Library/LaunchDaemons/com.apple.UserNotificationCenter.plist
#
# Recommended addition to your shell configuration:
#
# # .zshrc
# PATH=/Volumes/KEYS:$PATH
# function tf() { two-factor $1 | pbcopy }
root = File.expand_path("../", __FILE__)
$available_keys = Dir[root + '/keys/*']
def usage
$stderr.puts <<-EOS
usage: two-factor [--setup | KEYPREFIX]
Available keys:
#{$available_keys.map {|x| " " + File.basename(x) }.join("\n")}
EOS
exit 1
end
app = ARGV.shift
usage unless app
if app == '--setup'
require 'fileutils'
FileUtils.mkdir_p('keys')
FileUtils.mkdir_p('vendor')
Dir.chdir("vendor") do
puts "Vendoring rotp gem..."
system("gem unpack rotp")
end
exit 0
end
glob = root + "/vendor/*/lib"
Dir[glob].each do |lib|
$LOAD_PATH.unshift lib
end
require 'rotp'
key = $available_keys.detect {|x| File.basename(x).start_with?(app) }
usage unless key
puts ROTP::TOTP.new(File.read(key).chomp).now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment