strong_password_field 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 68384e3f32d4684b5bf4d10172c5e8225ec45d229933269b8e9bf16e8579b9d2
4
+ data.tar.gz: a547b1cbaee57b341bd614453d16d5989f9caecee6f177f167e59e49481ad998
5
+ SHA512:
6
+ metadata.gz: 0dd7fadd29ad02aec724d3cf4ce15e5f932acaa232daa1561ff5f66d95c7c8f8f333ddb7ff61da530a101efc1796cf3daccf9c2035dd9182ce57a030ea2cf213
7
+ data.tar.gz: ac8eed6aec68d50cebaefd5333c26206a0001649f59acb6befdb6f1178843c37e85d8039fc4e1be37bd870cfa6995d01ac680586c91837501e9de820efee14e6
@@ -0,0 +1,20 @@
1
+ Copyright 2018 gaotongfei
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ # StrongPasswordField
2
+ Rails engine for password strength checking
3
+
4
+ ## Usage
5
+
6
+ ```ruby
7
+ class User < ApplicationRecord
8
+ include StrongPasswordField
9
+
10
+ has_strong_password :password
11
+ end
12
+ ```
13
+
14
+ ```html
15
+ <%= form_for @user do |f| %>
16
+ <%= f.strong_password_field :password %>
17
+ <div class="password-strength-meter">
18
+ </div>
19
+ <% end %>
20
+ ```
21
+
22
+ ```javascript
23
+ //= require strong_password_field
24
+ //= require zxcvbn
25
+
26
+ document.addEventListener('DOMContentLoaded', function() {
27
+ var strongPasswordField = new StrongPasswordField({
28
+ strengthMeterSelector: '.password-strength-meter'
29
+ });
30
+ strongPasswordField.validate();
31
+ });
32
+ ```
33
+
34
+ ## Installation
35
+ Add this line to your application's Gemfile:
36
+
37
+ ```ruby
38
+ gem 'strong_password_field'
39
+ ```
40
+
41
+ And then execute:
42
+ ```bash
43
+ $ bundle
44
+ ```
45
+
46
+ Or install it yourself as:
47
+ ```bash
48
+ $ gem install strong_password_field
49
+ ```
50
+
51
+ ## Contributing
52
+ Contribution directions go here.
53
+
54
+ ## License
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'StrongPasswordField'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,36 @@
1
+ require 'strong_password_field/railtie'
2
+ require 'strong_password_field/form_helpers'
3
+ require 'strong_password_field/strategies/zxcvbn_strategy'
4
+
5
+ module StrongPasswordField
6
+ def self.included(base)
7
+ base.send :include, InstanceMethods
8
+ base.extend ClassMethods
9
+
10
+ base.class_attribute :spf_password_field
11
+ base.class_attribute :strategy
12
+ base.class_attribute :minimal_strength
13
+ # available security levels are: [:worst, :bad, :weak, :good, :strong]
14
+ base.class_attribute :spf_options
15
+
16
+ base.validate :strong_password_validate
17
+ end
18
+
19
+ module ClassMethods
20
+ def has_strong_password(field=:password, **options)
21
+ self.spf_password_field = field
22
+ self.spf_options = options
23
+ end
24
+
25
+ end
26
+
27
+ module InstanceMethods
28
+ def strong_password_validate(strategy_adapter = Strategies::ZxcvbnStrategy.new)
29
+ validate(strategy_adapter, self)
30
+ end
31
+
32
+ def validate(strategy_adapter, model_object)
33
+ strategy_adapter.validate(model_object)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ module StrongPasswordField
2
+ module FormHelper
3
+ def strong_password_field(object_name, method, options = {})
4
+ options.merge!(data: { strong_password: true })
5
+ password_field(object_name, method, options)
6
+ end
7
+ end
8
+
9
+ module FormBuilder
10
+ def strong_password_field(method, options = {})
11
+ @template.strong_password_field(@object_name, method, options)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module StrongPasswordField
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ initializer 'strong_password_field.form_helpers' do
5
+ ActionView::Helpers::FormHelper.send(:include, StrongPasswordField::FormHelper)
6
+ ActionView::Helpers::FormBuilder.send(:include, StrongPasswordField::FormBuilder)
7
+ end
8
+
9
+ config.assets.paths << File.expand_path("../../../vendor/assets/javascripts", __FILE__)
10
+ config.assets.paths << File.expand_path("../../../vendor/assets/stylesheets", __FILE__)
11
+ end
12
+ end
@@ -0,0 +1,51 @@
1
+ require 'zxcvbn'
2
+
3
+ module StrongPasswordField
4
+ module Strategies
5
+ class ZxcvbnStrategy
6
+ def initialize
7
+ @strength_lookup = {
8
+ 0 => :worst,
9
+ 1 => :bad,
10
+ 2 => :weak,
11
+ 3 => :good,
12
+ 4 => :strong
13
+ }
14
+
15
+ @tester ||= Zxcvbn::Tester.new
16
+ end
17
+
18
+ def validate(model_object)
19
+ @options = model_object.spf_options
20
+ @password = model_object.send(model_object.spf_password_field)
21
+
22
+ result = @tester.test(@password)
23
+ @score = result.score
24
+ add_errors(model_object) if @score < security_score
25
+ end
26
+
27
+ private
28
+
29
+ def security_level
30
+ @security_level ||= @options.fetch(:security_level, :good)
31
+ unless valid_security_level.include?(@security_level)
32
+ raise StandardError, "security_level: #{@security_level} is not valid,
33
+ use one of #{valid_security_level.join(', ')}"
34
+ end
35
+ @security_level
36
+ end
37
+
38
+ def security_score
39
+ @security_score ||= @strength_lookup.key(security_level)
40
+ end
41
+
42
+ def valid_security_level
43
+ %i[worst bad weak good strong]
44
+ end
45
+
46
+ def add_errors(model_object)
47
+ model_object.errors.add(:weak_password_error, 'password is not strong enough')
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module StrongPasswordField
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :strong_password_field do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: strong_password_field
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gaotongfei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: zxcvbn-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rails engine for password strength checking
70
+ email:
71
+ - gaotongfei1995@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/strong_password_field.rb
80
+ - lib/strong_password_field/form_helpers.rb
81
+ - lib/strong_password_field/railtie.rb
82
+ - lib/strong_password_field/strategies/zxcvbn_strategy.rb
83
+ - lib/strong_password_field/version.rb
84
+ - lib/tasks/strong_password_field_tasks.rake
85
+ homepage: https://github.com/gaotongfei/strong_password_field
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.7
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Rails engine for password strength checking
109
+ test_files: []