class StrongPasswordField::Strategies::ZxcvbnStrategy

Public Class Methods

new() click to toggle source
# File lib/strong_password_field/strategies/zxcvbn_strategy.rb, line 6
def initialize
  @strength_lookup = {
      0 => :worst,
      1 => :bad,
      2 => :weak,
      3 => :good,
      4 => :strong
  }

  @tester ||= Zxcvbn::Tester.new
end

Public Instance Methods

validate(model_object) click to toggle source
# File lib/strong_password_field/strategies/zxcvbn_strategy.rb, line 18
def validate(model_object)
  @options = model_object.spf_options
  @password = model_object.send(model_object.spf_password_field)

  result = @tester.test(@password)
  @score = result.score
  add_errors(model_object) if @score < security_score
end

Private Instance Methods

add_errors(model_object) click to toggle source
# File lib/strong_password_field/strategies/zxcvbn_strategy.rb, line 46
def add_errors(model_object)
  model_object.errors.add(:weak_password_error, 'password is not strong enough')
end
security_level() click to toggle source
# File lib/strong_password_field/strategies/zxcvbn_strategy.rb, line 29
def security_level
  @security_level ||= @options.fetch(:security_level, :good)
  unless valid_security_level.include?(@security_level)
    raise StandardError, "security_level: #{@security_level} is not valid,
                          use one of #{valid_security_level.join(', ')}"
  end
  @security_level
end
security_score() click to toggle source
# File lib/strong_password_field/strategies/zxcvbn_strategy.rb, line 38
def security_score
  @security_score ||= @strength_lookup.key(security_level)
end
valid_security_level() click to toggle source
# File lib/strong_password_field/strategies/zxcvbn_strategy.rb, line 42
def valid_security_level
  %i[worst bad weak good strong]
end