Class: Secrets::App::PasswordHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/secrets/app/password_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ PasswordHandler

Returns a new instance of PasswordHandler.



8
9
10
# File 'lib/secrets/app/password_handler.rb', line 8

def initialize(opts)
  self.opts = opts
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/secrets/app/password_handler.rb', line 6

def opts
  @opts
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/secrets/app/password_handler.rb', line 6

def password
  @password
end

Class Method Details

.handle_user_input(message, color) ⇒ Object



20
21
22
# File 'lib/secrets/app/password_handler.rb', line 20

def self.handle_user_input(message, color)
  HighLine.new(STDIN, STDERR).ask(message.bold) { |q| q.echo = ''.send(color) }
end

Instance Method Details

#askObject



12
13
14
15
16
17
18
# File 'lib/secrets/app/password_handler.rb', line 12

def ask
  retries       ||= 0
  self.password = self.class.handle_user_input('Password: ', :green)
rescue ::OpenSSL::Cipher::CipherError
  STDERR.puts 'Invalid password. Please try again.'
  retry if (retries += 1) < 3
end

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/secrets/app/password_handler.rb', line 24

def create
  if opts[:password]
    self.password    = self.class.handle_user_input('New Password     : ', :blue)
    password_confirm = self.class.handle_user_input('Confirm Password : ', :blue)

    raise Secrets::Errors::PasswordsDontMatch.new(
      'The passwords you entered do not match.') if password != password_confirm

    raise Secrets::Errors::PasswordTooShort.new(
      'Minimum length is 7 characters.') if password.length < 7
  end
  self
end