Sha256: e2ded4780a68fe2ef09f627dcc7406909b0e827c8eaf8b020f62a791b64a0024

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

require 'rotp/arguments'

module ROTP
  class CLI
    attr_reader :filename, :argv

    def initialize(filename, argv)
      @filename = filename
      @argv = argv
    end

    def run
      puts output
    end

    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

    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

    def arguments
      @arguments ||= ROTP::Arguments.new(filename, argv)
    end

    def options
      arguments.options
    end

    def red(string)
      "\033[31m#{string}\033[0m"
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rotp-3.3.1 lib/rotp/cli.rb
rotp-3.3.0 lib/rotp/cli.rb
rotp-3.2.0 lib/rotp/cli.rb
rotp-3.1.0 lib/rotp/cli.rb
rotp-3.0.1 lib/rotp/cli.rb
rotp-3.0.0 lib/rotp/cli.rb
rotp-2.1.2 lib/rotp/cli.rb
rotp-2.1.1 lib/rotp/cli.rb
rotp-2.1.0 lib/rotp/cli.rb