From a364cf4bea31a5c76d9a1b400a56ee5e7a241202 Mon Sep 17 00:00:00 2001 From: slainer68 Date: Tue, 10 Apr 2012 20:42:02 +0200 Subject: [PATCH] Adds strategy parameter with the strategy name in the failure end point redirect callback. --- lib/omniauth/failure_endpoint.rb | 9 +++++++-- spec/omniauth/failure_endpoint_spec.rb | 11 ++++++----- spec/omniauth/strategy_spec.rb | 23 ++--------------------- spec/spec_helper.rb | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/lib/omniauth/failure_endpoint.rb b/lib/omniauth/failure_endpoint.rb index b6ee176cd..15f5aedd7 100644 --- a/lib/omniauth/failure_endpoint.rb +++ b/lib/omniauth/failure_endpoint.rb @@ -22,15 +22,20 @@ def call end def raise_out! - raise env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type']) + raise env['omniauth.error'] || OmniAuth::Error.new(env['omniauth.error.type']) end 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}" + new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.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_name_query_param + return "" unless env['omniauth.error.strategy'] + "&strategy=#{env['omniauth.error.strategy'].name}" + end + def origin_query_param return "" unless env['omniauth.origin'] "&origin=#{CGI.escape(env['omniauth.origin'])}" diff --git a/spec/omniauth/failure_endpoint_spec.rb b/spec/omniauth/failure_endpoint_spec.rb index dc25e60e3..44307e00d 100644 --- a/spec/omniauth/failure_endpoint_spec.rb +++ b/spec/omniauth/failure_endpoint_spec.rb @@ -4,7 +4,7 @@ subject{ OmniAuth::FailureEndpoint } context 'development' do - before do + before do @rack_env = ENV['RACK_ENV'] ENV['RACK_ENV'] = 'development' end @@ -24,8 +24,9 @@ end context 'non-development' do - let(:env){ {'omniauth.error.type' => 'invalid_request'} } - + let(:env){ {'omniauth.error.type' => 'invalid_request', + 'omniauth.error.strategy' => ExampleStrategy.new({}) } } + it 'should be a redirect' do status, head, body = *subject.call(env) status.should == 302 @@ -33,13 +34,13 @@ it 'should include the SCRIPT_NAME' do status, head, body = *subject.call(env.merge('SCRIPT_NAME' => '/random')) - head['Location'].should == '/random/auth/failure?message=invalid_request' + head['Location'].should == '/random/auth/failure?message=invalid_request&strategy=test' end it 'should respect configured path prefix' do OmniAuth.config.stub(:path_prefix => '/boo') status, head, body = *subject.call(env) - head["Location"].should == '/boo/failure?message=invalid_request' + head["Location"].should == '/boo/failure?message=invalid_request&strategy=test' end it 'should include the origin (escaped) if one is provided' do diff --git a/spec/omniauth/strategy_spec.rb b/spec/omniauth/strategy_spec.rb index 41e71fa99..2d3a740dc 100644 --- a/spec/omniauth/strategy_spec.rb +++ b/spec/omniauth/strategy_spec.rb @@ -1,23 +1,4 @@ -require File.expand_path('../../spec_helper', __FILE__) - -class ExampleStrategy - include OmniAuth::Strategy - option :name, 'test' - def call(env); self.call!(env) end - attr_reader :last_env - def request_phase - @fail = fail!(options[:failure]) if options[:failure] - @last_env = env - return @fail if @fail - raise "Request Phase" - end - def callback_phase - @fail = fail!(options[:failure]) if options[:failure] - @last_env = env - return @fail if @fail - raise "Callback Phase" - end -end +require 'spec_helper' def make_env(path = '/auth/test', props = {}) { @@ -637,4 +618,4 @@ def make_env(path = '/auth/test', props = {}) OmniAuth.config.test_mode = false end end -end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fa2689f84..c29b0759e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,3 +13,21 @@ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy end +class ExampleStrategy + include OmniAuth::Strategy + option :name, 'test' + def call(env); self.call!(env) end + attr_reader :last_env + def request_phase + @fail = fail!(options[:failure]) if options[:failure] + @last_env = env + return @fail if @fail + raise "Request Phase" + end + def callback_phase + @fail = fail!(options[:failure]) if options[:failure] + @last_env = env + return @fail if @fail + raise "Callback Phase" + end +end \ No newline at end of file