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
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,22 @@
1
+ == 2.0.0
2
+
3
+ * Refactored nearly all code and tests, especially acts_as_authentic. Got rid of the meta programming and rewrote to use modules and hooks. Also moved all configuration into their related modules.
4
+ * Set up a strong API with hooks to allow you to modify behavior and most importantly, easily create "add on" modules or alternate authentication methods, etc.
5
+ * Changed configuration method for acts_as_authentic to accept a block instead of a hash.
6
+ * The record attribute will NEVER be set until after validation passes, similar to how ActiveRecord executes UPDATEs and CREATEs.
7
+ * Fixed bug with session maintenance where user would log in as new user when creating another user account, typically an admin function.
8
+ * Brute force protection is only a temporary ban by default, not a permanent one.
9
+ * Switched to Hoe for gem management instead of Echoe.
10
+ * Added MD5 crypto provider for legacy systems.
11
+ * Make password salt field optional for legacy systems.
12
+
13
+ == 1.4.4
14
+
15
+ * Moved session maintenance to a before_save, to save on queries executed and to skip an unexpected / additional save on the user object.
16
+ * Extracted random string generation into its own class and leverages SecureRandom if it is available
17
+ * Move cookies to a higher priority when trying to find the record to help with performance since Rails 3 lazily loads the sessions
18
+ * Reset perishable token in a before_save instead of a before_validation
19
+
1
20
  == 1.4.3 released 2009-2-22
2
21
 
3
22
  * Fixed issue with brute force protection.
data/Manifest.txt ADDED
@@ -0,0 +1,111 @@
1
+ CHANGELOG.rdoc
2
+ MIT-LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ generators/session/session_generator.rb
7
+ generators/session/templates/session.rb
8
+ init.rb
9
+ lib/authlogic.rb
10
+ lib/authlogic/acts_as_authentic/base.rb
11
+ lib/authlogic/acts_as_authentic/email.rb
12
+ lib/authlogic/acts_as_authentic/logged_in_status.rb
13
+ lib/authlogic/acts_as_authentic/login.rb
14
+ lib/authlogic/acts_as_authentic/magic_columns.rb
15
+ lib/authlogic/acts_as_authentic/password.rb
16
+ lib/authlogic/acts_as_authentic/perishable_token.rb
17
+ lib/authlogic/acts_as_authentic/persistence_token.rb
18
+ lib/authlogic/acts_as_authentic/restful_authentication.rb
19
+ lib/authlogic/acts_as_authentic/session_maintenance.rb
20
+ lib/authlogic/acts_as_authentic/single_access_token.rb
21
+ lib/authlogic/acts_as_authentic/validations_scope.rb
22
+ lib/authlogic/authenticates_many/association.rb
23
+ lib/authlogic/authenticates_many/base.rb
24
+ lib/authlogic/controller_adapters/abstract_adapter.rb
25
+ lib/authlogic/controller_adapters/merb_adapter.rb
26
+ lib/authlogic/controller_adapters/rails_adapter.rb
27
+ lib/authlogic/crypto_providers/aes256.rb
28
+ lib/authlogic/crypto_providers/bcrypt.rb
29
+ lib/authlogic/crypto_providers/md5.rb
30
+ lib/authlogic/crypto_providers/sha1.rb
31
+ lib/authlogic/crypto_providers/sha512.rb
32
+ lib/authlogic/i18n.rb
33
+ lib/authlogic/random.rb
34
+ lib/authlogic/session/activation.rb
35
+ lib/authlogic/session/active_record_trickery.rb
36
+ lib/authlogic/session/base.rb
37
+ lib/authlogic/session/brute_force_protection.rb
38
+ lib/authlogic/session/callbacks.rb
39
+ lib/authlogic/session/cookies.rb
40
+ lib/authlogic/session/existence.rb
41
+ lib/authlogic/session/foundation.rb
42
+ lib/authlogic/session/http_auth.rb
43
+ lib/authlogic/session/id.rb
44
+ lib/authlogic/session/klass.rb
45
+ lib/authlogic/session/magic_columns.rb
46
+ lib/authlogic/session/magic_states.rb
47
+ lib/authlogic/session/params.rb
48
+ lib/authlogic/session/password.rb
49
+ lib/authlogic/session/perishable_token.rb
50
+ lib/authlogic/session/persistence.rb
51
+ lib/authlogic/session/priority_record.rb
52
+ lib/authlogic/session/scopes.rb
53
+ lib/authlogic/session/session.rb
54
+ lib/authlogic/session/timeout.rb
55
+ lib/authlogic/session/unauthorized_record.rb
56
+ lib/authlogic/session/validation.rb
57
+ lib/authlogic/testing/test_unit_helpers.rb
58
+ lib/authlogic/version.rb
59
+ shoulda_macros/authlogic.rb
60
+ test/acts_as_authentic_test/base_test.rb
61
+ test/acts_as_authentic_test/email_test.rb
62
+ test/acts_as_authentic_test/logged_in_status_test.rb
63
+ test/acts_as_authentic_test/login_test.rb
64
+ test/acts_as_authentic_test/magic_columns_test.rb
65
+ test/acts_as_authentic_test/password_test.rb
66
+ test/acts_as_authentic_test/perishable_token_test.rb
67
+ test/acts_as_authentic_test/persistence_token_test.rb
68
+ test/acts_as_authentic_test/session_maintenance_test.rb
69
+ test/acts_as_authentic_test/single_access_test.rb
70
+ test/authenticates_many_test.rb
71
+ test/crypto_provider_test/aes256_test.rb
72
+ test/crypto_provider_test/bcrypt_test.rb
73
+ test/crypto_provider_test/sha1_test.rb
74
+ test/crypto_provider_test/sha512_test.rb
75
+ test/fixtures/companies.yml
76
+ test/fixtures/employees.yml
77
+ test/fixtures/projects.yml
78
+ test/fixtures/users.yml
79
+ test/libs/company.rb
80
+ test/libs/employee.rb
81
+ test/libs/employee_session.rb
82
+ test/libs/mock_controller.rb
83
+ test/libs/mock_cookie_jar.rb
84
+ test/libs/mock_request.rb
85
+ test/libs/ordered_hash.rb
86
+ test/libs/project.rb
87
+ test/libs/user.rb
88
+ test/libs/user_session.rb
89
+ test/random_test.rb
90
+ test/session_test/activation_test.rb
91
+ test/session_test/active_record_trickery_test.rb
92
+ test/session_test/brute_force_protection_test.rb
93
+ test/session_test/callbacks_test.rb
94
+ test/session_test/cookies_test.rb
95
+ test/session_test/credentials_test.rb
96
+ test/session_test/existence_test.rb
97
+ test/session_test/http_auth_test.rb
98
+ test/session_test/id_test.rb
99
+ test/session_test/klass_test.rb
100
+ test/session_test/magic_columns_test.rb
101
+ test/session_test/magic_states_test.rb
102
+ test/session_test/params_test.rb
103
+ test/session_test/password_test.rb
104
+ test/session_test/perishability_test.rb
105
+ test/session_test/persistence_test.rb
106
+ test/session_test/scopes_test.rb
107
+ test/session_test/session_test.rb
108
+ test/session_test/timeout_test.rb
109
+ test/session_test/unauthorized_record_test.rb
110
+ test/session_test/validation_test.rb
111
+ test/test_helper.rb
data/README.rdoc CHANGED
@@ -1,8 +1,8 @@
1
1
  = Authlogic
2
2
 
3
- Authlogic is a clean, simple, and unobtrusive ruby authentication solution. Put simply, its the Chuck Norris of authentication solutions for your framework of choice.
3
+ Authlogic is a clean, simple, and unobtrusive ruby authentication solution.
4
4
 
5
- So what is Authlogic, and why would I create a solution to a problem that already has plenty of solutions? Because none of the current solutions feel right. The feel wrong because logic is not organized properly in the MVC structure. As you may know, a common misconception with the MVC design pattern is that the model "M" is only for data access logic, which is wrong. A model is a place for domain logic. This is why the RESTful design pattern and the current authentication solutions don't play nice, because the logic is not in the right spot. Authlogic solves this by placing the session maintenance logic into its own domain (aka "model"), where it belongs. Moving session maintenance into its own domain has its benefits:
5
+ What inspired me to create Authlogic was the messiness of the current authentication solutions. Put simply, they just didn't feel right. They felt wrong because the logic was not organized properly. As you may know, a common misconception with the MVC design pattern is that the model "M" is only for data access logic, which is wrong. A model is a place for domain logic. This is why the RESTful design pattern and the current authentication solutions don't play nice. Authlogic solves this by placing the session maintenance logic into its own domain (aka "model"). Moving session maintenance into its own domain has its benefits:
6
6
 
7
7
  1. It's easier to update and stay current with the latest security practices. Since authlogic sits in between you and your session it can assist in keeping your security top notch. Such as upgrading your hashing algorithm, helping you transition to a new algorithm, etc. Also, Authlogic is a gem, which means you get all of these benefits easily, through a rubygems update.
8
8
  2. It ties everything together on the domain level. Take a new user registration for example, no reason to manually log the user in, authlogic handles this for you via callbacks. The same applies to a user changing their password. Authlogic handles maintaining the session for you.
@@ -13,11 +13,84 @@ So what is Authlogic, and why would I create a solution to a problem that alread
13
13
 
14
14
  Authlogic can do all of this and much more, keep reading to see...
15
15
 
16
- == Quick example
16
+ == Helpful links
17
+
18
+ * <b>Documentation:</b> http://authlogic.rubyforge.org
19
+ * <b>Tutorial: Authlogic basic setup:</b> http://www.binarylogic.com/2008/11/3/tutorial-authlogic-basic-setup
20
+ * <b>Tutorial: Reset passwords with Authlogic the RESTful way:</b> http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic
21
+ * <b>Tutorial: Using OpenID with Authlogic:</b> http://www.binarylogic.com/2008/11/21/tutorial-using-openid-with-authlogic
22
+ * <b>Live example of the tutorials above (with source):</b> http://authlogicexample.binarylogic.com
23
+ * <b>Tutorial: Easily migrate from restful_authentication:</b> http://www.binarylogic.com/2008/11/23/tutorial-easily-migrate-from-restful_authentication-to-authlogic
24
+ * <b>Tutorial: Upgrade passwords easily with Authlogic:</b> http://www.binarylogic.com/2008/11/23/tutorial-upgrade-passwords-easily-with-authlogic
25
+ * <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/18752-authlogic
26
+ * <b>Google group:</b> http://groups.google.com/group/authlogic
27
+
28
+ **Before contacting me, please read:**
29
+ If you find a bug or a problem please post it on lighthouse. If you need help with something, please use google groups. I check both regularly and get emails when anything happens, so that is the best place to get help. Please do not email me directly, with issues regarding Authlogic.
30
+
31
+ == Documentation
32
+
33
+ You can find anything you want about Authlogic in the documentation, all that you need to do is understand the basic design behind it.
34
+
35
+ That being said, Authlogic is split into 2 main parts:
36
+
37
+ 1. Authlogic::Session, which manages sessions.
38
+ 2. Authlogic::ActsAsAuthentic, which adds in functionality to your ActiveRecord model.
39
+
40
+ Each of the above has its various sub modules that contain common logic. The sub modules are responsible for including everything related to it: configuration, class methods, instance methods, etc.
41
+
42
+ For example, if you want to timeout users after a certain period of inactivity, you would look in Authlogic::Session::Timeout. To help you out, I listed the following "publicly relevant" modules with short descriptions. For the sake of brevity, there are more modules than listed here, the ones not listed are more for internal use, but you can easily read up on them in the documentation.
43
+
44
+ === Authlogic::ActsAsAuthentic sub modules
45
+
46
+ These modules are for the acts_as_authentic method you call in your model. It contains all code for the "model side" of the authentication.
47
+
48
+ * Authlogic::ActsAsAuthentic::Base - Provides the acts_as_authentic class method and includes all of the submodules.
49
+ * Authlogic::ActsAsAuthentic::Email - Handles everything related to the email field.
50
+ * Authlogic::ActsAsAuthentic::LoggedInStatus - Provides handy named scopes and methods for determining if the user is logged in or out.
51
+ * Authlogic::ActsAsAuthentic::Login - Handles everything related to the login field.
52
+ * Authlogic::ActsAsAuthentic::MagicColumns - Handles everything related to the "magic" fields: login_count, failed_login_count, etc.
53
+ * Authlogic::ActsAsAuthentic::Password - This one is important. It handles encrypting your password, salting it, etc. It also has support for transitioning password algorithms.
54
+ * Authlogic::ActsAsAuthentic::PerishableToken - Handles maintaining the perishable token field, also provides a class level method for finding record using the token.
55
+ * Authlogic::ActsAsAuthentic::PersistenceToken - Handles maintaining the persistence token. This is the token stored in cookies and sessions to persist the users session.
56
+ * Authlogic::ActsAsAuthentic::RestfulAuthentication - Provides configuration options to easily migrate from the restful_authentication plugin.
57
+ * Authlogic::ActsAsAuthentic::Scope - Allows you to scope validations, etc. Just like the :scope option for validates_uniqueness_of
58
+ * Authlogic::ActsAsAuthentic::SessionMaintenance - Handles automatically logging the user in. EX: a new user registers, automatically log them in.
59
+ * Authlogic::ActsAsAuthentic::SingleAccessToken - Handles maintaining the single access token.
60
+
61
+ === Authlogic::Session sub modules
62
+
63
+ These modules are for the "session side" of authentication. They create a new domain for session logic, allowing you to create, destroy, and ultimately manage your sessions.
64
+
65
+ * Authlogic::Session::BruteForceProtection - Disables accounts after a certain number of consecutive failed login attempted.
66
+ * Authlogic::Session::Callbacks - Your tools to extend Authlogic, lets you hook in and add/modify behavior, on top of overriding methods.
67
+ * Authlogic::Session::Cookies - Authentication via cookies.
68
+ * Authlogic::Session::Existence - Creating, saving, and destroying objects.
69
+ * Authlogic::Session::HttpAuth - Authentication via basic HTTP authentication.
70
+ * Authlogic::Session::Id - Allows sessions to be separated by an id, letting you have multiple sessions for a single user.
71
+ * Authlogic::Session::MagicColumns - Maintains "magic" database columns, similar to created_at and updated_at for ActiveRecord.
72
+ * Authlogic::Session::MagicStates - Automatically validates based on the records states: active, approved, and confirmed.
73
+ * Authlogic::Session::Params - Authentication via params, aka single access token.
74
+ * Authlogic::Session::Password - Authentication via a traditional username and password.
75
+ * Authlogic::Session::Persistence - Persisting sessions / finding sessions.
76
+ * Authlogic::Session::Session - Authentication via the session.
77
+ * Authlogic::Session::Timeout - Automatically logging out after a certain period of inactivity.
78
+ * Authlogic::Session::UnauthorizedRecord - Handles authentication by passing an ActiveRecord object.
79
+ * Authlogic::Session::Validation - Validation / errors.
17
80
 
18
- Let's take a rails application...
81
+ === Miscellaneous modules
19
82
 
20
- What if creating a user session could be as simple as...
83
+ Miscellaneous modules that don't really belong solely to either the session or model aspect.
84
+
85
+ * Authlogic::AuthenticatesMany - Responsible for allowing you to scope sessions to a parent record. Similar to a has_many and belongs_to relationship. This lets you do the same thing with sessions.
86
+ * Authlogic::CryptoProviders - Contains various encryption algorithms that Authlogic uses, allowing you to choose your encryption method.
87
+ * Authlogic::I18n - Acts JUST LIKE the rails I18n library, and provides internationalization to Authlogic.
88
+ * Authlogic::Testing - Various helper methods for testing frameworks to help you test your code.
89
+ * Authlogic::Version - A handy class for determine the version of Authlogic in a number of ways.
90
+
91
+ == Quick example
92
+
93
+ What if creating sessions worked like an ORM library on the surface...
21
94
 
22
95
  UserSession.create(params[:user_session])
23
96
 
@@ -43,7 +116,7 @@ What if your user sessions controller could look just like your other controller
43
116
  end
44
117
  end
45
118
 
46
- Look familiar? If you didn't know any better, you would think UserSession was an ActiveRecord model. I think that's pretty cool, because it fits nicely into the RESTful development pattern, a style we all know and love. What about the view...
119
+ As you can see, this fits nicely into the RESTful development pattern. What about the view...
47
120
 
48
121
  <% form_for @user_session do |f| %>
49
122
  <%= f.error_messages %>
@@ -73,22 +146,10 @@ Or how about persisting the session...
73
146
  end
74
147
  end
75
148
 
76
- == Helpful links
77
-
78
- * <b>Documentation:</b> http://authlogic.rubyforge.org
79
- * <b>Tutorial: Authlogic basic setup:</b> http://www.binarylogic.com/2008/11/3/tutorial-authlogic-basic-setup
80
- * <b>Tutorial: Reset passwords with Authlogic the RESTful way:</b> http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic
81
- * <b>Tutorial: Using OpenID with Authlogic:</b> http://www.binarylogic.com/2008/11/21/tutorial-using-openid-with-authlogic
82
- * <b>Live example of the tutorials above (with source):</b> http://authlogicexample.binarylogic.com
83
- * <b>Tutorial: Easily migrate from restful_authentication:</b> http://www.binarylogic.com/2008/11/23/tutorial-easily-migrate-from-restful_authentication-to-authlogic
84
- * <b>Tutorial: Upgrade passwords easily with Authlogic:</b> http://www.binarylogic.com/2008/11/23/tutorial-upgrade-passwords-easily-with-authlogic
85
- * <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/18752-authlogic
86
- * <b>Google group:</b> http://groups.google.com/group/authlogic
87
-
88
- If you find a bug or a problem please post it on lighthouse. If you need help with something or it is specific to your app, please use google groups. I check both regularly and get emails when anything happens, so that is the best place to get help.
89
-
90
149
  == Install and use
91
150
 
151
+ === 1. Install the gem
152
+
92
153
  Install the gem / plugin (recommended)
93
154
 
94
155
  $ sudo gem install authlogic
@@ -102,7 +163,7 @@ Or you install this as a plugin (for older versions of rails)
102
163
 
103
164
  script/plugin install git://github.com/binarylogic/authlogic.git
104
165
 
105
- === Create your session
166
+ === 2. Create your session
106
167
 
107
168
  Lets assume you are setting up a session for your User model.
108
169
 
@@ -114,388 +175,54 @@ This will create a file that looks similar to:
114
175
 
115
176
  # app/models/user_session.rb
116
177
  class UserSession < Authlogic::Session::Base
117
- # configuration here, just like ActiveRecord, or in an initializer
118
- # See Authlogic::Session::Config::ClassMethods for more details
119
- end
120
-
121
- === Ensure proper database fields
122
-
123
- The user model needs to have the following columns. The names of these columns can be changed with configuration. Better yet, Authlogic tries to guess these names by checking for the existence of common names. See Authlogic::Session::Config::ClassMethods for more details, but chances are you won't have to specify any configuration for your field names, even if they aren't the same names as below.
124
-
125
- t.string :login, :null => false
126
- t.string :crypted_password, :null => false
127
- t.string :password_salt, :null => false
128
- t.string :persistence_token, :null => false
129
- t.string :single_access_token, :null => false # optional, see the tokens section below.
130
- t.string :perishable_token, :null => false # optional, see the tokens section below.
131
- t.integer :login_count, :null => false, :default => 0 # optional, this is a "magic" column, see the magic columns section below
132
- t.integer :failed_login_count, :null => false, :default => 0 # optional, this is a "magic" column, see the magic columns section below
133
-
134
- === Set up your model
135
-
136
- Make sure you have a model that you will be authenticating with. For this example let's say you have a User model:
137
-
138
- class User < ActiveRecord::Base
139
- acts_as_authentic # for options see documentation: Authlogic::ORMAdapters::ActiveRecordAdapter::ActsAsAuthentic::Config
140
- end
141
-
142
- One thing to keep in mind here is that the default :crypto_provider for Authlogic is Sha512. You are *NOT* forced to use this. See the encryption methods section below for more information.
143
-
144
- You are all set, now go use it just like you would with any other ActiveRecord model. Either glance at the code at the beginning of this README or check out the tutorials (see above in "helpful links") for a more detailed walk through.
145
-
146
- == Migrating an existing app from restful_authentication and upgrading your encryption
147
-
148
- For those that are switching existing apps over, I made an option especially for you. Just do the following and everything will be taken care of, your users won't even know anything changed:
149
-
150
- # app/models/user.rb
151
- class User < ActiveRecord::Base
152
- acts_as_authentic :act_like_restful_authentication => true
153
- end
154
-
155
- The above will not change a thing, from your database's perspective it will be as if you are still using restful_authentication.
156
-
157
- Or you can upgrade from Sha1 and transition your users to a much more secure encryption algorithm:
158
-
159
- # app/models/user.rb
160
- class User < ActiveRecord::Base
161
- acts_as_authentic :transition_from_restful_authentication => true
162
- end
163
-
164
- By default this will switch your users to Authlogic's Sha512 implementation. You do *NOT* have to use this. Check out the encryption methods section below for a list of encryption methods Authlogic provides you. If you want to use something besides Sha512 just specify it by doing:
165
-
166
- # app/models/user.rb
167
- class User < ActiveRecord::Base
168
- acts_as_authentic :transition_from_restful_authentication => true,
169
- :crypto_provider => Authlogic::CryptoProviders::BCrypt
170
- end
171
-
172
- Every time a user logs in their password will be upgraded and every time a new account is created it will use the new algorithm all while allowing users to login with the old algorithm.
173
-
174
- For more information checkout my blog post on this: http://www.binarylogic.com/2008/11/23/tutorial-easily-migrate-from-restful_authentication-to-authlogic
175
-
176
- == Magic Columns
177
-
178
- Just like ActiveRecord has "magic" columns, such as: created_at and updated_at. Authlogic has its own "magic" columns too:
179
-
180
- Column name Description
181
- 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
182
- 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.
183
- last_request_at Updates every time the user logs in, either by explicitly logging in, or logging in by cookie, session, or http auth
184
- current_login_at Updates with the current time when an explicit login is made.
185
- last_login_at Updates with the value of current_login_at before it is reset.
186
- current_login_ip Updates with the request remote_ip when an explicit login is made.
187
- last_login_ip Updates with the value of current_login_ip before it is reset.
188
-
189
- == Magic States
190
-
191
- 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:
192
-
193
- Method name Description
194
- active? Is the record marked as active?
195
- approved? Has the record been approved?
196
- confirmed? Has the record been conirmed?
197
-
198
- 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.
199
-
200
- 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. 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.
201
-
202
- 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.
203
-
204
- == Hooks / Callbacks
205
-
206
- Just like ActiveRecord you can create your own hooks / callbacks so that you can do whatever you want when certain actions are performed. Such as before_save, after_save, etc.
207
-
208
- See Authlogic::Session::Callbacks for more information.
209
-
210
- == Multiple Sessions / Session Identifiers
211
-
212
- You're asking: "why would I want multiple sessions?". Take this example:
213
-
214
- You have an app where users login and then need to re-login to view / change their billing information. Similar to how Apple's me.com works. What you could do is have the user login with their normal session, then have an entirely new session that represents their "secure" session. But wait, this is 2 users sessions. No problem:
215
-
216
- # regular user session
217
- @user_session = UserSession.new
218
- @user_session.id
219
- # => nil
220
-
221
- # secure user session
222
- @secure_user_session = UserSession.new(:secure)
223
- @secure_user_session.id
224
- # => :secure
225
-
226
- This will keep everything separate. The :secure session will store its info in a separate cookie, separate session, etc. Just set the id and you are good to go. Need to retrieve the session?
227
-
228
- @user_session = UserSession.find
229
- @secure_user_session = UserSession.find(:secure)
230
-
231
- For more information on ids checkout Authlogic::Session::Base#id
232
-
233
- == Encryption methods
234
-
235
- Authlogic is designed so you can use *any* encryption method you want. It delegates this task to a class of your choice. Authlogic comes preloaded with some common algorithms:
236
-
237
- 1. Authlogic::CryptoProviders::Sha1 (used mainly for migrating from restful_authentication)
238
- 2. Authlogic::CryptoProviders::Sha512 (default)
239
- 3. Authlogic::CryptoProviders::BCrypt (requires bcrypt-ruby gem)
240
- 4. Authlogic::CryptoProviders::AES256 (requires you to supply a key, see the AES256 class in the docs for more info)
241
-
242
- By default Authlogic uses salted Sha512 with 20 stretches, but you can easily change this. For example, if you wanted to use the BCrypt algorithm just do the following:
243
-
244
- acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::BCrypt
245
-
246
- For more information on BCrypt checkout my blog post on it: http://www.binarylogic.com/2008/11/22/storing-nuclear-launch-codes-in-your-app-enter-bcrypt-for-authlogic
247
-
248
- Also, check out the Authlogic::CryptoProviders module and subclasses to get an idea of how to write your own crypto provider. You don't have to use the provided classes, you can easily write your own. All that you have to do is make a class with a class level encrypt and matches? method. That's it, all of the encryption and decryption logic is left to you.
249
-
250
- == Switching to a new encryption method
251
-
252
- Switching to a new encryption method used to be a pain in the ass. Authlogic has an option that makes this dead simple. Let's say you want to migrate to the BCrypt encryption method from Sha512:
253
-
254
- acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::BCrypt,
255
- :transition_from_crypto_provider => Authlogic::CryptoProviders::Sha512
256
-
257
- That's it. When a user successfully logs in and is using the old method their password will be updated with the new method and all new registrations will use the new method as well. Your users won't know anything changed.
258
-
259
- But wait, what if a couple of years later CCrypt comes out and its better than BCrypt and you're still in the middle of transitioning all of your users to BCrypt. Oh no!
260
-
261
- Not to worry, because Authlogic can transition your users from more than one algorithm. Just pass an array to :transition_from_crypto_provider
262
-
263
- acts_as_authentic :crypto_provider => CCrypt,
264
- :transition_from_crypto_provider => [Authlogic::CryptoProviders::Sha512, Authlogic::CryptoProviders::BCrypt]
265
-
266
- That's it, specify as many as you want. One thing to keep in mind here is that if you are using BCrypt you should never have to do this. All that you need to do is increase the cost to make the algorithm stronger, no need to jump to entirely new algorithm. I did this for example purposes only.
267
-
268
- == Tokens (persistence, resetting passwords, private feed access, etc.)
269
-
270
- To start, let me define tokens as Authlogic sees it. A token is a form of credentials that grants some type of access to their account. Depending on the type of access, a different type of token may be needed. Put simply, it's a way for the user to say "I am this person, let me proceed". What types of different access you ask? Here are just a few:
271
-
272
- 1. Regular account access
273
- 2. Access to reset their password
274
- 3. Access to a private feed
275
- 4. Access to confirm their account
276
-
277
- There could be many more depending on your application. What's great about Authlogic is that it doesn't care what you do or how you want to grant access to accounts. That's up to you and your application. Authlogic just cares about the type of tokens you need. Instead of giving you a token for each specific task, it gives you all of the necessary *types* of tokens, and you get to use them how you wish. It maintains the tokens and gives you all of the tools you need to use them. Just add the fields to your database and you are good to go.
278
-
279
- Here are the 3 tokens in more detail:
280
-
281
- === 1. Persistence token (stored in cookie / session)
282
-
283
- This token is used to persist the user's session. This is the token that is stored in the session and the cookie, so that during each request the user stays logged in. What's unique about this token is that the first time it is used the value is stored in the session, thus persisting the session. This field is required and must be in your database.
284
-
285
- === 2. Single access token (private feed access, etc.)
286
-
287
- This token is used for single access only, it is not persisted. Meaning the user provides it, Authlogic grants them access, and that's it. If they want access again they need to provide the token again. Authlogic will *NEVER* store this value in the session or a cookie. For added security, by default this token is *ONLY* allowed for RSS and ATOM requests. Also, this token does *NOT* change with the password. Meaning if the user changes their password, this token will remain the same. Lastly, this token uses a "friendly" token (see the URL example below) so that it is easier to email / copy and paste. You can change all of this with configuration (see Authlogic::Session::config), so if you don't like how this works by default, just set some simple configuration in your session.
288
-
289
- For even more flexibility Authlogic looks for a method in your controller called single_access_allowed?. If that method exists and returns true Authlogic will try to log in the user with this method. Here is a quick example:
290
-
291
- class UsersController < ApplicationController
292
- private
293
- def single_access_allowed?
294
- action_name == "index"
295
- end
296
-
297
- The above will only allow logging in via the single access toke with the index method only.
298
-
299
- This field is optional, if you want to use it just add the field to your database:
300
-
301
- t.string :single_access_token, :null => false
302
- # or call it feeds_token, feed_token, or whatever you want with configuration
303
-
304
- This is great for private feed access. So your URL to that user's private feed could look something like:
305
-
306
- http://www.mydomain.com/account/feed.rss?user_credentials=4LiXF7FiGUppIPubBPey
307
-
308
- The user_credentials parameter name is configurable (see Authlogic::Session::Config), but if that parameter exists Authlogic will automatically use it to try and grant that user access. You don't have to do a thing: UserSession.find will take care of it just like it does for everything else.
309
-
310
- For more information see: Authlogic::ORMAdapters::ActiveRecordAdapter::ActsAsAuthentic::SingleAccess
311
-
312
- === 3. Perishable token (resetting passwords, confirming accounts, etc)
313
-
314
- This token is used for temporary account access, hence the term "perishable". This token is constantly changing, it changes...
315
-
316
- 1. In a before_validation in your model, so basically every time the record is saved
317
- 2. Any time a new session is successfully saved (aka logged in)
318
-
319
- This is perfect for <b>resetting passwords</b> or <b>confirming accounts</b>. You email them a url with this token in it, and then use this token to find the record and perform your action.
320
-
321
- This field is optional, if you want to use it just add the field to your database:
322
-
323
- t.string :perishable_token, :null => false
324
- # or call it password_reset_token, pw_reset_token, activation_token, or whatever you want with configuration
325
-
326
- Finding the record with this token couldn't be easier, Authlogic provides a special finder method that you can use. I highly recommend using it as it adds extra security:
327
-
328
- User.find_using_perishable_token(token)
329
- User.find_using_perishable_token(token, 20.minutes)
330
-
331
- That's all you need to do to locate the record. Here is what it does for extra security:
332
-
333
- 1. Ignores blank tokens all together. If a blank token is passed nil will be returned.
334
- 2. It checks the age of the token, by default the threshold is 10 minutes, meaning if the token is older than 10 minutes, it is not valid and no record will be returned. You can change the default or just override it by passing the threshold as the second parameter. If you don't want a threshold at all, pass 0.
335
-
336
- Just like the single access token this uses a friendly token, so it is easier to email / copy and paste.
337
-
338
- For a detailed tutorial on how to reset password using this token see the helpful links section above.
339
-
340
- For more information see: Authlogic::ORMAdapters::ActiveRecordAdapter::ActsAsAuthentic::Perishability
341
-
342
- == Scoping
343
-
344
- Scoping with authentication is a little tricky because it can come in many different flavors:
345
-
346
- 1. Accounts have many users, meaning users can only belong to one account at a time.
347
- 2. Accounts have and belong to many users, meaning a user can belong to more than one account.
348
- 3. Users access their accounts via subdomains.
349
- 4. Users access their accounts by selecting their account and storing their selection, *NOT* using subdomains. Maybe you store their selection in a session, cookie, or the database. It doesn't matter.
350
-
351
- Now mix and match the above, it can get pretty hairy. Fear not, because Authlogic is designed in a manner where it doesn't care how you do it, all that you have to do is break it down. When scoping a session there are 3 parts you might want to scope:
352
-
353
- 1. The model (the validations, etc)
354
- 2. The session (finding the record)
355
- 3. The cookies (the names of the session key and cookie)
356
-
357
- I will describe each below, in order.
358
-
359
- === 1. Scoping your model
360
-
361
- This scopes your login field validation, so that users are allowed to have the same login, just not in the same account.
362
-
363
- # app/models/user.rb
364
- class User < ActiveRecord::Base
365
- acts_as_authentic :scope => :account_id
366
- end
367
-
368
- === 2. Scoping your session
369
-
370
- When the session tries to validate it searches for a record. You want to scope that search. No problem...
371
-
372
- The goal of Authlogic was to not try and introduce anything new. As a result I came up with:
373
-
374
- @account.user_sessions.find
375
- @account.user_sessions.create
376
- @account.user_sessions.build
377
- # ... etc
378
-
379
- This works just like ActiveRecord, so it should come natural. Here is how you get this functionality:
380
-
381
- class Account < ActiveRecord::Base
382
- authenticates_many :user_sessions
383
- end
384
-
385
- === 3. Scoping cookies
386
-
387
- What's neat about cookies is that if you use sub domains they automatically scope their self. Meaning if you create a cookie in whatever.yourdomain.com it will not exist in another.yourdomain.com. So if you are using subdomains to scope your users, you don't have to do anything.
388
-
389
- But what if you *don't* want to separate your cookies by subdomains? You can accomplish this by doing:
390
-
391
- ActionController::Base.session_options[:session_domain] = '.mydomain.com'
392
-
393
- or for Rails 2.3.0 or higher:
394
-
395
- ActionController::Base.session_options[:domain] = '.mydomain.com'
396
-
397
-
398
- Notice the above is configuration for your session, not your cookies. Authlogic notices this and assume this is how you want to treat your cookies as well. As a result, it applies this domain to the cookies it sets. Now your session and all cookies act the same and are scoped under the same domain under Authlogic.
399
-
400
- Now let's look at this from the other angle. What if you are *NOT* using subdomains, but still want to separate cookies for each account. Simple, set the :scope_cookies option for authenticate_many:
401
-
402
- class Account < ActiveRecord::Base
403
- authenticates_many :user_sessions, :scope_cookies => true
404
- end
405
-
406
- Done, Authlogic will give each cookie a unique name depending on the account.
407
-
408
- With the above information you should be able to scope your sessions any way you want. Just mix and match the tools above to accomplish this. Also check out the documentation on Authlogic::ActiveRecord::AuthenticatesMany.
409
-
410
- == Errors
411
-
412
- The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses the exact same ActiveRecord errors class. Use it the same way:
413
-
414
- class UserSession
415
- validate :check_if_awesome
416
-
417
- private
418
- def check_if_awesome
419
- errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
420
- errors.add_to_base("You must be awesome to log in") unless record.awesome?
421
- end
422
- end
423
-
424
- == Timing Out Sessions (Logging out after inactivity)
425
-
426
- Think about financial websites, if you are inactive for a certain period of time you will be asked to log back in on your next request. You can do this with Authlogic easily, there are 2 parts to this:
427
-
428
- 1. Define the timeout threshold:
429
-
430
- acts_as_authentic :logged_in_timeout => 10.minutes # default is 10.minutes
431
-
432
- 2. Enable logging out on timeouts
433
-
434
- class UserSession < Authlogic::Session::Base
435
- logout_on_timeout true # default if false
178
+ # configuration here, see sub modules of Authlogic::Session
436
179
  end
437
180
 
438
- This will require a user to log back in if they are inactive for more than 10 minutes. In order for this feature to be used you must have a last_request_at datetime column in your table for whatever model you are authenticating with.
439
-
440
- == Automatic Session Updating
441
-
442
- This is one of my favorite features that I think is pretty cool. It's things like this that make a library great and let you know you are on the right track.
181
+ === 3. Ensure proper database fields
443
182
 
444
- Just to clear up any confusion, Authlogic stores both the record id and the persistence token in the session. Why? So stale sessions can not be persisted. It stores the id so it can quickly find the record, and the persistence token to ensure no sessions are stale. The persistence token changes with the password, if someone is logged in and their password is changed, they should be logged out, unless they made the change. That being said, the person making the change needs their session to be updated with the new persistence token, so they stay logged in, which is what this section is all about.
183
+ The user model should have the following columns. The names of these columns can be changed with configuration. Better yet, Authlogic tries to guess these names by checking for the existence of common names. See the sub modules of Authlogic::Session for more details, but chances are you won't have to specify any configuration for your field names, even if they aren't the same names as below.
445
184
 
446
- So what if a user changes their password? You have to re-log them in with the new password, recreate the session, etc. This is messy and leads to redundant code. Or what if a user creates a new user account? You have to do the same thing. Here's an even better one: what if a user is in the admin area and changes their own password? There might even be another place passwords can change. It shouldn't matter, your code should be written in a way where you don't have to remember to do this.
185
+ t.string :login, :null => false # optional, you can use email instead, or both
186
+ t.string :crypted_password, :null => false
187
+ t.string :password_salt, :null => false # optional, but highly recommended
188
+ t.string :persistence_token, :null => false
189
+ t.string :single_access_token, :null => false # optional, see Authlogic::Session::Params
190
+ t.string :perishable_token, :null => false # optional, see Authlogic::Session::Perishability
191
+ t.integer :login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
192
+ t.integer :failed_login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
193
+ t.datetime :last_request_at # optional, see Authlogic::Session::MagicColumns
194
+ t.datetime :current_login_at # optional, see Authlogic::Session::MagicColumns
195
+ t.datetime :last_login_at # optional, see Authlogic::Session::MagicColumns
196
+ t.string :current_login_ip # optional, see Authlogic::Session::MagicColumns
197
+ t.string :last_login_ip # optional, see Authlogic::Session::MagicColumns
447
198
 
448
- Instead of updating sessions all over the place, doesn't it make sense to do this at a lower level? Like the User model? You're saying "but Ben, models can't mess around with sessions and cookies". True...but Authlogic can, and you can access Authlogic just like a model. I know in most situations it's not good practice to do this, but I view this in the same class as sweepers, and feel like it actually is good practice here. User sessions are directly tied to users, they should be connected on the model level.
199
+ === 4. Set up your model
449
200
 
450
- Fear not, because the acts_as_authentic method you call in your model takes care of this for you, by adding an after_save callback to automatically keep the session up to date. You don't have to worry about it anymore. Don't even think about it. Let your UsersController deal with users, not users *AND* sessions. *ANYTIME* the user changes his password in *ANY* way, his session will be updated.
451
-
452
- Here is basically how this is done....
201
+ Make sure you have a model that you will be authenticating with. Since we are using the User model it should look something like:
453
202
 
454
203
  class User < ActiveRecord::Base
455
- after_save :maintain_sessions!
456
-
457
- private
458
- def maintain_sessions!
459
- # If we aren't logged in and a user is created, log them in as that user
460
- # If we aren't logged in and a user's password changes, log them in as that user
461
- # If we are logged in and they change their password, update the session so they remain logged in
462
- end
204
+ acts_as_authentic do |c|
205
+ c.my_config_option = my_value # for available options see documentation in: Authlogic::ActsAsAuthentic
206
+ end # block optional
463
207
  end
464
208
 
465
- Obviously there is a little more to it than this, but hopefully this clarifies any confusion. Basically if you are *logged out* and you are changing passwords, Authlogic will log you in, since you already know the password for that account. Lastly, this can be altered / disabled via a configuration option. Just set :session_ids => nil when calling acts_as_authentic.
466
-
467
- When things come together like this I think its a sign that you are doing something right. Put that in your pipe and smoke it!
468
-
469
- == Internationalization (I18n) / Changing Messages
470
-
471
- Please see Authlogic::I18n for more information. Internationalization is very easy to implement, in fact if you are using the default rails I18n library then you don't need to do anything other than defining the messages in your localization configuration files. See Authlogic::I18n for a complete list of keys you need to define.
472
-
473
- == Testing
474
-
475
- Testing with authlogic is easy, there is a helper file that will add some convenient test helpers for you. In your test_helper.rb file do the following:
476
-
477
- # test/test_helper.rb
478
- require 'authlogic/testing/test_unit_helpers'
479
-
480
- You get the following methods:
481
-
482
- set_session_for(record_object)
483
- set_cookie_for(record_object)
484
- set_http_auth_for(username, password)
485
-
486
- In your test, before you execute a request, just call one of those methods and it will set the proper values so that it will seem as if that record is logged in.
487
-
488
- You can also checkout the authlogic_example application (see helpful links above), the tests there use this.
489
-
490
- == Framework agnostic (Rails, Merb, etc.)
209
+ You are all set.
491
210
 
492
- I designed Authlogic to be framework agnostic, meaning it doesn't care what framework you use it in. Right out of the box it supports rails and merb. I have not had the opportunity to use other frameworks, but the only thing stopping Authlogic from being used in other frameworks is a simple adapter. Check out controller_adapters/rails_adapter, or controller_adapters/merb_adapter.
211
+ === 5. Next Steps
493
212
 
494
- Since pretty much all of the frameworks in ruby follow the Rack standards, the code should be very similar across adapters. In fact that abstract adapter assumes you are following the Rack standards. If your framework is following the rack standards, there really isn't any code you should have to write. Check out the merb_adapter to see for yourself, the merb adapter is basically blank. You're saying "but Ben, why not just hook into Rack and avoid the need for controller adapters all together?". It's not that simple, because rails doesn't inherit from the Rack::Request class, plus there are small differences between how rack is implemented in each framework. Authlogic has to hook into your controller with a before_filter anyways, so it can "activate" itself. Why not just use the controller object? Also when we have access to the controller object we can do other nifty things. Checkout the OpenID tutorial in the helpful links section above to see what I mean.
213
+ Here are some common next steps. They might or might not apply to you. For a complete list of everything Authlogic can do please read the documentation or see the sub module list above.
495
214
 
496
- The point in all of this rambling is that implementing Authlogic is as simple as creating an adapter. I created both the rails and merb adapters in under 10 minutes. If you have an adapter you created and would like to add please let me know and I will add it into the source.
215
+ 1. Want to use another encryption algorithm, such as BCrypt? See Authlogic::ActsAsAuthentic::Password::Config
216
+ 2. Migrating from restful_authentication? See Authlogic::ActsAsAuthentic::RestfulAuthentication::Config
217
+ 3. Want to timeout sessions after a period if inactivity? See Authlogic::Session::Timeout
218
+ 4. Need to scope your sessions to an account or parent model? See Authlogic::AuthenticatesMany
219
+ 5. Need multiple session types in your app? Check out Authlogic::Session::Id
220
+ 6. Need to reset passwords or activate accounts? Use the perishable token. See Authlogic::ActsAsAuthentic::PerishableToken
221
+ 7. Need to give API access or access to a private feed? Use basic HTTP auth or authentication by params. See Authlogic::Session::HttpAuth or Authlogic::Session::Params
222
+ 8. Need to internationalize your app? See Authlogic::I18n
223
+ 9. Need help testing? See the Authlogic::Testing
497
224
 
498
- == How it works
225
+ == Interesting in how it works?
499
226
 
500
227
  Interested in how all of this all works? Basically a before filter is automatically set in your controller which lets Authlogic know about the current controller object. This "activates" Authlogic and allows Authlogic to set sessions, cookies, login via basic http auth, etc. If you are using your framework in a multiple thread environment, don't worry. I kept that in mind and made this thread safe.
501
228