Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

setAuth() should fail with error on non-204 response #25

Merged
merged 1 commit into from

2 participants

@kainosnoema

For some reason, when the request initiated by setAuth() returns a non-204 response (ie. 401 due to invalid api key, etc), no error is being returned and authorized is being set to true, even though all the config items (serverUrl, setStorageUrl, cdnUrl, etc) are undefined because of the unauthorized response. This leads to unexpected behavior since you're assuming you have authorization.

Relevant lines (lib/cloudfiles/core.js:62-75):

request(authOptions, function (err, res, body) {
  if (err) {
    return callback(err); 
  }

  self.authorized = true;
  self.config.serverUrl = res.headers['x-server-management-url'];
  self.config.setStorageUrl(res.headers['x-storage-url']);
  self.config.cdnUrl = res.headers['x-cdn-management-url'];
  self.config.authToken = res.headers['x-auth-token'];
  self.config.storageToken = res.headers['x-storage-token']

  callback(null, res, self.config);
});

We should probably also be checking that the res.statusCode is equal to 200, as per the documentation for request:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})
@bmeck bmeck merged commit f5654ea into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 16, 2011
  1. @kainosnoema
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 1 deletion.
  1. +6 −0 lib/cloudfiles/core.js
  2. +2 −1  test/authentication-test.js
View
6 lib/cloudfiles/core.js
@@ -64,6 +64,12 @@ Cloudfiles.prototype.setAuth = function (callback) {
return callback(err);
}
+ var statusCode = res.statusCode.toString();
+ if (Object.keys(common.failCodes).indexOf(statusCode) !== -1) {
+ err = new Error('Rackspace Error (' + statusCode + '): ' + common.failCodes[statusCode]);
+ return callback(err, res);
+ }
+
self.authorized = true;
self.config.serverUrl = res.headers['x-server-management-url'];
self.config.setStorageUrl(res.headers['x-storage-url']);
View
3  test/authentication-test.js
@@ -46,7 +46,8 @@ vows.describe('node-cloudfiles/authentication').addBatch({
invalidClient.setAuth(this.callback);
},
- "should respond with 401": function (err, res) {
+ "should respond with 401 and return an error": function (err, res) {
+ assert.ok(err instanceof Error);
assert.equal(res.statusCode, 401);
}
},
Something went wrong with that request. Please try again.