Note that there are some explanatory texts on larger screens.

plurals
  1. POI need simple but complete instructions for implementing a Devise authentication strategy
    primarykey
    data
    text
    <p>I am trying to write a devise authentication strategy to authenticate against an existing legacy API. I have no database, so I cannot migrate Users from some existing source. I want to do something like:</p> <p><a href="http://4trabes.com/2012/10/31/remote-authentication-with-devise/" rel="nofollow">http://4trabes.com/2012/10/31/remote-authentication-with-devise/</a></p> <p>However, after following those instructions, Devise refuses to call my authentication strategy. I've tested this by attempting to insert puts calls into my RemoteAuthenticatable modules...</p> <p>Peter.</p> <p>EDIT adding code as requested.</p> <p>app/models/User.rb:</p> <pre><code>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 </code></pre> <p>lib/remote_authenticatable.rb (Note the puts I've inserted to get some poor-man's tracing).</p> <pre><code>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 &lt; 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 </code></pre> <p>and the code I added to config/initializers/devise.rb</p> <pre><code> require 'remote_authenticatable' config.warden do |manager| manager.strategies.add(:remote, Devise::Strategies::RemoteAuthenticatable) manager.default_strategies(:scope =&gt; :user).unshift :remote end Devise.add_module :remote_authenticatable, :controller =&gt; :sessions, :route =&gt; { :session =&gt; :routes } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload