class Miam::PasswordManager

Constants

LOWERCASES
NUMBERS
SYMBOLS
UPPERCASES

Public Class Methods

new(output, options = {}) click to toggle source
# File lib/miam/password_manager.rb, line 9
def initialize(output, options = {})
  @output = output
  @options = options
end

Public Instance Methods

identify(user, type, policy) click to toggle source
# File lib/miam/password_manager.rb, line 14
def identify(user, type, policy)
  password = mkpasswd(policy)
  log(:debug, "mkpasswd: #{password}")
  puts_password(user, type, password)
  password
end
puts_password(user, type, password) click to toggle source
# File lib/miam/password_manager.rb, line 21
def puts_password(user, type, password)
  log(:info, "User `#{user}` > `#{type}`: put password to `#{@output}`")

  open_output do |f|
    f.puts("#{user},#{type},#{password}")
  end
end

Private Instance Methods

mkpasswd(policy) click to toggle source
# File lib/miam/password_manager.rb, line 31
def mkpasswd(policy)
  chars = []
  len = 8

  if policy
    len = policy.minimum_password_length if policy.minimum_password_length > len
    chars << LOWERCASES.shuffle.first if policy.require_lowercase_characters
    chars << UPPERCASES.shuffle.first if policy.require_uppercase_characters
    chars << NUMBERS.shuffle.first if policy.require_numbers
    chars << SYMBOLS.shuffle.first if policy.require_symbols

    len -= chars.length
  end

  (chars + [*1..9, *'A'..'Z', *'a'..'z'].shuffle.slice(0, len)).shuffle.join
end
open_output() { |$stdout| ... } click to toggle source
# File lib/miam/password_manager.rb, line 48
def open_output
  return if @options[:dry_run]

  if @output == '-'
    yield($stdout)
    $stdout.flush
  else
    open(@output, 'a') do |f|
      yield(f)
    end
  end
end