From 4b6122a649d4911fc9b6c6184e390d77857e12b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Lign=C3=A9?= Date: Mon, 22 Aug 2016 15:12:10 +0200 Subject: [PATCH] Override the `callback_path` method --- lib/omniauth/strategies/twitter.rb | 12 ++++++- spec/omniauth/strategies/twitter_spec.rb | 41 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/omniauth/strategies/twitter.rb b/lib/omniauth/strategies/twitter.rb index 6f83a8d0..3d890b61 100644 --- a/lib/omniauth/strategies/twitter.rb +++ b/lib/omniauth/strategies/twitter.rb @@ -62,7 +62,17 @@ def request_phase end def callback_url - request.params['callback_url'] || super + request.params['callback_url'] + end + + def callback_path + params = session['omniauth.params'] + + if params.nil? || params['callback_url'].nil? + super + else + URI(params['callback_url']).path + end end private diff --git a/spec/omniauth/strategies/twitter_spec.rb b/spec/omniauth/strategies/twitter_spec.rb index 3cb0e92b..08d726f7 100644 --- a/spec/omniauth/strategies/twitter_spec.rb +++ b/spec/omniauth/strategies/twitter_spec.rb @@ -130,6 +130,47 @@ end end + context 'with a specified callback_url in the params' do + before do + params = { 'callback_url' => 'http://foo.dev/auth/twitter/foobar' } + allow(subject).to receive(:request) do + double('Request', :params => params) + end + allow(subject).to receive(:session) do + double('Session', :[] => { 'callback_url' => params['callback_url'] }) + end + allow(subject).to receive(:old_request_phase) { :whatever } + end + + it 'should use the callback_url' do + expect(subject.callback_url).to eq 'http://foo.dev/auth/twitter/foobar' + end + + it 'should return the correct callback_path' do + expect(subject.callback_path).to eq '/auth/twitter/foobar' + end + end + + context 'with no callback_url set' do + before do + allow(subject).to receive(:request) do + double('Request', :params => {}) + end + allow(subject).to receive(:session) do + double('Session', :[] => {}) + end + allow(subject).to receive(:old_request_phase) { :whatever } + end + + it 'callback_url should return nil' do + expect(subject.callback_url).to be_nil + end + + it 'should return the default callback_path value' do + expect(subject.callback_path).to eq '/auth/twitter/callback' + end + end + context "with no request params set and force_login specified" do before do allow(subject).to receive(:request) do