passwd 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/CHANGELOG.md +30 -1
- data/Gemfile +0 -5
- data/LICENSE.txt +2 -1
- data/README.md +96 -156
- data/Rakefile +2 -1
- data/example/.gitignore +16 -0
- data/example/Gemfile +25 -0
- data/example/README.rdoc +28 -0
- data/example/Rakefile +6 -0
- data/example/app/assets/images/.keep +0 -0
- data/example/app/assets/javascripts/application.js +16 -0
- data/example/app/assets/stylesheets/application.css +16 -0
- data/example/app/controllers/application_controller.rb +10 -0
- data/example/app/controllers/concerns/.keep +0 -0
- data/example/app/controllers/profiles_controller.rb +28 -0
- data/example/app/controllers/root_controller.rb +5 -0
- data/example/app/controllers/sessions_controller.rb +29 -0
- data/example/app/helpers/application_helper.rb +2 -0
- data/example/app/mailers/.keep +0 -0
- data/example/app/models/.keep +0 -0
- data/example/app/models/concerns/.keep +0 -0
- data/example/app/models/user.rb +4 -0
- data/example/app/views/layouts/application.html.erb +15 -0
- data/example/app/views/profiles/edit.html.erb +14 -0
- data/example/app/views/profiles/show.html.erb +12 -0
- data/example/app/views/root/index.html.erb +5 -0
- data/example/app/views/sessions/new.html.erb +6 -0
- data/example/bin/bundle +3 -0
- data/example/bin/rails +4 -0
- data/example/bin/rake +4 -0
- data/example/config.ru +4 -0
- data/example/config/application.rb +40 -0
- data/example/config/boot.rb +4 -0
- data/example/config/database.yml +26 -0
- data/example/config/environment.rb +5 -0
- data/example/config/environments/development.rb +37 -0
- data/example/config/environments/production.rb +78 -0
- data/example/config/environments/test.rb +39 -0
- data/example/config/initializers/assets.rb +8 -0
- data/example/config/initializers/backtrace_silencers.rb +7 -0
- data/example/config/initializers/cookies_serializer.rb +3 -0
- data/example/config/initializers/filter_parameter_logging.rb +4 -0
- data/example/config/initializers/inflections.rb +16 -0
- data/example/config/initializers/mime_types.rb +4 -0
- data/example/config/initializers/passwd.rb +41 -0
- data/example/config/initializers/session_store.rb +3 -0
- data/example/config/initializers/wrap_parameters.rb +14 -0
- data/example/config/locales/en.yml +23 -0
- data/example/config/routes.rb +16 -0
- data/example/config/secrets.yml +22 -0
- data/example/db/migrate/20141122165914_create_users.rb +13 -0
- data/example/db/schema.rb +25 -0
- data/example/db/seeds.rb +7 -0
- data/example/lib/assets/.keep +0 -0
- data/example/lib/tasks/.keep +0 -0
- data/example/lib/tasks/user.rake +12 -0
- data/example/log/.keep +0 -0
- data/example/public/404.html +67 -0
- data/example/public/422.html +67 -0
- data/example/public/500.html +66 -0
- data/example/public/favicon.ico +0 -0
- data/example/public/robots.txt +5 -0
- data/example/vendor/assets/javascripts/.keep +0 -0
- data/example/vendor/assets/stylesheets/.keep +0 -0
- data/lib/generators/passwd/config_generator.rb +13 -0
- data/lib/generators/passwd/templates/passwd_config.rb +41 -0
- data/lib/passwd.rb +18 -3
- data/lib/passwd/action_controller_ext.rb +48 -0
- data/lib/passwd/active_record_ext.rb +65 -0
- data/lib/passwd/base.rb +17 -62
- data/lib/passwd/configuration.rb +82 -0
- data/lib/passwd/errors.rb +6 -13
- data/lib/passwd/password.rb +73 -25
- data/lib/passwd/policy.rb +28 -0
- data/lib/passwd/railtie.rb +19 -0
- data/lib/passwd/salt.rb +50 -0
- data/lib/passwd/version.rb +2 -1
- data/passwd.gemspec +8 -2
- data/spec/passwd/.keep +0 -0
- data/spec/passwd/active_record_ext_spec.rb +80 -0
- data/spec/passwd/base_spec.rb +55 -231
- data/spec/passwd/configuration_spec.rb +50 -0
- data/spec/passwd/password_spec.rb +129 -123
- data/spec/spec_helper.rb +14 -3
- data/spec/support/data_util.rb +11 -0
- data/spec/support/paths.rb +2 -0
- metadata +164 -30
- data/lib/passwd/active_record.rb +0 -62
- data/lib/passwd/configuration/abstract_config.rb +0 -37
- data/lib/passwd/configuration/config.rb +0 -24
- data/lib/passwd/configuration/policy.rb +0 -46
- data/lib/passwd/configuration/tmp_config.rb +0 -18
- data/spec/passwd/active_record_spec.rb +0 -163
- data/spec/passwd/configuration/config_spec.rb +0 -250
- data/spec/passwd/configuration/policy_spec.rb +0 -133
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15f7af650246f9de96bc2aa84b2a6be0f7057b43
|
4
|
+
data.tar.gz: 9d30c7e3e157b1bd21942502ec7664d0733650b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fec9addec10e3bec27923d51a94ceea7a5d6c82c12085fd8639990dd23ac6c922899b2ca01e1959aad83438a99a48dbc695df2e06c006c70d5733ae9dc92291
|
7
|
+
data.tar.gz: c1da0f7170c0ee57591d584cbc7bb72f3f37461dd52d6bc95877ce305785527f0f3e1a9a8f3d1f36aaca620316ff59d585331d16677aa01e030cd9ebd89c1246
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
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
data/LICENSE.txt
CHANGED
@@ -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.
|
4
|
-
[![Build Status](https://travis-ci.org/i2bskn/passwd.
|
5
|
-
[![Coverage Status](https://
|
6
|
-
[![Code Climate](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
|
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
|
-
|
29
|
-
require 'passwd'
|
30
|
-
```
|
24
|
+
### ActiveRecord with Rails
|
31
25
|
|
32
|
-
|
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
|
-
|
30
|
+
class User < ActiveRecord::Base
|
31
|
+
with_authenticate
|
32
|
+
end
|
36
33
|
```
|
37
34
|
|
38
|
-
|
35
|
+
#### Options
|
39
36
|
|
40
|
-
|
37
|
+
User model The following column are required.
|
38
|
+
Column name can be changed with the specified options.
|
41
39
|
|
42
|
-
|
43
|
-
|
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
|
-
|
49
|
-
Changing the default configs are as follows:
|
44
|
+
Use the `name` column as id.
|
50
45
|
|
51
46
|
```ruby
|
52
|
-
|
53
|
-
|
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
|
-
|
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
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
60
|
+
user = User.authenticate(params[:email], params[:password]) # => return user object or nil.
|
61
|
+
user.authenticate(params[:password])
|
78
62
|
```
|
79
63
|
|
80
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
72
|
+
new_user = User.new
|
73
|
+
password = new_user.passwd.plain
|
74
|
+
UserMailer.register(new_user, password).deliver!
|
75
|
+
```
|
96
76
|
|
97
|
-
|
98
|
-
|
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
|
-
|
102
|
-
|
103
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
95
|
+
### ActionController
|
125
96
|
|
126
|
-
|
97
|
+
Already several methods is available in your controller.
|
127
98
|
|
128
|
-
|
129
|
-
|
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
|
133
|
-
|
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
|
-
|
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
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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
|
-
|
142
|
+
`current_user` method available if already signin.
|
162
143
|
|
163
144
|
```ruby
|
164
|
-
|
165
|
-
|
166
|
-
|
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
|
-
|
150
|
+
# app/views/greet/greet.html.erb
|
151
|
+
<p>Hello <%= current_user.name %>!!<p>
|
152
|
+
```
|
176
153
|
|
177
|
-
|
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
|
-
|
183
|
-
|
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 '
|
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
data/example/.gitignore
ADDED
@@ -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
|
data/example/Gemfile
ADDED
@@ -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
|
+
|