我正在尝试编写一种设计身份验证策略来针对现有的旧 API 进行身份验证。我没有数据库,所以我无法从某些现有来源迁移用户。我想做类似的事情:
http://4trabes.com/2012/10/31/remote-authentication-with-devise/
但是,按照这些说明操作后,Devise 拒绝调用我的身份验证策略。我已经通过尝试将 puts 调用插入到我的 RemoteAuthenticatable 模块中来对此进行测试...
彼得。
根据要求编辑添加代码。
应用程序/模型/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 (注意我插入的puts以获得一些穷人的追踪)。
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
以及我添加到 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 }