Class: PasswordValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/password_validator.rb

Overview

Class is used to verify that the user’s password is strong enough

Constant Summary collapse

MINIMUM_LENGTH =
10
MAX_LENGTH =
256
MIN_UNIQUE_CHARACTERS =
5
IGNORE_SIMILARITY_SHORTER_THAN =
4
ADMIN_MINIMUM_LENGTH =
Decidim.config.admin_password_min_length
ADMIN_REPETITION_TIMES =
Decidim.config.admin_password_repetition_times
VALIDATION_METHODS =
[
  :password_too_short?,
  :password_too_long?,
  :not_enough_unique_characters?,
  :name_included_in_password?,
  :nickname_included_in_password?,
  :email_included_in_password?,
  :domain_included_in_password?,
  :password_too_common?,
  :blacklisted?,
  :password_repeated?
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.minimum_length_for(record) ⇒ Object



24
25
26
27
28
# File 'app/validators/password_validator.rb', line 24

def self.minimum_length_for(record)
  return ADMIN_MINIMUM_LENGTH if record.try(:admin?) && Decidim.config.admin_password_strong

  MINIMUM_LENGTH
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Check if user’s password is strong enough

record - Instance of a form (e.g. Decidim::RegistrationForm) or model attribute - “password” value - Actual password Returns true if password is strong enough



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/validators/password_validator.rb', line 36

def validate_each(record, attribute, value)
  return false if value.blank?

  @record = record
  @attribute = attribute
  @value = value
  @weak_password_reasons = []

  return true if strong?

  @weak_password_reasons.each do |reason|
    record.errors.add attribute, get_message(reason)
  end

  false
end