Comparing version 0.4.2 to 0.5.0
68
aws4.js
var aws4 = exports, | ||
url = require('url'), | ||
path = require('path'), | ||
querystring = require('querystring'), | ||
crypto = require('crypto'), | ||
@@ -58,8 +60,4 @@ lru = require('lru-cache'), | ||
var request = this.request, | ||
headers = request.headers = (request.headers || {}), | ||
date = new Date(headers.Date || new Date) | ||
headers = request.headers = (request.headers || {}) | ||
this.datetime = date.toISOString().replace(/[:\-]|\.\d{3}/g, '') | ||
this.date = this.datetime.substr(0, 8) | ||
if (!request.method && request.body) | ||
@@ -73,17 +71,20 @@ request.method = 'POST' | ||
if (request.body && !headers['Content-Type'] && !headers['content-type']) | ||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8' | ||
if (!request.doNotModifyHeaders) { | ||
if (request.body && !headers['Content-Type'] && !headers['content-type']) | ||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8' | ||
if (request.body && !headers['Content-Length'] && !headers['content-length']) | ||
headers['Content-Length'] = Buffer.byteLength(request.body) | ||
if (request.body && !headers['Content-Length'] && !headers['content-length']) | ||
headers['Content-Length'] = Buffer.byteLength(request.body) | ||
headers['X-Amz-Date'] = this.datetime | ||
headers['X-Amz-Date'] = this.getDateTime() | ||
if (this.credentials.sessionToken) | ||
headers['X-Amz-Security-Token'] = this.credentials.sessionToken | ||
if (this.credentials.sessionToken) | ||
headers['X-Amz-Security-Token'] = this.credentials.sessionToken | ||
if (this.service === 's3') | ||
headers['X-Amz-Content-Sha256'] = hash(this.request.body || '', 'hex') | ||
if (this.service === 's3') | ||
headers['X-Amz-Content-Sha256'] = hash(this.request.body || '', 'hex') | ||
} | ||
if (headers.Authorization) delete headers.Authorization | ||
delete headers.Authorization | ||
delete headers.authorization | ||
headers.Authorization = this.authHeader() | ||
@@ -94,2 +95,16 @@ | ||
RequestSigner.prototype.getDateTime = function() { | ||
if (!this.datetime) { | ||
var headers = (this.request.headers || {}), | ||
date = new Date(headers.Date || headers.date || new Date) | ||
this.datetime = date.toISOString().replace(/[:\-]|\.\d{3}/g, '') | ||
} | ||
return this.datetime | ||
} | ||
RequestSigner.prototype.getDate = function() { | ||
return this.getDateTime().substr(0, 8) | ||
} | ||
RequestSigner.prototype.authHeader = function() { | ||
@@ -104,6 +119,7 @@ return [ | ||
RequestSigner.prototype.signature = function() { | ||
var cacheKey = [this.credentials.secretAccessKey, this.date, this.region, this.service].join(), | ||
var date = this.getDate(), | ||
cacheKey = [this.credentials.secretAccessKey, date, this.region, this.service].join(), | ||
kDate, kRegion, kService, kCredentials = credentialsCache.get(cacheKey) | ||
if (!kCredentials) { | ||
kDate = hmac('AWS4' + this.credentials.secretAccessKey, this.date) | ||
kDate = hmac('AWS4' + this.credentials.secretAccessKey, date) | ||
kRegion = hmac(kDate, this.region) | ||
@@ -120,3 +136,3 @@ kService = hmac(kRegion, this.service) | ||
'AWS4-HMAC-SHA256', | ||
this.datetime, | ||
this.getDateTime(), | ||
this.credentialString(), | ||
@@ -128,7 +144,15 @@ hash(this.canonicalString(), 'hex') | ||
RequestSigner.prototype.canonicalString = function() { | ||
var pathParts = (this.request.path || '/').split('?', 2) | ||
var pathStr = this.request.path || '/', queryIx = pathStr.indexOf('?'), queryStr = '' | ||
if (queryIx >= 0) { | ||
var query = querystring.parse(pathStr.slice(queryIx + 1)) | ||
pathStr = pathStr.slice(0, queryIx) | ||
queryStr = querystring.stringify(Object.keys(query).sort().reduce(function(obj, key) { | ||
obj[key] = Array.isArray(query[key]) ? query[key].sort() : query[key] | ||
return obj | ||
}, {})) | ||
} | ||
return [ | ||
this.request.method || 'GET', | ||
pathParts[0] || '/', | ||
pathParts[1] || '', | ||
path.normalize(pathStr), | ||
queryStr, | ||
this.canonicalHeaders() + '\n', | ||
@@ -160,3 +184,3 @@ this.signedHeaders(), | ||
return [ | ||
this.date, | ||
this.getDate(), | ||
this.region, | ||
@@ -163,0 +187,0 @@ this.service, |
{ | ||
"name": "aws4", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Signs and prepares requests using AWS Signature Version 4", | ||
@@ -65,7 +65,7 @@ "author": "Michael Hart <michael.hart.au@gmail.com> (http://github.com/mhart)", | ||
"dependencies": { | ||
"lru-cache": "~2.3.1" | ||
"lru-cache": "^2.5.0" | ||
}, | ||
"devDependencies": { | ||
"should": "~1.2.1", | ||
"mocha": "~1.7.4" | ||
"mocha": "^2.2.1", | ||
"should": "^5.2.0" | ||
}, | ||
@@ -72,0 +72,0 @@ "scripts": { |
35090
478
+ Addedlru-cache@2.7.3(transitive)
- Removedlru-cache@2.3.1(transitive)
Updatedlru-cache@^2.5.0