Skip to content

Commit

Permalink
Adds dynamic path evaluation options. Closes omniauth#585
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Apr 12, 2012
1 parent ab3d87c commit a682ba1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/omniauth/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/omniauth/strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand Down

0 comments on commit a682ba1

Please sign in to comment.