Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS S3 Signature v4 #273

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"node": true,
"laxcomma": true
}
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ _./test/auth.json_, which contains your credentials as JSON, for example:
"secret": "<secret-here>",
"bucket": "<your-bucket-name>",
"bucket2": "<another-bucket-name>",
"bucketUsWest2": "<bucket-in-us-west-2-region-here>"
"bucketUsWest2": "<bucket-in-us-west-2-region-here>",
"bucketEuCentral1": "<bucket-in-eu-central-1-region-here>"
}
```

Expand Down
107 changes: 4 additions & 103 deletions lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,15 @@
*/

var crypto = require('crypto')
, parse = require('url').parse;
, parse = require('url').parse
, aws4 = require('aws4');

/**
* Query string params permitted in the canonicalized resource.
* @see http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheCanonicalizedResourceElement
*/

var whitelist = [
'acl'
, 'delete'
, 'lifecycle'
, 'location'
, 'logging'
, 'notification'
, 'partNumber'
, 'policy'
, 'requestPayment'
, 'torrent'
, 'uploadId'
, 'uploads'
, 'versionId'
, 'versioning'
, 'versions'
, 'website'
];

/**
* Return an "Authorization" header value with the given `options`
* in the form of "AWS <key>:<signature>"
*
* @param {Object} options
* @return {String}
* @api private
*/

exports.authorization = function(options){
return 'AWS ' + options.key + ':' + exports.sign(options);
};
exports.sign = aws4.sign;

/**
* Simple HMAC-SHA1 Wrapper
Expand All @@ -62,19 +33,6 @@ exports.hmacSha1 = function(options){
return crypto.createHmac('sha1', options.secret).update(new Buffer(options.message, 'utf-8')).digest('base64');
};

/**
* Create a base64 sha1 HMAC for `options`.
*
* @param {Object} options
* @return {String}
* @api private
*/

exports.sign = function(options){
options.message = exports.stringToSign(options);
return exports.hmacSha1(options);
};

/**
* Create a base64 sha1 HMAC for `options`.
*
Expand All @@ -90,35 +48,6 @@ exports.signQuery = function(options){
return exports.hmacSha1(options);
};

/**
* Return a string for sign() with the given `options`.
*
* Spec:
*
* <verb>\n
* <md5>\n
* <content-type>\n
* <date>\n
* [headers\n]
* <resource>
*
* @param {Object} options
* @return {String}
* @api private
*/

exports.stringToSign = function(options){
var headers = options.amazonHeaders || '';
if (headers) headers += '\n';
return [
options.verb
, options.md5
, options.contentType
, options.date instanceof Date ? options.date.toUTCString() : options.date
, headers + options.resource
].join('\n');
};

/**
* Return a string for sign() with the given `options`, but is meant exclusively
* for S3 presigned URLs
Expand Down Expand Up @@ -186,32 +115,4 @@ exports.canonicalizeHeaders = function(headers){
return a > b ? 1 : -1;
}
return buf.sort(headerSort).join('\n');
};

/**
* Perform the following:
*
* - ignore non sub-resources
* - sort lexicographically
*
* @param {String} a URI-encoded resource (path + query string)
* @return {String}
* @api private
*/

exports.canonicalizeResource = function(resource){
var url = parse(resource, true)
, path = url.pathname
, buf = [];

// apply the query string whitelist
Object.keys(url.query).forEach(function (key) {
if (whitelist.indexOf(key) != -1) {
buf.push(key + (url.query[key] ? "=" + url.query[key] : ''));
}
});

return path + (buf.length
? '?' + buf.sort().join('&')
: '');
};
};
Loading