Class: Authman::OtpGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/authman/otp_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = '~/.authman.asc') ⇒ OtpGenerator

Returns a new instance of OtpGenerator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/authman/otp_generator.rb', line 9

def initialize(filename = '~/.authman.asc')
  f = File.expand_path(filename)

  if File.exists?(f) 
    @settings         = JSON.load(GPGME::Crypto.new.decrypt(open(f).read))
    @totps            = @settings.inject({}) { |ts, s| ts[s[0]] = ROTP::TOTP.new(s[1]); ts }
    @max_name_length  = @settings.map { |k,v| k.length }.max
  else
    puts "Unable to open <\#{f}> make sure it contains a GPG-encrypted JSON structure of your OTP secrets.\nThe cleartext file should look like :\n\n{\n  \"Amazon AWS\": \"<secret>\",\n  \"Paymium\":    \"<secret>\"\n}\n    EOS\n  end\nend\n"

Instance Method Details

#cycle_otpsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/authman/otp_generator.rb', line 29

def cycle_otps
  @interrupted = false
  trap('INT') { @interrupted = true }

  if @settings
    while(!@interrupted) do
      system('clear')
      puts "-- Cycling one-time passwords, <Ctrl-C> to exit"
      print_current_otps
      sleep(1)
    end

    puts "\nBye!"
  end
end


45
46
47
# File 'lib/authman/otp_generator.rb', line 45

def print_current_otps
  @totps.each { |name, totp| puts "#{"%-#{@max_name_length}.#{@max_name_length}s" % name} : #{totp.now}" }
end