authlogic 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of authlogic might be problematic. Click here for more details.

Files changed (26) hide show
  1. data/CHANGELOG.rdoc +8 -1
  2. data/Manifest +4 -0
  3. data/README.rdoc +50 -24
  4. data/authlogic.gemspec +5 -5
  5. data/lib/authlogic.rb +3 -0
  6. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/config.rb +34 -10
  7. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb +8 -3
  8. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/logged_in.rb +2 -2
  9. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/password_reset.rb +73 -0
  10. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/single_access.rb +27 -27
  11. data/lib/authlogic/session/base.rb +41 -38
  12. data/lib/authlogic/session/config.rb +125 -32
  13. data/lib/authlogic/session/password_reset.rb +17 -0
  14. data/lib/authlogic/session/scopes.rb +2 -6
  15. data/lib/authlogic/version.rb +2 -2
  16. data/test/fixtures/users.yml +3 -0
  17. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/config_test.rb +6 -1
  18. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/credentials_test.rb +16 -2
  19. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/password_reset_test.rb +40 -0
  20. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/session_maintenance_test.rb +1 -1
  21. data/test/session_tests/base_test.rb +9 -8
  22. data/test/session_tests/config_test.rb +84 -12
  23. data/test/session_tests/password_reset_test.rb +15 -0
  24. data/test/session_tests/scopes_test.rb +5 -4
  25. data/test/test_helper.rb +12 -6
  26. metadata +10 -2
@@ -0,0 +1,17 @@
1
+ module Authlogic
2
+ module Session
3
+ # = Password Reset
4
+ #
5
+ # Provides utilities that assist in maintaining the password reset token. This module just resets the token after a session has been saved, just to keep changing it and add extra security.
6
+ module PasswordReset
7
+ def self.included(klass)
8
+ klass.after_save :reset_password_reset_token!
9
+ end
10
+
11
+ private
12
+ def reset_password_reset_token!
13
+ record.send("reset_#{password_reset_token_field}!") if record.respond_to?("reset_#{password_reset_token_field}!")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -57,7 +57,7 @@ module Authlogic
57
57
  module ClassMethods
58
58
  # The current scope set, should be used in the block passed to with_scope.
59
59
  def scope
60
- scopes[Thread.current]
60
+ Thread.current[:authlogic_scope]
61
61
  end
62
62
 
63
63
  # See the documentation for this class for more information on how to use this method.
@@ -71,11 +71,7 @@ module Authlogic
71
71
 
72
72
  private
73
73
  def scope=(value)
74
- scopes[Thread.current] = value
75
- end
76
-
77
- def scopes
78
- @scopes ||= {}
74
+ Thread.current[:authlogic_scope] = value
79
75
  end
80
76
  end
81
77
 
@@ -43,8 +43,8 @@ module Authlogic # :nodoc:
43
43
  end
44
44
 
45
45
  MAJOR = 1
46
- MINOR = 1
47
- TINY = 2
46
+ MINOR = 2
47
+ TINY = 0
48
48
 
49
49
  # The current version as a Version instance
50
50
  CURRENT = new(MAJOR, MINOR, TINY)
@@ -6,6 +6,8 @@ ben:
6
6
  crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("benrocks" + salt) %>
7
7
  remember_token: 6cde0674657a8a313ce952df979de2830309aa4c11ca65805dd00bfdc65dbcc2f5e36718660a1d2e68c1a08c276d996763985d2f06fd3d076eb7bc4d97b1e317
8
8
  single_access_token: <%= User.friendly_unique_token %>
9
+ password_reset_token: <%= User.friendly_unique_token %>
10
+ email: bjohnson@binarylogic.com
9
11
  first_name: Ben
10
12
  last_name: Johnson
11
13
 
@@ -17,5 +19,6 @@ zack:
17
19
  crypted_password: <%= Authlogic::CryptoProviders::Sha512.encrypt("zackrocks" + salt) %>
18
20
  remember_token: fd3c2d5ce09ab98e7547d21f1b3dcf9158a9a19b5d3022c0402f32ae197019fce3fdbc6614d7ee57d719bae53bb089e30edc9e5d6153e5bc3afca0ac1d320342
19
21
  single_access_token: <%= User.friendly_unique_token %>
22
+ email: zham@ziggityzack.com
20
23
  first_name: Zack
21
24
  last_name: Ham
@@ -21,12 +21,17 @@ module ORMAdaptersTests
21
21
  :password_field => :password,
22
22
  :logged_in_timeout => 600,
23
23
  :password_salt_field => :password_salt,
24
+ :password_reset_token_valid_for => 600,
25
+ :password_reset_token_field => :password_reset_token,
24
26
  :login_field_type => :login,
25
27
  :crypto_provider => Authlogic::CryptoProviders::Sha512,
26
28
  :password_blank_message => "can not be blank",
27
29
  :crypted_password_field => :crypted_password,
28
30
  :session_class => "UserSession",
29
- :login_field => :login
31
+ :login_field => :login,
32
+ :email_field => :email,
33
+ :email_field_regex => /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i,
34
+ :email_field_regex_failed_message=>"should look like an email address."
30
35
  }
31
36
  assert_equal default_config, User.acts_as_authentic_config
32
37
  end
@@ -9,26 +9,31 @@ module ORMAdaptersTests
9
9
  assert !user.valid?
10
10
  assert user.errors.on(:login)
11
11
  assert user.errors.on(:password)
12
+ assert user.errors.on(:email)
12
13
 
13
14
  user.login = "a"
14
15
  assert !user.valid?
15
16
  assert user.errors.on(:login)
16
17
  assert user.errors.on(:password)
18
+ assert user.errors.on(:email)
17
19
 
18
20
  user.login = "%ben*"
19
21
  assert !user.valid?
20
22
  assert user.errors.on(:login)
21
23
  assert user.errors.on(:password)
24
+ assert user.errors.on(:email)
22
25
 
23
26
  user.login = "bjohnson"
24
27
  assert !user.valid?
25
28
  assert user.errors.on(:login)
26
29
  assert user.errors.on(:password)
30
+ assert user.errors.on(:email)
27
31
 
28
32
  user.login = "my login"
29
33
  assert !user.valid?
30
34
  assert !user.errors.on(:login)
31
35
  assert user.errors.on(:password)
36
+ assert user.errors.on(:email)
32
37
 
33
38
  user.password = "my pass"
34
39
  assert !user.valid?
@@ -39,8 +44,17 @@ module ORMAdaptersTests
39
44
  assert !user.valid?
40
45
  assert !user.errors.on(:password)
41
46
  assert user.errors.on(:confirm_password)
47
+ assert user.errors.on(:email)
42
48
 
43
49
  user.confirm_password = "my pass"
50
+ assert !user.valid?
51
+ assert user.errors.on(:email)
52
+
53
+ user.email = "some email"
54
+ assert !user.valid?
55
+ assert user.errors.on(:email)
56
+
57
+ user.email = "a@a.com"
44
58
  assert user.valid?
45
59
  end
46
60
 
@@ -95,11 +109,11 @@ module ORMAdaptersTests
95
109
  def test_valid_password
96
110
  ben = users(:ben)
97
111
  assert ben.valid_password?("benrocks")
98
- assert ben.valid_password?(ben.crypted_password)
112
+ assert !ben.valid_password?(ben.crypted_password)
99
113
 
100
114
  drew = employees(:drew)
101
115
  assert drew.valid_password?("drewrocks")
102
- assert drew.valid_password?(drew.crypted_password)
116
+ assert !drew.valid_password?(drew.crypted_password)
103
117
  end
104
118
 
105
119
  def test_reset_password
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper.rb'
2
+
3
+ module ORMAdaptersTests
4
+ module ActiveRecordAdapterTests
5
+ module ActsAsAuthenticTests
6
+ class PasswordResetTest < ActiveSupport::TestCase
7
+ def test_before_validation
8
+ ben = users(:ben)
9
+ old_password_reset_token = ben.password_reset_token
10
+ assert ben.valid?
11
+ assert_not_equal old_password_reset_token, ben.password_reset_token
12
+ ben.reload
13
+ assert_equal old_password_reset_token, ben.password_reset_token
14
+ assert ben.save
15
+ assert_not_equal old_password_reset_token, ben.password_reset_token
16
+ end
17
+
18
+ def test_find_using_password_reset_token
19
+ ben = users(:ben)
20
+ assert_nil User.find_using_password_reset_token("")
21
+ assert_equal ben, User.find_using_password_reset_token(ben.password_reset_token)
22
+ assert ben.class.connection.execute("update users set updated_at = '#{10.minutes.ago.to_s(:db)}' where id = '#{ben.id}';")
23
+ assert_nil User.find_using_password_reset_token(ben.password_reset_token)
24
+ end
25
+
26
+ def test_reset_password_reset_token
27
+ ben = users(:ben)
28
+ old_password_reset_token = ben.password_reset_token
29
+ ben.reset_password_reset_token
30
+ assert_not_equal old_password_reset_token, ben.password_reset_token
31
+ ben.reload
32
+ assert_equal old_password_reset_token, ben.password_reset_token
33
+ ben.reset_password_reset_token!
34
+ ben.reload
35
+ assert_not_equal old_password_reset_token, ben.password_reset_token
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -5,7 +5,7 @@ module ORMAdaptersTests
5
5
  module ActsAsAuthenticTests
6
6
  class SessionMaintenanceTest < ActiveSupport::TestCase
7
7
  def test_login_after_create
8
- assert User.create(:login => "awesome", :password => "saweet", :confirm_password => "saweet")
8
+ assert User.create(:login => "awesome", :password => "saweet", :confirm_password => "saweet", :email => "awesome@awesome.com")
9
9
  assert UserSession.find
10
10
  end
11
11
 
@@ -4,28 +4,29 @@ module SessionTests
4
4
  class BaseTest < ActiveSupport::TestCase
5
5
  def test_activated
6
6
  assert UserSession.activated?
7
- Authlogic::Session::Base.reset_controllers!
7
+ Authlogic::Session::Base.controller = nil
8
8
  assert !UserSession.activated?
9
9
  end
10
10
 
11
- def test_controllers
12
- Authlogic::Session::Base.reset_controllers!
13
- assert_equal 0, Authlogic::Session::Base.send(:controllers).size
11
+ def test_controller
12
+ Authlogic::Session::Base.controller = nil
13
+ assert_nil Authlogic::Session::Base.controller
14
14
  thread1 = Thread.new do
15
15
  controller = MockController.new
16
16
  Authlogic::Session::Base.controller = controller
17
17
  assert_equal controller, Authlogic::Session::Base.controller
18
18
  end
19
19
  thread1.join
20
- assert_equal 1, Authlogic::Session::Base.send(:controllers).size
20
+
21
21
  assert_nil Authlogic::Session::Base.controller
22
+
22
23
  thread2 = Thread.new do
23
24
  controller = MockController.new
24
25
  Authlogic::Session::Base.controller = controller
25
26
  assert_equal controller, Authlogic::Session::Base.controller
26
27
  end
27
28
  thread2.join
28
- assert_equal 2, Authlogic::Session::Base.send(:controllers).size
29
+
29
30
  assert_nil Authlogic::Session::Base.controller
30
31
  end
31
32
 
@@ -82,7 +83,7 @@ module SessionTests
82
83
  end
83
84
 
84
85
  def test_init
85
- UserSession.reset_controllers!
86
+ UserSession.controller = nil
86
87
  assert_raise(Authlogic::Session::NotActivated) { UserSession.new }
87
88
  UserSession.controller = @controller
88
89
 
@@ -272,7 +273,7 @@ module SessionTests
272
273
  assert session.valid_http_auth?
273
274
  assert_equal ben, session.record
274
275
  assert_equal ben.login, session.login
275
- assert_equal ben.crypted_password, session.send(:protected_password)
276
+ assert_equal "benrocks", session.send(:protected_password)
276
277
  end
277
278
  end
278
279
  end
@@ -65,6 +65,30 @@ module SessionTests
65
65
  assert_equal 0, session.last_request_at_threshold
66
66
  end
67
67
 
68
+ def test_login_blank_message
69
+ UserSession.login_blank_message = "message"
70
+ assert_equal "message", UserSession.login_blank_message
71
+ session = UserSession.new
72
+ assert_equal "message", session.login_blank_message
73
+
74
+ UserSession.login_blank_message "can not be blank"
75
+ assert_equal "can not be blank", UserSession.login_blank_message
76
+ session = UserSession.new
77
+ assert_equal "can not be blank", session.login_blank_message
78
+ end
79
+
80
+ def test_login_not_found_message
81
+ UserSession.login_not_found_message = "message"
82
+ assert_equal "message", UserSession.login_not_found_message
83
+ session = UserSession.new
84
+ assert_equal "message", session.login_not_found_message
85
+
86
+ UserSession.login_not_found_message "does not exist"
87
+ assert_equal "does not exist", UserSession.login_not_found_message
88
+ session = UserSession.new
89
+ assert_equal "does not exist", session.login_not_found_message
90
+ end
91
+
68
92
  def test_login_field
69
93
  UserSession.login_field = :saweet
70
94
  assert_equal :saweet, UserSession.login_field
@@ -79,6 +103,42 @@ module SessionTests
79
103
  assert session.respond_to?(:login)
80
104
  end
81
105
 
106
+ def test_not_active_message
107
+ UserSession.not_active_message = "message"
108
+ assert_equal "message", UserSession.not_active_message
109
+ session = UserSession.new
110
+ assert_equal "message", session.not_active_message
111
+
112
+ UserSession.not_active_message "Your account is not active"
113
+ assert_equal "Your account is not active", UserSession.not_active_message
114
+ session = UserSession.new
115
+ assert_equal "Your account is not active", session.not_active_message
116
+ end
117
+
118
+ def test_not_approved_message
119
+ UserSession.not_approved_message = "message"
120
+ assert_equal "message", UserSession.not_approved_message
121
+ session = UserSession.new
122
+ assert_equal "message", session.not_approved_message
123
+
124
+ UserSession.not_approved_message "Your account is not approved"
125
+ assert_equal "Your account is not approved", UserSession.not_approved_message
126
+ session = UserSession.new
127
+ assert_equal "Your account is not approved", session.not_approved_message
128
+ end
129
+
130
+ def test_not_confirmed_message
131
+ UserSession.not_confirmed_message = "message"
132
+ assert_equal "message", UserSession.not_confirmed_message
133
+ session = UserSession.new
134
+ assert_equal "message", session.not_confirmed_message
135
+
136
+ UserSession.not_confirmed_message "Your account is not confirmed"
137
+ assert_equal "Your account is not confirmed", UserSession.not_confirmed_message
138
+ session = UserSession.new
139
+ assert_equal "Your account is not confirmed", session.not_confirmed_message
140
+ end
141
+
82
142
  def test_params_key
83
143
  UserSession.params_key = "my_params_key"
84
144
  assert_equal "my_params_key", UserSession.params_key
@@ -90,6 +150,18 @@ module SessionTests
90
150
  session = UserSession.new
91
151
  assert_equal "user_credentials", session.params_key
92
152
  end
153
+
154
+ def test_password_blank_message
155
+ UserSession.password_blank_message = "message"
156
+ assert_equal "message", UserSession.password_blank_message
157
+ session = UserSession.new
158
+ assert_equal "message", session.password_blank_message
159
+
160
+ UserSession.password_blank_message "can not be blank"
161
+ assert_equal "can not be blank", UserSession.password_blank_message
162
+ session = UserSession.new
163
+ assert_equal "can not be blank", session.password_blank_message
164
+ end
93
165
 
94
166
  def test_password_field
95
167
  UserSession.password_field = :saweet
@@ -104,6 +176,18 @@ module SessionTests
104
176
  assert_equal :password, session.password_field
105
177
  assert session.respond_to?(:password)
106
178
  end
179
+
180
+ def test_password_invalid_message
181
+ UserSession.password_invalid_message = "message"
182
+ assert_equal "message", UserSession.password_invalid_message
183
+ session = UserSession.new
184
+ assert_equal "message", session.password_invalid_message
185
+
186
+ UserSession.password_invalid_message "is invalid"
187
+ assert_equal "is invalid", UserSession.password_invalid_message
188
+ session = UserSession.new
189
+ assert_equal "is invalid", session.password_invalid_message
190
+ end
107
191
 
108
192
  def test_remember_me
109
193
  UserSession.remember_me = true
@@ -131,18 +215,6 @@ module SessionTests
131
215
  assert_equal 3.months, session.remember_me_for
132
216
  end
133
217
 
134
- def test_remember_token_field
135
- UserSession.remember_token_field = :saweet
136
- assert_equal :saweet, UserSession.remember_token_field
137
- session = UserSession.new
138
- assert_equal :saweet, session.remember_token_field
139
-
140
- UserSession.remember_token_field :remember_token
141
- assert_equal :remember_token, UserSession.remember_token_field
142
- session = UserSession.new
143
- assert_equal :remember_token, session.remember_token_field
144
- end
145
-
146
218
  def test_session_key
147
219
  UserSession.session_key = "my_session_key"
148
220
  assert_equal "my_session_key", UserSession.session_key
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SessionTests
4
+ class PasswordResetTest < ActiveSupport::TestCase
5
+ def test_after_save
6
+ ben = users(:ben)
7
+ old_password_reset_token = ben.password_reset_token
8
+ session = UserSession.create(ben)
9
+ assert_not_equal old_password_reset_token, ben.password_reset_token
10
+
11
+ drew = employees(:drew)
12
+ assert UserSession.create(drew)
13
+ end
14
+ end
15
+ end
@@ -3,24 +3,25 @@ require File.dirname(__FILE__) + '/../test_helper.rb'
3
3
  module SessionTests
4
4
  class ScopesTest < ActiveSupport::TestCase
5
5
  def test_scope_method
6
- assert_equal 0, Authlogic::Session::Base.send(:scopes).size
6
+ assert_nil Authlogic::Session::Base.scope
7
+
7
8
  thread1 = Thread.new do
8
9
  scope = {:id => :scope1}
9
10
  Authlogic::Session::Base.send(:scope=, scope)
10
11
  assert_equal scope, Authlogic::Session::Base.scope
11
12
  end
12
13
  thread1.join
13
- assert_equal 1, Authlogic::Session::Base.send(:scopes).size
14
+
14
15
  assert_nil Authlogic::Session::Base.scope
16
+
15
17
  thread2 = Thread.new do
16
18
  scope = {:id => :scope2}
17
19
  Authlogic::Session::Base.send(:scope=, scope)
18
20
  assert_equal scope, Authlogic::Session::Base.scope
19
21
  end
20
22
  thread2.join
21
- assert_equal 2, Authlogic::Session::Base.send(:scopes).size
23
+
22
24
  assert_nil Authlogic::Session::Base.scope
23
- Authlogic::Session::Base.send(:scopes).clear
24
25
  end
25
26
 
26
27
  def test_with_scope_method
@@ -40,6 +40,8 @@ ActiveRecord::Schema.define(:version => 1) do
40
40
  t.string :password_salt
41
41
  t.string :remember_token
42
42
  t.string :single_access_token
43
+ t.string :password_reset_token
44
+ t.string :email
43
45
  t.string :first_name
44
46
  t.string :last_name
45
47
  t.integer :login_count
@@ -104,22 +106,26 @@ class Test::Unit::TestCase
104
106
  self.pre_loaded_fixtures = true
105
107
  fixtures :all
106
108
  setup :activate_authlogic
107
- teardown :deactivate_authlogic
108
109
 
109
110
  private
110
111
  def activate_authlogic
111
112
  @controller = MockController.new
112
113
  Authlogic::Session::Base.controller = @controller
113
114
  end
114
-
115
- def deactivate_authlogic
116
- Authlogic::Session::Base.reset_controllers!
117
- end
118
115
 
119
116
  def http_basic_auth_for(user = nil, &block)
120
117
  unless user.blank?
121
118
  @controller.http_user = user.login
122
- @controller.http_password = user.crypted_password
119
+
120
+ password = nil
121
+ case user
122
+ when users(:ben)
123
+ password = "benrocks"
124
+ when users(:zack)
125
+ password = "zackrocks"
126
+ end
127
+
128
+ @controller.http_password = password
123
129
  end
124
130
  yield
125
131
  @controller.http_user = @controller.http_password = nil