From 8685272feb6907f13492aa1b58ad71ef64d0eb7e Mon Sep 17 00:00:00 2001 From: Ruben Fonseca Date: Tue, 30 Nov 2010 14:26:39 +0000 Subject: [PATCH] Added support to :request_keys, like the :database_authenticatable strategy This allows us to use this module to authenticate passing a scope (ex: a subdomain) to the model --- lib/devise_rpx_connectable/model.rb | 17 +++++++++++++---- lib/devise_rpx_connectable/strategy.rb | 9 +++++++-- spec/devise_rpx_connectable_spec.rb | 6 +++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/devise_rpx_connectable/model.rb b/lib/devise_rpx_connectable/model.rb index fcc656b..d8943bf 100755 --- a/lib/devise_rpx_connectable/model.rb +++ b/lib/devise_rpx_connectable/model.rb @@ -41,6 +41,11 @@ def store_rpx_credentials!(attributes = {}) # Only populate +email+ field if it's available (e.g. if +authenticable+ module is used). self.email = attributes[:email] || '' if self.respond_to?(:email) + # Populate optional request fields + attributes[:request_keys].each do |k, v| + self.send(:"#{k}=", v) + end + # Lazy hack: These database fields are required if +authenticable+/+confirmable+ # module(s) is used. Could be avoided with :null => true for authenticatable # migration, but keeping this to avoid unnecessary problems. @@ -122,7 +127,8 @@ module ClassMethods :rpx_identifier_field, :rpx_auto_create_account, :rpx_extended_user_data, - :rpx_additional_user_data + :rpx_additional_user_data, + :request_keys ) # Alias don't work for some reason, so...a more Ruby-ish alias @@ -136,7 +142,7 @@ def rpx_auto_create_account? # def authenticate_with_rpx(attributes = {}) if attributes[:identifier].present? - self.find_for_rpx(attributes[:identifier]) + self.find_for_rpx(attributes[:identifier], attributes[:request_keys]) end end @@ -146,8 +152,11 @@ def authenticate_with_rpx(attributes = {}) # Overwrite to add customized conditions, create a join, or maybe use a # namedscope to filter records while authenticating. # - def find_for_rpx(identifier) - self.first(:conditions => { rpx_identifier_field => identifier }) + def find_for_rpx(identifier, request_keys) + conditions = { rpx_identifier_field => identifier } + conditions.merge! request_keys + + self.first(:conditions => conditions) end # Contains the logic used in authentication. Overwritten by other devise modules. diff --git a/lib/devise_rpx_connectable/strategy.rb b/lib/devise_rpx_connectable/strategy.rb index f5f9da3..d7bde91 100755 --- a/lib/devise_rpx_connectable/strategy.rb +++ b/lib/devise_rpx_connectable/strategy.rb @@ -19,10 +19,15 @@ def authenticate! klass = mapping.to raise StandardError, "RPXNow API key is not defined, please see the documentation of RPXNow gem to setup it." unless RPXNow.api_key.present? begin + request_keys = klass.request_keys.inject({}) do |res, k| + res[k] = params[k] + res + end rescue {} + rpx_user = (RPXNow.user_data(params[:token], :extended => klass.rpx_extended_user_data, :additional => klass.rpx_additional_user_data) rescue nil) fail!(:rpx_invalid) and return unless rpx_user - if user = klass.authenticate_with_rpx(:identifier => rpx_user["identifier"]) + if user = klass.authenticate_with_rpx(:identifier => rpx_user["identifier"], :request_keys => request_keys ) user.on_before_rpx_success(rpx_user) success!(user) return @@ -31,7 +36,7 @@ def authenticate! fail!(:rpx_invalid) and return unless klass.rpx_auto_create_account? user = klass.new - user.store_rpx_credentials!(rpx_user) + user.store_rpx_credentials!(rpx_user.merge(:request_keys => request_keys)) user.on_before_rpx_auto_create(rpx_user) user.save(:validate => false) diff --git a/spec/devise_rpx_connectable_spec.rb b/spec/devise_rpx_connectable_spec.rb index 51d9b91..36eee4f 100644 --- a/spec/devise_rpx_connectable_spec.rb +++ b/spec/devise_rpx_connectable_spec.rb @@ -13,7 +13,7 @@ class RPXNow cattr_accessor :api_key end -RPX_USER_DATA = { "identifier" => "superpipo_user" } +RPX_USER_DATA = { "identifier" => "superpipo_user", :request_keys => {}} PARAMS = { :token => "rpx_token" } describe 'DeviseRpxConnectable' do @@ -41,7 +41,7 @@ class RPXNow end it "should authenticate if a user exists in database" do - User.should_receive(:authenticate_with_rpx).with({ :identifier => RPX_USER_DATA["identifier"] }).and_return(@user) + User.should_receive(:authenticate_with_rpx).with({ :identifier => RPX_USER_DATA["identifier"], :request_keys => {} }).and_return(@user) @user.should_receive(:on_before_rpx_success).with(RPX_USER_DATA).and_return(true) @@ -52,7 +52,7 @@ class RPXNow describe 'when no user exists in database' do before(:each) do - User.should_receive(:authenticate_with_rpx).with({ :identifier => RPX_USER_DATA["identifier"] }).and_return(nil) + User.should_receive(:authenticate_with_rpx).with({ :identifier => RPX_USER_DATA["identifier"], :request_keys => {} }).and_return(nil) end it "should fail unless rpx_auto_create_account" do