Last active
March 8, 2019 16:09
-
-
Save Dan-Q/9f03057cddbda08ee7dfe4910f686150 to your computer and use it in GitHub Desktop.
Command-line Google Authenticator (TOTP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
# encoding: utf-8 | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'rotp' | |
gem 'thor' | |
end | |
class GoogleAuthenticator < Thor | |
desc 'add', 'add a new account' | |
def add | |
print 'New account name: ' | |
acName = STDIN.gets.strip | |
print 'New account code: ' | |
acCode = STDIN.gets.strip | |
File.open(File.expand_path('~/.google-authenticator-accounts'), 'a'){|f| f.printf "%-23s %s\n", acName, acCode} | |
end | |
desc 'generate', 'generate TOTP codes' | |
def generate | |
printf "%-30s %3s (%02ds) %4s\n", 'Account', 'Now', (30 - (Time::now.utc.to_i % 30)), 'Next' | |
puts '-' * 47 | |
File.read(File.expand_path('~/.google-authenticator-accounts')).split("\n").reject{|l| l.strip == ''}.each do |account| | |
if account =~ /^(.+) ([\w\d]+)$/ | |
totp = ROTP::TOTP.new($2) | |
printf "%-30s %06s %06s\n", $1, totp.at(Time::now.utc), totp.at(Time::now.utc + 30) | |
end | |
end | |
end | |
default_task :generate | |
end | |
GoogleAuthenticator.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW, I made some small fixes in my fork if your interested.