Skip to content

Commit

Permalink
Adds strategy parameter with the strategy name in the failure end poi…
Browse files Browse the repository at this point in the history
…nt redirect callback.
  • Loading branch information
slainer68 committed Apr 10, 2012
1 parent 1032562 commit a364cf4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
9 changes: 7 additions & 2 deletions lib/omniauth/failure_endpoint.rb
Expand Up @@ -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'])}"
Expand Down
11 changes: 6 additions & 5 deletions spec/omniauth/failure_endpoint_spec.rb
Expand Up @@ -4,7 +4,7 @@
subject{ OmniAuth::FailureEndpoint }

context 'development' do
before do
before do
@rack_env = ENV['RACK_ENV']
ENV['RACK_ENV'] = 'development'
end
Expand All @@ -24,22 +24,23 @@
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
end

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
Expand Down
23 changes: 2 additions & 21 deletions 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 = {})
{
Expand Down Expand Up @@ -637,4 +618,4 @@ def make_env(path = '/auth/test', props = {})
OmniAuth.config.test_mode = false
end
end
end
end
18 changes: 18 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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

0 comments on commit a364cf4

Please sign in to comment.