diff --git a/lib/omniauth/strategy.rb b/lib/omniauth/strategy.rb index cd565da..7686543 100644 --- a/lib/omniauth/strategy.rb +++ b/lib/omniauth/strategy.rb @@ -226,11 +226,19 @@ module OmniAuth end def on_request_path? - on_path?(request_path) + if options.request_path.respond_to?(:call) + options.request_path.call(env) + else + on_path?(request_path) + end end def on_callback_path? - on_path?(callback_path) + if options.callback_path.respond_to?(:call) + options.callback_path.call(env) + else + on_path?(callback_path) + end end def on_path?(path) @@ -350,12 +358,22 @@ module OmniAuth options[:path_prefix] || OmniAuth.config.path_prefix end + def custom_path(kind) + if options[kind].respond_to?(:call) + result = options[kind].call(env) + return nil unless result.is_a?(String) + result + else + options[kind] + end + end + def request_path - options[:request_path] || "#{path_prefix}/#{name}" + options[:request_path].is_a?(String) ? options[:request_path] : "#{path_prefix}/#{name}" end def callback_path - options[:callback_path] || "#{path_prefix}/#{name}/callback" + options[:callback_path].is_a?(String) ? options[:callback_path] : (custom_path(:request_path) || "#{path_prefix}/#{name}/callback") end def setup_path diff --git a/spec/omniauth/strategy_spec.rb b/spec/omniauth/strategy_spec.rb index 2d3a740..a1b0b53 100644 --- a/spec/omniauth/strategy_spec.rb +++ b/spec/omniauth/strategy_spec.rb @@ -346,6 +346,23 @@ describe OmniAuth::Strategy do end end + context 'dynamic paths' do + it 'should run the request phase if the custom request path evaluator is truthy' do + @options = {:request_path => lambda{|env| true}} + lambda{ strategy.call(make_env('/asoufibasfi')) }.should raise_error("Request Phase") + end + + it 'should run the callback phase if the custom callback path evaluator is truthy' do + @options = {:callback_path => lambda{|env| true}} + lambda{ strategy.call(make_env('/asoufiasod')) }.should raise_error("Callback Phase") + end + + it 'should provide a custom callback path if request_path evals to a string' do + strategy_instance = fresh_strategy.new(nil, :request_path => lambda{|env| "/auth/boo/callback/22" }) + strategy_instance.callback_path.should == '/auth/boo/callback/22' + end + end + context 'custom paths' do it 'should use a custom request_path if one is provided' do @options = {:request_path => '/awesome'}