aboutsummaryrefslogtreecommitdiff
path: root/twofa
blob: d5e25261da3789adc26365c19693cc7b0798efde (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env ruby

#!DESCRIBE: CLI based TOTP 2FA authenticator

require "openssl"
require "base32"
require "optimist"

def db32(str)
  Base32.decode(str)
end

def hs1(key, counter, hsh = "sha1")
  OpenSSL::HMAC.hexdigest(hsh, key.to_s, [counter].pack("Q>"))
end

def dt(str)
  offset = str[-1].to_i(16)
  p = str[(offset*2)...((offset+4)*2)]
  p[0] = (p[0].to_i(16) & 0x7).to_s(16)
  p.to_i(16)
end

def hotp(num, dig = 6)
  num % (10 ** dig)
end

def fatal(msg)
  $stderr.puts(msg)
  exit 1
end

class Secrets
  class MultipleDefaultException < StandardError
    def initialize
      super("Multiple default issuers specified in config file")
    end
  end

  class Secret
    def initialize(method, secret, tdc, dig, hsh)
      @method = method
      @secret = secret
      @tdc = tdc
      @dig = dig
      @hsh = hsh
    end

    attr_reader :method

    def verify(tx = Time.now.to_i)
      case @method
      when 'totp'
        decoded = db32(@secret)
        hmac = hs1(decoded, tx.to_i/@tdc.to_i, @hsh)
        trunc = dt(hmac)
        code = hotp(trunc, @dig)
        "%0#{@dig}d" % code
      when 'hotp'
        decoded = db32(@secret)
        hmac = hs1(decoded, @tdc, @hsh)
        trunc = dt(hmac)
        code = hotp(trunc, @dig)
        @tdc += 1
        "%0#{@dig}d" % code
      else
        fatal("I don't know how to #{@method}")
      end
    end

    def time_remaining(tx = Time.now.to_i)
      case @method
      when 'totp'
        (tx.to_i/@tdc.to_i + 1) * @tdc - tx
      end
    end

    def puts
      "#{@secret} #{@tdc} #{@dig} #{@hsh}"
    end
  end

  def initialize(arr)
    @secrets = {}
    @default = nil

    arr.each do |secretline|
      m, i, s, tc, d, h = secretline.split

      if m[0] == ?*
        raise MultipleDefaultException.new unless @default.nil?
        m = m[1..-1]
        @default = i
      end

      case m
      when 'totp'
        tc = tc&.to_i || 30
      when 'hotp'
        tc = tc&.to_i || 0
      end

      @secrets[i] = Secret.new(m, s, tc, d&.to_i || 6, h || "sha1")
    end
  end

  attr_reader :default

  def [](issuer)
    @secrets[issuer]
  end

  def puts
    @secrets.map do |i, s|
      "#{i == @default ? ?* : ''}#{s.method} #{i} #{s.puts}"
    end
  end
end

opts = Optimist::options do
  version "twofa (c) 2019 Nat Lasseter"
  banner <<-EOS
twofa is a command line OTP code generator.

Usage:
  twofa [opts] ISSUER

where [opts] are:
EOS

  opt :no_clip, "Do not copy code to the clipboard"
  opt :twofa_file, "Location of the twofa secrets file",
    type: :string, default: File.join(ENV["HOME"], ".twofa")
end

TWOFAFILE = opts[:twofa_file]
fatal("No 2fa issuers file at #{File.absolute_path(TWOFAFILE)}") unless File.exist?(TWOFAFILE)

begin
  SECRETS = Secrets.new(File.readlines(TWOFAFILE).map(&:strip))
rescue Secrets::MultipleDefaultException => e
  fatal(e.message)
end

issuer = ARGV.shift&.strip
if issuer.nil?
  issuer = SECRETS.default
  fatal("Specify issuer") if issuer.nil?
  puts "Using default issuer: #{issuer}"
end

sec = SECRETS[issuer]
fatal("No such issuer") if sec.nil?

case sec.method
when 'totp'
  code = sec.verify
  time = sec.time_remaining
  puts "#{code} (for #{time} more seconds)"
when 'hotp'
  code = sec.verify
  puts "#{code} (rolled counter)"
  File.open(TWOFAFILE, ?w).puts SECRETS.puts
end

unless opts[:no_clip] then
  require "clipboard"
  Clipboard.copy(code)
  puts "(Copied to clipboard)"
end