Class: Trustworthy::Prompt
- Inherits:
-
Object
- Object
- Trustworthy::Prompt
- Defined in:
- lib/trustworthy/prompt.rb
Instance Method Summary collapse
- #_ask(question) ⇒ Object
- #_ask_password(question) ⇒ Object
- #_ask_password_with_strength_requirements(question) ⇒ Object
- #_error(message) ⇒ Object
- #_say(message) ⇒ Object
- #_strong_password?(password) ⇒ Boolean
-
#_unlock_key(settings, usernames_in_use) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #add_user_key(key) ⇒ Object
- #change_user_password ⇒ Object
-
#initialize(config_file, terminal = HighLine.new) ⇒ Prompt
constructor
A new instance of Prompt.
- #unlock_master_key ⇒ Object
Constructor Details
#initialize(config_file, terminal = HighLine.new) ⇒ Prompt
Returns a new instance of Prompt.
5 6 7 8 |
# File 'lib/trustworthy/prompt.rb', line 5 def initialize(config_file, terminal = HighLine.new) @config_file = config_file @terminal = terminal end |
Instance Method Details
#_ask(question) ⇒ Object
97 98 99 |
# File 'lib/trustworthy/prompt.rb', line 97 def _ask(question) @terminal.ask(question).to_s end |
#_ask_password(question) ⇒ Object
101 102 103 |
# File 'lib/trustworthy/prompt.rb', line 101 def _ask_password(question) @terminal.ask(question) { |q| q.echo = false }.to_s end |
#_ask_password_with_strength_requirements(question) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/trustworthy/prompt.rb', line 105 def _ask_password_with_strength_requirements(question) loop do password = @terminal.ask(question) { |q| q.echo = false }.to_s return password if _strong_password?(password) _error('Password is too weak') end end |
#_error(message) ⇒ Object
125 126 127 128 |
# File 'lib/trustworthy/prompt.rb', line 125 def _error() = @terminal.color(, :error) _say() end |
#_say(message) ⇒ Object
113 114 115 |
# File 'lib/trustworthy/prompt.rb', line 113 def _say() @terminal.say() end |
#_strong_password?(password) ⇒ Boolean
117 118 119 120 121 122 123 |
# File 'lib/trustworthy/prompt.rb', line 117 def _strong_password?(password) return false unless password =~ /.{8,}/ return false unless password =~ /[0-9]/ return false unless password =~ /[A-Za-z]/ return false unless password =~ /\W/ true end |
#_unlock_key(settings, usernames_in_use) ⇒ Object
rubocop:disable Metrics/MethodLength
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 |
# File 'lib/trustworthy/prompt.rb', line 70 def _unlock_key(settings, usernames_in_use) # rubocop:disable Metrics/MethodLength 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
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/trustworthy/prompt.rb', line 10 def add_user_key(key) Trustworthy::Settings.open(@config_file) do |settings| username = nil loop do username = _ask('Username: ') break unless settings.key?(username) _error("Key #{username} is already in use") end loop do password = _ask_password_with_strength_requirements('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 |
#change_user_password ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/trustworthy/prompt.rb', line 34 def change_user_password Trustworthy::Settings.open(@config_file) do |settings| username, key = _unlock_key(settings, []) loop do password = _ask_password_with_strength_requirements('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_key ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/trustworthy/prompt.rb', line 53 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 _, key2 = _unlock_key(settings, usernames_in_use) master_key = Trustworthy::MasterKey.create_from_keys(key1, key2) _say('Reconstructed master key') master_key end end |