authlogic 1.4.3 → 2.0.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 (131) hide show
  1. data/CHANGELOG.rdoc +19 -0
  2. data/Manifest.txt +111 -0
  3. data/README.rdoc +116 -389
  4. data/Rakefile +14 -7
  5. data/lib/authlogic.rb +33 -35
  6. data/lib/authlogic/acts_as_authentic/base.rb +91 -0
  7. data/lib/authlogic/acts_as_authentic/email.rb +77 -0
  8. data/lib/authlogic/acts_as_authentic/logged_in_status.rb +54 -0
  9. data/lib/authlogic/acts_as_authentic/login.rb +65 -0
  10. data/lib/authlogic/acts_as_authentic/magic_columns.rb +24 -0
  11. data/lib/authlogic/acts_as_authentic/password.rb +215 -0
  12. data/lib/authlogic/acts_as_authentic/perishable_token.rb +100 -0
  13. data/lib/authlogic/acts_as_authentic/persistence_token.rb +66 -0
  14. data/lib/authlogic/acts_as_authentic/restful_authentication.rb +60 -0
  15. data/lib/authlogic/acts_as_authentic/session_maintenance.rb +127 -0
  16. data/lib/authlogic/acts_as_authentic/single_access_token.rb +58 -0
  17. data/lib/authlogic/acts_as_authentic/validations_scope.rb +32 -0
  18. data/lib/authlogic/{session/authenticates_many_association.rb → authenticates_many/association.rb} +10 -6
  19. data/lib/authlogic/authenticates_many/base.rb +55 -0
  20. data/lib/authlogic/controller_adapters/abstract_adapter.rb +2 -3
  21. data/lib/authlogic/controller_adapters/merb_adapter.rb +0 -4
  22. data/lib/authlogic/controller_adapters/rails_adapter.rb +0 -4
  23. data/lib/authlogic/crypto_providers/aes256.rb +0 -2
  24. data/lib/authlogic/crypto_providers/bcrypt.rb +0 -2
  25. data/lib/authlogic/crypto_providers/md5.rb +34 -0
  26. data/lib/authlogic/crypto_providers/sha1.rb +0 -2
  27. data/lib/authlogic/crypto_providers/sha512.rb +1 -3
  28. data/lib/authlogic/i18n.rb +1 -4
  29. data/lib/authlogic/random.rb +33 -0
  30. data/lib/authlogic/session/activation.rb +56 -0
  31. data/lib/authlogic/session/active_record_trickery.rb +15 -7
  32. data/lib/authlogic/session/base.rb +31 -456
  33. data/lib/authlogic/session/brute_force_protection.rb +50 -27
  34. data/lib/authlogic/session/callbacks.rb +24 -15
  35. data/lib/authlogic/session/cookies.rb +108 -22
  36. data/lib/authlogic/session/existence.rb +89 -0
  37. data/lib/authlogic/session/foundation.rb +63 -0
  38. data/lib/authlogic/session/http_auth.rb +23 -0
  39. data/lib/authlogic/session/id.rb +41 -0
  40. data/lib/authlogic/session/klass.rb +75 -0
  41. data/lib/authlogic/session/magic_columns.rb +75 -0
  42. data/lib/authlogic/session/magic_states.rb +58 -0
  43. data/lib/authlogic/session/params.rb +82 -19
  44. data/lib/authlogic/session/password.rb +156 -0
  45. data/lib/authlogic/session/{perishability.rb → perishable_token.rb} +4 -4
  46. data/lib/authlogic/session/persistence.rb +70 -0
  47. data/lib/authlogic/session/priority_record.rb +34 -0
  48. data/lib/authlogic/session/scopes.rb +57 -53
  49. data/lib/authlogic/session/session.rb +46 -31
  50. data/lib/authlogic/session/timeout.rb +65 -31
  51. data/lib/authlogic/session/unauthorized_record.rb +50 -0
  52. data/lib/authlogic/session/validation.rb +76 -0
  53. data/lib/authlogic/testing/test_unit_helpers.rb +3 -3
  54. data/lib/authlogic/version.rb +3 -3
  55. data/test/acts_as_authentic_test/base_test.rb +12 -0
  56. data/test/acts_as_authentic_test/email_test.rb +79 -0
  57. data/test/acts_as_authentic_test/logged_in_status_test.rb +36 -0
  58. data/test/acts_as_authentic_test/login_test.rb +79 -0
  59. data/test/acts_as_authentic_test/magic_columns_test.rb +27 -0
  60. data/test/acts_as_authentic_test/password_test.rb +212 -0
  61. data/test/acts_as_authentic_test/perishable_token_test.rb +56 -0
  62. data/test/acts_as_authentic_test/persistence_token_test.rb +55 -0
  63. data/test/acts_as_authentic_test/session_maintenance_test.rb +68 -0
  64. data/test/acts_as_authentic_test/single_access_test.rb +39 -0
  65. data/test/authenticates_many_test.rb +16 -0
  66. data/test/{crypto_provider_tests → crypto_provider_test}/aes256_test.rb +1 -1
  67. data/test/{crypto_provider_tests → crypto_provider_test}/bcrypt_test.rb +1 -1
  68. data/test/{crypto_provider_tests → crypto_provider_test}/sha1_test.rb +1 -1
  69. data/test/{crypto_provider_tests → crypto_provider_test}/sha512_test.rb +1 -1
  70. data/test/fixtures/employees.yml +4 -4
  71. data/test/fixtures/users.yml +6 -6
  72. data/test/libs/company.rb +6 -0
  73. data/test/libs/employee.rb +7 -0
  74. data/test/libs/employee_session.rb +2 -0
  75. data/test/libs/project.rb +3 -0
  76. data/test/libs/user_session.rb +2 -0
  77. data/test/random_test.rb +49 -0
  78. data/test/session_test/activation_test.rb +43 -0
  79. data/test/session_test/active_record_trickery_test.rb +26 -0
  80. data/test/session_test/brute_force_protection_test.rb +76 -0
  81. data/test/session_test/callbacks_test.rb +6 -0
  82. data/test/session_test/cookies_test.rb +107 -0
  83. data/test/session_test/credentials_test.rb +0 -0
  84. data/test/session_test/existence_test.rb +64 -0
  85. data/test/session_test/http_auth_test.rb +16 -0
  86. data/test/session_test/id_test.rb +17 -0
  87. data/test/session_test/klass_test.rb +35 -0
  88. data/test/session_test/magic_columns_test.rb +59 -0
  89. data/test/session_test/magic_states_test.rb +60 -0
  90. data/test/session_test/params_test.rb +53 -0
  91. data/test/session_test/password_test.rb +84 -0
  92. data/test/{session_tests → session_test}/perishability_test.rb +1 -1
  93. data/test/session_test/persistence_test.rb +21 -0
  94. data/test/{session_tests → session_test}/scopes_test.rb +2 -3
  95. data/test/session_test/session_test.rb +59 -0
  96. data/test/session_test/timeout_test.rb +43 -0
  97. data/test/session_test/unauthorized_record_test.rb +13 -0
  98. data/test/session_test/validation_test.rb +23 -0
  99. data/test/test_helper.rb +14 -29
  100. metadata +120 -112
  101. data/Manifest +0 -76
  102. data/authlogic.gemspec +0 -38
  103. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/base.rb +0 -22
  104. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/config.rb +0 -238
  105. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/credentials.rb +0 -155
  106. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/logged_in.rb +0 -51
  107. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/perishability.rb +0 -71
  108. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/persistence.rb +0 -94
  109. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/session_maintenance.rb +0 -87
  110. data/lib/authlogic/orm_adapters/active_record_adapter/acts_as_authentic/single_access.rb +0 -61
  111. data/lib/authlogic/orm_adapters/active_record_adapter/authenticates_many.rb +0 -58
  112. data/lib/authlogic/session/config.rb +0 -421
  113. data/lib/authlogic/session/errors.rb +0 -18
  114. data/lib/authlogic/session/record_info.rb +0 -24
  115. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/config_test.rb +0 -154
  116. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/credentials_test.rb +0 -157
  117. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/logged_in_test.rb +0 -24
  118. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/perishability_test.rb +0 -41
  119. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/persistence_test.rb +0 -54
  120. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/session_maintenance_test.rb +0 -62
  121. data/test/orm_adapters_tests/active_record_adapter_tests/acts_as_authentic_tests/single_access_test.rb +0 -41
  122. data/test/orm_adapters_tests/active_record_adapter_tests/authenticates_many_test.rb +0 -32
  123. data/test/session_tests/active_record_trickery_test.rb +0 -14
  124. data/test/session_tests/authenticates_many_association_test.rb +0 -28
  125. data/test/session_tests/base_test.rb +0 -307
  126. data/test/session_tests/brute_force_protection_test.rb +0 -53
  127. data/test/session_tests/config_test.rb +0 -184
  128. data/test/session_tests/cookies_test.rb +0 -32
  129. data/test/session_tests/params_test.rb +0 -32
  130. data/test/session_tests/session_test.rb +0 -45
  131. data/test/session_tests/timeout_test.rb +0 -71
@@ -0,0 +1,50 @@
1
+ module Authlogic
2
+ module Session
3
+ # Allows you to create session with an object. Ex:
4
+ #
5
+ # UserSession.create(my_user_object)
6
+ #
7
+ # Be careful with this, because Authlogic is assuming that you have already confirmed that the
8
+ # user is who he says he is.
9
+ #
10
+ # For example, this is the method used to persist the session internally. Authlogic finds the user with
11
+ # the persistence token. At this point we know the user is who he says he is, so Authlogic just creates a
12
+ # session with the record. This is particularly useful for 3rd party authentication methods, such as
13
+ # OpenID. Let that method verify the identity, once it's verified, pass the object and create a session.
14
+ module UnauthorizedRecord
15
+ def self.included(klass)
16
+ klass.class_eval do
17
+ attr_accessor :unauthorized_record
18
+ validate :validate_by_unauthorized_record, :if => :authenticating_with_unauthorized_record?
19
+ end
20
+ end
21
+
22
+ # Returning meaningful credentials
23
+ def credentials
24
+ if authenticating_with_unauthorized_record?
25
+ details = {}
26
+ details[:unauthorized_record] = "<protected>"
27
+ details
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ # Setting the unauthorized record if it exists in the credentials passed.
34
+ def credentials=(value)
35
+ super
36
+ values = value.is_a?(Array) ? value : [value]
37
+ self.unauthorized_record = values.first if values.first.class < ::ActiveRecord::Base
38
+ end
39
+
40
+ private
41
+ def authenticating_with_unauthorized_record?
42
+ !unauthorized_record.nil?
43
+ end
44
+
45
+ def validate_by_unauthorized_record
46
+ self.attempted_record = unauthorized_record
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,76 @@
1
+ module Authlogic
2
+ module Session
3
+ # Responsible for session validation
4
+ module Validation
5
+ # The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses the exact same ActiveRecord errors class. Use it the same way:
6
+ #
7
+ # class UserSession
8
+ # validate :check_if_awesome
9
+ #
10
+ # private
11
+ # def check_if_awesome
12
+ # errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
13
+ # errors.add_to_base("You must be awesome to log in") unless record.awesome?
14
+ # end
15
+ # end
16
+ class Errors < ::ActiveRecord::Errors
17
+ end
18
+
19
+ # You should use this as a place holder for any records that you find during validation. The main reason for this is to
20
+ # allow other modules to use it if needed. Take the failed_login_count feature, it needs this in order to increase
21
+ # the failed login count.
22
+ def attempted_record
23
+ @attempted_record
24
+ end
25
+
26
+ # See attempted_record
27
+ def attempted_record=(value)
28
+ @attempted_record = value
29
+ end
30
+
31
+ # The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses the exact same ActiveRecord errors class.
32
+ # Use it the same way:
33
+ #
34
+ # === Example
35
+ #
36
+ # class UserSession
37
+ # before_validation :check_if_awesome
38
+ #
39
+ # private
40
+ # def check_if_awesome
41
+ # errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
42
+ # errors.add_to_base("You must be awesome to log in") unless record.awesome?
43
+ # end
44
+ # end
45
+ def errors
46
+ @errors ||= Errors.new(self)
47
+ end
48
+
49
+ # Determines if the information you provided for authentication is valid or not. If there is
50
+ # a problem with the information provided errors will be added to the errors object and this
51
+ # method will return false.
52
+ def valid?
53
+ errors.clear
54
+ self.attempted_record = nil
55
+
56
+ before_validation
57
+ new_session? ? before_validation_on_create : before_validation_on_update
58
+ validate
59
+ ensure_authentication_attempted
60
+
61
+ if errors.empty?
62
+ new_session? ? after_validation_on_create : after_validation_on_update
63
+ after_validation
64
+ end
65
+
66
+ save_record(attempted_record)
67
+ errors.empty?
68
+ end
69
+
70
+ private
71
+ def ensure_authentication_attempted
72
+ errors.add_to_base(I18n.t('error_messages.no_authentication_details', :default => "You did not provide any details for authentication.")) if errors.empty? && attempted_record.nil?
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,7 +1,7 @@
1
1
  module Authlogic
2
- module Testing # :nodoc:
3
- # = Test Unit Helpers
4
- #
2
+ # Various utilities to help with testing. Keep in mind, Authlogic is thoroughly tested for you, the only thing you should be
3
+ # testing is code you write, such as code in your controller.
4
+ module Testing
5
5
  # Provides useful methods for testing in Test::Unit, lets you log records in, etc. Just include this in your test_helper filter:
6
6
  #
7
7
  # require "authlogic/testing/test_unit_helpers"
@@ -42,9 +42,9 @@ module Authlogic # :nodoc:
42
42
  [@major, @minor, @tiny]
43
43
  end
44
44
 
45
- MAJOR = 1
46
- MINOR = 4
47
- TINY = 3
45
+ MAJOR = 2
46
+ MINOR = 0
47
+ TINY = 0
48
48
 
49
49
  # The current version as a Version instance
50
50
  CURRENT = new(MAJOR, MINOR, TINY)
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class BaseTest < ActiveSupport::TestCase
5
+ def test_acts_as_authentic
6
+ assert_nothing_raised do
7
+ User.acts_as_authentic do
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class EmailTest < ActiveSupport::TestCase
5
+ def test_email_field_config
6
+ assert_equal :email, User.email_field
7
+ assert_equal :email, Employee.email_field
8
+
9
+ User.email_field = :nope
10
+ assert_equal :nope, User.email_field
11
+ User.email_field :email
12
+ assert_equal :email, User.email_field
13
+ end
14
+
15
+ def test_validate_email_field_config
16
+ assert User.validate_email_field
17
+ assert Employee.validate_email_field
18
+
19
+ User.validate_email_field = false
20
+ assert !User.validate_email_field
21
+ User.validate_email_field true
22
+ assert User.validate_email_field
23
+ end
24
+
25
+ def test_validates_length_of_email_field_options_config
26
+ assert_equal({:within => 6..100}, User.validates_length_of_email_field_options)
27
+ assert_equal({:within => 6..100}, Employee.validates_length_of_email_field_options)
28
+
29
+ User.validates_length_of_email_field_options = {:yes => "no"}
30
+ assert_equal({:yes => "no"}, User.validates_length_of_email_field_options)
31
+ User.validates_length_of_email_field_options({:within => 6..100})
32
+ assert_equal({:within => 6..100}, User.validates_length_of_email_field_options)
33
+ end
34
+
35
+ def test_validates_format_of_email_field_options_config
36
+ default = {:with => User.send(:email_regex), :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")}
37
+ assert_equal default, User.validates_format_of_email_field_options
38
+ assert_equal default, Employee.validates_format_of_email_field_options
39
+
40
+ User.validates_format_of_email_field_options = {:yes => "no"}
41
+ assert_equal({:yes => "no"}, User.validates_format_of_email_field_options)
42
+ User.validates_format_of_email_field_options default
43
+ assert_equal default, User.validates_format_of_email_field_options
44
+ end
45
+
46
+ def test_validates_length_of_email_field
47
+ u = User.new
48
+ u.email = "a@a.a"
49
+ assert !u.valid?
50
+ assert u.errors.on(:email)
51
+
52
+ u.email = "a@a.com"
53
+ assert !u.valid?
54
+ assert !u.errors.on(:email)
55
+ end
56
+
57
+ def test_validates_format_of_email_field
58
+ u = User.new
59
+ u.email = "aaaaaaaaaaaaa"
60
+ assert !u.valid?
61
+ assert u.errors.on(:email)
62
+
63
+ u.email = "a@a.com"
64
+ assert !u.valid?
65
+ assert !u.errors.on(:email)
66
+ end
67
+
68
+ def test_validates_uniqueness_of_email_field
69
+ u = User.new
70
+ u.email = "bjohnson@binarylogic.com"
71
+ assert !u.valid?
72
+ assert u.errors.on(:email)
73
+
74
+ u.email = "a@a.com"
75
+ assert !u.valid?
76
+ assert !u.errors.on(:email)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class LoggedInStatusTest < ActiveSupport::TestCase
5
+ def test_logged_in_timeout_config
6
+ assert_equal 10.minutes.to_i, User.logged_in_timeout
7
+ assert_equal 10.minutes.to_i, Employee.logged_in_timeout
8
+
9
+ User.logged_in_timeout = 1.hour
10
+ assert_equal 1.hour.to_i, User.logged_in_timeout
11
+ User.logged_in_timeout 10.minutes
12
+ assert_equal 10.minutes.to_i, User.logged_in_timeout
13
+ end
14
+
15
+ def test_named_scope_logged_in
16
+ assert_equal 0, User.logged_in.count
17
+ User.first.update_attribute(:last_request_at, Time.now)
18
+ assert_equal 1, User.logged_in.count
19
+ end
20
+
21
+ def test_named_scope_logged_out
22
+ assert_equal 2, User.logged_out.count
23
+ User.first.update_attribute(:last_request_at, Time.now)
24
+ assert_equal 1, User.logged_out.count
25
+ end
26
+
27
+ def test_logged_in_logged_out
28
+ u = User.first
29
+ assert !u.logged_in?
30
+ assert u.logged_out?
31
+ u.last_request_at = Time.now
32
+ assert u.logged_in?
33
+ assert !u.logged_out?
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class LoginTest < ActiveSupport::TestCase
5
+ def test_login_field_config
6
+ assert_equal :login, User.login_field
7
+ assert_nil Employee.login_field
8
+
9
+ User.login_field = :nope
10
+ assert_equal :nope, User.login_field
11
+ User.login_field :login
12
+ assert_equal :login, User.login_field
13
+ end
14
+
15
+ def test_validate_login_field_config
16
+ assert User.validate_login_field
17
+ assert Employee.validate_login_field
18
+
19
+ User.validate_login_field = false
20
+ assert !User.validate_login_field
21
+ User.validate_login_field true
22
+ assert User.validate_login_field
23
+ end
24
+
25
+ def test_validates_length_of_login_field_options_config
26
+ assert_equal({:within => 3..100}, User.validates_length_of_login_field_options)
27
+ assert_equal({:within => 3..100}, Employee.validates_length_of_login_field_options)
28
+
29
+ User.validates_length_of_login_field_options = {:yes => "no"}
30
+ assert_equal({:yes => "no"}, User.validates_length_of_login_field_options)
31
+ User.validates_length_of_login_field_options({:within => 3..100})
32
+ assert_equal({:within => 3..100}, User.validates_length_of_login_field_options)
33
+ end
34
+
35
+ def test_validates_format_of_login_field_options_config
36
+ default = {:with => /\A\w[\w\.\-_@ ]+\z/, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")}
37
+ assert_equal default, User.validates_format_of_login_field_options
38
+ assert_equal default, Employee.validates_format_of_login_field_options
39
+
40
+ User.validates_format_of_login_field_options = {:yes => "no"}
41
+ assert_equal({:yes => "no"}, User.validates_format_of_login_field_options)
42
+ User.validates_format_of_login_field_options default
43
+ assert_equal default, User.validates_format_of_login_field_options
44
+ end
45
+
46
+ def test_validates_length_of_login_field
47
+ u = User.new
48
+ u.login = "a"
49
+ assert !u.valid?
50
+ assert u.errors.on(:login)
51
+
52
+ u.login = "aaaaaaaaaa"
53
+ assert !u.valid?
54
+ assert !u.errors.on(:login)
55
+ end
56
+
57
+ def test_validates_format_of_login_field
58
+ u = User.new
59
+ u.login = "fdsf@^&*"
60
+ assert !u.valid?
61
+ assert u.errors.on(:login)
62
+
63
+ u.login = "fdsfdsfdsfdsfs"
64
+ assert !u.valid?
65
+ assert !u.errors.on(:login)
66
+ end
67
+
68
+ def test_validates_uniqueness_of_login_field
69
+ u = User.new
70
+ u.login = "bjohnson"
71
+ assert !u.valid?
72
+ assert u.errors.on(:login)
73
+
74
+ u.login = "fdsfdsf"
75
+ assert !u.valid?
76
+ assert !u.errors.on(:login)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class MagicColumnsTest < ActiveSupport::TestCase
5
+ def test_validates_numericality_of_login_count
6
+ u = User.new
7
+ u.login_count = -1
8
+ assert !u.valid?
9
+ assert u.errors.on(:login_count)
10
+
11
+ u.login_count = 0
12
+ assert !u.valid?
13
+ assert !u.errors.on(:login_count)
14
+ end
15
+
16
+ def test_validates_numericality_of_failed_login_count
17
+ u = User.new
18
+ u.failed_login_count = -1
19
+ assert !u.valid?
20
+ assert u.errors.on(:failed_login_count)
21
+
22
+ u.failed_login_count = 0
23
+ assert !u.valid?
24
+ assert !u.errors.on(:failed_login_count)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,212 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ActsAsAuthenticTest
4
+ class PasswordTest < ActiveSupport::TestCase
5
+ def test_crypted_password_field_config
6
+ assert_equal :crypted_password, User.crypted_password_field
7
+ assert_equal :crypted_password, Employee.crypted_password_field
8
+
9
+ User.crypted_password_field = :nope
10
+ assert_equal :nope, User.crypted_password_field
11
+ User.crypted_password_field :crypted_password
12
+ assert_equal :crypted_password, User.crypted_password_field
13
+ end
14
+
15
+ def test_password_salt_field_config
16
+ assert_equal :password_salt, User.password_salt_field
17
+ assert_equal :password_salt, Employee.password_salt_field
18
+
19
+ User.password_salt_field = :nope
20
+ assert_equal :nope, User.password_salt_field
21
+ User.password_salt_field :password_salt
22
+ assert_equal :password_salt, User.password_salt_field
23
+ end
24
+
25
+ def test_validate_password_field_config
26
+ assert User.validate_password_field
27
+ assert Employee.validate_password_field
28
+
29
+ User.validate_password_field = false
30
+ assert !User.validate_password_field
31
+ User.validate_password_field true
32
+ assert User.validate_password_field
33
+ end
34
+
35
+ def test_validates_confirmation_of_password_field_options_config
36
+ default = {:minimum => 4, :if => "#{User.password_salt_field}_changed?".to_sym}
37
+ assert_equal default, User.validates_confirmation_of_password_field_options
38
+ assert_equal default, Employee.validates_confirmation_of_password_field_options
39
+
40
+ User.validates_confirmation_of_password_field_options = {:yes => "no"}
41
+ assert_equal({:yes => "no"}, User.validates_confirmation_of_password_field_options)
42
+ User.validates_confirmation_of_password_field_options default
43
+ assert_equal default, User.validates_confirmation_of_password_field_options
44
+ end
45
+
46
+ def test_validates_length_of_password_confirmation_field_options_config
47
+ default = {:minimum => 4, :if => :require_password_confirmation?}
48
+ assert_equal default, User.validates_length_of_password_confirmation_field_options
49
+ assert_equal default, Employee.validates_length_of_password_confirmation_field_options
50
+
51
+ User.validates_length_of_password_confirmation_field_options = {:yes => "no"}
52
+ assert_equal({:yes => "no"}, User.validates_length_of_password_confirmation_field_options)
53
+ User.validates_length_of_password_confirmation_field_options default
54
+ assert_equal default, User.validates_length_of_password_confirmation_field_options
55
+ end
56
+
57
+ def test_crypto_provider_config
58
+ assert_equal Authlogic::CryptoProviders::Sha512, User.crypto_provider
59
+ assert_equal Authlogic::CryptoProviders::AES256, Employee.crypto_provider
60
+
61
+ User.crypto_provider = Authlogic::CryptoProviders::BCrypt
62
+ assert_equal Authlogic::CryptoProviders::BCrypt, User.crypto_provider
63
+ User.crypto_provider Authlogic::CryptoProviders::Sha512
64
+ assert_equal Authlogic::CryptoProviders::Sha512, User.crypto_provider
65
+ end
66
+
67
+ def test_transition_from_crypto_providers_config
68
+ assert_equal [], User.transition_from_crypto_providers
69
+ assert_equal [], Employee.transition_from_crypto_providers
70
+
71
+ User.transition_from_crypto_providers = [Authlogic::CryptoProviders::BCrypt]
72
+ assert_equal [Authlogic::CryptoProviders::BCrypt], User.transition_from_crypto_providers
73
+ User.transition_from_crypto_providers []
74
+ assert_equal [], User.transition_from_crypto_providers
75
+ end
76
+
77
+ def test_act_like_restful_authentication_config
78
+ assert !User.act_like_restful_authentication
79
+ assert !Employee.act_like_restful_authentication
80
+
81
+ User.act_like_restful_authentication = true
82
+ assert User.act_like_restful_authentication
83
+ assert_equal Authlogic::CryptoProviders::Sha1, User.crypto_provider
84
+ assert defined?(::REST_AUTH_SITE_KEY)
85
+ assert_equal 1, Authlogic::CryptoProviders::Sha1.stretches
86
+
87
+ User.act_like_restful_authentication false
88
+ assert !User.act_like_restful_authentication
89
+
90
+ User.crypto_provider = Authlogic::CryptoProviders::Sha512
91
+ User.transition_from_crypto_providers = []
92
+ end
93
+
94
+ def test_transition_from_restful_authentication_config
95
+ assert !User.transition_from_restful_authentication
96
+ assert !Employee.transition_from_restful_authentication
97
+
98
+ User.transition_from_restful_authentication = true
99
+ assert User.transition_from_restful_authentication
100
+ assert defined?(::REST_AUTH_SITE_KEY)
101
+ assert_equal 1, Authlogic::CryptoProviders::Sha1.stretches
102
+
103
+ User.transition_from_restful_authentication false
104
+ assert !User.transition_from_restful_authentication
105
+
106
+ User.crypto_provider = Authlogic::CryptoProviders::Sha512
107
+ User.transition_from_crypto_providers = []
108
+ end
109
+
110
+ def test_validates_confirmation_of_password
111
+ u = User.new
112
+ u.password = "test"
113
+ u.password_confirmation = "test2"
114
+ assert !u.valid?
115
+ assert u.errors.on(:password)
116
+
117
+ u.password_confirmation = "test"
118
+ assert !u.valid?
119
+ assert !u.errors.on(:password)
120
+ end
121
+
122
+ def test_validates_length_of_password_confirmation
123
+ u = User.new
124
+
125
+ assert !u.valid?
126
+ assert u.errors.on(:password_confirmation)
127
+
128
+ u.password = "test"
129
+ u.password_confirmation = ""
130
+ assert !u.valid?
131
+ assert u.errors.on(:password_confirmation)
132
+
133
+ u.password_confirmation = "test"
134
+ assert !u.valid?
135
+ assert !u.errors.on(:password_confirmation)
136
+
137
+ ben = users(:ben)
138
+ assert ben.valid?
139
+
140
+ ben.password = "newpass"
141
+ assert !ben.valid?
142
+ assert ben.errors.on(:password_confirmation)
143
+
144
+ ben.password_confirmation = "newpass"
145
+ assert ben.valid?
146
+ end
147
+
148
+ def test_password
149
+ u = User.new
150
+ old_password_salt = u.password_salt
151
+ old_crypted_password = u.crypted_password
152
+ u.password = "test"
153
+ assert_not_equal old_password_salt, u.password_salt
154
+ assert_not_equal old_crypted_password, u.crypted_password
155
+ end
156
+
157
+ def test_transitioning_password
158
+ ben = users(:ben)
159
+ transition_password_to(Authlogic::CryptoProviders::BCrypt, ben)
160
+ transition_password_to(Authlogic::CryptoProviders::Sha1, ben, [Authlogic::CryptoProviders::Sha512, Authlogic::CryptoProviders::BCrypt])
161
+ transition_password_to(Authlogic::CryptoProviders::Sha512, ben, [Authlogic::CryptoProviders::Sha1, Authlogic::CryptoProviders::BCrypt])
162
+ end
163
+
164
+ def test_reset_password
165
+ ben = users(:ben)
166
+ old_crypted_password = ben.crypted_password
167
+ old_password_salt = ben.password_salt
168
+
169
+ # soft reset
170
+ ben.reset_password
171
+ assert_not_equal old_crypted_password, ben.crypted_password
172
+ assert_not_equal old_password_salt, ben.password_salt
173
+
174
+ # make sure it didn't go into the db
175
+ ben.reload
176
+ assert_equal old_crypted_password, ben.crypted_password
177
+ assert_equal old_password_salt, ben.password_salt
178
+
179
+ # hard reset
180
+ assert ben.reset_password!
181
+ assert_not_equal old_crypted_password, ben.crypted_password
182
+ assert_not_equal old_password_salt, ben.password_salt
183
+
184
+ # make sure it did go into the db
185
+ ben.reload
186
+ assert_not_equal old_crypted_password, ben.crypted_password
187
+ assert_not_equal old_password_salt, ben.password_salt
188
+ end
189
+
190
+ private
191
+ def transition_password_to(crypto_provider, records, from_crypto_providers = Authlogic::CryptoProviders::Sha512)
192
+ records = [records] unless records.is_a?(Array)
193
+ User.acts_as_authentic do |c|
194
+ c.crypto_provider = crypto_provider
195
+ c.transition_from_crypto_providers = from_crypto_providers
196
+ end
197
+ records.each do |record|
198
+ old_hash = record.crypted_password
199
+ old_persistence_token = record.persistence_token
200
+ assert record.valid_password?(password_for(record))
201
+ assert_not_equal old_hash.to_s, record.crypted_password.to_s
202
+ assert_not_equal old_persistence_token.to_s, record.persistence_token.to_s
203
+
204
+ old_hash = record.crypted_password
205
+ old_persistence_token = record.persistence_token
206
+ assert record.valid_password?(password_for(record))
207
+ assert_equal old_hash.to_s, record.crypted_password.to_s
208
+ assert_equal old_persistence_token.to_s, record.persistence_token.to_s
209
+ end
210
+ end
211
+ end
212
+ end