ruby on rails - I need simple but complete instructions for implementing a Devise authentication strategy -
i trying write devise authentication strategy authenticate against existing legacy api. have no database, cannot migrate users existing source. want like:
http://4trabes.com/2012/10/31/remote-authentication-with-devise/
however, after next instructions, devise refuses phone call authentication strategy. i've tested attempting insert puts calls remoteauthenticatable modules...
peter.
edit adding code requested.
app/models/user.rb:
class user attr_accessor :id include activemodel::validations #required because before_validations defined in devise extend activemodel::callbacks #required define callbacks extend devise::models define_model_callbacks :validation #required devise devise :remote_authenticatable end
lib/remote_authenticatable.rb (note puts i've inserted poor-man's tracing).
module devise module models module remoteauthenticatable extend activesupport::concern # # here request external webservice # # if authentication successful should homecoming # resource instance # # if authentication fails should homecoming false # def remote_authentication(authentication_hash) puts "in devise::models::remoteauthenticatable.remote_authentication()" # logic authenticate external webservice end module classmethods #################################### # overriden methods devise::models::authenticatable #################################### # # method called from: # warden::sessionserializer in devise # # takes many params elements had array # returned in serialize_into_session # # recreates resource session info # def serialize_from_session(id) resource = self.new resource.id = id resource end # # here have homecoming , array info of resource # want serialize session # # might want include authentication info # 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 # # illustration check : https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb # # method called warden authenticate resource. # def authenticate! puts "in devise::strategies::remoteauthenticatable.authenticate!()" # # authentication_hash doesn't include password # auth_params = authentication_hash auth_params[:password] = password # # mapping.to wrapper on resource model # resource = mapping.to.new homecoming fail! unless resource # remote_authentication method defined in devise::models::remoteauthenticatable # # validate method defined in devise::strategies::authenticatable. takes #a block must homecoming boolean value. # # if block returns true resource loged in # if block returns false authentication fail! # if validate(resource){ resource.remote_authentication(auth_params) } success!(resource) end end end end end
and code added config/initializers/devise.rb
require 'remote_authenticatable' config.warden |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 }
can seek removing phone call to
manager.strategies.add
and instead adding phone call @ end of strategy file
warden::strategies.add(:rememberable, devise::strategies::remoteauthenticatable)
ruby-on-rails authentication devise
No comments:
Post a Comment