passwd 0.1.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +28 -0
  3. data/.gitignore +11 -17
  4. data/.rubocop.yml +168 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +3 -6
  7. data/LICENSE +21 -0
  8. data/README.md +89 -138
  9. data/Rakefile +5 -6
  10. data/bin/console +7 -0
  11. data/bin/setup +8 -0
  12. data/lib/generators/passwd/install/USAGE +5 -0
  13. data/lib/generators/passwd/install/install_generator.rb +10 -0
  14. data/lib/generators/passwd/install/templates/passwd.rb +21 -0
  15. data/lib/passwd.rb +33 -6
  16. data/lib/passwd/config.rb +27 -0
  17. data/lib/passwd/errors.rb +4 -0
  18. data/lib/passwd/rails/action_controller_ext.rb +78 -0
  19. data/lib/passwd/rails/active_record_ext.rb +35 -0
  20. data/lib/passwd/railtie.rb +18 -0
  21. data/lib/passwd/version.rb +2 -4
  22. data/passwd.gemspec +20 -11
  23. metadata +83 -53
  24. data/.coveralls.yml +0 -1
  25. data/.travis.yml +0 -10
  26. data/LICENSE.txt +0 -22
  27. data/lib/passwd/active_record.rb +0 -58
  28. data/lib/passwd/base.rb +0 -72
  29. data/lib/passwd/configuration/abstract_config.rb +0 -36
  30. data/lib/passwd/configuration/config.rb +0 -23
  31. data/lib/passwd/configuration/policy.rb +0 -46
  32. data/lib/passwd/configuration/tmp_config.rb +0 -18
  33. data/lib/passwd/password.rb +0 -41
  34. data/samples/activerecord/user.rake +0 -28
  35. data/spec/passwd/active_record_spec.rb +0 -142
  36. data/spec/passwd/base_spec.rb +0 -224
  37. data/spec/passwd/configuration/config_spec.rb +0 -242
  38. data/spec/passwd/configuration/policy_spec.rb +0 -133
  39. data/spec/passwd/configuration/tmp_config_spec.rb +0 -257
  40. data/spec/passwd/password_spec.rb +0 -150
  41. data/spec/spec_helper.rb +0 -23
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fcb00a834c359109c28cbe26c845b8baa2ec93b7
4
- data.tar.gz: 07cff7e7538f4f59889fecad96ab0715edd9ee05
2
+ SHA256:
3
+ metadata.gz: f9a0399d0e6b478da6e96ad38e2806fb32199bae624d5bddf4abd5093d652cf8
4
+ data.tar.gz: 6b7a805295dcd19b924156838b24463cbdd9bf233280027cf3ce49d4abeeb9df
5
5
  SHA512:
6
- metadata.gz: cc8ac70bace4de95e9ae1d9d6f05b16843b1c27d3c2af09b748c38100db9f79105a991da929a387b9bf76733b57a7885d98238fccc27f9b7b9b5ca5bd5af804b
7
- data.tar.gz: b53e2c20abb856f932de3612a63f0cde50d6d7498ec233b7bc2a3652899c6420d63801ee5e1901c1adf7b32b0a9c6a5040f462d4ce3ab7ac21cce4dc0f20d1f9
6
+ metadata.gz: 99d186d8eb981e43d3ece62f8d96c74ec84cb328d5d5fc56338d322cd9e0cef8f626ef405647eaad01e5cb0fda7e1af3f077beed16ec2fe9adc2798bc3ad9fa7
7
+ data.tar.gz: 7498777e9c67f19b18be0e4c1119fa810551bc4a2fc01411ac1ada816879fce1c2b16d9f31635e1f0426aa2c1486af985dc5b47894529a3b31e657457b5ec748
@@ -0,0 +1,28 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: CI
9
+ on:
10
+ push:
11
+ branches: [ master ]
12
+ pull_request:
13
+ branches: [ master ]
14
+ jobs:
15
+ test:
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - name: Set up Ruby
20
+ uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: 2.7.2
23
+ - name: Install dependencies
24
+ run: bundle install
25
+ - name: Run tests
26
+ run: bundle exec rake spec
27
+ - name: Run rubocop
28
+ run: bundle exec rake rubocop
data/.gitignore CHANGED
@@ -1,17 +1,11 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ /Gemfile.lock
11
+ /.rspec_status
data/.rubocop.yml ADDED
@@ -0,0 +1,168 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.7
4
+ Exclude:
5
+ - "node_modules/**/*"
6
+ - "vendor/**/*"
7
+
8
+ # Private methods indent.
9
+ Layout/IndentationConsistency:
10
+ EnforcedStyle: indented_internal_methods
11
+
12
+ # Warning: 120 characters
13
+ # Error: 160 characters
14
+ # Make the library more restrictive.
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ # Multi-line indentation with receiver.
19
+ Layout/MultilineMethodCallIndentation:
20
+ EnforcedStyle: indented_relative_to_receiver
21
+
22
+ Layout/SpaceInsideBlockBraces:
23
+ SpaceBeforeBlockParameters: false
24
+
25
+ Lint/AmbiguousBlockAssociation:
26
+ Exclude:
27
+ - "spec/**/*_spec.rb"
28
+
29
+ # May define constants within the block in spec.
30
+ Lint/ConstantDefinitionInBlock:
31
+ Exclude:
32
+ - "spec/**/*_spec.rb"
33
+
34
+ Lint/InheritException:
35
+ EnforcedStyle: standard_error
36
+
37
+ Lint/UnderscorePrefixedVariableName:
38
+ Enabled: false
39
+
40
+ Lint/UnusedMethodArgument:
41
+ Enabled: false
42
+
43
+ Metrics/AbcSize:
44
+ Max: 24
45
+
46
+ Metrics/BlockLength:
47
+ Exclude:
48
+ - "spec/**/*.rb"
49
+ - "Gemfile"
50
+ - "*.gemspec"
51
+
52
+ Metrics/CyclomaticComplexity:
53
+ Max: 10
54
+
55
+ Metrics/MethodLength:
56
+ Max: 20
57
+
58
+ Security/YAMLLoad:
59
+ Enabled: false
60
+
61
+ Style/Alias:
62
+ EnforcedStyle: prefer_alias_method
63
+
64
+ Style/AndOr:
65
+ EnforcedStyle: conditionals
66
+
67
+ Style/AsciiComments:
68
+ Enabled: false
69
+
70
+ Style/BlockDelimiters:
71
+ Enabled: false
72
+
73
+ Style/ClassAndModuleChildren:
74
+ Enabled: false
75
+
76
+ Style/CollectionMethods:
77
+ PreferredMethods:
78
+ detect: "detect"
79
+ find: "detect"
80
+ inject: "inject"
81
+ reduce: "inject"
82
+
83
+ Style/Documentation:
84
+ Enabled: false
85
+
86
+ Style/DoubleNegation:
87
+ Enabled: false
88
+
89
+ Style/EmptyCaseCondition:
90
+ Enabled: false
91
+
92
+ Style/EmptyElse:
93
+ EnforcedStyle: empty
94
+
95
+ Style/EmptyMethod:
96
+ EnforcedStyle: expanded
97
+
98
+ Style/FormatString:
99
+ EnforcedStyle: percent
100
+
101
+ # Do not use frozen_string_literal comment.
102
+ Style/FrozenStringLiteralComment:
103
+ Enabled: false
104
+
105
+ Style/HashSyntax:
106
+ Exclude:
107
+ - "Rakefile"
108
+
109
+ Style/MultilineBlockChain:
110
+ Enabled: false
111
+
112
+ Style/MixinUsage:
113
+ Exclude:
114
+ - "bin/setup"
115
+
116
+ # Use _ when 7 digits or more.
117
+ Style/NumericLiterals:
118
+ MinDigits: 7
119
+ Strict: true
120
+
121
+ Style/NumericPredicate:
122
+ Enabled: false
123
+
124
+ Style/OrAssignment:
125
+ Enabled: false
126
+
127
+ Style/PercentLiteralDelimiters:
128
+ Enabled: false
129
+
130
+ # `has_xxx?` is more readable.
131
+ Style/PreferredHashMethods:
132
+ EnforcedStyle: verbose
133
+
134
+ # Do not use unnecessary returns. (Allow to return multiple values.)
135
+ Style/RedundantReturn:
136
+ AllowMultipleReturnValues: true
137
+
138
+ # Do not specify error class when rescuing StandardError.
139
+ Style/RescueStandardError:
140
+ EnforcedStyle: implicit
141
+
142
+ # String literals use double quotes.
143
+ Style/StringLiterals:
144
+ EnforcedStyle: double_quotes
145
+
146
+ # String literal inside the string interpolation use double quotes too.
147
+ Style/StringLiteralsInInterpolation:
148
+ EnforcedStyle: double_quotes
149
+
150
+ # Percent(`%i(a b)`) and brackets(`[:a, :b]`) are acceptable.
151
+ Style/SymbolArray:
152
+ Enabled: false
153
+
154
+ # Put a trailing comma in argument list.
155
+ Style/TrailingCommaInArguments:
156
+ EnforcedStyleForMultiline: comma
157
+
158
+ # Put a trailing comma in Array literal.
159
+ Style/TrailingCommaInArrayLiteral:
160
+ EnforcedStyleForMultiline: comma
161
+
162
+ # Put a trailing comma in Hash literal.
163
+ Style/TrailingCommaInHashLiteral:
164
+ EnforcedStyleForMultiline: comma
165
+
166
+ # Percent(`%w(a b)`) and brackets(`["a", "b"]`) are acceptable.
167
+ Style/WordArray:
168
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/Gemfile CHANGED
@@ -1,9 +1,6 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
2
4
 
3
5
  # Specify your gem's dependencies in passwd.gemspec
4
6
  gemspec
5
-
6
- group :test do
7
- gem 'coveralls', require: false
8
- gem 'simplecov', require: false
9
- end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2018 Ken Iiboshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,207 +1,158 @@
1
1
  # Passwd
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/passwd.png)](http://badge.fury.io/rb/passwd)
4
- [![Build Status](https://travis-ci.org/i2bskn/passwd.png?branch=master)](https://travis-ci.org/i2bskn/passwd)
5
- [![Coverage Status](https://coveralls.io/repos/i2bskn/passwd/badge.png?branch=master)](https://coveralls.io/r/i2bskn/passwd?branch=master)
6
- [![Code Climate](https://codeclimate.com/github/i2bskn/passwd.png)](https://codeclimate.com/github/i2bskn/passwd)
3
+ [![Gem Version](https://badge.fury.io/rb/passwd.svg)](http://badge.fury.io/rb/passwd)
7
4
 
8
- Password utilities.
5
+ Passwd is provide hashed password creation and authentication.
9
6
 
10
7
  ## Installation
11
8
 
12
9
  Add this line to your application's Gemfile:
13
10
 
14
11
  ```ruby
15
- gem 'passwd'
12
+ gem "passwd"
16
13
  ```
17
14
 
18
15
  And then execute:
19
16
 
20
- $ bundle
21
-
22
- Or install it yourself as:
23
-
24
- $ gem install passwd
25
-
26
- ## Usage
27
-
28
- ```ruby
29
- require 'passwd'
30
17
  ```
31
-
32
- ### Create random password
33
-
34
- ```ruby
35
- password = Passwd.create
18
+ $ bundle install
36
19
  ```
37
20
 
38
- ### Hashing password
21
+ Create config file(Only Rails) with:
39
22
 
40
- Hashing with SHA1.
41
-
42
- ```ruby
43
- password_hash = Passwd.hashing(password)
44
23
  ```
45
-
46
- ### Password settings
47
-
48
- Default config is stored in the class instance variable.
49
- Changing the default configs are as follows:
50
-
51
- ```ruby
52
- Passwd.config # => Get config object.
53
- Passwd.config(length: 10) # => Change to the default length.
54
-
55
- Passwd.configure do |c|
56
- c.length = 10
57
- end
24
+ $ bundle exec rails generate passwd:install
58
25
  ```
59
26
 
60
- Options that can be specified:
27
+ The following file will be created.
28
+ See [config](https://github.com/i2bskn/passwd/blob/master/lib/generators/passwd/install/templates/passwd.rb) if not Rails.
61
29
 
62
- * :length => Number of characters. default is 8.
63
- * :lower => Skip lower case if set false. default is true.
64
- * :upper => Skip upper case if set false. default is true.
65
- * :number => Skip numbers if set false. default is true.
66
- * :letters_lower => Define an array of lower case. default is ("a".."z").to_a
67
- * :letters_upper => Define an array of upper case. default is ("A".."Z").to_a
68
- * :letters_number => Define an array of numbers. default is ("0".."9").to_a
30
+ - `config/initializers/passwd.rb`
69
31
 
70
- ### Policy check
32
+ ## Usage
71
33
 
72
- Default policy is 8 more characters and require lower case and require number.
34
+ ### Ruby
73
35
 
74
36
  ```ruby
75
- Passwd.policy_check("secret") # => true or false
37
+ passwd = Passwd.current
38
+ passwd.random(10) # Create random password of 10 characters.
39
+ password = passwd.password_hashing("secret") # Create hashed password from plain text.
40
+ password == "secret" # => true
41
+ load_password = passwd.load_password("hashed_password") # Load hashed password.
42
+ load_password == "secret"
76
43
  ```
77
44
 
78
- ### Policy settings
45
+ ### ActiveRecord with Rails
46
+
47
+ Add authentication to your `User` model.
48
+ Model name is `User` by default, but can be changed in configuration file.
79
49
 
80
50
  ```ruby
81
- Passwd.policy_configure do |c|
82
- c.min_length = 10
51
+ class User < ActiveRecord::Base
52
+ with_authenticate
83
53
  end
84
54
  ```
85
55
 
86
- Options that can be specified:
56
+ #### Options
87
57
 
88
- * :min_length => Number of minimum characters. default is 8.
89
- * :require_lower => Require lower case if set true. default is true.
90
- * :require_upper => Require upper case if set true. default is false.
91
- * :require_number => Require number if set true. default is true.
58
+ User model The following column are required.
59
+ Column name can be changed with the specified options.
92
60
 
93
- ### Password object
61
+ - `:id => :email` Unique value to be used for authentication.
62
+ - `:password => :password` Column of String to save the hashed password.
94
63
 
95
- Default password is randomly generated.
96
- Default salt is "#{Time.now.to_s}".
64
+ Use the `name` column as id.
97
65
 
98
66
  ```ruby
99
- password = Passwd::Password.new
100
- password.text # return text password.
101
- password.salt_text # return text salt.
102
- password.salt_hash # return hash salt.
103
- password.hash # return hash password.
67
+ class User < ActiveRecord::Base
68
+ with_authenticate id: :name
69
+ end
104
70
  ```
105
71
 
106
- Options that can be specified:
72
+ #### Authenticate
107
73
 
108
- * :password => Text password. default is random.
109
- * :salt_text => Text salt. default is #{Time.now.to_s}.
110
-
111
- Password authenticate:
74
+ `authenticate` method is available in both instance and class.
75
+ Returns user object if the authentication successful.
76
+ Returns nil if authentication fails or doesn't exists user.
77
+ Instance method is not required `id`.
112
78
 
113
79
  ```ruby
114
- password = Passwd::Password.new
115
- Passwd.auth(password.text, password.salt_hash, password.hash) # => true
116
- Passwd.auth("invalid!!", password.salt_hash, password.hash) # => false
117
-
118
- password == password.text # => true
119
- password == "invalid!!" # => false
80
+ user = User.authenticate(params[:email], params[:password]) # Returns user object or nil.
81
+ user.authenticate(params[:password]) # Returns true if authentication succeeded.
120
82
  ```
121
83
 
122
- ## For ActiveRecord
123
-
124
- ### User model
125
-
126
- Include `Passwd::ActiveRecord` module and define id/salt/password column from `define_column` method.
127
- `id` column is required uniqueness.
84
+ `set_password` method will be set random password.
85
+ To specify password as an argument if you want to specify a password.
128
86
 
129
87
  ```ruby
130
- class User < ActiveRecord::Base
131
- include Passwd::ActiveRecord
132
- # if not specified arguments for define_column => {id: :email, salt: :salt, password: :password}
133
- define_column id: :id_colname, salt: :salt_colname, password: :password_colname
88
+ current_user.set_password("secret") # Set random password if not specified a argument.
89
+ current_user.save
134
90
 
135
- ...
136
- end
91
+ new_user = User.new
92
+ random_plain_password = new_user.set_password
93
+ UserMailer.register(new_user, random_plain_password).deliver!
137
94
  ```
138
95
 
139
- Available following method by defining id/salt/password column.
96
+ ### ActionController
140
97
 
141
- ### Authentication
98
+ Already several methods is available in your controller.
142
99
 
143
- `authenticate` method is available in both instance and class.
144
- Return the user object if the authentication successful.
145
- Return the nil if authentication fails or doesn't exists user.
100
+ If you want to authenticate the application.
101
+ Unauthorized access is thrown exception.
102
+ Can be specified to redirect in configuration file.
146
103
 
147
104
  ```ruby
148
- user = User.authenticate("foo@example.com", "secret") # => return user object or nil.
149
-
150
- if user
151
- puts "Hello #{user.name}!"
152
- else
153
- puts "Authentication failed"
105
+ class ApplicationController < ActionController::Base
106
+ before_action :require_signin
154
107
  end
155
108
  ```
156
109
 
157
- instance method is not required `id`.
110
+ If you want to implement the session management.
158
111
 
159
112
  ```ruby
160
- user = User.find(params[:id])
161
- if user.authenticate("secret") # => return true or false
162
- puts "Authentication is successful!"
163
- else
164
- puts "Authentication failed!"
113
+ class SessionsController < ApplicationController
114
+ # If you has been enabled `require_signin` in ApplicationController
115
+ skip_before_action :require_signin
116
+
117
+ # GET /signin
118
+ def new; end
119
+
120
+ # POST /signin
121
+ def create
122
+ # Returns nil or user
123
+ @user = User.authenticate(params[:email], params[:password])
124
+
125
+ if @user
126
+ # Save user_id to session
127
+ signin(@user)
128
+ redirect_to_referer_or some_path, notice: "Signin was successful. Hello #{current_user.name}"
129
+ else # Authentication fails
130
+ render action: :new
131
+ end
132
+ end
133
+
134
+ # DELETE /signout
135
+ def destroy
136
+ # Clear session (Only user_id)
137
+ signout
138
+ redirect_to some_path
139
+ end
165
140
  end
166
141
  ```
167
142
 
168
- ### Change passowrd
169
-
170
- `set_password` method will be set random password.
171
- Return value is plain text password.
172
- To specify the password as an argument if you want to specify a password.
173
- `salt` also set if salt is nil.
143
+ `current_user` and `signin?` method available in controllers and views.
174
144
 
175
145
  ```ruby
176
- user = User.find(params[:id])
177
- password_text = user.set_password
178
-
179
- if user.save
180
- NoticeMailer.change_mail(user, password_text).deliver
146
+ def greet
147
+ name = signin? ? current_user.name : "Guest"
148
+ render text: "Hello #{name}!!"
181
149
  end
182
150
  ```
183
- `update_password` method will be set new password if the authentication successful.
184
- Return the nil if authentication fails.
185
- But `update_password` method doesn't call `save` method.
186
151
 
187
- ```ruby
188
- @user = User.find(params[:id])
152
+ ## Contributing
189
153
 
190
- if Passwd.confirm_check(params[:new_pass], params[:new_pass_confirm])
191
- if @user.update_password(old_pass, new_pass) && @user.save # => return new password(text) or false
192
- NoticeMailer.change_mail(user, password_text).deliver
193
- else
194
- puts "Authentication failed!"
195
- end
196
- else
197
- puts "Password don't match!"
198
- end
199
- ```
154
+ Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/passwd.
200
155
 
201
- ## Contributing
156
+ ## License
202
157
 
203
- 1. Fork it
204
- 2. Create your feature branch (`git checkout -b my-new-feature`)
205
- 3. Commit your changes (`git commit -am 'Added some feature'`)
206
- 4. Push to the branch (`git push origin my-new-feature`)
207
- 5. Create new Pull Request
158
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).