Class: PasswordIsStrongValidator

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

Constant Summary collapse

COMMON_PASSWORDS =
%w{
	password pass root admin metasploit
	msf 123456 qwerty abc123 letmein monkey link182 demo
	changeme test1234 rapid7
}
SPECIAL_CHARS =
%q{!@"#$%&'()*+,-./:;<=>?[\\]^_`{|}~ }

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/validators/password_is_strong_validator.rb', line 10

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

  if is_simple?(value)
    record.errors[attribute] << "must contain letters, numbers, and at least one special character"
  end

  if contains_username?(record.username, value)
    record.errors[attribute] << "must not contain the username"
  end

  if is_common_password?(value)
    record.errors[attribute] << "must not be a common password"
  end

  if contains_repetition?(value)
    record.errors[attribute] << "must not be a predictable sequence of characters"
  end
end