I need simple but complete instructions for implementing the Devise authentication strategy.

I am trying to write an authentication strategy for authentication against an existing deprecated API. I do not have a database, so I cannot transfer users from some existing source. I want to do something like:

http://4trabes.com/2012/10/31/remote-authentication-with-devise/

However, after following these instructions, Devise refuses to invoke my authentication strategy. I tested this by trying to insert puts calls into my RemoteAuthenticatable modules ...

Peter.

EDIT add code as requested.

application / models / User.rb:

class User attr_accessor :id include ActiveModel::Validations #required because some before_validations are defined in devise extend ActiveModel::Callbacks #required to define callbacks extend Devise::Models define_model_callbacks :validation #required by Devise devise :remote_authenticatable end 

lib / remote_authenticatable.rb (pay attention to the insets I marked to get tracking of poor people).

 module Devise module Models module RemoteAuthenticatable extend ActiveSupport::Concern # # Here you do the request to the external webservice # # If the authentication is successful you should return # a resource instance # # If the authentication fails you should return false # def remote_authentication(authentication_hash) puts "In Devise::Models::RemoteAuthenticatable.remote_authentication()" # Your logic to authenticate with the external webservice end module ClassMethods #################################### # Overriden methods from Devise::Models::Authenticatable #################################### # # This method is called from: # Warden::SessionSerializer in devise # # It takes as many params as elements had the array # returned in serialize_into_session # # Recreates a resource from session data # def serialize_from_session(id) resource = self.new resource.id = id resource end # # Here you have to return and array with the data of your resource # that you want to serialize into the session # # You might want to include some authentication data # def serialize_into_session(record) [record.id] end end end end module Strategies class RemoteAuthenticatable < Authenticatable def valid? puts "In Devise::Strategies::RemoteAuthenticatable.valid?()" true end # # For an example check : https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb # # Method called by warden to authenticate a resource. # def authenticate! puts "In Devise::Strategies::RemoteAuthenticatable.authenticate!()" # # authentication_hash doesn't include the password # auth_params = authentication_hash auth_params[:password] = password # # mapping.to is a wrapper over the resource model # resource = mapping.to.new return fail! unless resource # remote_authentication method is defined in Devise::Models::RemoteAuthenticatable # # validate is a method defined in Devise::Strategies::Authenticatable. It takes #a block which must return a boolean value. # # If the block returns true the resource will be loged in # If the block returns false the authentication will fail! # if validate(resource){ resource.remote_authentication(auth_params) } success!(resource) end end end end end 

and the code that I added to config / initializers / devise.rb

  require 'remote_authenticatable' config.warden do |manager| manager.strategies.add(:remote, Devise::Strategies::RemoteAuthenticatable) manager.default_strategies(:scope => :user).unshift :remote end Devise.add_module :remote_authenticatable, :controller => :sessions, :route => { :session => :routes } 
+4
source share
4 answers

Can you try to remove the call

 manager.strategies.add 

And instead, add this call at the end of your strategy file

 Warden::Strategies.add(:rememberable, Devise::Strategies::RemoteAuthenticatable) 
0
source

I added the remote_authenticable.rb (model) model to app> models> problems> devise> models, and inserted the debugger operator to check it correctly

 def remote_authentication(authentication_hash) debugger false # Your logic to authenticate with the external webservice end 

the remote_authenticable.rb (strategy) in lib> devise> strategy

and added a superior block to devise.rb (initializers)

 require "devise/strategies/remote_authenticable.rb" config.warden do |manager| manager.strategies.add(:remote, Devise::Strategies::RemoteAuthenticatable) manager.default_strategies(:scope => :user).unshift :remote end 

And it works ...

0
source

in config / initializers / devise.rb you wrote this

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

now create another file in the same directory, namely "remote.rb" and add something like this

 Warden::Strategies.add(:remote) do def valid? true end def authenticate! if params[:user] user = User.find_by_login_name(params[:user][:login_name]) if user && user.account_status != "active" # add your own checks here for authentication fail halt! #after this succes no other authentication will process end else fail end end end 
0
source

Following the same tutorial, this worked for me:

Breaking remote_authenticated code in: / lib / devise / models / remote _authenticatable.rb and / lib / devise / strategies / remote _authenticatable.rb

In config / initializers / devise.rb I added

 require 'devise/orm/active_record' require "devise/strategies/remote_authenticatable" require "devise/models/remote_authenticatable" Devise.add_module :remote_authenticatable, :controller => :sessions, :route => { :session => :routes } 

and under Devise.setup:

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

All Articles