Skip to content

Instantly share code, notes, and snippets.

@thalweg
Created January 6, 2013 06:26
Show Gist options
  • Save thalweg/4465623 to your computer and use it in GitHub Desktop.
Save thalweg/4465623 to your computer and use it in GitHub Desktop.
TOTP authentication script that uses the ~/.google_authenticator file in the user's home directory. Based on https://moocode.com/posts/5-simple-two-factor-ssh-authentication
#!/usr/bin/env ruby
require 'rubygems'
require 'rotp'
# use ~/.google_authenticator to store the secret for
# compatibility with the google authenticator pam lib
secret_file = "#{ENV['HOME']}/.google_authenticator"
begin
secret = File.open(secret_file).read
rescue
abort "Could not open #{secret_file}"
end
# prompt the user for their validation code
STDERR.write "Enter the validation code: "
until validation_code = STDIN.gets.strip
sleep 1
end
# check the validation code is correct
security = ROTP::TOTP.new(secret)
if security.verify(validation_code)
Kernel.exec ENV['SSH_ORIGINAL_COMMAND'] || ENV['SHELL']
else
abort "Invalid"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment