Permalink
Browse files

support automatic reauthentication for username/password flow

  • Loading branch information...
1 parent 4cd78b4 commit 1df27f3c84c85e663bdb84c06398f81570db56a6 @dburkes dburkes committed Sep 22, 2011
@@ -326,13 +326,23 @@ def with_logging(encoded_path, optional_data = nil)
def ensure_expected_response(expected_result_class)
yield.tap do |response|
unless response.is_a?(expected_result_class || Net::HTTPSuccess)
- if response.is_a?(Net::HTTPUnauthorized) && self.refresh_token
- with_encoded_path_and_checked_response("/services/oauth2/token", { :grant_type => "refresh_token", :refresh_token => self.refresh_token, :client_id => self.client_id, :client_secret => self.client_secret}) do |encoded_path|
- response = https_request(self.host).post(encoded_path, nil)
- if response.is_a?(Net::HTTPOK)
- parse_auth_response(response.body)
+ if response.is_a?(Net::HTTPUnauthorized)
+ if self.refresh_token
+ with_encoded_path_and_checked_response("/services/oauth2/token", { :grant_type => "refresh_token", :refresh_token => self.refresh_token, :client_id => self.client_id, :client_secret => self.client_secret}) do |encoded_path|
+ response = https_request(self.host).post(encoded_path, nil)
+ if response.is_a?(Net::HTTPOK)
+ parse_auth_response(response.body)
+ end
+ response
+ end
+ elsif self.username && self.password
+ with_encoded_path_and_checked_response("/services/oauth2/token", { :grant_type => "password", :username => self.username, :password => self.password, :client_id => self.client_id, :client_secret => self.client_secret}) do |encoded_path|
+ response = https_request(self.host).post(encoded_path, nil)
+ if response.is_a?(Net::HTTPOK)
+ parse_auth_response(response.body)
+ end
+ response
end
- response
end
if response.is_a?(Net::HTTPSuccess)
@@ -0,0 +1 @@
+{"id":"https://login.salesforce.com/id/foo/bar","issued_at":"1309974610026","instance_url":"https://na1.salesforce.com","signature":"sig=","access_token":"new_access_token"}
@@ -9,6 +9,10 @@
@client.refresh_token = "refresh"
end
+ after do
+ @client.refresh_token = nil
+ end
+
context "when the refresh token flow succeeds" do
before do
response_body = File.read(File.join(File.dirname(__FILE__), "../../fixtures/refresh_success_response.json"))
@@ -40,7 +44,48 @@
end
end
- context "without a refresh token" do
+ context "with a username and password" do
+ before do
+ @client.username = "username"
+ @client.password = "password"
+ end
+
+ after do
+ @client.username = @client.password = nil
+ end
+
+ context "when reauthentication succeeds" do
+ before do
+ response_body = File.read(File.join(File.dirname(__FILE__), "../../fixtures/reauth_success_response.json"))
+ stub_request(:post, "https://bro.baz/services/oauth2/token?client_id=client_id&client_secret=client_secret&grant_type=password&username=username&password=password").to_return(:body => response_body, :status => 200)
+ end
+
+ it "stores the new access token" do
+ @client.send("http_#{request_method_name}", URI.parse(request_url).path, {})
+ @client.oauth_token.should == "new_access_token"
+ end
+
+ it "retries the request" do
+ @client.send("http_#{request_method_name}", URI.parse(request_url).path, {})
+ WebMock.should have_requested(request_method, request_url).twice
+ end
+ end
+
+ context "when reauthentication fails" do
+ before do
+ response_body = File.read(File.join(File.dirname(__FILE__), "../../fixtures/auth_error_response.json"))
+ stub_request(:post, "https://bro.baz/services/oauth2/token?client_id=client_id&client_secret=client_secret&grant_type=password&username=username&password=password").to_return(:body => response_body, :status => 400)
+ end
+
+ it "raises SalesForceError" do
+ lambda {
+ @client.send("http_#{request_method_name}", URI.parse(request_url).path, {})
+ }.should raise_error(Databasedotcom::SalesForceError)
+ end
+ end
+ end
+
+ context "without a refresh token or username/password" do
it "raises SalesForceError" do
lambda {
@client.send("http_#{request_method_name}", URI.parse(request_url).path, {})

0 comments on commit 1df27f3

Please sign in to comment.