Skip to content

Commit

Permalink
Origin is preserved in the failure endpoint. Closes omniauth#227
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Apr 5, 2011
1 parent 4ba963e commit e6d0d4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 9 additions & 9 deletions oa-core/lib/omniauth/strategy.rb
Expand Up @@ -36,15 +36,15 @@ def call!(env)
response
else
if request.params['origin']
env['rack.session']['omniauth.origin'] = request.params['origin']
@env['rack.session']['omniauth.origin'] = request.params['origin']
elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
@env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
end
request_phase
end
elsif current_path == callback_path
env['omniauth.origin'] = session.delete('omniauth.origin')
env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
@env['omniauth.origin'] = session.delete('omniauth.origin')
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''

callback_phase
else
Expand All @@ -62,9 +62,9 @@ def mock_call!(env)
response
else
if request.params['origin']
env['rack.session']['omniauth.origin'] = request.params['origin']
@env['rack.session']['omniauth.origin'] = request.params['origin']
elsif env['HTTP_REFERER'] && !env['HTTP_REFERER'].match(/#{request_path}$/)
env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
@env['rack.session']['omniauth.origin'] = env['HTTP_REFERER']
end
redirect(callback_path)
end
Expand All @@ -74,8 +74,8 @@ def mock_call!(env)
fail!(mocked_auth)
else
@env['omniauth.auth'] = mocked_auth
env['omniauth.origin'] = session.delete('omniauth.origin')
env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
@env['omniauth.origin'] = session.delete('omniauth.origin')
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
call_app!
end
else
Expand Down Expand Up @@ -192,7 +192,7 @@ def fail!(message_key, exception = nil)
self.env['omniauth.error'] = exception
self.env['omniauth.error.type'] = message_key.to_sym
self.env['omniauth.error.strategy'] = self

OmniAuth.config.on_failure.call(self.env)
end
end
Expand Down
13 changes: 12 additions & 1 deletion oa-core/spec/omniauth/strategy_spec.rb
Expand Up @@ -5,11 +5,16 @@ class ExampleStrategy
def call(env); self.call!(env) end
attr_reader :last_env
def request_phase
@last_env = env
@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
puts @fail.inspect
return @fail if @fail
raise "Callback Phase"
end
end
Expand Down Expand Up @@ -64,6 +69,12 @@ def make_env(path = '/auth/test', props = {})
strategy.last_env['rack.session']['omniauth.origin'].should == '/foo'
end

it 'should be set on the failure env' do
OmniAuth.config.should_receive(:on_failure).and_return(lambda{|env| env})
@options = {:failure => :forced_fail}
strategy.call(make_env('/auth/test/callback', 'rack.session' => {'omniauth.origin' => '/awesome'}))
end

context "with script_name" do
it 'should be set on the request phase, containing full path' do
env = {'HTTP_REFERER' => 'http://example.com/sub_uri/origin', 'SCRIPT_NAME' => '/sub_uri' }
Expand Down

0 comments on commit e6d0d4e

Please sign in to comment.