Sha256: 1218ec819744d3567c0771553955f832c44f6771b0385a82651688268d9d3d1d

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

class Miam::PasswordManager
  include Miam::Logger::Helper

  LOWERCASES = ('a'..'z').to_a
  UPPERCASES = ('A'..'Z').to_a
  NUMBERS = ('0'..'9').to_a
  SYMBOLS = "!@\#$%^&*()_+-=[]{}|'".split(//)

  def initialize(output, options = {})
    @output = output
    @options = options
  end

  def identify(user, type, policy)
    password = mkpasswd(policy)
    log(:info, "mkpasswd: #{password}")
    puts_password(user, type, password)
    password
  end

  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

  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

  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
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
miam-0.2.4.beta12 lib/miam/password_manager.rb
miam-0.2.4.beta11 lib/miam/password_manager.rb
miam-0.2.4.beta10 lib/miam/password_manager.rb
miam-0.2.4.beta9 lib/miam/password_manager.rb
miam-0.2.4.beta8 lib/miam/password_manager.rb
miam-0.2.4.beta7 lib/miam/password_manager.rb