Permalink
Browse files

Adds dynamic path evaluation options. Closes #585

  • Loading branch information...
1 parent ab3d87c commit a682ba1f6d181146527b3897b594d53bce1a749e Michael Bleigh committed Apr 12, 2012
Showing with 39 additions and 4 deletions.
  1. +22 −4 lib/omniauth/strategy.rb
  2. +17 −0 spec/omniauth/strategy_spec.rb
View
@@ -226,11 +226,19 @@ def on_auth_path?
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 @@ def path_prefix
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
@@ -346,6 +346,23 @@ def make_env(path = '/auth/test', props = {})
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'}

0 comments on commit a682ba1

Please sign in to comment.