password_validation 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5574bb7fc20f382c0343ee838cf97528f07b3b5e
4
+ data.tar.gz: 9d6db0da44b4bca20153f98d17f08d19046ed189
5
+ SHA512:
6
+ metadata.gz: fa23b7db2d044a2d708158397b716dd8b752ba168d06e445c3fe5bf35c59879163390ae8ce24a916aaf7270166449ed19d259525cc42b03e7ef9a7b3102299d2
7
+ data.tar.gz: 53fae074d12aca5146553bf9b4aed2dba17ce9791efa97e50c71106740f76d54a6a20bfc374450db03d4a0d8acf3fc0b2710c58153d5ff64789b77a7698dd552
@@ -0,0 +1,76 @@
1
+ class PasswordValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attr, password)
3
+ opts = {
4
+ length: {minimum: 8, message: ->(opts, _) { "must be at least #{opts[:minimum]} characters" }},
5
+ uppercase: {required: true, message: 'must include an uppercase letter (A-Z)'},
6
+ lowercase: {required: true, message: 'must include a lowercase letter (a-z)'},
7
+ numeral: {required: true, message: 'must include a number (0-9)'},
8
+ special: {required: true, pattern: /[`~!@#$%\^&*()\-_=+\[\]{}\\|;:'",.<>\/?]/, message: ->(opts, _) {
9
+ list = opts[:pattern].source[1..-2].gsub('\\', '')
10
+ if opts[:pattern].source.include?('\\\\')
11
+ list << '\\'
12
+ end
13
+ "must include one of these special characters: #{list}"
14
+ }}
15
+ }.deep_merge(options)
16
+
17
+ errors = record.errors[attr]
18
+ if password
19
+ maybe_validate_length password, errors, opts
20
+ maybe_validate_lowercase password, errors, opts
21
+ maybe_validate_uppercase password, errors, opts
22
+ maybe_validate_numeral password, errors, opts
23
+ maybe_validate_special password, errors, opts
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def maybe_validate_length(password, errors, opts)
30
+ if opts[:length] and opts[:length][:minimum]
31
+ if password.length < opts[:length][:minimum]
32
+ errors << message(opts[:length], password)
33
+ end
34
+ end
35
+ end
36
+
37
+ def maybe_validate_lowercase(password, errors, opts)
38
+ if opts[:lowercase] and opts[:lowercase][:required]
39
+ unless password.match(/[a-z]/)
40
+ errors << message(opts[:lowercase], password)
41
+ end
42
+ end
43
+ end
44
+
45
+ def maybe_validate_numeral(password, errors, opts)
46
+ if opts[:numeral] and opts[:numeral][:required]
47
+ unless password.match(/[0-9]/)
48
+ errors << message(opts[:numeral], password)
49
+ end
50
+ end
51
+ end
52
+
53
+ def maybe_validate_special(password, errors, opts)
54
+ if opts[:special] and opts[:special][:pattern] and opts[:special][:required]
55
+ unless password.match(opts[:special][:pattern])
56
+ errors << message(opts[:special], password)
57
+ end
58
+ end
59
+ end
60
+
61
+ def maybe_validate_uppercase(password, errors, opts)
62
+ if opts[:uppercase] and opts[:uppercase][:required]
63
+ unless password.match(/[A-Z]/)
64
+ errors << message(opts[:uppercase], password)
65
+ end
66
+ end
67
+ end
68
+
69
+ def message(opts, password)
70
+ if opts[:message].is_a? Proc
71
+ opts[:message].call opts, password
72
+ else
73
+ opts[:message]
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module PasswordValidation
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_model'
2
+
3
+ require 'password_validation/version'
4
+ require 'password_validation/validators'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: password_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jarrett Colby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 5.4.1
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '5'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 5.4.1
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '5'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest-reporters
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 1.0.5
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '1'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.5
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1'
81
+ description: Provides validates :password for ActiveModel
82
+ email: jarrett@madebyhq.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - lib/password_validation.rb
88
+ - lib/password_validation/validators.rb
89
+ - lib/password_validation/version.rb
90
+ homepage: https://github.com/jarrett/password_validation
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Password Validation
114
+ test_files: []