authlogic 2.0.9 → 2.0.11

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.

@@ -1,3 +1,20 @@
1
+ == 2.0.11 release 2009-4-25
2
+
3
+ * Fix bug when password is turned off and the SingleAccessToken module calls the after_password_set callback.
4
+ * HTTP basic auth can now be toggled on or off. It also checks for the existence of a standard username and password before enabling itself.
5
+ * Added option check_passwords_against_database for Authlogic::ActsAsAuthentic::Password to toggle between checking the password against the database value or the object value. Also added the same functionality to the instance method: valid_password?("password", true), where the second argument tells Authlogic to check the password against the database value. The default for this new feature is true.
6
+ * Add a maintain_sessions configuration option to Authlogic::ActsAsAuthentic::SessionMaintenance as a "clearer" option to disable automatic session maintenance.
7
+ * single_access_allowed_request_types can also be equal to :all instead of just [:all].
8
+ * Refactor params_enabled? so that the single_access_allowed? method in controllers takes precedence.
9
+ * Added testing comments in the README and expanded on the documentation in Authlogic::TestCase
10
+
11
+ == 2.0.10 release 2009-4-21
12
+
13
+ * Mock request is now transparent to non existent methods. Since the methods calls really have no functional value when testing authlogic.
14
+ * Allow password confirmation to be disabled.
15
+ * Modified login format validation to allow for the + character since emails addresses allow that as a valid character.
16
+ * Added merge_* configuration methods for acts_as_authentic to make merging options into configuration options that default to hashes. Just a few convenience methods.
17
+
1
18
  == 2.0.9 release 2009-4-9
2
19
 
3
20
  * Fixed bug where hooks provided by the password module were called when the password module was not being used due to the fact that the password field did not exist.
@@ -13,7 +30,7 @@
13
30
 
14
31
  == 2.0.6 release 2009-4-9
15
32
 
16
- * Don't use second, use [1] instead so older rails systems don't complain.
33
+ * Don't use second, use [1] instead so older rails versions don't complain.
17
34
  * Update email regular expression to be less TLD specific: (?:[A-Z]{2,4}|museum|travel)
18
35
  * Update shoulda macro for 2.0
19
36
  * validates_length_of_password_confirmation_field_options defaults to validates_confirmation_of_password_field_options
@@ -82,9 +82,11 @@ test/fixtures/companies.yml
82
82
  test/fixtures/employees.yml
83
83
  test/fixtures/projects.yml
84
84
  test/fixtures/users.yml
85
+ test/libs/affiliate.rb
85
86
  test/libs/company.rb
86
87
  test/libs/employee.rb
87
88
  test/libs/employee_session.rb
89
+ test/libs/ldaper.rb
88
90
  test/libs/ordered_hash.rb
89
91
  test/libs/project.rb
90
92
  test/libs/user.rb
@@ -2,103 +2,111 @@
2
2
 
3
3
  Authlogic is a clean, simple, and unobtrusive ruby authentication solution.
4
4
 
5
- What inspired me to create Authlogic was the messiness of the current authentication solutions. Put simply, they just didn't feel right, 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:
5
+ A code example can replace a thousand words...
6
+
7
+ Authlogic introduces a new type of model. You can have as many as you want, and name them whatever you want, just like your other models. In this example we want to authenticate with the User model, which is inferred by the name:
8
+
9
+ class UserSession < Authlogic::Session::Base
10
+ end
11
+
12
+ Log in with any of the following. Use it just like your other models:
6
13
 
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 up to date. For example: upgrading your hashing algorithm, helping you transition to a new algorithm, etc. Since all of this logic is in the Authlogic library, staying up to date is as easy as updating the library.
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.
9
- 3. Your application can stay clean, focused, and free of redundant authentication code from app to app. Meaning generators are *NOT* necessary. Not any more neccessary than any other control
10
- 4. A byproduct of #3 is that you don't have to test the same code over and over in each of your apps. You don't test the internals of ActiveRecord in each of your apps, so why would you test the internals of Authlogic? It's already been thoroughly tested for you. Focus on your application, and get rid of the noise by testing your application specific code and not generated code that you didn't write.
11
- 5. You get to write your own code, just like you do for any other model. Meaning the code you write is specific to your application, the way you want it, and more importantly you understand it.
12
- 6. You are not restricted to a single session. Think about Apple's me.com, where they need you to authenticate a second time before changing your billing information. Why not just create a second session for this? It works just like your initial session. Then your billing controller can require an "ultra secure" session.
14
+ UserSession.create(my_user_object)
15
+ UserSession.create(:login => "bjohnson", :password => "my password")
16
+ session = UserSession.new(:login => "bjohnson", :password => "my password"); session.save
17
+ UserSession.create(:openid_identifier => "identifier") # requires the authlogic-oid "add on" gem
13
18
 
14
- Authlogic can do all of this and much more, keep reading to see...
19
+ After a session has been created, you can persist it across requests. Thus keeping the user logged in:
20
+
21
+ session = UserSession.find
22
+
23
+ You can also log out / destroy the session:
24
+
25
+ session.destroy
15
26
 
16
27
  == Helpful links
17
28
 
18
29
  * <b>Documentation:</b> http://authlogic.rubyforge.org
19
- * <b>Live example with OpenID "add on" & source code:</b> http://authlogicexample.binarylogic.com
20
- * <b>Tutorial: Authlogic basic setup:</b> http://www.binarylogic.com/2008/11/3/tutorial-authlogic-basic-setup
30
+ * <b>Repository:</b> http://github.com/binarylogic/authlogic/tree/master
31
+ * <b>Live example with OpenID "add on":</b> http://authlogicexample.binarylogic.com
32
+ * <b>Live example repository with tutorial:</b> http://github.com/binarylogic/authlogic_example/tree/master
21
33
  * <b>Tutorial: Reset passwords with Authlogic the RESTful way:</b> http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic
22
- * <b>Tutorial: Easily migrate from restful_authentication:</b> http://www.binarylogic.com/2008/11/23/tutorial-easily-migrate-from-restful_authentication-to-authlogic
23
- * <b>Tutorial: Upgrade passwords easily with Authlogic:</b> http://www.binarylogic.com/2008/11/23/tutorial-upgrade-passwords-easily-with-authlogic
24
34
  * <b>Bugs / feature suggestions:</b> http://binarylogic.lighthouseapp.com/projects/18752-authlogic
25
35
  * <b>Google group:</b> http://groups.google.com/group/authlogic
26
36
 
27
- <b>Before contacting me, please read:</b>
37
+ <b>Before contacting me directly, please read:</b>
28
38
 
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.
30
-
31
- Please do not email me directly with issues regarding Authlogic. This is an inefficient way to get help because it doesn't help people who have the same problem in the future.
39
+ 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. This also benefits other people in the future with the same questions / problems. Thank you.
32
40
 
33
41
  == Authlogic "add ons"
34
42
 
35
43
  * <b>Authlogic OpenID addon:</b> http://github.com/binarylogic/authlogic_openid
36
44
  * <b>Authlogic LDAP addon:</b> http://github.com/binarylogic/authlogic_ldap
37
45
 
38
- If you create one of your own, please let me know about it so I can add it to this list.
46
+ If you create one of your own, please let me know about it so I can add it to this list. Or just fork the project, add your link, and send me a pull request.
39
47
 
40
- == Documentation
48
+ == Documentation explanation
41
49
 
42
- You can find anything you want about Authlogic in the documentation, all that you need to do is understand the basic design behind it.
50
+ You can find anything you want about Authlogic in the {documentation}, all that you need to do is understand the basic design behind it.
43
51
 
44
- That being said, Authlogic is split into 2 main parts:
52
+ That being said, there are 2 models involved during authentication. Your Authlogic model and your ActiveRecord model:
45
53
 
46
- 1. Authlogic::Session, which manages sessions.
47
- 2. Authlogic::ActsAsAuthentic, which adds in functionality to your ActiveRecord model.
54
+ 1. <b>Authlogic::Session</b>, your session models that extend Authlogic::Session::Base.
55
+ 2. <b>Authlogic::ActsAsAuthentic</b>, which adds in functionality to your ActiveRecord model when you call acts_as_authentic.
48
56
 
49
- 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.
57
+ 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.
50
58
 
51
- 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.
59
+ For example, if you want to timeout users after a certain period of inactivity, you would look in <b>Authlogic::Session::Timeout</b>. 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}[http://authlogic.rubyforge.org].
52
60
 
53
61
  === Authlogic::ActsAsAuthentic sub modules
54
62
 
55
- 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.
56
-
57
- * Authlogic::ActsAsAuthentic::Base - Provides the acts_as_authentic class method and includes all of the submodules.
58
- * Authlogic::ActsAsAuthentic::Email - Handles everything related to the email field.
59
- * Authlogic::ActsAsAuthentic::LoggedInStatus - Provides handy named scopes and methods for determining if the user is logged in or out.
60
- * Authlogic::ActsAsAuthentic::Login - Handles everything related to the login field.
61
- * Authlogic::ActsAsAuthentic::MagicColumns - Handles everything related to the "magic" fields: login_count, failed_login_count, etc.
62
- * Authlogic::ActsAsAuthentic::Password - This one is important. It handles encrypting your password, salting it, etc. It also has support for transitioning password algorithms.
63
- * Authlogic::ActsAsAuthentic::PerishableToken - Handles maintaining the perishable token field, also provides a class level method for finding record using the token.
64
- * Authlogic::ActsAsAuthentic::PersistenceToken - Handles maintaining the persistence token. This is the token stored in cookies and sessions to persist the users session.
65
- * Authlogic::ActsAsAuthentic::RestfulAuthentication - Provides configuration options to easily migrate from the restful_authentication plugin.
66
- * Authlogic::ActsAsAuthentic::SessionMaintenance - Handles automatically logging the user in. EX: a new user registers, automatically log them in.
67
- * Authlogic::ActsAsAuthentic::SingleAccessToken - Handles maintaining the single access token.
68
- * Authlogic::ActsAsAuthentic::ValidationsScope - Allows you to scope validations, etc. Just like the :scope option for validates_uniqueness_of
63
+ These modules are for the ActiveRecord side of things, the models that call acts_as_authentic.
64
+
65
+ * <b>Authlogic::ActsAsAuthentic::Base</b> - Provides the acts_as_authentic class method and includes all of the submodules.
66
+ * <b>Authlogic::ActsAsAuthentic::Email</b> - Handles everything related to the email field.
67
+ * <b>Authlogic::ActsAsAuthentic::LoggedInStatus</b> - Provides handy named scopes and methods for determining if the user is logged in or out.
68
+ * <b>Authlogic::ActsAsAuthentic::Login</b> - Handles everything related to the login field.
69
+ * <b>Authlogic::ActsAsAuthentic::MagicColumns</b> - Handles everything related to the "magic" fields: login_count, failed_login_count, etc.
70
+ * <b>Authlogic::ActsAsAuthentic::Password</b> - This one is important. It handles encrypting your password, salting it, etc. It also has support for transitioning password algorithms.
71
+ * <b>Authlogic::ActsAsAuthentic::PerishableToken</b> - Handles maintaining the perishable token field, also provides a class level method for finding record using the token.
72
+ * <b>Authlogic::ActsAsAuthentic::PersistenceToken</b> - Handles maintaining the persistence token. This is the token stored in cookies and sessions to persist the users session.
73
+ * <b>Authlogic::ActsAsAuthentic::RestfulAuthentication</b> - Provides configuration options to easily migrate from the restful_authentication plugin.
74
+ * <b>Authlogic::ActsAsAuthentic::SessionMaintenance</b> - Handles automatically logging the user in. EX: a new user registers, automatically log them in.
75
+ * <b>Authlogic::ActsAsAuthentic::SingleAccessToken</b> - Handles maintaining the single access token.
76
+ * <b>Authlogic::ActsAsAuthentic::ValidationsScope</b> - Allows you to scope validations, etc. Just like the :scope option for validates_uniqueness_of
69
77
 
70
78
  === Authlogic::Session sub modules
71
79
 
72
- 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.
73
-
74
- * Authlogic::Session::BruteForceProtection - Disables accounts after a certain number of consecutive failed logins attempted.
75
- * Authlogic::Session::Callbacks - Your tools to extend, change, or add onto Authlogic. Lets you hook in and do just about anything you want. Start here if you want to write a plugin or add on for Authlogic
76
- * Authlogic::Session::Cookies - Authentication via cookies.
77
- * Authlogic::Session::Existence - Creating, saving, and destroying objects.
78
- * Authlogic::Session::HttpAuth - Authentication via basic HTTP authentication.
79
- * Authlogic::Session::Id - Allows sessions to be separated by an id, letting you have multiple sessions for a single user.
80
- * Authlogic::Session::MagicColumns - Maintains "magic" database columns, similar to created_at and updated_at for ActiveRecord.
81
- * Authlogic::Session::MagicStates - Automatically validates based on the records states: active?, approved?, and confirmed?. If those methods exist for the record.
82
- * Authlogic::Session::Params - Authentication via params, aka single access token.
83
- * Authlogic::Session::Password - Authentication via a traditional username and password.
84
- * Authlogic::Session::Persistence - Persisting sessions / finding sessions.
85
- * Authlogic::Session::Session - Authentication via the session, the controller session that is.
86
- * Authlogic::Session::Timeout - Automatically logging out after a certain period of inactivity.
87
- * Authlogic::Session::UnauthorizedRecord - Handles authentication by passing an ActiveRecord object.
88
- * Authlogic::Session::Validation - Validation / errors.
80
+ These modules are for the models that extend Authlogic::Session::Base.
81
+
82
+ * <b>Authlogic::Session::BruteForceProtection</b> - Disables accounts after a certain number of consecutive failed logins attempted.
83
+ * <b>Authlogic::Session::Callbacks</b> - Your tools to extend, change, or add onto Authlogic. Lets you hook in and do just about anything you want. Start here if you want to write a plugin or add on for Authlogic
84
+ * <b>Authlogic::Session::Cookies</b> - Authentication via cookies.
85
+ * <b>Authlogic::Session::Existence</b> - Creating, saving, and destroying objects.
86
+ * <b>Authlogic::Session::HttpAuth</b> - Authentication via basic HTTP authentication.
87
+ * <b>Authlogic::Session::Id</b> - Allows sessions to be separated by an id, letting you have multiple sessions for a single user.
88
+ * <b>Authlogic::Session::MagicColumns</b> - Maintains "magic" database columns, similar to created_at and updated_at for ActiveRecord.
89
+ * <b>Authlogic::Session::MagicStates</b> - Automatically validates based on the records states: active?, approved?, and confirmed?. If those methods exist for the record.
90
+ * <b>Authlogic::Session::Params</b> - Authentication via params, aka single access token.
91
+ * <b>Authlogic::Session::Password</b> - Authentication via a traditional username and password.
92
+ * <b>Authlogic::Session::Persistence</b> - Persisting sessions / finding sessions.
93
+ * <b>Authlogic::Session::Session</b> - Authentication via the session, the controller session that is.
94
+ * <b>Authlogic::Session::Timeout</b> - Automatically logging out after a certain period of inactivity.
95
+ * <b>Authlogic::Session::UnauthorizedRecord</b> - Handles authentication by passing an ActiveRecord object.
96
+ * <b>Authlogic::Session::Validation</b> - Validation / errors.
89
97
 
90
98
  === Miscellaneous modules
91
99
 
92
- Miscellaneous modules that don't really belong solely to either the session or model aspect.
100
+ Miscellaneous modules that shared across the authentication process and are more "utility" modules and classes.
93
101
 
94
- * 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.
95
- * Authlogic::CryptoProviders - Contains various encryption algorithms that Authlogic uses, allowing you to choose your encryption method.
96
- * Authlogic::I18n - Acts JUST LIKE the rails I18n library, and provides internationalization to Authlogic.
97
- * Authlogic::Random - A simple class to generate random tokens.
98
- * Authlogic::TestCase - Various helper methods for testing frameworks to help you test your code.
99
- * Authlogic::Version - A handy class for determine the version of Authlogic in a number of ways.
102
+ * <b>Authlogic::AuthenticatesMany</b> - 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.
103
+ * <b>Authlogic::CryptoProviders</b> - Contains various encryption algorithms that Authlogic uses, allowing you to choose your encryption method.
104
+ * <b>Authlogic::I18n</b> - Acts JUST LIKE the rails I18n library, and provides internationalization to Authlogic.
105
+ * <b>Authlogic::Random</b> - A simple class to generate random tokens.
106
+ * <b>Authlogic::TestCase</b> - Various helper methods for testing frameworks to help you test your code.
107
+ * <b>Authlogic::Version</b> - A handy class for determine the version of Authlogic in a number of ways.
100
108
 
101
- == Quick example
109
+ == Quick Rails example
102
110
 
103
111
  What if creating sessions worked like an ORM library on the surface...
104
112
 
@@ -156,9 +164,7 @@ Or how about persisting the session...
156
164
  end
157
165
  end
158
166
 
159
- == Install and use
160
-
161
- === 1. Install the gem
167
+ == Install & Use
162
168
 
163
169
  Install the gem / plugin (recommended)
164
170
 
@@ -173,108 +179,32 @@ Or you install this as a plugin (for older versions of rails)
173
179
 
174
180
  script/plugin install git://github.com/binarylogic/authlogic.git
175
181
 
176
- === 2. Create your session
177
-
178
- Lets assume you are setting up a session for your User model.
179
-
180
- Create your user_session.rb file:
181
-
182
- $ script/generate session user_session
183
-
184
- This will create a file that looks similar to:
185
-
186
- # app/models/user_session.rb
187
- class UserSession < Authlogic::Session::Base
188
- # configuration here, see sub modules of Authlogic::Session
189
- end
190
-
191
- === 3. Ensure proper database fields
192
-
193
- 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.
194
-
195
- t.string :login, :null => false # optional, you can use email instead, or both
196
- t.string :crypted_password, :null => false # optional, see below
197
- t.string :password_salt, :null => false # optional, but highly recommended
198
- t.string :persistence_token, :null => false # required
199
- t.string :single_access_token, :null => false # optional, see Authlogic::Session::Params
200
- t.string :perishable_token, :null => false # optional, see Authlogic::Session::Perishability
201
- t.integer :login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
202
- t.integer :failed_login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
203
- t.datetime :last_request_at # optional, see Authlogic::Session::MagicColumns
204
- t.datetime :current_login_at # optional, see Authlogic::Session::MagicColumns
205
- t.datetime :last_login_at # optional, see Authlogic::Session::MagicColumns
206
- t.string :current_login_ip # optional, see Authlogic::Session::MagicColumns
207
- t.string :last_login_ip # optional, see Authlogic::Session::MagicColumns
182
+ == Detailed Setup Tutorial
208
183
 
209
- Notice the login and crypted_password fields are optional. If you prefer, you could use OpenID, LDAP, or whatever you want as your main authentication source and not even provide your own authentication system. I recommend providing your own as an option though. Your interface, such as the registration form, can dictate which method is the default. Lastly, adding 3rd party authentication methods should be as easy as installing an Authlogic "add on" gem. See "Authligic add ons" above.
184
+ See the {authlogic example}[http://github.com/binarylogic/authlogic_example/tree/master] for a detailed setup tutorial. I did this because not only do you have a tutorial to go by, but you have an example app that uses the same tutorial, so you can play around with with the code. If you have problems you can compare the code to see what you are doing differently.
210
185
 
211
- === 4. Set up your model
186
+ == Testing
212
187
 
213
- Make sure you have a model that you will be authenticating with. Since we are using the User model it should look something like:
188
+ I think one of the best aspects of Authlogic is testing. For one, it cuts out <b>a lot</b> of redundant tests in your applications because Authlogic is already thoroughly tested for you. It doesn't include a bunch of tests into your application, because it comes tested, just like any other library.
214
189
 
215
- class User < ActiveRecord::Base
216
- acts_as_authentic do |c|
217
- c.my_config_option = my_value # for available options see documentation in: Authlogic::ActsAsAuthentic
218
- end # block optional
219
- end
220
-
221
- You are all set.
222
-
223
- === 5. Next Steps
224
-
225
- 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.
226
-
227
- 1. Want to use another encryption algorithm, such as BCrypt? See Authlogic::ActsAsAuthentic::Password::Config
228
- 2. Migrating from restful_authentication? See Authlogic::ActsAsAuthentic::RestfulAuthentication::Config
229
- 3. Want to timeout sessions after a period if inactivity? See Authlogic::Session::Timeout
230
- 4. Need to scope your sessions to an account or parent model? See Authlogic::AuthenticatesMany
231
- 5. Need multiple session types in your app? Check out Authlogic::Session::Id
232
- 6. Need to reset passwords or activate accounts? Use the perishable token. See Authlogic::ActsAsAuthentic::PerishableToken
233
- 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
234
- 8. Need to internationalize your app? See Authlogic::I18n
235
- 9. Need help testing? See the Authlogic::TestCase
236
-
237
- == Interested in how it works?
238
-
239
- 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.
240
-
241
- From there it is pretty simple. When you try to create a new session the record is authenticated and then all of the session / cookie magic is done for you. The sky is the limit.
242
-
243
- == What's wrong with the current solutions?
244
-
245
- You probably don't care, but I think releasing the millionth ruby authentication solution requires a little explanation.
246
-
247
- I don't necessarily think the current solutions are "wrong", nor am I saying Authlogic is the answer to your prayers. But, to me, the current solutions were lacking something. Here's what I came up with...
190
+ For example, think about ActiveRecord. You don't test the internals of ActiveRecord, because the creators of ActiveRecord have already tested the internals for you. It wouldn't make sense for ActiveRecord to copy it's hundreds of tests into your applications. The same concept applies to Authlogic. You only need to test code you write that is specific to your application, just like everything else in your application.
248
191
 
249
- === Generators are messy
192
+ That being said, testing your code that uses Authlogic is easy. Since everyone uses different testing suites, I created a helpful module called Authlogic::TestCase, which is basically a set of tools for testing code using Authlogic. I explain testing Authlogic thoroughly in the {Authlogic::TestCase section of the documentation}[http://authlogic.rubyforge.org/classes/Authlogic/TestCase.html]. It should answer any questions you have in regards to testing Authlogic.
250
193
 
251
- Generators have their place, and it is not to add authentication to an app. It doesn't make sense. Generators are meant to be a starting point for repetitive tasks that have no sustainable pattern. Take controllers, the set up is the same thing over and over, but they eventually evolve to a point where there is no clear cut pattern. Trying to extract a pattern out into a library would be extremely hard, messy, and overly complicated. As a result, generators make sense here.
194
+ == Tell me quickly how Authlogic works
252
195
 
253
- Authentication is a one time set up process for your app. It's the same thing over and over and the pattern never really changes. The only time it changes is to conform with newer / stricter security techniques. This is exactly why generators should not be an authentication solution. Generators add code to your application, once code crosses that line, you are responsible for maintaining it. You get to make sure it stays up with the latest and greatest security techniques. And when the plugin you used releases some major update, you can't just re-run the generator, you get to sift through the code to see what changed. You don't really have a choice either, because you can't ignore security updates.
196
+ Interested in how all of this all works? Think about an ActiveRecord model. A database connection must be established before you can use it. In the case of Authlogic, a controller connection must be established before you can use it. It uses that controller connection to modify cookies, the current session, login with HTTP basic, etc. It connects to the controller through a before filter that is automatically set in your controller which lets Authlogic know about the current controller object. Then Authlogic leverages that to do everything, it's a pretty simple design.
254
197
 
255
- Using a library that hundreds of other people use has it advantages. Probably one of the biggest advantages if that you get to benefit from other people using the same code. When Bob in California figures out a new awesome security technique and adds it into Authlogic, you get to benefit from that with a single update. The catch is that this benefit is limited to code that is not "generated" or added into your app. As I said above, once code is "generated" and added into your app, it's your responsibility.
198
+ == What sets Authlogic apart and why I created it
256
199
 
257
- Lastly, there is a pattern here, why clutter up all of your applications with the same code over and over?
258
-
259
- === Security gets outdated
260
-
261
- Just as I stated in the above section, you can't stay up to date with your security since the code is generated and updating the plugin does nothing. If there is one thing you should stay up to date with, it's security. But it's not just the fact that there is no reasonable method for receiving updates. It's the fact that they tie you down to an encryption algorithm *AND* they use a bad one at that. Every single solution I've seen uses Sha1, which is joining the party with MD5. Sha1 is not as secure as it used to be. But that's the nature of algorithms, they eventually get phased out, which is fine. Everyone knows this, why not accommodate for this? Authlogic does this with the :transition_from_crypto_provider option. It takes care of transitioning all of your users to a new algorithm. Even better, it provides BCrypt as an option which should, in theory, never require you to switch since you can adjust the cost and make the encryption stronger. At the same time, still compatible with older passwords using the lower cost.
262
-
263
- === Why test the same code over and over?
264
-
265
- I've noticed my apps get cluttered with authentication tests, and they are the same exact tests! This irritates me. When you have identical tests across your apps thats a red flag that code can be extracted into a library. What's great about Authlogic is that I tested it for you. You don't write tests that test the internals of ActiveRecord do you? The same applies for Authlogic. Only test code that you've written. Essentially testing authentication is similar to testing any another RESTful controller. This makes your tests focused and easier to understand.
266
-
267
- === Limited to a single authentication
268
-
269
- I recently had an app where you could log in as a user and also log in as an employee. I won't go into the specifics of the app, but it made the most sense to do it this way. So I had two sessions in one app. None of the current solutions I found easily supported this. They all assumed a single session. One session was messy enough, adding another just put me over the edge and eventually forced me to write Authlogic. Authlogic can support 100 different sessions easily and in a clean format. Just like an app can support 100 different models and 100 different records of each model.
270
-
271
- === Too presumptuous
272
-
273
- A lot of them forced me to name my password column as "this", or the key of my cookie had to be "this". They were a little too presumptuous. I am probably overly picky, but little details like that should be configurable. This also made it very hard to implement into an existing app.
274
-
275
- === Disclaimer
200
+ What inspired me to create Authlogic was the messiness of the current authentication solutions. Put simply, they just didn't feel right, 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:
276
201
 
277
- I am not trying to "bash" any other authentication solutions. These are just my opinions, formulate your own opinion. I released Authlogic because I was "scratching my own itch". It has made my life easier and I enjoy using it, hopefully it does the same for you.
202
+ 1. <b>It's cleaner.</b> There are no generators in Authlogic. Authlogic provides a class that you can use, it's plain and simple ruby. More importantly, the code in your app is code you write, written the way you want, nice and clean. It's code that should be in your app and is specific to your app, not a redundant authentication pattern.
203
+ 2. <b>Easier to stay up-to-date.</b> To make my point, take a look at the commits to any other authentication solution, then look at the {commits for authlogic}[http://github.com/binarylogic/authlogic/commits/master]. How many commits could you easily start using if you already had an app using that solution? With an alternate solution, very few, if any. All of those cool new features and bug fixes are going to have be manually added or wait for your next application. Which is the main reason a generator is not suitable as an authentication solution. With Authlogic you can start using the latest code with a simple update of a gem. No generators, no mess.
204
+ 3. <b>It ties everything together on the domain level.</b> 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.
205
+ 4. <b>No redundant tests.</b> Because Authlogic doesn't use generators, #1 also applies to tests. Authlogic is *thoroughly* tested for you. You don't go and test the internals of ActiveRecord in each of your apps do you? So why do the same for Authlogic? Your application tests should be for application specific code. Get rid of the noise and make your tests focused and concise, no reason to copy tests from app to app.
206
+ 5. <b>You are not restricted to a single session.</b> Think about Apple's me.com, where they need you to authenticate a second time before changing your billing information. Why not just create a second session for this? It works just like your initial session. Then your billing controller can require an "ultra secure" session.
207
+ 6. <b>Easily extendable.</b> One of the distinct advantages of using a library is the ability to use it's API, assuming it has one. Authlogic has an *excellent* public API, meaning it can easily be extended and grow beyond the core library. Checkout the "add ons" list above to see what I mean.
278
208
 
279
209
 
280
- Copyright (c) 2009 {Ben Johnson of Binary Logic}(http://www.binarylogic.com), released under the MIT license
210
+ Copyright (c) 2009 {Ben Johnson of Binary Logic}[http://www.binarylogic.com], released under the MIT license
@@ -34,6 +34,10 @@ module Authlogic
34
34
 
35
35
  # A hash of options for the validates_length_of call for the email field. Allows you to change this however you want.
36
36
  #
37
+ # <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
38
+ # merge options into it. Checkout the convenience function merge_validates_length_of_email_field_options to merge
39
+ # options.</b>
40
+ #
37
41
  # * <tt>Default:</tt> {:within => 6..100}
38
42
  # * <tt>Accepts:</tt> Hash of options accepted by validates_length_of
39
43
  def validates_length_of_email_field_options(value = nil)
@@ -41,8 +45,23 @@ module Authlogic
41
45
  end
42
46
  alias_method :validates_length_of_email_field_options=, :validates_length_of_email_field_options
43
47
 
48
+ # A convenience function to merge options into the validates_length_of_email_field_options. So intead of:
49
+ #
50
+ # self.validates_length_of_email_field_options = validates_length_of_email_field_options.merge(:my_option => my_value)
51
+ #
52
+ # You can do this:
53
+ #
54
+ # merge_validates_length_of_email_field_options :my_option => my_value
55
+ def merge_validates_length_of_email_field_options(options = {})
56
+ self.validates_length_of_email_field_options = validates_length_of_email_field_options.merge(options)
57
+ end
58
+
44
59
  # A hash of options for the validates_format_of call for the email field. Allows you to change this however you want.
45
60
  #
61
+ # <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
62
+ # merge options into it. Checkout the convenience function merge_validates_format_of_email_field_options to merge
63
+ # options.</b>
64
+ #
46
65
  # * <tt>Default:</tt> {:with => email_regex, :message => I18n.t('error_messages.email_invalid', :default => "should look like an email address.")}
47
66
  # * <tt>Accepts:</tt> Hash of options accepted by validates_format_of
48
67
  def validates_format_of_email_field_options(value = nil)
@@ -50,8 +69,17 @@ module Authlogic
50
69
  end
51
70
  alias_method :validates_format_of_email_field_options=, :validates_format_of_email_field_options
52
71
 
72
+ # See merge_validates_length_of_email_field_options. The same thing except for validates_format_of_email_field_options.
73
+ def merge_validates_format_of_email_field_options(options = {})
74
+ self.validates_format_of_email_field_options = validates_format_of_email_field_options.merge(options)
75
+ end
76
+
53
77
  # A hash of options for the validates_uniqueness_of call for the email field. Allows you to change this however you want.
54
78
  #
79
+ # <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
80
+ # merge options into it. Checkout the convenience function merge_validates_uniqueness_of_email_field_options to merge
81
+ # options.</b>
82
+ #
55
83
  # * <tt>Default:</tt> {:case_sensitive => false, :scope => validations_scope, :if => "#{email_field}_changed?".to_sym}
56
84
  # * <tt>Accepts:</tt> Hash of options accepted by validates_uniqueness_of
57
85
  def validates_uniqueness_of_email_field_options(value = nil)
@@ -59,6 +87,11 @@ module Authlogic
59
87
  end
60
88
  alias_method :validates_uniqueness_of_email_field_options=, :validates_uniqueness_of_email_field_options
61
89
 
90
+ # See merge_validates_length_of_email_field_options. The same thing except for validates_uniqueness_of_email_field_options.
91
+ def merge_validates_uniqueness_of_email_field_options(options = {})
92
+ self.validates_uniqueness_of_email_field_options = validates_uniqueness_of_email_field_options.merge(options)
93
+ end
94
+
62
95
  private
63
96
  def email_regex
64
97
  return @email_regex if @email_regex
@@ -31,24 +31,52 @@ module Authlogic
31
31
 
32
32
  # A hash of options for the validates_length_of call for the login field. Allows you to change this however you want.
33
33
  #
34
- # * <tt>Default:</tt> {:within => 6..100}
34
+ # <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
35
+ # merge options into it. Checkout the convenience function merge_validates_length_of_login_field_options to merge
36
+ # options.</b>
37
+ #
38
+ # * <tt>Default:</tt> {:within => 3..100}
35
39
  # * <tt>Accepts:</tt> Hash of options accepted by validates_length_of
36
40
  def validates_length_of_login_field_options(value = nil)
37
41
  config(:validates_length_of_login_field_options, value, {:within => 3..100})
38
42
  end
39
43
  alias_method :validates_length_of_login_field_options=, :validates_length_of_login_field_options
40
44
 
45
+ # A convenience function to merge options into the validates_length_of_login_field_options. So intead of:
46
+ #
47
+ # self.validates_length_of_login_field_options = validates_length_of_login_field_options.merge(:my_option => my_value)
48
+ #
49
+ # You can do this:
50
+ #
51
+ # merge_validates_length_of_login_field_options :my_option => my_value
52
+ def merge_validates_length_of_login_field_options(options = {})
53
+ self.validates_length_of_login_field_options = validates_length_of_login_field_options.merge(options)
54
+ end
55
+
41
56
  # A hash of options for the validates_format_of call for the login field. Allows you to change this however you want.
42
57
  #
58
+ # <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
59
+ # merge options into it. Checkout the convenience function merge_validates_format_of_login_field_options to merge
60
+ # options.</b>
61
+ #
43
62
  # * <tt>Default:</tt> {:with => /\A\w[\w\.\-_@ ]+\z/, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")}
44
63
  # * <tt>Accepts:</tt> Hash of options accepted by validates_format_of
45
64
  def validates_format_of_login_field_options(value = nil)
46
- config(:validates_format_of_login_field_options, value, {:with => /\A\w[\w\.\-_@ ]+\z/, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")})
65
+ config(:validates_format_of_login_field_options, value, {:with => /\A\w[\w\.+-_@ ]+\z/, :message => I18n.t('error_messages.login_invalid', :default => "should use only letters, numbers, spaces, and .-_@ please.")})
47
66
  end
48
67
  alias_method :validates_format_of_login_field_options=, :validates_format_of_login_field_options
49
68
 
69
+ # See merge_validates_length_of_login_field_options. The same thing, except for validates_format_of_login_field_options
70
+ def merge_validates_format_of_login_field_options(options = {})
71
+ self.validates_format_of_login_field_options = validates_format_of_login_field_options.merge(options)
72
+ end
73
+
50
74
  # A hash of options for the validates_uniqueness_of call for the login field. Allows you to change this however you want.
51
75
  #
76
+ # <b>Keep in mind this is ruby. I wanted to keep this as flexible as possible, so you can completely replace the hash or
77
+ # merge options into it. Checkout the convenience function merge_validates_format_of_login_field_options to merge
78
+ # options.</b>
79
+ #
52
80
  # * <tt>Default:</tt> {:case_sensitive => false, :scope => validations_scope, :if => "#{login_field}_changed?".to_sym}
53
81
  # * <tt>Accepts:</tt> Hash of options accepted by validates_uniqueness_of
54
82
  def validates_uniqueness_of_login_field_options(value = nil)
@@ -56,6 +84,11 @@ module Authlogic
56
84
  end
57
85
  alias_method :validates_uniqueness_of_login_field_options=, :validates_uniqueness_of_login_field_options
58
86
 
87
+ # See merge_validates_length_of_login_field_options. The same thing, except for validates_uniqueness_of_login_field_options
88
+ def merge_validates_uniqueness_of_login_field_options(options = {})
89
+ self.validates_uniqueness_of_login_field_options = validates_uniqueness_of_login_field_options.merge(options)
90
+ end
91
+
59
92
  # This method allows you to find a record with the given login. If you notice, with ActiveRecord you have the
60
93
  # validates_uniqueness_of validation function. They give you a :case_sensitive option. I handle this in the same
61
94
  # manner that they handle that. If you are using the login field and set false for the :case_sensitive option in