simple_auth 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -1
  3. data/.travis.yml +11 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +0 -2
  6. data/Gemfile.lock +102 -79
  7. data/README.md +243 -0
  8. data/Rakefile +15 -0
  9. data/gemfiles/rails_3_1.gemfile +5 -0
  10. data/gemfiles/rails_3_1.gemfile.lock +151 -0
  11. data/gemfiles/rails_3_2.gemfile +5 -0
  12. data/gemfiles/rails_3_2.gemfile.lock +149 -0
  13. data/gemfiles/rails_4_0.gemfile +4 -0
  14. data/gemfiles/rails_4_0.gemfile.lock +140 -0
  15. data/gemfiles/rails_4_1.gemfile +4 -0
  16. data/gemfiles/rails_4_1.gemfile.lock +146 -0
  17. data/lib/simple_auth.rb +1 -6
  18. data/lib/simple_auth/action_controller.rb +14 -10
  19. data/lib/simple_auth/active_record.rb +86 -0
  20. data/lib/simple_auth/compat.rb +2 -0
  21. data/lib/simple_auth/compat/active_record.rb +31 -0
  22. data/lib/simple_auth/compat/config.rb +17 -0
  23. data/lib/simple_auth/config.rb +0 -20
  24. data/lib/simple_auth/exceptions.rb +0 -1
  25. data/lib/simple_auth/railtie.rb +1 -1
  26. data/lib/simple_auth/rspec.rb +2 -2
  27. data/lib/simple_auth/session.rb +1 -1
  28. data/lib/simple_auth/version.rb +2 -2
  29. data/simple_auth.gemspec +4 -4
  30. data/spec/controllers/redirect_logged_user_spec.rb +16 -16
  31. data/spec/controllers/require_logged_user_spec.rb +34 -34
  32. data/spec/schema.rb +5 -1
  33. data/spec/simple_auth/active_record_spec.rb +104 -2
  34. data/spec/simple_auth/compat_spec.rb +31 -0
  35. data/spec/simple_auth/config_spec.rb +8 -27
  36. data/spec/simple_auth/helper_spec.rb +7 -7
  37. data/spec/simple_auth/session_spec.rb +76 -76
  38. data/spec/spec_helper.rb +2 -168
  39. data/spec/support/app/models/customer.rb +3 -0
  40. data/templates/initializer.rb +0 -8
  41. metadata +62 -33
  42. data/README.markdown +0 -202
  43. data/lib/simple_auth/orm/active_record.rb +0 -80
  44. data/lib/simple_auth/orm/base.rb +0 -89
  45. data/lib/simple_auth/orm/mongo_mapper.rb +0 -62
  46. data/spec/simple_auth/mongo_mapper_spec.rb +0 -10
  47. data/spec/support/app/models/account.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9ccc966be8f5d91ed83365e91fd161dd3855411
4
- data.tar.gz: ddeab7fa3b0cf45404e19a06f1b8ac0860ae39e4
3
+ metadata.gz: 90b77901780358f8890699611f29c46dd78d844b
4
+ data.tar.gz: 00db8239eb7dd70a701a532f4d491d3cb8ce8110
5
5
  SHA512:
6
- metadata.gz: df425ff3d123c56b312517437615d925a979eb15b001061b6b304fbfcfe2e99594486365de3dff1e45cff6138f7a7543fe1cc8b9c1f837c672bf008131df5327
7
- data.tar.gz: c1a2c7e841bfd56dd870fbddc96d6045df8cb6ed6eb4cf53ffd8c1ee53160159ff3ad202d7b361ab4bf4b25bd70f3fccc28bec444822d3b6cef1d9ad92c23165
6
+ metadata.gz: 86bda4fb617c416cb2e669002443ea61bf88098dddd19d7725affa62c6a78753be0e8288a83cb83180c6932b4fbcf1c7df7212fea21b0d7fa14402164ff831dc
7
+ data.tar.gz: 5829b734bcede51e7c14859b329bb0480b47d4311cd6c72174073570e5dc60889028ebb53eb136a9567812d0c9d280acb08259b193fc9e3460615d63c47c0752
data/.rspec CHANGED
@@ -1 +1 @@
1
- --color --format documentation
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
5
+ script: bundle exec rspec
6
+ gemfile:
7
+ - Gemfile
8
+ - gemfiles/rails_3_1.gemfile
9
+ - gemfiles/rails_3_2.gemfile
10
+ - gemfiles/rails_4_0.gemfile
11
+ - gemfiles/rails_4_1.gemfile
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # v2.0.0
2
+
3
+ - Released version 2.0.0. This version removes support for MongoDB
4
+ and switches to `has_secure_password` encryption method. This
5
+ change requires Rails 3.1.0+.
data/Gemfile CHANGED
@@ -1,4 +1,2 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
-
4
- gem "mongo_mapper", github: "jnunemaker/mongomapper"
data/Gemfile.lock CHANGED
@@ -1,122 +1,145 @@
1
- GIT
2
- remote: git://github.com/jnunemaker/mongomapper.git
3
- revision: e0d7db141b2c330eb44773ef00d1b179bbdf9209
4
- specs:
5
- mongo_mapper (0.13.0.beta1)
6
- activemodel (>= 3.0.0)
7
- activesupport (>= 3.0)
8
- mongo (~> 1.8)
9
- plucky (~> 0.6.5)
10
-
11
1
  PATH
12
2
  remote: .
13
3
  specs:
14
- simple_auth (1.5.0)
4
+ simple_auth (2.0.0)
5
+ rails (>= 3.1.0)
15
6
 
16
7
  GEM
17
8
  remote: http://rubygems.org/
18
9
  specs:
19
- actionmailer (4.0.0)
20
- actionpack (= 4.0.0)
21
- mail (~> 2.5.3)
22
- actionpack (4.0.0)
23
- activesupport (= 4.0.0)
24
- builder (~> 3.1.0)
25
- erubis (~> 2.7.0)
10
+ actionmailer (4.1.0)
11
+ actionpack (= 4.1.0)
12
+ actionview (= 4.1.0)
13
+ mail (~> 2.5.4)
14
+ actionpack (4.1.0)
15
+ actionview (= 4.1.0)
16
+ activesupport (= 4.1.0)
26
17
  rack (~> 1.5.2)
27
18
  rack-test (~> 0.6.2)
28
- activemodel (4.0.0)
29
- activesupport (= 4.0.0)
30
- builder (~> 3.1.0)
31
- activerecord (4.0.0)
32
- activemodel (= 4.0.0)
33
- activerecord-deprecated_finders (~> 1.0.2)
34
- activesupport (= 4.0.0)
35
- arel (~> 4.0.0)
36
- activerecord-deprecated_finders (1.0.3)
37
- activesupport (4.0.0)
38
- i18n (~> 0.6, >= 0.6.4)
39
- minitest (~> 4.2)
40
- multi_json (~> 1.3)
19
+ actionview (4.1.0)
20
+ activesupport (= 4.1.0)
21
+ builder (~> 3.1)
22
+ erubis (~> 2.7.0)
23
+ activemodel (4.1.0)
24
+ activesupport (= 4.1.0)
25
+ builder (~> 3.1)
26
+ activerecord (4.1.0)
27
+ activemodel (= 4.1.0)
28
+ activesupport (= 4.1.0)
29
+ arel (~> 5.0.0)
30
+ activesupport (4.1.0)
31
+ i18n (~> 0.6, >= 0.6.9)
32
+ json (~> 1.7, >= 1.7.7)
33
+ minitest (~> 5.1)
41
34
  thread_safe (~> 0.1)
42
- tzinfo (~> 0.3.37)
43
- arel (4.0.0)
44
- atomic (1.1.12)
45
- bson (1.9.1)
46
- bson_ext (1.9.1)
47
- bson (~> 1.9.1)
48
- builder (3.1.4)
49
- diff-lcs (1.2.4)
35
+ tzinfo (~> 1.1)
36
+ arel (5.0.1.20140414130214)
37
+ awesome_print (1.2.0)
38
+ bcrypt (3.1.7)
39
+ builder (3.2.2)
40
+ coderay (1.1.0)
41
+ columnize (0.3.6)
42
+ debugger (1.6.6)
43
+ columnize (>= 0.3.1)
44
+ debugger-linecache (~> 1.2.0)
45
+ debugger-ruby_core_source (~> 1.3.2)
46
+ debugger-linecache (1.2.0)
47
+ debugger-ruby_core_source (1.3.2)
48
+ diff-lcs (1.2.5)
50
49
  erubis (2.7.0)
51
50
  hike (1.2.3)
52
- i18n (0.6.4)
51
+ i18n (0.6.9)
52
+ json (1.8.1)
53
53
  mail (2.5.4)
54
54
  mime-types (~> 1.16)
55
55
  treetop (~> 1.4.8)
56
- mime-types (1.23)
57
- minitest (4.7.5)
58
- mongo (1.9.1)
59
- bson (~> 1.9.1)
60
- multi_json (1.7.7)
61
- plucky (0.6.5)
62
- mongo (~> 1.5)
63
- polyglot (0.3.3)
56
+ method_source (0.8.2)
57
+ mime-types (1.25.1)
58
+ minitest (5.3.3)
59
+ multi_json (1.9.2)
60
+ polyglot (0.3.4)
61
+ pry (0.9.12.6)
62
+ coderay (~> 1.0)
63
+ method_source (~> 0.8)
64
+ slop (~> 3.4)
65
+ pry-debugger (0.2.2)
66
+ debugger (~> 1.3)
67
+ pry (~> 0.9.10)
68
+ pry-meta (0.0.6)
69
+ awesome_print
70
+ pry
71
+ pry-debugger
72
+ pry-remote
73
+ pry-remote (0.1.8)
74
+ pry (~> 0.9)
75
+ slop (~> 3.0)
64
76
  rack (1.5.2)
65
77
  rack-test (0.6.2)
66
78
  rack (>= 1.0)
67
- rails (4.0.0)
68
- actionmailer (= 4.0.0)
69
- actionpack (= 4.0.0)
70
- activerecord (= 4.0.0)
71
- activesupport (= 4.0.0)
79
+ rails (4.1.0)
80
+ actionmailer (= 4.1.0)
81
+ actionpack (= 4.1.0)
82
+ actionview (= 4.1.0)
83
+ activemodel (= 4.1.0)
84
+ activerecord (= 4.1.0)
85
+ activesupport (= 4.1.0)
72
86
  bundler (>= 1.3.0, < 2.0)
73
- railties (= 4.0.0)
74
- sprockets-rails (~> 2.0.0)
75
- railties (4.0.0)
76
- actionpack (= 4.0.0)
77
- activesupport (= 4.0.0)
87
+ railties (= 4.1.0)
88
+ sprockets-rails (~> 2.0)
89
+ railties (4.1.0)
90
+ actionpack (= 4.1.0)
91
+ activesupport (= 4.1.0)
78
92
  rake (>= 0.8.7)
79
93
  thor (>= 0.18.1, < 2.0)
80
- rake (10.1.0)
81
- rspec-core (2.14.4)
82
- rspec-expectations (2.14.0)
83
- diff-lcs (>= 1.1.3, < 2.0)
84
- rspec-mocks (2.14.2)
85
- rspec-rails (2.14.0)
94
+ rake (10.3.1)
95
+ rspec-collection_matchers (0.0.3)
96
+ rspec-expectations (>= 2.99.0.beta1)
97
+ rspec-core (3.0.0.beta2)
98
+ rspec-support (= 3.0.0.beta2)
99
+ rspec-expectations (3.0.0.beta2)
100
+ diff-lcs (>= 1.2.0, < 2.0)
101
+ rspec-support (= 3.0.0.beta2)
102
+ rspec-mocks (3.0.0.beta2)
103
+ rspec-support (= 3.0.0.beta2)
104
+ rspec-rails (3.0.0.beta2)
86
105
  actionpack (>= 3.0)
106
+ activemodel (>= 3.0)
87
107
  activesupport (>= 3.0)
88
108
  railties (>= 3.0)
89
- rspec-core (~> 2.14.0)
90
- rspec-expectations (~> 2.14.0)
91
- rspec-mocks (~> 2.14.0)
92
- sprockets (2.10.0)
109
+ rspec-collection_matchers
110
+ rspec-core (= 3.0.0.beta2)
111
+ rspec-expectations (= 3.0.0.beta2)
112
+ rspec-mocks (= 3.0.0.beta2)
113
+ rspec-support (= 3.0.0.beta2)
114
+ rspec-support (3.0.0.beta2)
115
+ slop (3.5.0)
116
+ sprockets (2.12.1)
93
117
  hike (~> 1.2)
94
118
  multi_json (~> 1.0)
95
119
  rack (~> 1.0)
96
120
  tilt (~> 1.1, != 1.3.0)
97
- sprockets-rails (2.0.0)
121
+ sprockets-rails (2.1.3)
98
122
  actionpack (>= 3.0)
99
123
  activesupport (>= 3.0)
100
124
  sprockets (~> 2.8)
101
- sqlite3 (1.3.7)
125
+ sqlite3 (1.3.9)
102
126
  sqlite3-ruby (1.3.3)
103
127
  sqlite3 (>= 1.3.3)
104
- thor (0.18.1)
105
- thread_safe (0.1.2)
106
- atomic
128
+ thor (0.19.1)
129
+ thread_safe (0.3.3)
107
130
  tilt (1.4.1)
108
- treetop (1.4.14)
131
+ treetop (1.4.15)
109
132
  polyglot
110
133
  polyglot (>= 0.3.1)
111
- tzinfo (0.3.37)
134
+ tzinfo (1.1.0)
135
+ thread_safe (~> 0.1)
112
136
 
113
137
  PLATFORMS
114
138
  ruby
115
139
 
116
140
  DEPENDENCIES
117
- bson_ext
118
- mongo_mapper!
119
- rails (>= 4.0.0)
120
- rspec-rails
141
+ bcrypt (~> 3.1.7)
142
+ pry-meta
143
+ rspec-rails (= 3.0.0.beta2)
121
144
  simple_auth!
122
145
  sqlite3-ruby
data/README.md ADDED
@@ -0,0 +1,243 @@
1
+ # Simple Auth
2
+
3
+ [![Build Status](https://travis-ci.org/fnando/simple_auth.svg)](https://travis-ci.org/fnando/simple_auth)
4
+ [![Code Climate](https://codeclimate.com/github/fnando/simple_auth.png)](https://codeclimate.com/github/fnando/simple_auth)
5
+
6
+ SimpleAuth is an authentication library to be used when everything else is just too complicated.
7
+
8
+ This library only supports in-site authentication and won't implement OpenID, Facebook Connect and like.
9
+
10
+ Rails 3.1.0+ is required.
11
+
12
+ ## Installation
13
+
14
+ Just the following line to your Gemfile:
15
+
16
+ gem "simple_auth"
17
+
18
+ Then run `rails generate simple_auth:install` to copy the initializer file.
19
+
20
+ ## Usage
21
+
22
+ Your user model should have the attribute `password_digest`. The credential field can be anything you want, but SimpleAuth uses `[:email, :login]` by default.
23
+
24
+ ```ruby
25
+ class CreateUsers < ActiveRecord::Migration
26
+ def change
27
+ create_table :users do |t|
28
+ t.string :email, null: false
29
+ t.string :login, null: false
30
+ t.string :password_digest, null: false
31
+
32
+ t.timestamps
33
+ end
34
+
35
+ add_index :users, :email, unique: true
36
+ add_index :users, :login, unique: true
37
+ add_index :users, [:email, :login]
38
+ end
39
+ end
40
+ ```
41
+
42
+ In your model, use the `authentication` macro.
43
+
44
+ ```ruby
45
+ class User < ActiveRecord::Base
46
+ authentication
47
+ end
48
+ ```
49
+
50
+ This will add some callbacks and password validations. It will also inject helper methods like `Model.authenticate`.
51
+
52
+ Session is valid only when both `Model#authorized?` and `Controller#authorized?` methods return `true`, which is the default behavior. You can override these methods with your own rules:
53
+
54
+ ```ruby
55
+ class User < ActiveRecord::Base
56
+ authentication
57
+
58
+ def authorized?
59
+ deleted_at.nil?
60
+ end
61
+ end
62
+
63
+ class Admin::DashboardController < ApplicationController
64
+ private
65
+ def authorized?
66
+ current_user.admin?
67
+ end
68
+ end
69
+ ```
70
+
71
+ After you set up the model, you can go to the controller.
72
+
73
+ ```ruby
74
+ class SessionsController < ApplicationController
75
+ def new
76
+ @user_session = SimpleAuth::Session.new
77
+ end
78
+
79
+ def create
80
+ @user_session = SimpleAuth::Session.new(params[:session])
81
+
82
+ if @user_session.save
83
+ redirect_to return_to(dashboard_path)
84
+ else
85
+ flash[:alert] = "Invalid username or password"
86
+ render :new
87
+ end
88
+ end
89
+
90
+ def destroy
91
+ current_session.destroy if logged_in?
92
+ redirect_to root_path
93
+ end
94
+ end
95
+ ```
96
+
97
+ The `return_to` helper will give you the requested url (before the user logged in) or the default url.
98
+
99
+ You can restrict access by using 2 macros:
100
+
101
+ ```ruby
102
+ class SignupController < ApplicationController
103
+ redirect_logged_user :to => "/"
104
+ end
105
+ ```
106
+
107
+ Here's some usage examples:
108
+
109
+ ```ruby
110
+ redirect_logged_user :to => proc { login_path }
111
+ redirect_logged_user :to => {:controller => "dashboard"}
112
+ redirect_logged_user :only => [:index], :to => login_path
113
+ redirect_logged_user :except => [:public], :to => login_path
114
+ ```
115
+
116
+ You can skip the `:to` option if you set it globally on your initializer:
117
+
118
+ ```ruby
119
+ SimpleAuth::Config.logged_url = {:controller => "session", :action => "new"}
120
+ SimpleAuth::Config.logged_url = proc { login_path }
121
+ ```
122
+
123
+ To require a logged user, use the `require_logged_user` macro:
124
+
125
+ ```ruby
126
+ class DashboardController < ApplicationController
127
+ require_logged_user :to => proc { login_path }
128
+ end
129
+ ```
130
+
131
+ Here's some usage examples:
132
+
133
+ ```ruby
134
+ require_logged_user :to => proc { login_path }
135
+ require_logged_user :to => {:controller => "session", :action => "new"}
136
+ require_logged_user :only => [:index], :to => login_path
137
+ require_logged_user :except => [:public], :to => login_path
138
+ ```
139
+
140
+ You can skip the `:to` option if you set it globally on your initializer:
141
+
142
+ ```ruby
143
+ SimpleAuth::Config.login_url = {:controller => "session", :action => "new"}
144
+ SimpleAuth::Config.login_url = proc { login_path }
145
+ ```
146
+
147
+ There are some helpers:
148
+
149
+ ```ruby
150
+ logged_in? # controller & views
151
+ current_user # controller & views
152
+ current_session # controller & views
153
+ when_logged(&block) # views
154
+ find_by_credential # model
155
+ find_by_credential! # model
156
+ ```
157
+
158
+ If you're having problems to use any helper, include the module `SimpleAuth::Helper` on your `ApplicationHelper`.
159
+
160
+ ```ruby
161
+ module ApplicationHelper
162
+ include SimpleAuth::Helper
163
+ end
164
+ ```
165
+
166
+ ### Translations
167
+
168
+ These are the translations you'll need:
169
+
170
+ ```yaml
171
+ en:
172
+ simple_auth:
173
+ sessions:
174
+ need_to_be_logged: "You need to be logged"
175
+ invalid_credentials: "Invalid username or password"
176
+ ```
177
+
178
+ ### Compatibility Mode with v1
179
+
180
+ The previous version was based on hashing with salt. If you want to migrate to the v2 release, you must do some things.
181
+
182
+ First, add the following line to the configuration initializer (available at `config/initializers/simple_auth.rb`:
183
+
184
+ ```ruby
185
+ require "simple_auth/compat"
186
+ ```
187
+
188
+ Then create a field called `password_digest`. This field is required by the `ActiveRecord::Base.has_secure_password` method. You can create a migration with the following content:
189
+
190
+ ```ruby
191
+ class AddPasswordDigestToUsers < ActiveRecord::Migration
192
+ def up
193
+ add_column :users, :password_digest, :string, null: true
194
+ SimpleAuth.migrate_passwords!
195
+ change_column_null :users, :password_digest, false
196
+ end
197
+
198
+ def down
199
+ remove_column :users, :password_digest
200
+ end
201
+ end
202
+ ```
203
+
204
+ Apply this migration with `rake db:migrate`. Go read a book; this is going to take a while.
205
+
206
+ Check if your application is still working. If so, you can remove the `password_hash` column. Here's the migration to do it so.
207
+
208
+ ```ruby
209
+ class RemovePasswordHashFromUsers < ActiveRecord::Migration
210
+ def change
211
+ remove_column :users, :password_hash
212
+ end
213
+ end
214
+ ```
215
+
216
+ Again, apply this migration with `rake db:migrate`.
217
+
218
+ ## Maintainer
219
+
220
+ * Nando Vieira (<http://simplesideias.com.br>)
221
+
222
+ ## License:
223
+
224
+ (The MIT License)
225
+
226
+ Permission is hereby granted, free of charge, to any person obtaining
227
+ a copy of this software and associated documentation files (the
228
+ 'Software'), to deal in the Software without restriction, including
229
+ without limitation the rights to use, copy, modify, merge, publish,
230
+ distribute, sublicense, and/or sell copies of the Software, and to
231
+ permit persons to whom the Software is furnished to do so, subject to
232
+ the following conditions:
233
+
234
+ The above copyright notice and this permission notice shall be
235
+ included in all copies or substantial portions of the Software.
236
+
237
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
238
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
239
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
240
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
241
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
242
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
243
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.