Skip to content

Commit

Permalink
refactor aws.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkokiefer committed Nov 8, 2013
1 parent 81e72be commit 4d637ed
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions lib/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ exports.createMetaDataClient = metadata.init();

// a generic AWS API Client which handles the general parts
function genericAWSClient(obj) {
if (obj.secure == null) obj.secure = true;
var token = obj.token;
var signHeader = obj.signHeader;
var host = obj.host;
var accessKeyId = obj.accessKeyId;
var path = obj.path;
var agent = obj.agent;
var secretAccessKey = obj.secretAccessKey;
var secure = obj.secure == null ? true : false;
var connection = secure ? https : http;

obj.connection = obj.secure ? https : http;
obj.call = function (action, query, callback) {
return {call: call};

function call(action, query, callback) {
// Wrap the callback to prevent it from being called multiple times.
callback = (function(next) {
var isCalled = false;
Expand All @@ -53,42 +62,42 @@ function genericAWSClient(obj) {
}
})(callback)
// Try to set credentials with metadata API if no credentials provided
metadata.readCredentials(obj, function(err, obj) {
metadata.readCredentials(obj, function(err) {
if (err) throw err;
var now = new Date();
if (!obj.signHeader) {
if (!signHeader) {
// Add the standard parameters required by all AWS APIs
if (obj.token !== undefined) query["SecurityToken"] = obj.token;
if (token !== undefined) query["SecurityToken"] = token;
query["Timestamp"] = now.toISOString();
query["AWSAccessKeyId"] = obj.accessKeyId;
query["Signature"] = obj.sign(query);
query["AWSAccessKeyId"] = accessKeyId;
query["Signature"] = sign(query);
}

var body = qs.stringify(query);
var headers = {
"Host": obj.host,
"Host": host,
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-Length": body.length
};

if (obj.signHeader) {
if (signHeader) {
headers["Date"] = now.toUTCString();
if (obj.token !== undefined) headers["x-amz-security-token"] = obj.token;
if (token !== undefined) headers["x-amz-security-token"] = token;
headers["x-amzn-authorization"] =
"AWS3-HTTPS " +
"AWSAccessKeyId=" + obj.accessKeyId + ", " +
"AWSAccessKeyId=" + accessKeyId + ", " +
"Algorithm=HmacSHA256, " +
"Signature=" + utils.hmacSha256(obj.secretAccessKey, now.toUTCString());
"Signature=" + utils.hmacSha256(secretAccessKey, now.toUTCString());
}

var options = {
host: obj.host,
path: obj.path,
agent: obj.agent,
host: host,
path: path,
agent: agent,
method: 'POST',
headers: headers
};
var req = obj.connection.request(options, function (res) {
var req = connection.request(options, function (res) {
var data = '';
//the listener that handles the response chunks
res.addListener('data', function (chunk) {
Expand Down Expand Up @@ -122,7 +131,7 @@ function genericAWSClient(obj) {
/*
Calculate HMAC signature of the query
*/
obj.sign = function (query) {
function sign(query) {
var keys = []
var sorted = {}

Expand All @@ -135,7 +144,7 @@ function genericAWSClient(obj) {
var key = keys[n]
sorted[key] = query[key]
}
var stringToSign = ["POST", obj.host, obj.path, qs.stringify(sorted)].join("\n");
var stringToSign = ["POST", host, path, qs.stringify(sorted)].join("\n");

// Amazon signature algorithm seems to require this
stringToSign = stringToSign.replace(/!/g,"%21");
Expand All @@ -144,7 +153,6 @@ function genericAWSClient(obj) {
stringToSign = stringToSign.replace(/\(/g,"%28");
stringToSign = stringToSign.replace(/\)/g,"%29");

return utils.hmacSha256(obj.secretAccessKey, stringToSign);
return utils.hmacSha256(secretAccessKey, stringToSign);
}
return obj;
}

0 comments on commit 4d637ed

Please sign in to comment.