Created
September 2, 2016 19:12
-
-
Save stbuehler/2a2d920e4defb9a93b93cf5c0cf9cb2f to your computer and use it in GitHub Desktop.
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 'rotp' | |
require 'uri' | |
require 'cgi' | |
otpuri = STDIN.readline | |
otpuri = URI(otpuri) | |
if otpuri.scheme != "otpauth" | |
raise "invalid uri scheme" | |
end | |
if otpuri.host != "totp" | |
raise "invalid uri host '#{otpuri.host}' (only TOTP supported)" | |
end | |
opts = CGI::parse(otpuri.query) | |
def getopt(opts, name, default) | |
opt = opts[name] | |
return default if !opt || opt.length != 1 | |
return opt[0] | |
end | |
totp = ROTP::TOTP.new(opts['secret'][0], interval: getopt(opts, 'period', '30').to_i, digits: getopt(opts, 'digits', '6').to_i) | |
puts totp.now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment