Class: Trustworthy::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/trustworthy/prompt.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file, terminal = HighLine.new) ⇒ Prompt

Returns a new instance of Prompt.



3
4
5
6
# File 'lib/trustworthy/prompt.rb', line 3

def initialize(config_file, terminal = HighLine.new)
  @config_file = config_file
  @terminal = terminal
end

Instance Method Details

#_ask(question) ⇒ Object



79
80
81
# File 'lib/trustworthy/prompt.rb', line 79

def _ask(question)
  @terminal.ask(question).to_s
end

#_ask_password(question) ⇒ Object



83
84
85
# File 'lib/trustworthy/prompt.rb', line 83

def _ask_password(question)
  @terminal.ask(question) { |q| q.echo = false }.to_s
end

#_error(message) ⇒ Object



91
92
93
94
# File 'lib/trustworthy/prompt.rb', line 91

def _error(message)
  colored_message = @terminal.color(message, :error)
  _say(colored_message)
end

#_say(message) ⇒ Object



87
88
89
# File 'lib/trustworthy/prompt.rb', line 87

def _say(message)
  @terminal.say(message)
end

#_unlock_key(settings, usernames_in_use) ⇒ Object



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
# File 'lib/trustworthy/prompt.rb', line 52

def _unlock_key(settings, usernames_in_use)
  username = nil
  loop do
    username = _ask('Username: ')
    if usernames_in_use.include?(username)
      _error("Key #{username} is already in use")
    elsif settings.find_key(username).nil?
      _error("Key #{username} does not exist")
    else
      break
    end
  end

  key = nil
  begin
    password = _ask_password('Password: ')
    key = settings.unlock_key(username, password)
  rescue ArgumentError
    _error("Password incorrect for #{username}")
    retry
  end

  _say("Unlocked #{username}")

  [username, key]
end

#add_user_key(key) ⇒ Object



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
# File 'lib/trustworthy/prompt.rb', line 8

def add_user_key(key)
  Trustworthy::Settings.open(@config_file) do |settings|
    username = nil
    loop do
      username = _ask('Username: ')
      if settings.has_key?(username)
        _error("Key #{username} is already in use")
      else
        break
      end
    end

    loop do
      password = _ask_password('Password: ')
      password_confirm = _ask_password('Password (again): ')
      if password == password_confirm
        settings.add_key(key, username, password)
        break
      else
        _error('Passwords do not match.')
      end
    end

    username
  end
end

#unlock_master_keyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trustworthy/prompt.rb', line 35

def unlock_master_key
  usernames_in_use = []
  Trustworthy::Settings.open(@config_file) do |settings|
    raise 'must have two keys to unlock master key' unless settings.recoverable?

    username1, key1 = _unlock_key(settings, usernames_in_use)
    usernames_in_use << username1

    username2, key2 = _unlock_key(settings, usernames_in_use)

    master_key = Trustworthy::MasterKey.create_from_keys(key1, key2)
    _say('Reconstructed master key')

    master_key
  end
end