Created
January 6, 2013 06:26
-
-
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
This file contains hidden or 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 | |
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