thoughtbot/clearance — pull request #177 by jferris (2011-12-01 03:50:03+00:00)
Title: Perform authentication in Rack middleware

This moves most of the stuff from `Clearance::Authentication` into a `Clearance::Session` object, and delegates from the controllers to that. A piece of Rack middleware injects the session object into the Rack environment hash.

This lets you keep using the same methods in Rails controllers, but provides `env[:clearance]` for Rack middleware/apps you might be using. This is summed up pretty well in the README changes I made: https://github.com/thoughtbot/clearance/commit/f845887dd3431d31a2e201161a8e0000ff0446fa#diff-1

The major reason I want to do this is to move more things out of Rails action filters and into middleware. Things like Saucy's account filters, Kissmetrics and identity handling, and so on can all move into middleware this way. I find middleware easier to deal with in general, particularly in tests.

This also starts us down the road of making it easier to introduce alternative authentication methods, which has historically been a pain in Clearance, since we can make a new piece of middleware that injects its own session. A little refactoring of `Clearance::Session` would make that much easier.

The only downside I can find to this is that Rails functional tests don't go through the middleware stack by default. That means that, out of the box, this branch won't work with functional tests as-is. You'll need to require `clearance/testing` to make them work. We could try to automatically include the helpers if we think that's a problem.