diff --git a/lib/omniauth/failure_endpoint.rb b/lib/omniauth/failure_endpoint.rb index 3d4e300..84de91e 100644 --- a/lib/omniauth/failure_endpoint.rb +++ b/lib/omniauth/failure_endpoint.rb @@ -27,10 +27,19 @@ module OmniAuth def redirect_to_failure message_key = env['omniauth.error.type'] - new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{Rack::Utils.escape(message_key)}#{origin_query_param}#{strategy_name_query_param}" + + new_path = "#{env['SCRIPT_NAME']}#{strategy_path_prefix}/failure?message=#{Rack::Utils.escape(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 39445e4..1c44884 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)