File: cli.rb

package info (click to toggle)
ruby-rotp 2.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 340 kB
  • ctags: 79
  • sloc: ruby: 726; makefile: 16
file content (56 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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