class ROTP::CLI

Attributes

argv[R]
filename[R]

Public Class Methods

new(filename, argv) click to toggle source
# File lib/rotp/cli.rb, line 7
def initialize(filename, argv)
  @filename = filename
  @argv = argv
end

Public Instance Methods

arguments() click to toggle source
# File lib/rotp/cli.rb, line 43
def arguments
  @arguments ||= ROTP::Arguments.new(filename, argv)
end
errors() click to toggle source
# File lib/rotp/cli.rb, line 16
def errors
  if [:time, :hmac].include?(options.mode)
    if options.secret.to_s == ''
      red 'You must also specify a --secret. Try --help for help.'
    elsif options.secret.to_s.chars.any? { |c| ROTP::Base32::CHARS.index(c.downcase) == nil }
      red 'Secret must be in RFC4648 Base32 format - http://en.wikipedia.org/wiki/Base32#RFC_4648_Base32_alphabet'
    end
  elsif options.mode == :hmac && options.counter.to_i < 0
    red 'You must also specify a --counter. Try --help for help.'
  end
end
options() click to toggle source
# File lib/rotp/cli.rb, line 47
def options
  arguments.options
end
output() click to toggle source
# File lib/rotp/cli.rb, line 28
def output
  return options.warnings if options.warnings
  return errors if errors
  return arguments.to_s if options.mode == :help

  if options.mode == :time
    ROTP::TOTP.new(options.secret).now
  elsif options.mode == :hmac
    ROTP::HOTP.new(options.secret).at options.counter

  else
    fail NotImplementedError
  end
end
red(string) click to toggle source
# File lib/rotp/cli.rb, line 51
def red(string)
  "\033[31m#{string}\033[0m"
end
run() click to toggle source
# File lib/rotp/cli.rb, line 12
def run
  puts output
end