hassox/warden — pull request #67 by fphilipe (2013-03-15 09:36:18+00:00)
Title: Add support for multiple managers in middleware

I'm working on a gem for rails ([warden-github-rails](/fphilipe/warden-github-rails)) that inserts a warden middleware. Unfortunately, when the rails app already has a warden middleware (e.g. when using devise), the middleware downstream is ignored.

I'd like to propose and discuss a possibility to make multiple warden managers work.

Inside [`Warden::Manager#call`](https://github.com/hassox/warden/blob/master/lib/warden/manager.rb#L30) there is the check for an upstream manager:

```ruby
return @app.call(env) if env['warden'] && env['warden'].manager != self
```

I think an easy approach would be some kind of configuration merging such as:

```ruby
if env['warden'] && env['warden'].manager != self
env['warden'].config.deep_merge!(config)
return @app.call(env)
end
```

Would you guys think this would make sense at all?

Obviously the example above is a naïve approach. It might make sense to only copy over certain configs. One downside of simply merging is that it is not possible to have conditional assignments à la:

```ruby
# Upstream manager
config.failure_app = BadAuth

# Downstream manager
config.failure_app ||= lambda { |env| [401, {}, ['unauthorized']] }
```

Another thought I had was to allow all configs to be scoped, similarly to specifying `:strategies` per scope. This would allow a warden "plugin" to have a smaller footprint.

Any thoughts?

***

PS: See the [initial discussion](https://groups.google.com/forum/?fromgroups=#!topic/plataformatec-devise/ubEiaFSJwNc) with @josevalim at the devise email list.