Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Created January 13, 2015 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmawhorter/659b7aeec4ce590e3497 to your computer and use it in GitHub Desktop.
Save cmawhorter/659b7aeec4ce590e3497 to your computer and use it in GitHub Desktop.
hmmac issue #10 workaround
// https://github.com/cmawhorter/hmmac/issues/10
var app = require('express')();
var Hmmac = require('hmmac');
var AWS = require('aws-sdk');
var accessKeyId = 's3box';
var secretAccessKey = 's3box';
var hmmac = new Hmmac({
scheme: Hmmac.schemes.load('aws4'),
acceptableDateSkew: false,
debug: 1,
schemeConfig: {
region: 's3box',
service: 's3'
},
credentialProvider: function(key, callback) {
if(key !== accessKeyId) {
return callback(null)
} else {
callback({ key: accessKeyId, secret: secretAccessKey});
}
}
});
function convertAwsISO8601DateToStd(dt) {
// 20111014T235959Z
var parts = dt.split('Z')[0].split('T')
, d = parts[0]
, t = parts[1]
, dYear = d.substr(0, 4)
, dMonth = d.substr(4, 2)
, dDay = d.substr(6, 2)
, tHour = t.substr(0, 2)
, tMinute = t.substr(2, 2)
, tSecond = t.substr(4, 2)
, fixedDate = [ dYear, dMonth, dDay ].join('-')
, fixedTime = [ tHour, tMinute, tSecond ].join(':')
, fixedDateTime = [ fixedDate, 'T', fixedTime, 'Z' ].join('');
return new Date(fixedDateTime).toISOString();
}
app.use(function (req, res, next) {
req.headers['date'] = convertAwsISO8601DateToStd(req.headers['x-amz-date']);
hmmac.validate(req, function(valid) {
console.log('This should be valid', valid);
});
});
var server = app.listen(3000, function () {
AWS.config.update({
"accessKeyId": accessKeyId,
"secretAccessKey": secretAccessKey,
"region": "s3box",
"endpoint": "127.0.0.1:3000",
"maxRetries": 0,
"s3ForcePathStyle": true,
"sslEnabled": false,
"signatureVersion": "v4"
});
var s3bucket = new AWS.S3({params: {Bucket: 'fooobar'}});
s3bucket.createBucket(function(err) {
console.log(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment