class Auth0Password

Constants

ALL_CHARS
DEFAULT_LENGTH
LOWERCASES
NUMBERS
SPECIAL_CHARS
UPPERCASES
VERSION

Public Class Methods

new(min_length: DEFAULT_LENGTH) click to toggle source
# File lib/auth0_password.rb, line 13
def initialize(min_length: DEFAULT_LENGTH)
  @min_length = min_length
end

Public Instance Methods

excellent(length=DEFAULT_LENGTH) click to toggle source
# File lib/auth0_password.rb, line 17
def excellent(length=DEFAULT_LENGTH)
  good_password = good(roundup_length(length))
  replace_continuous_chars!(good_password)
end
fair(length=DEFAULT_LENGTH) click to toggle source
# File lib/auth0_password.rb, line 27
def fair(length=DEFAULT_LENGTH)
  required_chars = [random_lowercase, random_uppercase, random_number]
  add_random_chars(LOWERCASES + UPPERCASES + NUMBERS, required_chars, roundup_length(length))
end
good(length=DEFAULT_LENGTH) click to toggle source
# File lib/auth0_password.rb, line 22
def good(length=DEFAULT_LENGTH)
  required_chars = [random_lowercase, random_uppercase, random_number, random_special_char]
  add_random_chars(ALL_CHARS, required_chars, roundup_length(length))
end
low(length=DEFAULT_LENGTH) click to toggle source
# File lib/auth0_password.rb, line 32
def low(length=DEFAULT_LENGTH)
  roundup_length(length).times.map { ALL_CHARS.sample }.join
end

Private Instance Methods

add_random_chars(chars, required_chars, length) click to toggle source
# File lib/auth0_password.rb, line 38
def add_random_chars(chars, required_chars, length)
  additional_char_length = length - required_chars.size
  random_chars = additional_char_length.times.map { chars.sample }
  (required_chars + random_chars).shuffle.join
end
logger() click to toggle source
# File lib/auth0_password.rb, line 74
def logger
  @logger ||= Logger.new(STDOUT)
end
random_lowercase() click to toggle source
# File lib/auth0_password.rb, line 58
def random_lowercase
  LOWERCASES.sample
end
random_number() click to toggle source
# File lib/auth0_password.rb, line 66
def random_number
  NUMBERS.sample
end
random_special_char() click to toggle source
# File lib/auth0_password.rb, line 70
def random_special_char
  SPECIAL_CHARS.sample
end
random_uppercase() click to toggle source
# File lib/auth0_password.rb, line 62
def random_uppercase
  UPPERCASES.sample
end
replace_continuous_chars!(password) click to toggle source
# File lib/auth0_password.rb, line 49
def replace_continuous_chars!(password)
  password.tap do |pw|
    while m = /(.)\1{2,}/.match(pw) do
      excepted_chars = ALL_CHARS - [m[1]]
      pw[m.begin(0) + 1] = excepted_chars.sample
    end
  end
end
roundup_length(length) click to toggle source
# File lib/auth0_password.rb, line 44
def roundup_length(length)
  logger.warn("length parameter #{length} is less than min_length #{@min_length}") if @min_length > length
  @min_length > length ? @min_length : length
end