Skip to content

Commit

Permalink
support for named omniauth open_id strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
skojin committed May 24, 2011
1 parent acd1c76 commit 58f8c7c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/devise.rb
Expand Up @@ -364,7 +364,8 @@ def self.warden(&block)
#
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.
Expand Down
6 changes: 6 additions & 0 deletions lib/devise/omniauth/config.rb
Expand Up @@ -10,6 +10,12 @@ def initialize(provider, args)
@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
Expand Down
4 changes: 4 additions & 0 deletions test/omniauth/url_helpers_test.rb
Expand Up @@ -35,6 +35,10 @@ def assert_path(action, provider, with_param=true)
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")
Expand Down
1 change: 1 addition & 0 deletions test/rails_app/config/initializers/devise.rb
Expand Up @@ -172,6 +172,7 @@
# ==> 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
Expand Down
5 changes: 5 additions & 0 deletions test/routes_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 58f8c7c

Please sign in to comment.