Question

I am trying to setup 2 authentification strategies using Devise the standard one ( :database_authenticable ) and if this one fails another try is performed against another remote server :remote ( as described http://4trabes.com/2012/10/31/remote-authentication-with-devise/ )

if I add in my devise.rb initializer the config.warden block

config.warden do |manager|
  manager.strategies.add(:remote, Devise::Strategies::RemoteAuthenticatable)
  manager.default_strategies(:scope => :user).unshift :remote
end

then :remote becomes the default strategy, but this not what I am looking for .. I would like to have first Devise::Strategies::DatabaseAuthenticatable then if it fails Devise::Strategies::RemoteAuthenticatable

I know it's possible as warden accepts cascading strategies.... as described at https://github.com/hassox/warden/wiki/Strategies Using strategies..

but I don't see how to implement it ...

Was it helpful?

Solution 3

the warden block in devise initializer defines an array of strategies in warden config,

require 'devise/strategies/remote_authenticable'

config.warden do |manager|
 manager.strategies.add(:remote, Devise::Strategies::RemoteAuthenticatable)
 manager.default_strategies(:scope => :user).unshift :remote
end

however the :remote strategy is listed as first...

    # warden.config[:default_strategies][:user] =>  [:remote, :rememberable, :database_authenticatable]

so I modified the #createaction in my users/sessions_controller, to move :remote to the end of the list

# POST /resource/sign_in
def create
    warden.config[:default_strategies][:user].push(warden.config[:default_strategies][:user].shift)
    self.resource = warden.authenticate!(auth_options)

now, user authentication starts wit Devise ( :rememberable, :database_authenticable) and if it fails , then authentication on the remote server is tried with :remote_authenticable

OTHER TIPS

While I don't have specific technical advice here, I can report that in January of 2015 I spent about 2 days trying to get the 4trabes advice (cited above and elsewhere on stack overflow) working to absolute complete and crushing failure. Its unclear to me that remote authentication in Devise actually works fully. I was able to

To use a strategy, refer to it via the label that it was given when it was declared. eg.

env['warden'].authenticate(:password)

To use the :password strategy. You can use multiple strategies, and each one will be tried in order until either one is halted or none have been.

env['warden'].authenticate(:password, :basic)

This will use the :password, and failing that, the :basic strategies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top