Class: TFA::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/tfa/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(name, secret) ⇒ Object


14
15
16
17
# File 'lib/tfa/cli.rb', line 14

def add(name, secret)
  storage.save(name, clean(secret))
  "Added #{name}"
end

#decryptObject


81
82
83
84
85
# File 'lib/tfa/cli.rb', line 81

def decrypt
  return unless ensure_upgraded!

  secure_storage.decrypt!
end

#destroy(name) ⇒ Object


20
21
22
# File 'lib/tfa/cli.rb', line 20

def destroy(name)
  storage.delete(name)
end

#encryptObject


74
75
76
77
78
# File 'lib/tfa/cli.rb', line 74

def encrypt
  return unless ensure_upgraded!

  secure_storage.encrypt!
end

#now(secret) ⇒ Object


47
48
49
# File 'lib/tfa/cli.rb', line 47

def now(secret)
  TotpCommand.new(storage).run('', secret)
end

#show(name = nil) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tfa/cli.rb', line 26

def show(name = nil)
  return storage.all.map { |x| x.keys }.flatten.sort unless name

  case options[:format]
  when "qrcode"
    RQRCode::QRCode.new(uri_for(name, storage.secret_for(name))).as_ansi(
      light: "\033[47m", dark: "\033[40m", fill_character: '  ', quiet_zone_size: 1
    )
  when "uri"
    uri_for(name, storage.secret_for(name))
  else
    storage.secret_for(name)
  end
end

#totp(name) ⇒ Object


42
43
44
# File 'lib/tfa/cli.rb', line 42

def totp(name)
  TotpCommand.new(storage).run(name)
end

#upgradeObject


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tfa/cli.rb', line 52

def upgrade
  if !File.exist?(pstore_path)
    say_status :error, "Unable to detect #{pstore_path}", :red
    return
  end
  if File.exist?(secure_path)
    say_status :error, "The new database format was detected.", :red
    return
  end

  if yes? "Upgrade to #{secure_path}?"
    secure_storage
    pstore_storage.each do |row|
      row.each do |name, secret|
        secure_storage.save(name, secret) if yes?("Migrate `#{name}`?")
      end
    end
    File.delete(pstore_path) if yes?("Delete `#{pstore_path}`?")
  end
end