Skip to content

Commit

Permalink
Merge pull request #13 from allenmichael/master
Browse files Browse the repository at this point in the history
Updated SHA256 Validation Utilities
  • Loading branch information
zetlen committed Dec 10, 2015
2 parents b5974c3 + f757abd commit 8b11275
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ module.exports = {
MASTERCATALOG: 'master-catalog',
CATALOG: 'catalog',
DATAVIEWMODE: 'dataview-mode',
VERSION: 'version'
VERSION: 'version',
SHA256: 'hmac-sha256'
},
dataViewModes: {
LIVE: 'Live',
Expand Down
4 changes: 3 additions & 1 deletion security/hash-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function makeHashStream() {
return h;
}

module.exports = function hashStream(secretKey, date) {
module.exports = function hashStream(secretKey, date, body) {
var hash1 = makeHashStream();
var hash2 = makeHashStream();

Expand All @@ -20,5 +20,7 @@ module.exports = function hashStream(secretKey, date) {

hash2.write(date);

hash2.write(body);

return hash2;
};
13 changes: 6 additions & 7 deletions security/is-request-valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
var hashStream = require('./hash-stream'),
concat = require('concat-stream'),
constants = require('../constants'),
url = require('url'),
util = require('util'),
defaultTimeout = constants.capabilityTimeoutInSeconds;

module.exports = function isRequestValid(context, req, cb) {
var timeout = context.capabilityTimeoutInSeconds || defaultTimeout;
var uri = url.parse(req.url, true),
queryString = uri.query,
requestDate = new Date(queryString.dt),
var headers = req.headers,
body = JSON.stringify(req.body),
requestDate = new Date(headers.date),
currentDate = new Date(),
diff = (currentDate - requestDate) / 1000;

req.pipe(hashStream(context.sharedSecret, queryString.dt)).pipe(concat(function (hash) {
if (hash !== queryString.messageHash || diff > timeout) {
return cb(new Error(util.format("Unauthorized access from %s, %s, %s Computed: %s", req.headers.host, queryString.messageHash, queryString.dt, hash)));
req.pipe(hashStream(context.sharedSecret, headers.date, body)).pipe(concat(function (hash) {
if (hash !== headers[constants.headerPrefix + constants.headers['SHA256']] || diff > timeout) {
return cb(new Error(util.format("Unauthorized access from %s, %s, %s Computed: %s", headers.host, headers[constants.headerPrefix + constants.headers['SHA256']], headers.date, hash)));
} else {
return cb(null);
}
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = {
MASTERCATALOG: 'master-catalog',
CATALOG: 'catalog',
DATAVIEWMODE: 'dataview-mode',
VERSION: 'version'
VERSION: 'version',
SHA256: 'hmac-sha256'
},
dataViewModes: {
LIVE: 'Live',
Expand Down
4 changes: 3 additions & 1 deletion src/security/hash-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function makeHashStream() {
return h;
}

module.exports = function hashStream(secretKey, date) {
module.exports = function hashStream(secretKey, date, body) {
var hash1 = makeHashStream();
var hash2 = makeHashStream();

Expand All @@ -18,6 +18,8 @@ module.exports = function hashStream(secretKey, date) {
hash2.write(sha256key);

hash2.write(date);

hash2.write(body);

return hash2;
};
13 changes: 6 additions & 7 deletions src/security/is-request-valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
var hashStream = require('./hash-stream'),
concat = require('concat-stream'),
constants = require('../constants'),
url = require('url'),
util = require('util'),

defaultTimeout = constants.capabilityTimeoutInSeconds;

module.exports = function isRequestValid(context, req, cb) {
var timeout = context.capabilityTimeoutInSeconds || defaultTimeout;
var uri = url.parse(req.url, true),
queryString = uri.query,
requestDate = new Date(queryString.dt),
var headers = req.headers,
body = JSON.stringify(req.body),
requestDate = new Date(headers.date),
currentDate = new Date(),
diff = (currentDate - requestDate) / 1000;

req.pipe(hashStream(context.sharedSecret, queryString.dt)).pipe(concat(function(hash) {
if (hash !== queryString.messageHash || diff > timeout) {
req.pipe(hashStream(context.sharedSecret, headers.date, body)).pipe(concat(function(hash) {
if (hash !== headers[constants.headerPrefix + constants.headers['SHA256']] || diff > timeout) {
return cb(new Error(util.format("Unauthorized access from %s, %s, %s Computed: %s",
req.headers.host, queryString.messageHash, queryString.dt, hash)))
headers.host, headers[constants.headerPrefix + constants.headers['SHA256']], headers.date, hash)))
} else {
return cb(null);
}
Expand Down

0 comments on commit 8b11275

Please sign in to comment.