既存のレガシー API に対して認証するための認証戦略を作成しようとしています。データベースがないため、既存のソースからユーザーを移行できません。私は次のようなことをしたい:
http://4trabes.com/2012/10/31/remote-authentication-with-devise/
しかし、これらの指示に従った後、Devise は私の認証戦略を呼び出すことを拒否しました。私は RemoteAuthenticatable モジュールに puts 呼び出しを挿入しようとしてこれをテストしました...
ピーター。
要求に応じてコードを追加して編集します。
アプリ/モデル/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 }