From eb5047ad51eda33fa083be08b59295bc2a88864c Mon Sep 17 00:00:00 2001 From: Cyril Rohr Date: Thu, 15 Mar 2018 12:14:50 +0100 Subject: [PATCH] Make the FailureEndpoint respect the strategy's path_prefix. Previously, it just fetched the global path_prefix. --- lib/omniauth/failure_endpoint.rb | 10 +++++++++- spec/omniauth/failure_endpoint_spec.rb | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/omniauth/failure_endpoint.rb b/lib/omniauth/failure_endpoint.rb index 7c39a34..947a7fd 100644 --- a/lib/omniauth/failure_endpoint.rb +++ b/lib/omniauth/failure_endpoint.rb @@ -27,10 +27,18 @@ module OmniAuth def redirect_to_failure message_key = env['omniauth.error.type'] - new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}" + new_path = "#{env['SCRIPT_NAME']}#{strategy_path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}" Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish end + def strategy_path_prefix + if env['omniauth.error.strategy'] + env['omniauth.error.strategy'].path_prefix + else + OmniAuth.config.path_prefix + end + end + def strategy_name_query_param return '' unless env['omniauth.error.strategy'] diff --git a/spec/omniauth/failure_endpoint_spec.rb b/spec/omniauth/failure_endpoint_spec.rb index 714e029..4b75c1a 100644 --- a/spec/omniauth/failure_endpoint_spec.rb +++ b/spec/omniauth/failure_endpoint_spec.rb @@ -43,12 +43,18 @@ describe OmniAuth::FailureEndpoint do expect(head['Location']).to eq('/random/auth/failure?message=invalid_request&strategy=test') end - it 'respects the configured path prefix' do + it 'respects the globally configured path prefix' do allow(OmniAuth.config).to receive(:path_prefix).and_return('/boo') _, head, = *subject.call(env) expect(head['Location']).to eq('/boo/failure?message=invalid_request&strategy=test') end + it 'respects the custom path prefix configured on the strategy' do + env['omniauth.error.strategy'] = ExampleStrategy.new({}, path_prefix: "/some/custom/path") + _, head, = *subject.call(env) + expect(head['Location']).to eq('/some/custom/path/failure?message=invalid_request&strategy=test') + end + it 'includes the origin (escaped) if one is provided' do env['omniauth.origin'] = '/origin-example' _, head, = *subject.call(env)