Skip to content

Commit d74b767

Browse files
allow a redirect with different host, but same path
When the response returns a redirect with a location that has a different host, but the path is still the same an error was thrown. In the case of a change of host however, we do want to continue with a new request. The site and http object have to be updated with the new host url and a new request can be made.
1 parent cb9b9db commit d74b767

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
@@ -230,7 +230,14 @@ def token_request(http_method, path, token = nil, request_options = {}, *argumen
230230
when (300..399)
231231
# this is a redirect
232232
uri = URI.parse(response['location'])
233-
response.error! if uri.path == path # careful of those infinite redirects
233+
our_uri = URI.parse(site)
234+
235+
if uri.path == path && our_uri.host != uri.host
236+
options[:site] = "#{uri.scheme}://#{uri.host}"
237+
@http = create_http
238+
end
239+
240+
response.error! if uri.path == path && our_uri.host == uri.host # careful of those infinite redirects
234241
self.token_request(http_method, uri.path, token, request_options, arguments)
235242
when (400..499)
236243
raise OAuth::Unauthorized, response

test/units/test_consumer.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ def test_token_request_follows_redirect
202202
assert_equal 'secret', hash[:oauth_token_secret]
203203
end
204204

205+
def test_follow_redirect_different_host_same_path
206+
request_uri = URI.parse("https://example.com/request_token")
207+
redirect_uri = URI.parse("https://foobar.com/request_token")
208+
209+
stub_request(:get, "http://example.com/request_token").to_return(:status => 301, :headers => {'Location' => redirect_uri.to_s})
210+
stub_request(:get, "https://foobar.com/request_token").to_return(:body => "oauth_token=token&oauth_token_secret=secret")
211+
212+
hash = @consumer.token_request(:get, request_uri.path) {{ :oauth_token => 'token', :oauth_token_secret => 'secret' }}
213+
214+
assert_equal 'token', hash[:oauth_token]
215+
assert_equal 'secret', hash[:oauth_token_secret]
216+
end
217+
205218
def test_that_can_provide_a_block_to_interpret_a_request_token_response
206219
@consumer.expects(:request).returns(create_stub_http_response)
207220

0 commit comments

Comments
 (0)