Module: Pwss::Password

Defined in:
lib/pwss/password.rb

Overview

some functions to manage password generation and clipboard

Constant Summary collapse

DEFAULT_PASSWORD_LENGTH =
16

Class Method Summary collapse

Class Method Details

.ask_password(prompt = "Enter master password: ") ⇒ Object

Ask for a password fom the command line



37
38
39
40
41
42
43
44
# File 'lib/pwss/password.rb', line 37

def self.ask_password prompt="Enter master password: "
  printf prompt
  system "stty -echo"
  password = $stdin.gets.chomp
  system "stty echo"
  puts ""
  password
end

.ask_password_twice(prompt = "master password") ⇒ Object

Ask for a password twice and make sure it is entered the same



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pwss/password.rb', line 47

def self.ask_password_twice prompt="master password"
  match = false
  while ! match
    password = ask_password "Enter #{prompt}: "
    repeat = ask_password "Repeat #{prompt}: "
    match = (password == repeat)

    if match == false then
      puts "Error! Password do not match.  Please enter them again."
    end
  end
  password
end

.password(arguments = {}) ⇒ Object

generate a password

optional hash arguments allows to define a strategy for generating a password and the password length of automatically generated passwords)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pwss/password.rb', line 15

def self.password arguments = {}
  length = arguments[:length] || DEFAULT_PASSWORD_LENGTH
  strategy = arguments[:strategy] || 'random'

  case strategy
  when 'random', 'strong', 'alpha'
    return Pwss::Password.random_password(length, strategy)
  when 'ask'
    return Pwss::Password.ask_password_twice "new password for entry"
  when 'pwgen'
    begin
      password = %x(pwgen -N1 #{length}).chomp
      return password
    rescue
      raise "Error: pwgen not found.  Use one of random, strong, alpha, or ask."
    end
  else
    raise "Error: password generation strategy #{strategy} not understood."
  end
end

.random_password(length = DEFAULT_PASSWORD_LENGTH, strategy = "random") ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pwss/password.rb', line 63

def self.random_password length=DEFAULT_PASSWORD_LENGTH, strategy="random"
  lower = 'abcdefghijkmnpqrstuvwxyz'
  upper = 'ABCDEFGHJKLMNPQRSTUVWXYZ'
  digits = '123456789'
  ambiguous = '0oOlI' 
  safe_symbols = '~!@#$%^-+_.,;:'
  difficult_symbols = '&*()={}[]|\\\"\'`<>?/'

  case strategy
  when 'random'
    chars = lower + upper + digits + safe_symbols + difficult_symbols + ambiguous
  when 'strong'
    chars = lower + upper + digits + safe_symbols
  when 'alpha'
    chars = lower + upper + digits
  else
    raise "Error: password generation strategy #{strategy} is not understood."
  end        

  Array.new(length) { chars[rand(chars.length)].chr }.join
end

.to_clipboard(field_name, value, counter = 30) ⇒ Object

make a field available to the clipboard for a given time (in seconds).



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pwss/password.rb', line 88

def self.to_clipboard field_name, value, counter = 30
  old_clipboard = Clipboard.paste
  Clipboard.copy value

  begin
    if counter <= 0
      STDIN.flush
      puts "\n#{field_name.capitalize} available in clipboard: press enter when you are done."
      STDIN.getc
    else
      puts "\n#{field_name.capitalize} available in clipboard for #{counter} seconds."
      sleep(counter)
    end
    Clipboard.copy old_clipboard
  rescue Exception => e
    Clipboard.copy old_clipboard
    puts "Clipboard restored"
  end
end