From 58f8c7c613ed07eae7c5391f6105a4aec7f85d70 Mon Sep 17 00:00:00 2001 From: Sergey Kojin Date: Tue, 24 May 2011 23:59:36 +0400 Subject: [PATCH] support for named omniauth open_id strategies --- lib/devise.rb | 3 ++- lib/devise/omniauth/config.rb | 6 ++++++ test/omniauth/url_helpers_test.rb | 4 ++++ test/rails_app/config/initializers/devise.rb | 1 + test/routes_test.rb | 5 +++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/devise.rb b/lib/devise.rb index 688bf0c4..f5d9bcfb 100644 --- a/lib/devise.rb +++ b/lib/devise.rb @@ -364,7 +364,8 @@ module Devise # def self.omniauth(provider, *args) @@helpers << Devise::OmniAuth::UrlHelpers - @@omniauth_configs[provider] = Devise::OmniAuth::Config.new(provider, args) + config = Devise::OmniAuth::Config.new(provider, args) + @@omniauth_configs[config.strategy_name.to_sym] = config end # Include helpers in the given scope to AC and AV. diff --git a/lib/devise/omniauth/config.rb b/lib/devise/omniauth/config.rb index b1d50f63..aece1c03 100644 --- a/lib/devise/omniauth/config.rb +++ b/lib/devise/omniauth/config.rb @@ -10,6 +10,12 @@ module Devise @strategy = nil end + # open_id strategy can have configurable name + def strategy_name + options = @args.last.is_a?(Hash) && @args.last + options && options[:name] ? options[:name] : @provider + end + def strategy_class ::OmniAuth::Strategies.const_get("#{::OmniAuth::Utils.camelize(@provider.to_s)}") end diff --git a/test/omniauth/url_helpers_test.rb b/test/omniauth/url_helpers_test.rb index 88894696..a9c6a966 100644 --- a/test/omniauth/url_helpers_test.rb +++ b/test/omniauth/url_helpers_test.rb @@ -35,6 +35,10 @@ class OmniAuthRoutesTest < ActionController::TestCase end end + test 'should generate authorization path for named open_id omniauth' do + assert_match "/users/auth/google", @controller.omniauth_authorize_path(:user, :google) + end + test 'should generate authorization path with params' do assert_match "/users/auth/open_id?openid_url=http%3A%2F%2Fyahoo.com", @controller.omniauth_authorize_path(:user, :open_id, :openid_url => "http://yahoo.com") diff --git a/test/rails_app/config/initializers/devise.rb b/test/rails_app/config/initializers/devise.rb index be501f3a..9473db6a 100644 --- a/test/rails_app/config/initializers/devise.rb +++ b/test/rails_app/config/initializers/devise.rb @@ -172,6 +172,7 @@ Devise.setup do |config| # ==> OmniAuth config.omniauth :facebook, 'APP_ID', 'APP_SECRET', :scope => 'email,offline_access' config.omniauth :open_id + config.omniauth :open_id, :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id' # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/test/routes_test.rb b/test/routes_test.rb index a0bfd0d1..c02a4e70 100644 --- a/test/routes_test.rb +++ b/test/routes_test.rb @@ -96,6 +96,11 @@ class DefaultRoutingTest < ActionController::TestCase assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'facebook'}, {:path => 'users/auth/facebook/callback', :method => :post}) assert_named_route "/users/auth/facebook/callback", :user_omniauth_callback_path, :facebook + # named open_id + assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'google'}, {:path => 'users/auth/google/callback', :method => :get}) + assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'google'}, {:path => 'users/auth/google/callback', :method => :post}) + assert_named_route "/users/auth/google/callback", :user_omniauth_callback_path, :google + assert_raise ActionController::RoutingError do assert_recognizes({:controller => 'ysers/omniauth_callbacks', :action => 'twitter'}, {:path => 'users/auth/twitter/callback', :method => :get}) end