Delegate omniauth_authorize_path to the router, closes #1843

This commit is contained in:
José Valim 2012-06-16 13:42:24 +02:00
parent 41a91188f5
commit b1633f2454
4 changed files with 16 additions and 25 deletions

View File

@ -1,6 +1,10 @@
class Devise::OmniauthCallbacksController < DeviseController
prepend_before_filter { request.env["devise.skip_timeout"] = true }
def passthru
render :status => 404, :text => "Not found. Authentication passthru."
end
def failure
set_flash_message :alert, :failure, :kind => failed_strategy.name.to_s.humanize, :reason => failure_message
redirect_to after_omniauth_failure_path_for(resource_name)

View File

@ -2,21 +2,6 @@ module Devise
module OmniAuth
module UrlHelpers
def self.define_helpers(mapping)
return unless mapping.omniauthable?
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
def #{mapping.name}_omniauth_authorize_path(provider, params = {})
if Devise.omniauth_configs[provider.to_sym]
script_name = request.env["SCRIPT_NAME"]
path = "\#{script_name}/#{mapping.path}/auth/\#{provider}\".squeeze("/")
path << '?' + params.to_param if params.present?
path
else
raise ArgumentError, "Could not find omniauth provider \#{provider.inspect}"
end
end
URL_HELPERS
end
def omniauth_authorize_path(resource_or_scope, *args)

View File

@ -396,8 +396,17 @@ module ActionDispatch::Routing
path_prefix = Devise.omniauth_path_prefix || "/#{mapping.path}/auth".squeeze("/")
set_omniauth_path_prefix!(path_prefix)
match "#{path_prefix}/:action/callback", :constraints => { :action => Regexp.union(mapping.to.omniauth_providers.map(&:to_s)) },
:to => controllers[:omniauth_callbacks], :as => :omniauth_callback
providers = Regexp.union(mapping.to.omniauth_providers.map(&:to_s))
match "#{path_prefix}/:provider",
:constraints => { :provider => providers },
:to => "#{controllers[:omniauth_callbacks]}#passthru",
:as => :omniauth_authorize
match "#{path_prefix}/:action/callback",
:constraints => { :action => providers },
:to => controllers[:omniauth_callbacks],
:as => :omniauth_callback
ensure
@scope[:path] = path
end

View File

@ -30,7 +30,7 @@ class OmniAuthRoutesTest < ActionController::TestCase
test 'should generate authorization path' do
assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook)
assert_raise ArgumentError do
assert_raise ActionController::RoutingError do
@controller.omniauth_authorize_path(:user, :github)
end
end
@ -48,11 +48,4 @@ class OmniAuthRoutesTest < ActionController::TestCase
assert_equal "/users/auth/openid",
@controller.omniauth_authorize_path(:user, :openid)
end
test 'should set script name in the path if present' do
@request.env['SCRIPT_NAME'] = '/q'
assert_equal "/q/users/auth/facebook",
@controller.omniauth_authorize_path(:user, :facebook)
end
end