passwd 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -1
  3. data/CHANGELOG.md +30 -1
  4. data/Gemfile +0 -5
  5. data/LICENSE.txt +2 -1
  6. data/README.md +96 -156
  7. data/Rakefile +2 -1
  8. data/example/.gitignore +16 -0
  9. data/example/Gemfile +25 -0
  10. data/example/README.rdoc +28 -0
  11. data/example/Rakefile +6 -0
  12. data/example/app/assets/images/.keep +0 -0
  13. data/example/app/assets/javascripts/application.js +16 -0
  14. data/example/app/assets/stylesheets/application.css +16 -0
  15. data/example/app/controllers/application_controller.rb +10 -0
  16. data/example/app/controllers/concerns/.keep +0 -0
  17. data/example/app/controllers/profiles_controller.rb +28 -0
  18. data/example/app/controllers/root_controller.rb +5 -0
  19. data/example/app/controllers/sessions_controller.rb +29 -0
  20. data/example/app/helpers/application_helper.rb +2 -0
  21. data/example/app/mailers/.keep +0 -0
  22. data/example/app/models/.keep +0 -0
  23. data/example/app/models/concerns/.keep +0 -0
  24. data/example/app/models/user.rb +4 -0
  25. data/example/app/views/layouts/application.html.erb +15 -0
  26. data/example/app/views/profiles/edit.html.erb +14 -0
  27. data/example/app/views/profiles/show.html.erb +12 -0
  28. data/example/app/views/root/index.html.erb +5 -0
  29. data/example/app/views/sessions/new.html.erb +6 -0
  30. data/example/bin/bundle +3 -0
  31. data/example/bin/rails +4 -0
  32. data/example/bin/rake +4 -0
  33. data/example/config.ru +4 -0
  34. data/example/config/application.rb +40 -0
  35. data/example/config/boot.rb +4 -0
  36. data/example/config/database.yml +26 -0
  37. data/example/config/environment.rb +5 -0
  38. data/example/config/environments/development.rb +37 -0
  39. data/example/config/environments/production.rb +78 -0
  40. data/example/config/environments/test.rb +39 -0
  41. data/example/config/initializers/assets.rb +8 -0
  42. data/example/config/initializers/backtrace_silencers.rb +7 -0
  43. data/example/config/initializers/cookies_serializer.rb +3 -0
  44. data/example/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/example/config/initializers/inflections.rb +16 -0
  46. data/example/config/initializers/mime_types.rb +4 -0
  47. data/example/config/initializers/passwd.rb +41 -0
  48. data/example/config/initializers/session_store.rb +3 -0
  49. data/example/config/initializers/wrap_parameters.rb +14 -0
  50. data/example/config/locales/en.yml +23 -0
  51. data/example/config/routes.rb +16 -0
  52. data/example/config/secrets.yml +22 -0
  53. data/example/db/migrate/20141122165914_create_users.rb +13 -0
  54. data/example/db/schema.rb +25 -0
  55. data/example/db/seeds.rb +7 -0
  56. data/example/lib/assets/.keep +0 -0
  57. data/example/lib/tasks/.keep +0 -0
  58. data/example/lib/tasks/user.rake +12 -0
  59. data/example/log/.keep +0 -0
  60. data/example/public/404.html +67 -0
  61. data/example/public/422.html +67 -0
  62. data/example/public/500.html +66 -0
  63. data/example/public/favicon.ico +0 -0
  64. data/example/public/robots.txt +5 -0
  65. data/example/vendor/assets/javascripts/.keep +0 -0
  66. data/example/vendor/assets/stylesheets/.keep +0 -0
  67. data/lib/generators/passwd/config_generator.rb +13 -0
  68. data/lib/generators/passwd/templates/passwd_config.rb +41 -0
  69. data/lib/passwd.rb +18 -3
  70. data/lib/passwd/action_controller_ext.rb +48 -0
  71. data/lib/passwd/active_record_ext.rb +65 -0
  72. data/lib/passwd/base.rb +17 -62
  73. data/lib/passwd/configuration.rb +82 -0
  74. data/lib/passwd/errors.rb +6 -13
  75. data/lib/passwd/password.rb +73 -25
  76. data/lib/passwd/policy.rb +28 -0
  77. data/lib/passwd/railtie.rb +19 -0
  78. data/lib/passwd/salt.rb +50 -0
  79. data/lib/passwd/version.rb +2 -1
  80. data/passwd.gemspec +8 -2
  81. data/spec/passwd/.keep +0 -0
  82. data/spec/passwd/active_record_ext_spec.rb +80 -0
  83. data/spec/passwd/base_spec.rb +55 -231
  84. data/spec/passwd/configuration_spec.rb +50 -0
  85. data/spec/passwd/password_spec.rb +129 -123
  86. data/spec/spec_helper.rb +14 -3
  87. data/spec/support/data_util.rb +11 -0
  88. data/spec/support/paths.rb +2 -0
  89. metadata +164 -30
  90. data/lib/passwd/active_record.rb +0 -62
  91. data/lib/passwd/configuration/abstract_config.rb +0 -37
  92. data/lib/passwd/configuration/config.rb +0 -24
  93. data/lib/passwd/configuration/policy.rb +0 -46
  94. data/lib/passwd/configuration/tmp_config.rb +0 -18
  95. data/spec/passwd/active_record_spec.rb +0 -163
  96. data/spec/passwd/configuration/config_spec.rb +0 -250
  97. data/spec/passwd/configuration/policy_spec.rb +0 -133
  98. data/spec/passwd/configuration/tmp_config_spec.rb +0 -265
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a606e91758a61195dc7a2f4a82a16ce1d845c68e
4
- data.tar.gz: 70f0ece850a255a83972a3ac47c4eb3a6a994a1d
3
+ metadata.gz: 15f7af650246f9de96bc2aa84b2a6be0f7057b43
4
+ data.tar.gz: 9d30c7e3e157b1bd21942502ec7664d0733650b9
5
5
  SHA512:
6
- metadata.gz: 0cd8cd9771d2252d9c0123da43ac2324fcd7d33cb194accb5bff78c4732d7713d61d7b8556513a6b4a747aeb14e35473d873d3731f0ad9e4cfad56347bd98fb2
7
- data.tar.gz: 9a78a83972cbf53c3a8ded6caa0ee7e84b58d0598c3a8b368d710ead163e63b1e114d4ecb47bd2d91b29f151845c4cee4ea391fb0b86aa7b2961390cae92100b
6
+ metadata.gz: 2fec9addec10e3bec27923d51a94ceea7a5d6c82c12085fd8639990dd23ac6c922899b2ca01e1959aad83438a99a48dbc695df2e06c006c70d5733ae9dc92291
7
+ data.tar.gz: c1da0f7170c0ee57591d584cbc7bb72f3f37461dd52d6bc95877ce305785527f0f3e1a9a8f3d1f36aaca620316ff59d585331d16677aa01e030cd9ebd89c1246
@@ -1,7 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
4
+ - 2.1.0
5
+ branches:
6
+ only:
7
+ - master
5
8
  gemfile:
6
9
  - Gemfile
7
10
  script: bundle exec rake spec
@@ -1,6 +1,35 @@
1
+ ## 0.2.0
2
+
3
+ Remake the this library.
4
+
5
+ #### Upgrade
6
+
7
+ 1. Run the following commands.
8
+
9
+ ```
10
+ $ bundle update passwd
11
+ $ bundle exec rails gneratate passwd:config
12
+ ```
13
+
14
+ 2. Migrate your passwd settings to `config/initializers/passwd.rb`.
15
+ 3. Updates your code!
16
+
17
+ #### Changes
18
+
19
+ - Add extention to ActiveController.
20
+ - Add `current_user`, `signin!` and `signout!` to ActionController.
21
+ - Add `require_signin` method for `before_action`.
22
+ - Include the `Passwd::ActiveRecord` was no longer needed.
23
+ - Rename method `define_column` to `with_authenticate` in your User model.
24
+ - Rename method `Passwd.create` to `Passwd.random`.
25
+ - Rename method `Passwd.hashing` to `Passwd.digest`.
26
+ - Add `passwd` method User class. Create Passwd::Password object from target user attributes.
27
+ - Split object password and salt.
28
+
1
29
  ## 0.1.5
2
30
 
3
- Features:
31
+ #### Changes
4
32
 
5
33
  - Can be specified algorithm of hashing
6
34
  - Change default hashing algorithm to SHA512 from SHA1
35
+
data/Gemfile CHANGED
@@ -2,8 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in passwd.gemspec
4
4
  gemspec
5
-
6
- group :test do
7
- gem 'coveralls', require: false
8
- gem 'simplecov', require: false
9
- end
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 i2bskn
1
+ Copyright (c) 2013-2014 i2bskn
2
2
 
3
3
  MIT License
4
4
 
@@ -20,3 +20,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.md CHANGED
@@ -1,230 +1,170 @@
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)
4
+ [![Build Status](https://travis-ci.org/i2bskn/passwd.svg?branch=master)](https://travis-ci.org/i2bskn/passwd)
5
+ [![Coverage Status](https://img.shields.io/coveralls/i2bskn/passwd.svg)](https://coveralls.io/r/i2bskn/passwd?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/i2bskn/passwd/badges/gpa.svg)](https://codeclimate.com/github/i2bskn/passwd)
7
7
 
8
- Password utilities.
8
+ Password utilities and integration to Rails.
9
9
 
10
10
  ## Installation
11
11
 
12
12
  Add this line to your application's Gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'passwd'
15
+ gem "passwd"
16
16
  ```
17
17
 
18
18
  And then execute:
19
19
 
20
20
  $ bundle
21
21
 
22
- Or install it yourself as:
23
-
24
- $ gem install passwd
25
-
26
22
  ## Usage
27
23
 
28
- ```ruby
29
- require 'passwd'
30
- ```
24
+ ### ActiveRecord with Rails
31
25
 
32
- ### Create random password
26
+ Add authentication to your `User` model.
27
+ Model name is `User` by default, but can be changed in configuration file.
33
28
 
34
29
  ```ruby
35
- password = Passwd.create
30
+ class User < ActiveRecord::Base
31
+ with_authenticate
32
+ end
36
33
  ```
37
34
 
38
- ### Hashing password
35
+ #### Options
39
36
 
40
- Hashing with SHA1.
37
+ User model The following column are required.
38
+ Column name can be changed with the specified options.
41
39
 
42
- ```ruby
43
- password_hash = Passwd.hashing(password)
44
- ```
45
-
46
- ### Password settings
40
+ - `:id => :email` Unique value to be used for authentication.
41
+ - `:salt => :salt` Column of String to save the salt.
42
+ - `:password => :password` Column of String to save the hashed password.
47
43
 
48
- Default config is stored in the class instance variable.
49
- Changing the default configs are as follows:
44
+ Use the `name` column as id.
50
45
 
51
46
  ```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.algorithm = :sha512
57
- c.length = 10
47
+ class User < ActiveRecord::Base
48
+ with_authenticate id: :name
58
49
  end
59
50
  ```
60
51
 
61
- Options that can be specified:
62
-
63
- * :algorithm => Hashing algorithm. default is :sha512.
64
- * :length => Number of characters. default is 8.
65
- * :lower => Skip lower case if set false. default is true.
66
- * :upper => Skip upper case if set false. default is true.
67
- * :number => Skip numbers if set false. default is true.
68
- * :letters_lower => Define an array of lower case. default is ("a".."z").to_a
69
- * :letters_upper => Define an array of upper case. default is ("A".."Z").to_a
70
- * :letters_number => Define an array of numbers. default is ("0".."9").to_a
52
+ #### Authenticate
71
53
 
72
- ### Policy check
73
-
74
- Default policy is 8 more characters and require lower case and require number.
54
+ `authenticate` method is available in both instance and class.
55
+ Returns user object if the authentication successful.
56
+ Returns nil if authentication fails or doesn't exists user.
57
+ Instance method is not required `id`.
75
58
 
76
59
  ```ruby
77
- Passwd.policy_check("secret") # => true or false
60
+ user = User.authenticate(params[:email], params[:password]) # => return user object or nil.
61
+ user.authenticate(params[:password])
78
62
  ```
79
63
 
80
- ### Policy settings
64
+ `set_password` method will be set random password.
65
+ To specify password as an argument if you want to specify a password.
81
66
 
82
67
  ```ruby
83
- Passwd.policy_configure do |c|
84
- c.min_length = 10
85
- end
86
- ```
87
-
88
- Options that can be specified:
89
-
90
- * :min_length => Number of minimum characters. default is 8.
91
- * :require_lower => Require lower case if set true. default is true.
92
- * :require_upper => Require upper case if set true. default is false.
93
- * :require_number => Require number if set true. default is true.
68
+ current_user.set_password("secret") # => random password if not specified a argument.
69
+ current_user.passwd.plain # => new password
70
+ current_user.save
94
71
 
95
- ### Password object
72
+ new_user = User.new
73
+ password = new_user.passwd.plain
74
+ UserMailer.register(new_user, password).deliver!
75
+ ```
96
76
 
97
- Default password is randomly generated.
98
- Default salt is "#{Time.now.to_s}".
77
+ `update_password` method will be set new password if the authentication successful.
78
+ But `update_password` method doesn't call `save` method.
99
79
 
100
80
  ```ruby
101
- password = Passwd::Password.new
102
- password.text # return text password.
103
- password.salt_text # return text salt.
104
- password.salt_hash # return hash salt.
105
- password.hash # return hash password.
81
+ # update_password(OLD_PASSWORD, NEW_PASSWORD[, POLICY_CHECK=false])
82
+ current_user.update_password(old_pass, new_pass, true)
83
+ current_user.save
106
84
  ```
107
85
 
108
- Options that can be specified:
109
-
110
- * :password => Text password. default is random.
111
- * :salt_text => Text salt. default is #{Time.now.to_s}.
86
+ #### Policy check
112
87
 
113
- Password authenticate:
88
+ Default policy is 8 more characters and require lower case and require number.
89
+ Can be changed in configuration file.
114
90
 
115
91
  ```ruby
116
- password = Passwd::Password.new
117
- Passwd.auth(password.text, password.salt_hash, password.hash) # => true
118
- Passwd.auth("invalid!!", password.salt_hash, password.hash) # => false
119
-
120
- password == password.text # => true
121
- password == "invalid!!" # => false
92
+ Passwd.policy_check("secret") # => true or false
122
93
  ```
123
94
 
124
- ## For ActiveRecord
95
+ ### ActionController
125
96
 
126
- ### User model
97
+ Already several methods is available in your controller.
127
98
 
128
- Include `Passwd::ActiveRecord` module and define id/salt/password column from `define_column` method.
129
- `id` column is required uniqueness.
99
+ If you want to authenticate the application.
100
+ Unauthorized access is thrown exception.
101
+ Can be specified to redirect in configuration file.
130
102
 
131
103
  ```ruby
132
- class User < ActiveRecord::Base
133
- include Passwd::ActiveRecord
134
- # if not specified arguments for define_column => {id: :email, salt: :salt, password: :password}
135
- define_column id: :id_colname, salt: :salt_colname, password: :password_colname
136
-
137
- ...
104
+ class ApplicationController < ActionController::Base
105
+ before_action :require_signin
138
106
  end
139
107
  ```
140
108
 
141
- Available following method by defining id/salt/password column.
142
-
143
- ### Authentication
144
-
145
- `authenticate` method is available in both instance and class.
146
- Return the user object if the authentication successful.
147
- Return the nil if authentication fails or doesn't exists user.
109
+ If you want to implement the session management.
148
110
 
149
111
  ```ruby
150
- user = User.authenticate(params[:email], params[:password]) # => return user object or nil.
151
-
152
- if user
153
- session[:user] = user.id
154
- redirect_to bar_path, notice: "Hello #{user.name}!"
155
- else
156
- flash.now[:alert] = "Authentication failed"
157
- render action: :new
112
+ class SessionsController < ApplicationController
113
+ # If you has been enabled `require_signin` in ApplicationController
114
+ skip_before_action :require_signin
115
+
116
+ # GET /signin
117
+ def new; end
118
+
119
+ # POST /signin
120
+ def create
121
+ # Returns nil or user
122
+ @user = User.authenticate(params[:email], params[:password])
123
+
124
+ if @user
125
+ # Save user_id to session
126
+ signin!(@user)
127
+ redirect_to some_url, notice: "Signin was successful. Hello #{current_user.name}"
128
+ else # Authentication fails
129
+ render action: :new
130
+ end
131
+ end
132
+
133
+ # DELETE /signout
134
+ def destroy
135
+ # Clear session (Only user_id)
136
+ signout!
137
+ redirect_to some_url
138
+ end
158
139
  end
159
140
  ```
160
141
 
161
- instance method is not required `id`.
142
+ `current_user` method available if already signin.
162
143
 
163
144
  ```ruby
164
- current_user = User.find(session[:user])
165
-
166
- if current_user.authenticate(params[:password]) # => return true or false
167
- # some process
168
- redirect_to bar_path, notice: "Some process is successfully"
169
- else
170
- flash.now[:alert] = "Authentication failed"
171
- render action: :edit
145
+ # app/controllers/greet_controller.rb
146
+ def greet
147
+ render text: "Hello #{current_user.name}!!"
172
148
  end
173
- ```
174
149
 
175
- ### Change passowrd
150
+ # app/views/greet/greet.html.erb
151
+ <p>Hello <%= current_user.name %>!!<p>
152
+ ```
176
153
 
177
- `set_password` method will be set random password.
178
- Return value is plain text password.
179
- To specify the password as an argument if you want to specify a password.
180
- `salt` also set if salt is nil.
154
+ ### Generate configuration file
181
155
 
182
- ```ruby
183
- current_user = User.find(session[:user])
184
- password_text = current_user.set_password
156
+ Run generator of Rails.
157
+ Configuration file created to `config/initializers/passwd.rb`.
185
158
 
186
- if current_user.save
187
- redirect_to bar_path, notice: "Password update successfully"
188
- else
189
- render action: :edit
190
- end
191
159
  ```
192
-
193
- `update_password` method will be set new password if the authentication successful.
194
- But `update_password` method doesn't call `save` method.
195
-
196
- ```ruby
197
- current_user = User.find(session[:user])
198
-
199
- begin
200
- Passwd.confirm_check(params[:password], params[:password_confirmation])
201
- # update_password(OLD_PASSWORD, NEW_PASSWORD[, POLICY_CHECK=false])
202
- current_user.update_password(old_pass, new_pass, true)
203
- current_user.save!
204
- redirect_to bar_path, notice: "Password updated successfully"
205
- rescue Passwd::PasswordNotMatch
206
- # PASSWORD != PASSWORD_CONFIRMATION from Passwd.#confirm_check
207
- flash.now[:alert] = "Password not match"
208
- render action: :edit
209
- rescue Passwd::AuthError
210
- # Authentication failed from #update_password
211
- flash.now[:alert] = "Password is incorrect"
212
- render action: :edit
213
- rescue Passwd::PolicyNotMatch
214
- # Policy not match from #update_password
215
- flash.now[:alert] = "Policy not match"
216
- render action: :edit
217
- rescue
218
- # Other errors
219
- flash.now[:alert] = "Password update failed"
220
- render action: :edit
221
- end
160
+ $ bundle exec rails generate passwd:config
222
161
  ```
223
162
 
224
163
  ## Contributing
225
164
 
226
- 1. Fork it
165
+ 1. Fork it ( https://github.com/i2bskn/passwd/fork )
227
166
  2. Create your feature branch (`git checkout -b my-new-feature`)
228
- 3. Commit your changes (`git commit -am 'Added some feature'`)
167
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
229
168
  4. Push to the branch (`git push origin my-new-feature`)
230
- 5. Create new Pull Request
169
+ 5. Create a new Pull Request
170
+
data/Rakefile CHANGED
@@ -3,7 +3,8 @@ require "rspec/core/rake_task"
3
3
 
4
4
  desc "Run all specs"
5
5
  RSpec::Core::RakeTask.new(:spec) do |t|
6
- t.rspec_opts = ["-c", "-fs"]
6
+ t.rspec_opts = %w(--color --format documentation)
7
7
  end
8
8
 
9
9
  task :default => :spec
10
+
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,25 @@
1
+ source 'https://rubygems.org'
2
+
3
+
4
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
5
+ gem 'rails', '4.1.8'
6
+ # Use sqlite3 as the database for Active Record
7
+ gem 'sqlite3'
8
+ # Use SCSS for stylesheets
9
+ gem 'sass-rails', '~> 4.0.3'
10
+ # Use Uglifier as compressor for JavaScript assets
11
+ gem 'uglifier', '>= 1.3.0'
12
+ # Use CoffeeScript for .js.coffee assets and views
13
+ gem 'coffee-rails', '~> 4.0.0'
14
+
15
+ # Use jquery as the JavaScript library
16
+ gem 'jquery-rails'
17
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
18
+ gem 'jbuilder', '~> 2.0'
19
+
20
+ gem 'passwd', path: File.expand_path("../..", __FILE__)
21
+
22
+ group :development do
23
+ gem 'pry-rails'
24
+ end
25
+