Skip to content

Commit 9919f7f

Browse files
authored
Merge pull request #153 from moneybird/master
allow a redirect with different host, but same path
2 parents 952cea5 + d74b767 commit 9919f7f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/oauth/consumer.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,14 @@ def token_request(http_method, path, token = nil, request_options = {}, *argumen
241241
when (300..399)
242242
# this is a redirect
243243
uri = URI.parse(response['location'])
244-
response.error! if uri.path == path # careful of those infinite redirects
244+
our_uri = URI.parse(site)
245+
246+
if uri.path == path && our_uri.host != uri.host
247+
options[:site] = "#{uri.scheme}://#{uri.host}"
248+
@http = create_http
249+
end
250+
251+
response.error! if uri.path == path && our_uri.host == uri.host # careful of those infinite redirects
245252
self.token_request(http_method, uri.path, token, request_options, arguments)
246253
when (400..499)
247254
raise OAuth::Unauthorized, response

test/units/test_consumer.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,19 @@ def test_token_request_follows_redirect
250250
assert_equal 'secret', hash[:oauth_token_secret]
251251
end
252252

253+
def test_follow_redirect_different_host_same_path
254+
request_uri = URI.parse("https://example.com/request_token")
255+
redirect_uri = URI.parse("https://foobar.com/request_token")
256+
257+
stub_request(:get, "http://example.com/request_token").to_return(:status => 301, :headers => {'Location' => redirect_uri.to_s})
258+
stub_request(:get, "https://foobar.com/request_token").to_return(:body => "oauth_token=token&oauth_token_secret=secret")
259+
260+
hash = @consumer.token_request(:get, request_uri.path) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
261+
262+
assert_equal 'token', hash[:oauth_token]
263+
assert_equal 'secret', hash[:oauth_token_secret]
264+
end
265+
253266
def test_that_can_provide_a_block_to_interpret_a_request_token_response
254267
@consumer.expects(:request).returns(create_stub_http_response)
255268

0 commit comments

Comments
 (0)