password_validator 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/password_validator.rb +54 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67d52b7421abf276459e23cf901c256418103032
|
4
|
+
data.tar.gz: 0c9e521d4e00022de645216839e223ba004c4e0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20dc7b341ab2d361d2ac7d3973c54133d3658a180a37a4d9c8e2e2be85941bfbf06ed634d05ed245cfd037210366b3be736738fca425874f6ff655fda0598a0a
|
7
|
+
data.tar.gz: 141157b5e361c0dea91c595f3ebc80017249a3295b5a1b6241dacdb0e029f680c77f613775c2751e82814b62ab1625ccedd29ee0a293796ce113916a58a2383a
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class PasswordValidator < ActiveModel::Validator
|
4
|
+
|
5
|
+
# Using multiple validators for passwords sucks because either the user receives a flood of
|
6
|
+
# (often redundant) errors, or each validation must become conditional, which can get complex
|
7
|
+
# fairly quickly.
|
8
|
+
#
|
9
|
+
# This class attempts to perform validations in a specific order of importance and only displays
|
10
|
+
# the errors that are relevant.
|
11
|
+
#
|
12
|
+
# todo: add tests
|
13
|
+
# todo: add more options
|
14
|
+
# todo: check for password strength
|
15
|
+
# todo: use I18n for errors
|
16
|
+
|
17
|
+
def validate(record)
|
18
|
+
@password = record.try(:password)
|
19
|
+
@password_confirmation = record.try(:password_confirmation)
|
20
|
+
|
21
|
+
case
|
22
|
+
when blank?
|
23
|
+
record.errors.add(:password, "can't be blank") unless options[:allow_blank]
|
24
|
+
when too_short?
|
25
|
+
record.errors.add(:password, "must be a minimum of #{options[:min_length]} characters in length")
|
26
|
+
when too_common?
|
27
|
+
record.errors.add(:password, "is too common")
|
28
|
+
when not_confirmed?
|
29
|
+
record.errors.add(:password_confirmation, "doesn't match password")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def blank?
|
36
|
+
@password.blank?
|
37
|
+
end
|
38
|
+
|
39
|
+
def too_short?
|
40
|
+
options[:min_length] && @password.length < options[:min_length]
|
41
|
+
end
|
42
|
+
|
43
|
+
def too_common?
|
44
|
+
# todo: improve this
|
45
|
+
common_passwords = %w(password qwerty abc123 abcdef 123456 111111)
|
46
|
+
options[:common] && common_passwords.include?(@password)
|
47
|
+
end
|
48
|
+
|
49
|
+
def not_confirmed?
|
50
|
+
options[:confirmation] && @password_confirmation != @password
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: password_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Anthes
|
@@ -30,7 +30,8 @@ email:
|
|
30
30
|
executables: []
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
|
-
files:
|
33
|
+
files:
|
34
|
+
- lib/password_validator.rb
|
34
35
|
homepage: https://github.com/bloomdido/password_validator
|
35
36
|
licenses:
|
36
37
|
- MIT
|