wulffeld_authlogic 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/CHANGELOG.rdoc +345 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +246 -0
  4. data/Rakefile +41 -0
  5. data/VERSION.yml +5 -0
  6. data/generators/session/session_generator.rb +9 -0
  7. data/generators/session/templates/session.rb +2 -0
  8. data/init.rb +1 -0
  9. data/lib/authlogic.rb +64 -0
  10. data/lib/authlogic/acts_as_authentic/base.rb +107 -0
  11. data/lib/authlogic/acts_as_authentic/email.rb +110 -0
  12. data/lib/authlogic/acts_as_authentic/logged_in_status.rb +60 -0
  13. data/lib/authlogic/acts_as_authentic/login.rb +141 -0
  14. data/lib/authlogic/acts_as_authentic/magic_columns.rb +24 -0
  15. data/lib/authlogic/acts_as_authentic/password.rb +355 -0
  16. data/lib/authlogic/acts_as_authentic/perishable_token.rb +105 -0
  17. data/lib/authlogic/acts_as_authentic/persistence_token.rb +68 -0
  18. data/lib/authlogic/acts_as_authentic/restful_authentication.rb +61 -0
  19. data/lib/authlogic/acts_as_authentic/session_maintenance.rb +139 -0
  20. data/lib/authlogic/acts_as_authentic/single_access_token.rb +65 -0
  21. data/lib/authlogic/acts_as_authentic/validations_scope.rb +32 -0
  22. data/lib/authlogic/authenticates_many/association.rb +42 -0
  23. data/lib/authlogic/authenticates_many/base.rb +55 -0
  24. data/lib/authlogic/controller_adapters/abstract_adapter.rb +67 -0
  25. data/lib/authlogic/controller_adapters/merb_adapter.rb +30 -0
  26. data/lib/authlogic/controller_adapters/rails_adapter.rb +52 -0
  27. data/lib/authlogic/controller_adapters/sinatra_adapter.rb +61 -0
  28. data/lib/authlogic/crypto_providers/aes256.rb +43 -0
  29. data/lib/authlogic/crypto_providers/bcrypt.rb +90 -0
  30. data/lib/authlogic/crypto_providers/md5.rb +34 -0
  31. data/lib/authlogic/crypto_providers/sha1.rb +35 -0
  32. data/lib/authlogic/crypto_providers/sha256.rb +50 -0
  33. data/lib/authlogic/crypto_providers/sha512.rb +50 -0
  34. data/lib/authlogic/crypto_providers/wordpress.rb +43 -0
  35. data/lib/authlogic/i18n.rb +83 -0
  36. data/lib/authlogic/i18n/translator.rb +15 -0
  37. data/lib/authlogic/random.rb +33 -0
  38. data/lib/authlogic/regex.rb +25 -0
  39. data/lib/authlogic/session/activation.rb +58 -0
  40. data/lib/authlogic/session/active_record_trickery.rb +64 -0
  41. data/lib/authlogic/session/base.rb +39 -0
  42. data/lib/authlogic/session/brute_force_protection.rb +96 -0
  43. data/lib/authlogic/session/callbacks.rb +99 -0
  44. data/lib/authlogic/session/cookies.rb +130 -0
  45. data/lib/authlogic/session/existence.rb +93 -0
  46. data/lib/authlogic/session/foundation.rb +65 -0
  47. data/lib/authlogic/session/http_auth.rb +58 -0
  48. data/lib/authlogic/session/id.rb +41 -0
  49. data/lib/authlogic/session/klass.rb +78 -0
  50. data/lib/authlogic/session/magic_columns.rb +95 -0
  51. data/lib/authlogic/session/magic_states.rb +59 -0
  52. data/lib/authlogic/session/params.rb +101 -0
  53. data/lib/authlogic/session/password.rb +240 -0
  54. data/lib/authlogic/session/perishable_token.rb +18 -0
  55. data/lib/authlogic/session/persistence.rb +70 -0
  56. data/lib/authlogic/session/priority_record.rb +34 -0
  57. data/lib/authlogic/session/scopes.rb +101 -0
  58. data/lib/authlogic/session/session.rb +62 -0
  59. data/lib/authlogic/session/timeout.rb +82 -0
  60. data/lib/authlogic/session/unauthorized_record.rb +50 -0
  61. data/lib/authlogic/session/validation.rb +82 -0
  62. data/lib/authlogic/test_case.rb +120 -0
  63. data/lib/authlogic/test_case/mock_controller.rb +45 -0
  64. data/lib/authlogic/test_case/mock_cookie_jar.rb +14 -0
  65. data/lib/authlogic/test_case/mock_logger.rb +10 -0
  66. data/lib/authlogic/test_case/mock_request.rb +19 -0
  67. data/lib/authlogic/test_case/rails_request_adapter.rb +30 -0
  68. data/lib/generators/authlogic/session/USAGE +5 -0
  69. data/lib/generators/authlogic/session/session_generator.rb +14 -0
  70. data/lib/generators/authlogic/session/templates/session.rb +2 -0
  71. data/rails/init.rb +1 -0
  72. data/shoulda_macros/authlogic.rb +69 -0
  73. data/test/acts_as_authentic_test/base_test.rb +18 -0
  74. data/test/acts_as_authentic_test/email_test.rb +97 -0
  75. data/test/acts_as_authentic_test/logged_in_status_test.rb +36 -0
  76. data/test/acts_as_authentic_test/login_test.rb +109 -0
  77. data/test/acts_as_authentic_test/magic_columns_test.rb +27 -0
  78. data/test/acts_as_authentic_test/password_test.rb +236 -0
  79. data/test/acts_as_authentic_test/perishable_token_test.rb +90 -0
  80. data/test/acts_as_authentic_test/persistence_token_test.rb +55 -0
  81. data/test/acts_as_authentic_test/restful_authentication_test.rb +40 -0
  82. data/test/acts_as_authentic_test/session_maintenance_test.rb +84 -0
  83. data/test/acts_as_authentic_test/single_access_test.rb +44 -0
  84. data/test/authenticates_many_test.rb +16 -0
  85. data/test/crypto_provider_test/aes256_test.rb +14 -0
  86. data/test/crypto_provider_test/bcrypt_test.rb +14 -0
  87. data/test/crypto_provider_test/sha1_test.rb +23 -0
  88. data/test/crypto_provider_test/sha256_test.rb +14 -0
  89. data/test/crypto_provider_test/sha512_test.rb +14 -0
  90. data/test/fixtures/companies.yml +5 -0
  91. data/test/fixtures/employees.yml +17 -0
  92. data/test/fixtures/projects.yml +3 -0
  93. data/test/fixtures/users.yml +24 -0
  94. data/test/i18n_test.rb +33 -0
  95. data/test/libs/affiliate.rb +7 -0
  96. data/test/libs/company.rb +6 -0
  97. data/test/libs/employee.rb +7 -0
  98. data/test/libs/employee_session.rb +2 -0
  99. data/test/libs/ldaper.rb +3 -0
  100. data/test/libs/ordered_hash.rb +9 -0
  101. data/test/libs/project.rb +3 -0
  102. data/test/libs/user.rb +5 -0
  103. data/test/libs/user_session.rb +6 -0
  104. data/test/random_test.rb +49 -0
  105. data/test/session_test/activation_test.rb +43 -0
  106. data/test/session_test/active_record_trickery_test.rb +36 -0
  107. data/test/session_test/brute_force_protection_test.rb +101 -0
  108. data/test/session_test/callbacks_test.rb +6 -0
  109. data/test/session_test/cookies_test.rb +112 -0
  110. data/test/session_test/credentials_test.rb +0 -0
  111. data/test/session_test/existence_test.rb +64 -0
  112. data/test/session_test/http_auth_test.rb +28 -0
  113. data/test/session_test/id_test.rb +17 -0
  114. data/test/session_test/klass_test.rb +40 -0
  115. data/test/session_test/lint_test.rb +9 -0
  116. data/test/session_test/magic_columns_test.rb +62 -0
  117. data/test/session_test/magic_states_test.rb +60 -0
  118. data/test/session_test/params_test.rb +53 -0
  119. data/test/session_test/password_test.rb +106 -0
  120. data/test/session_test/perishability_test.rb +15 -0
  121. data/test/session_test/persistence_test.rb +21 -0
  122. data/test/session_test/scopes_test.rb +60 -0
  123. data/test/session_test/session_test.rb +59 -0
  124. data/test/session_test/timeout_test.rb +52 -0
  125. data/test/session_test/unauthorized_record_test.rb +13 -0
  126. data/test/session_test/validation_test.rb +23 -0
  127. data/test/test_helper.rb +183 -0
  128. data/wulffeld_authlogic.gemspec +220 -0
  129. metadata +258 -0
@@ -0,0 +1,41 @@
1
+ module Authlogic
2
+ module Session
3
+ # Allows you to separate sessions with an id, ultimately letting you create multiple sessions for the same user.
4
+ module Id
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ attr_writer :id
8
+ end
9
+ end
10
+
11
+ # Setting the id if it is passed in the credentials.
12
+ def credentials=(value)
13
+ super
14
+ values = value.is_a?(Array) ? value : [value]
15
+ self.id = values.last if values.last.is_a?(Symbol)
16
+ end
17
+
18
+ # Allows you to set a unique identifier for your session, so that you can have more than 1 session at a time.
19
+ # A good example when this might be needed is when you want to have a normal user session and a "secure" user session.
20
+ # The secure user session would be created only when they want to modify their billing information, or other sensitive
21
+ # information. Similar to me.com. This requires 2 user sessions. Just use an id for the "secure" session and you should be good.
22
+ #
23
+ # You can set the id during initialization (see initialize for more information), or as an attribute:
24
+ #
25
+ # session.id = :my_id
26
+ #
27
+ # Just be sure and set your id before you save your session.
28
+ #
29
+ # Lastly, to retrieve your session with the id check out the find class method.
30
+ def id
31
+ @id
32
+ end
33
+
34
+ private
35
+ # Used for things like cookie_key, session_key, etc.
36
+ def build_key(last_part)
37
+ [id, super].compact.join("_")
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,78 @@
1
+ module Authlogic
2
+ module Session
3
+ # Handles authenticating via a traditional username and password.
4
+ module Klass
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ extend Config
8
+ include InstanceMethods
9
+
10
+ class << self
11
+ attr_accessor :configured_klass_methods
12
+ end
13
+ end
14
+ end
15
+
16
+ module Config
17
+ # Lets you change which model to use for authentication.
18
+ #
19
+ # * <tt>Default:</tt> inferred from the class name. UserSession would automatically try User
20
+ # * <tt>Accepts:</tt> an ActiveRecord class
21
+ def authenticate_with(klass)
22
+ @klass_name = klass.name
23
+ @klass = klass
24
+ end
25
+ alias_method :authenticate_with=, :authenticate_with
26
+
27
+ # The name of the class that this session is authenticating with. For example, the UserSession class will
28
+ # authenticate with the User class unless you specify otherwise in your configuration. See authenticate_with
29
+ # for information on how to change this value.
30
+ def klass
31
+ @klass ||=
32
+ if klass_name
33
+ klass_name.constantize
34
+ else
35
+ nil
36
+ end
37
+ end
38
+
39
+ # Same as klass, just returns a string instead of the actual constant.
40
+ def klass_name
41
+ @klass_name ||= guessed_klass_name
42
+ end
43
+
44
+ # The string of the model name class guessed from the actual session class name.
45
+ def guessed_klass_name
46
+ guessed_name = name.scan(/(.*)Session/)[0]
47
+ guessed_name[0] if guessed_name
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+ # Creating an alias method for the "record" method based on the klass name, so that we can do:
53
+ #
54
+ # session.user
55
+ #
56
+ # instead of:
57
+ #
58
+ # session.record
59
+ def initialize(*args)
60
+ if !self.class.configured_klass_methods
61
+ self.class.send(:alias_method, klass_name.demodulize.underscore.to_sym, :record)
62
+ self.class.configured_klass_methods = true
63
+ end
64
+ super
65
+ end
66
+
67
+ private
68
+ def klass
69
+ self.class.klass
70
+ end
71
+
72
+ def klass_name
73
+ self.class.klass_name
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,95 @@
1
+ module Authlogic
2
+ module Session
3
+ # Just like ActiveRecord has "magic" columns, such as: created_at and updated_at. Authlogic has its own "magic" columns too:
4
+ #
5
+ # Column name Description
6
+ # login_count Increased every time an explicit login is made. This will *NOT* increase if logging in by a session, cookie, or basic http auth
7
+ # failed_login_count This increases for each consecutive failed login. See Authlogic::Session::BruteForceProtection and the consecutive_failed_logins_limit config option for more details.
8
+ # last_request_at Updates every time the user logs in, either by explicitly logging in, or logging in by cookie, session, or http auth
9
+ # current_login_at Updates with the current time when an explicit login is made.
10
+ # last_login_at Updates with the value of current_login_at before it is reset.
11
+ # current_login_ip Updates with the request remote_ip when an explicit login is made.
12
+ # last_login_ip Updates with the value of current_login_ip before it is reset.
13
+ module MagicColumns
14
+ def self.included(klass)
15
+ klass.class_eval do
16
+ extend Config
17
+ include InstanceMethods
18
+ after_persisting :set_last_request_at, :if => :set_last_request_at?
19
+ validate :increase_failed_login_count
20
+ before_save :update_info
21
+ before_save :set_last_request_at, :if => :set_last_request_at?
22
+ end
23
+ end
24
+
25
+ # Configuration for the magic columns feature.
26
+ module Config
27
+ # Every time a session is found the last_request_at field for that record is updatd with the current time, if that field exists.
28
+ # If you want to limit how frequent that field is updated specify the threshold here. For example, if your user is making a
29
+ # request every 5 seconds, and you feel this is too frequent, and feel a minute is a good threashold. Set this to 1.minute.
30
+ # Once a minute has passed in between requests the field will be updated.
31
+ #
32
+ # * <tt>Default:</tt> 0
33
+ # * <tt>Accepts:</tt> integer representing time in seconds
34
+ def last_request_at_threshold(value = nil)
35
+ rw_config(:last_request_at_threshold, value, 0)
36
+ end
37
+ alias_method :last_request_at_threshold=, :last_request_at_threshold
38
+ end
39
+
40
+ # The methods available for an Authlogic::Session::Base object that make up the magic columns feature.
41
+ module InstanceMethods
42
+ private
43
+ def increase_failed_login_count
44
+ if invalid_password? && attempted_record.respond_to?(:failed_login_count)
45
+ attempted_record.failed_login_count ||= 0
46
+ attempted_record.failed_login_count += 1
47
+ end
48
+ end
49
+
50
+ def update_info
51
+ record.login_count = (record.login_count.blank? ? 1 : record.login_count + 1) if record.respond_to?(:login_count)
52
+ record.failed_login_count = 0 if record.respond_to?(:failed_login_count)
53
+
54
+ if record.respond_to?(:current_login_at)
55
+ record.last_login_at = record.current_login_at if record.respond_to?(:last_login_at)
56
+ record.current_login_at = klass.default_timezone == :utc ? Time.now.utc : Time.now
57
+ end
58
+
59
+ if record.respond_to?(:current_login_ip)
60
+ record.last_login_ip = record.current_login_ip if record.respond_to?(:last_login_ip)
61
+ record.current_login_ip = controller.request.remote_ip
62
+ end
63
+ end
64
+
65
+ # This method lets authlogic know whether it should allow the last_request_at field to be updated
66
+ # with the current time (Time.now). One thing to note here is that it also checks for the existence of a
67
+ # last_request_update_allowed? method in your controller. This allows you to control this method pragmatically
68
+ # in your controller.
69
+ #
70
+ # For example, what if you had a javascript function that polled the server updating how much time is left in their
71
+ # session before it times out. Obviously you would want to ignore this request, because then the user would never time out.
72
+ # So you can do something like this in your controller:
73
+ #
74
+ # def last_request_update_allowed?
75
+ # action_name =! "update_session_time_left"
76
+ # end
77
+ #
78
+ # You can do whatever you want with that method.
79
+ def set_last_request_at? # :doc:
80
+ return false if !record || !klass.column_names.include?("last_request_at")
81
+ return controller.last_request_update_allowed? if controller.responds_to_last_request_update_allowed?
82
+ record.last_request_at.blank? || last_request_at_threshold.to_i.seconds.ago >= record.last_request_at
83
+ end
84
+
85
+ def set_last_request_at
86
+ record.last_request_at = klass.default_timezone == :utc ? Time.now.utc : Time.now
87
+ end
88
+
89
+ def last_request_at_threshold
90
+ self.class.last_request_at_threshold
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,59 @@
1
+ module Authlogic
2
+ module Session
3
+ # Authlogic tries to check the state of the record before creating the session. If your record responds to the following methods and any of them return false, validation will fail:
4
+ #
5
+ # Method name Description
6
+ # active? Is the record marked as active?
7
+ # approved? Has the record been approved?
8
+ # confirmed? Has the record been conirmed?
9
+ #
10
+ # Authlogic does nothing to define these methods for you, its up to you to define what they mean. If your object responds to these methods Authlogic will use them, otherwise they are ignored.
11
+ #
12
+ # What's neat about this is that these are checked upon any type of login. When logging in explicitly, by cookie, session, or basic http auth.
13
+ # So if you mark a user inactive in the middle of their session they wont be logged back in next time they refresh the page. Giving you complete control.
14
+ #
15
+ # Need Authlogic to check your own "state"? No problem, check out the hooks section below. Add in a before_validation to do your own checking. The sky is the limit.
16
+ module MagicStates
17
+ def self.included(klass)
18
+ klass.class_eval do
19
+ extend Config
20
+ include InstanceMethods
21
+ validate :validate_magic_states, :unless => :disable_magic_states?
22
+ end
23
+ end
24
+
25
+ # Configuration for the magic states feature.
26
+ module Config
27
+ # Set this to true if you want to disable the checking of active?, approved?, and confirmed? on your record. This is more or less of a
28
+ # convenience feature, since 99% of the time if those methods exist and return false you will not want the user logging in. You could
29
+ # easily accomplish this same thing with a before_validation method or other callbacks.
30
+ #
31
+ # * <tt>Default:</tt> false
32
+ # * <tt>Accepts:</tt> Boolean
33
+ def disable_magic_states(value = nil)
34
+ rw_config(:disable_magic_states, value, false)
35
+ end
36
+ alias_method :disable_magic_states=, :disable_magic_states
37
+ end
38
+
39
+ # The methods available for an Authlogic::Session::Base object that make up the magic states feature.
40
+ module InstanceMethods
41
+ private
42
+ def disable_magic_states?
43
+ self.class.disable_magic_states == true
44
+ end
45
+
46
+ def validate_magic_states
47
+ return true if attempted_record.nil?
48
+ [:active, :approved, :confirmed].each do |required_status|
49
+ if attempted_record.respond_to?("#{required_status}?") && !attempted_record.send("#{required_status}?")
50
+ errors.add(:base, I18n.t("error_messages.not_#{required_status}", :default => "Your account is not #{required_status}"))
51
+ return false
52
+ end
53
+ end
54
+ true
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,101 @@
1
+ module Authlogic
2
+ module Session
3
+ # This module is responsible for authenticating the user via params, which ultimately allows the user to log in using a URL like the following:
4
+ #
5
+ # https://www.domain.com?user_credentials=4LiXF7FiGUppIPubBPey
6
+ #
7
+ # Notice the token in the URL, this is a single access token. A single access token is used for single access only, it is not persisted. Meaning the user
8
+ # provides it, Authlogic grants them access, and that's it. If they want access again they need to provide the token again. Authlogic will
9
+ # *NEVER* try to persist the session after authenticating through this method.
10
+ #
11
+ # For added security, this token is *ONLY* allowed for RSS and ATOM requests. You can change this with the configuration. You can also define if
12
+ # it is allowed dynamically by defining a single_access_allowed? method in your controller. For example:
13
+ #
14
+ # class UsersController < ApplicationController
15
+ # private
16
+ # def single_access_allowed?
17
+ # action_name == "index"
18
+ # end
19
+ #
20
+ # Also, by default, this token is permanent. Meaning if the user changes their password, this token will remain the same. It will only change
21
+ # when it is explicitly reset.
22
+ #
23
+ # You can modify all of this behavior with the Config sub module.
24
+ module Params
25
+ def self.included(klass)
26
+ klass.class_eval do
27
+ extend Config
28
+ include InstanceMethods
29
+ attr_accessor :single_access
30
+ persist :persist_by_params
31
+ end
32
+ end
33
+
34
+ # Configuration for the params / single access feature.
35
+ module Config
36
+ # Works exactly like cookie_key, but for params. So a user can login via params just like a cookie or a session. Your URL would look like:
37
+ #
38
+ # http://www.domain.com?user_credentials=my_single_access_key
39
+ #
40
+ # You can change the "user_credentials" key above with this configuration option. Keep in mind, just like cookie_key, if you supply an id
41
+ # the id will be appended to the front. Check out cookie_key for more details. Also checkout the "Single Access / Private Feeds Access" section in the README.
42
+ #
43
+ # * <tt>Default:</tt> cookie_key
44
+ # * <tt>Accepts:</tt> String
45
+ def params_key(value = nil)
46
+ rw_config(:params_key, value, cookie_key)
47
+ end
48
+ alias_method :params_key=, :params_key
49
+
50
+ # Authentication is allowed via a single access token, but maybe this is something you don't want for your application as a whole. Maybe this is
51
+ # something you only want for specific request types. Specify a list of allowed request types and single access authentication will only be
52
+ # allowed for the ones you specify.
53
+ #
54
+ # * <tt>Default:</tt> ["application/rss+xml", "application/atom+xml"]
55
+ # * <tt>Accepts:</tt> String of a request type, or :all or :any to allow single access authentication for any and all request types
56
+ def single_access_allowed_request_types(value = nil)
57
+ rw_config(:single_access_allowed_request_types, value, ["application/rss+xml", "application/atom+xml"])
58
+ end
59
+ alias_method :single_access_allowed_request_types=, :single_access_allowed_request_types
60
+ end
61
+
62
+ # The methods available for an Authlogic::Session::Base object that make up the params / single access feature.
63
+ module InstanceMethods
64
+ private
65
+ def persist_by_params
66
+ return false if !params_enabled?
67
+ self.unauthorized_record = search_for_record("find_by_single_access_token", params_credentials)
68
+ self.single_access = valid?
69
+ end
70
+
71
+ def params_enabled?
72
+ return false if !params_credentials || !klass.column_names.include?("single_access_token")
73
+ return controller.single_access_allowed? if controller.responds_to_single_access_allowed?
74
+
75
+ case single_access_allowed_request_types
76
+ when Array
77
+ single_access_allowed_request_types.include?(controller.request_content_type) || single_access_allowed_request_types.include?(:all)
78
+ else
79
+ [:all, :any].include?(single_access_allowed_request_types)
80
+ end
81
+ end
82
+
83
+ def params_key
84
+ build_key(self.class.params_key)
85
+ end
86
+
87
+ def single_access?
88
+ single_access == true
89
+ end
90
+
91
+ def single_access_allowed_request_types
92
+ self.class.single_access_allowed_request_types
93
+ end
94
+
95
+ def params_credentials
96
+ controller.params[params_key]
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,240 @@
1
+ module Authlogic
2
+ module Session
3
+ # Handles authenticating via a traditional username and password.
4
+ module Password
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ extend Config
8
+ include InstanceMethods
9
+ validate :validate_by_password, :if => :authenticating_with_password?
10
+
11
+ class << self
12
+ attr_accessor :configured_password_methods
13
+ end
14
+ end
15
+ end
16
+
17
+ # Password configuration
18
+ module Config
19
+ # Authlogic tries to validate the credentials passed to it. One part of validation is actually finding the user and
20
+ # making sure it exists. What method it uses the do this is up to you.
21
+ #
22
+ # Let's say you have a UserSession that is authenticating a User. By default UserSession will call User.find_by_login(login).
23
+ # You can change what method UserSession calls by specifying it here. Then in your User model you can make that method do
24
+ # anything you want, giving you complete control of how users are found by the UserSession.
25
+ #
26
+ # Let's take an example: You want to allow users to login by username or email. Set this to the name of the class method
27
+ # that does this in the User model. Let's call it "find_by_username_or_email"
28
+ #
29
+ # class User < ActiveRecord::Base
30
+ # def self.find_by_username_or_email(login)
31
+ # find_by_username(login) || find_by_email(login)
32
+ # end
33
+ # end
34
+ #
35
+ # Now just specify the name of this method for this configuration option and you are all set. You can do anything you
36
+ # want here. Maybe you allow users to have multiple logins and you want to search a has_many relationship, etc. The sky is the limit.
37
+ #
38
+ # * <tt>Default:</tt> "find_by_smart_case_login_field"
39
+ # * <tt>Accepts:</tt> Symbol or String
40
+ def find_by_login_method(value = nil)
41
+ rw_config(:find_by_login_method, value, "find_by_smart_case_login_field")
42
+ end
43
+ alias_method :find_by_login_method=, :find_by_login_method
44
+
45
+ # The text used to identify credentials (username/password) combination when a bad login attempt occurs.
46
+ # When you show error messages for a bad login, it's considered good security practice to hide which field
47
+ # the user has entered incorrectly (the login field or the password field). For a full explanation, see
48
+ # http://www.gnucitizen.org/blog/username-enumeration-vulnerabilities/
49
+ #
50
+ # Example of use:
51
+ #
52
+ # class UserSession < Authlogic::Session::Base
53
+ # generalize_credentials_error_messages true
54
+ # end
55
+ #
56
+ # This would make the error message for bad logins and bad passwords look identical:
57
+ #
58
+ # Login/Password combination is not valid
59
+ #
60
+ # Alternatively you may use a custom message:
61
+ #
62
+ # class UserSession < AuthLogic::Session::Base
63
+ # generalize_credentials_error_messages "Your login information is invalid"
64
+ # end
65
+ #
66
+ # This will instead show your custom error message when the UserSession is invalid.
67
+ #
68
+ # The downside to enabling this is that is can be too vague for a user that has a hard time remembering
69
+ # their username and password combinations. It also disables the ability to to highlight the field
70
+ # with the error when you use form_for.
71
+ #
72
+ # If you are developing an app where security is an extreme priority (such as a financial application),
73
+ # then you should enable this. Otherwise, leaving this off is fine.
74
+ #
75
+ # * <tt>Default</tt> false
76
+ # * <tt>Accepts:</tt> Boolean
77
+ def generalize_credentials_error_messages(value = nil)
78
+ rw_config(:generalize_credentials_error_messages, value, false)
79
+ end
80
+ alias_method :generalize_credentials_error_messages=, :generalize_credentials_error_messages
81
+
82
+ # The name of the method you want Authlogic to create for storing the login / username. Keep in mind this is just for your
83
+ # Authlogic::Session, if you want it can be something completely different than the field in your model. So if you wanted people to
84
+ # login with a field called "login" and then find users by email this is compeltely doable. See the find_by_login_method configuration
85
+ # option for more details.
86
+ #
87
+ # * <tt>Default:</tt> klass.login_field || klass.email_field
88
+ # * <tt>Accepts:</tt> Symbol or String
89
+ def login_field(value = nil)
90
+ rw_config(:login_field, value, klass.login_field || klass.email_field)
91
+ end
92
+ alias_method :login_field=, :login_field
93
+
94
+ # Works exactly like login_field, but for the password instead. Returns :password if a login_field exists.
95
+ #
96
+ # * <tt>Default:</tt> :password
97
+ # * <tt>Accepts:</tt> Symbol or String
98
+ def password_field(value = nil)
99
+ rw_config(:password_field, value, login_field && :password)
100
+ end
101
+ alias_method :password_field=, :password_field
102
+
103
+ # The name of the method in your model used to verify the password. This should be an instance method. It should also
104
+ # be prepared to accept a raw password and a crytped password.
105
+ #
106
+ # * <tt>Default:</tt> "valid_password?"
107
+ # * <tt>Accepts:</tt> Symbol or String
108
+ def verify_password_method(value = nil)
109
+ rw_config(:verify_password_method, value, "valid_password?")
110
+ end
111
+ alias_method :verify_password_method=, :verify_password_method
112
+ end
113
+
114
+ # Password related instance methods
115
+ module InstanceMethods
116
+ def initialize(*args)
117
+ if !self.class.configured_password_methods
118
+ if login_field
119
+ self.class.send(:attr_writer, login_field) if !respond_to?("#{login_field}=")
120
+ self.class.send(:attr_reader, login_field) if !respond_to?(login_field)
121
+ end
122
+
123
+ if password_field
124
+ self.class.send(:attr_writer, password_field) if !respond_to?("#{password_field}=")
125
+ self.class.send(:define_method, password_field) {} if !respond_to?(password_field)
126
+
127
+ self.class.class_eval <<-"end_eval", __FILE__, __LINE__
128
+ private
129
+ # The password should not be accessible publicly. This way forms using form_for don't fill the password with the
130
+ # attempted password. To prevent this we just create this method that is private.
131
+ def protected_#{password_field}
132
+ @#{password_field}
133
+ end
134
+ end_eval
135
+ end
136
+
137
+ self.class.configured_password_methods = true
138
+ end
139
+
140
+ super
141
+ end
142
+
143
+ # Returns the login_field / password_field credentials combination in hash form.
144
+ def credentials
145
+ if authenticating_with_password?
146
+ details = {}
147
+ details[login_field.to_sym] = send(login_field)
148
+ details[password_field.to_sym] = "<protected>"
149
+ details
150
+ else
151
+ super
152
+ end
153
+ end
154
+
155
+ # Accepts the login_field / password_field credentials combination in hash form.
156
+ def credentials=(value)
157
+ super
158
+ values = value.is_a?(Array) ? value : [value]
159
+ if values.first.is_a?(Hash)
160
+ values.first.with_indifferent_access.slice(login_field, password_field).each do |field, value|
161
+ next if value.blank?
162
+ send("#{field}=", value)
163
+ end
164
+ end
165
+ end
166
+
167
+ def invalid_password?
168
+ invalid_password == true
169
+ end
170
+
171
+ private
172
+ def authenticating_with_password?
173
+ login_field && (!send(login_field).nil? || !send("protected_#{password_field}").nil?)
174
+ end
175
+
176
+ def validate_by_password
177
+ self.invalid_password = false
178
+
179
+ errors.add(login_field, I18n.t('error_messages.login_blank', :default => "cannot be blank")) if send(login_field).blank?
180
+ errors.add(password_field, I18n.t('error_messages.password_blank', :default => "cannot be blank")) if send("protected_#{password_field}").blank?
181
+ return if errors.count > 0
182
+
183
+ self.attempted_record = search_for_record(find_by_login_method, send(login_field))
184
+ if attempted_record.blank?
185
+ generalize_credentials_error_messages? ?
186
+ add_general_credentials_error :
187
+ errors.add(login_field, I18n.t('error_messages.login_not_found', :default => "is not valid"))
188
+ return
189
+ end
190
+
191
+ if !attempted_record.send(verify_password_method, send("protected_#{password_field}"))
192
+ self.invalid_password = true
193
+ generalize_credentials_error_messages? ?
194
+ add_general_credentials_error :
195
+ errors.add(password_field, I18n.t('error_messages.password_invalid', :default => "is not valid"))
196
+ return
197
+ end
198
+ end
199
+
200
+ def invalid_password
201
+ @invalid_password
202
+ end
203
+
204
+ def invalid_password=(value)
205
+ @invalid_password = value
206
+ end
207
+
208
+ def find_by_login_method
209
+ self.class.find_by_login_method
210
+ end
211
+
212
+ def login_field
213
+ self.class.login_field
214
+ end
215
+
216
+ def add_general_credentials_error
217
+ error_message =
218
+ if self.class.generalize_credentials_error_messages.is_a? String
219
+ self.class.generalize_credentials_error_messages
220
+ else
221
+ "#{login_field.to_s.humanize}/Password combination is not valid"
222
+ end
223
+ errors.add(:base, I18n.t('error_messages.general_credentials_error', :default => error_message))
224
+ end
225
+
226
+ def generalize_credentials_error_messages?
227
+ self.class.generalize_credentials_error_messages
228
+ end
229
+
230
+ def password_field
231
+ self.class.password_field
232
+ end
233
+
234
+ def verify_password_method
235
+ self.class.verify_password_method
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end