aboutsummaryrefslogtreecommitdiff
path: root/twofa
blob: 487b094e6b7b67e0a2eeb3bf6d6bb39f5196f9ad (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
#!/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 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 = {}
    arr.each do |secretline|
      m, i, s, tc, d, h = secretline.split
      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

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

  def puts
    @secrets.map do |i, s|
      "#{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)

SECRETS = Secrets.new(File.readlines(TWOFAFILE).map(&:strip))

ISSUER = ARGV.shift&.strip&.downcase
fatal("Specify issuer") if ISSUER.nil?

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