Class: Simp::Cli::Config::PasswordItem

Inherits:
Item
  • Object
show all
Defined in:
lib/simp/cli/config/item.rb

Overview

An Item that asks for Passwords, with:

- special validation
- invisible input
- optional password generation

Instance Attribute Summary collapse

Attributes inherited from Item

#allow_user_apply, #config_items, #description, #die_on_apply_fail, #fact, #fail_on_missing_answer, #key, #next_items_tree, #silent, #skip_apply, #skip_query, #skip_yaml, #value

Instance Method Summary collapse

Methods inherited from Item

#apply, #default_value, #highline_question_type, #next_items, #not_valid_message, #os_value, #print_banner, #print_summary, #puppet_value, #query, #query_status, #recommended_value, #safe_apply, #say_blue, #say_green, #say_red, #say_yellow, #to_yaml_s

Constructor Details

#initializePasswordItem

Returns a new instance of PasswordItem.



269
270
271
272
273
# File 'lib/simp/cli/config/item.rb', line 269

def initialize
  super
  @can_generate        = true
  @generate_by_default = true
end

Instance Attribute Details

#can_generateObject

Returns the value of attribute can_generate.



268
269
270
# File 'lib/simp/cli/config/item.rb', line 268

def can_generate
  @can_generate
end

#generate_by_defaultObject

Returns the value of attribute generate_by_default.



268
269
270
# File 'lib/simp/cli/config/item.rb', line 268

def generate_by_default
  @generate_by_default
end

Instance Method Details

#encrypt(password, salt = nil) ⇒ Object



281
282
283
284
# File 'lib/simp/cli/config/item.rb', line 281

def encrypt( password, salt=nil )
  say_yellow 'WARNING: password not encrypted; override in child class'
  password
end

#query_askObject

ask for the password twice (and verify that both match)



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/simp/cli/config/item.rb', line 309

def query_ask
  password = false
  password = query_generate_password if @can_generate

  while !password
    answers = []
    [0,1].each{ |x|
      say "please enter a password:"     if x == 0
      say "please confirm the password:" if x == 1
      answers[x] = super
    }
    if answers.first == answers.last
      password = answers.first
    else
      say_yellow( 'WARNING: passwords did not match!  Please try again.' )
    end
  end

  encrypt password
end

#query_extras(q) ⇒ Object



276
277
278
# File 'lib/simp/cli/config/item.rb', line 276

def query_extras( q )
  q.echo = '*'     # don't print password characters to stdout
end

#query_generate_passwordObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/simp/cli/config/item.rb', line 287

def query_generate_password
  password = false
  default  = @generate_by_default ? 'yes' : 'no'
  if agree( "auto-generate a password? " ){ |q| q.default = default }
    password = Simp::Cli::Config::Utils.generate_password
    say "<%= color( %q{#{''.ljust(80,'-')}}, GREEN)%>\n"
    say '<%= color( %q{NOTE: }, GREEN, BOLD)%>' +
        "<%= color( %q{ the generated password is: }) %>\n"
    say "\n"
    say "<%= color( %q{   #{password}}, YELLOW, BOLD )%>  "
    say "\n"
    say "\n"
    say 'Please remember it!'
    say "<%= color( %q{#{''.ljust(80,'-')}}, GREEN)%>\n"
    say_blue '*** Press enter to continue ***' , ['BOLD', 'BLINK']
    ask ''
  end
  password
end

#validate(x) ⇒ Object



331
332
333
334
335
336
337
338
339
340
# File 'lib/simp/cli/config/item.rb', line 331

def validate x
  result = true
  begin
    Simp::Cli::Config::Utils.validate_password x
  rescue Simp::Cli::Config::PasswordError => e
    say_yellow "WARNING: Invalid Password: #{e.message}"
    result = false
  end
  result
end