Permalink
Browse files

Mixed case headers are support, tests and examples are updated.

  • Loading branch information...
1 parent 772df6c commit 07849a5786aa65cc3c7880fa8a7126a17c0fd7fe @wolfeidau committed Feb 28, 2013
Showing with 21 additions and 24 deletions.
  1. +1 −0 examples/app.js
  2. +4 −2 examples/client.js
  3. +15 −21 lib/ofuda.js
  4. +1 −1 test/ofuda_test.js
View
@@ -10,6 +10,7 @@ var validateCredentials = function(requestAccessKeyId){
http.createServer(function (request, response) {
if(ofuda.validateHttpRequest(request, validateCredentials)){
+ console.log(require('util').inspect(request.headers))
response.writeHead(200);
response.end('Success!');
} else {
View
@@ -13,16 +13,18 @@ http_options = {
headers: {
'Content-Type': 'application/json',
'Content-MD5': 'ee930827ccb58cd846ca31af5faa3634',
- 'date': 'Thu, 07 Feb 2013 20:53:04 GMT'
+ 'Date': 'Thu, 07 Feb 2013 20:53:04 GMT'
}
};
signedOptions = ofuda.signHttpRequest(credentials, http_options);
+console.log(require('util').inspect(http_options.headers))
+
var req = http.request(signedOptions, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
});
req.write('{"some":"thing"}');
-req.end();
+req.end();
View
@@ -10,7 +10,6 @@ var crypto = require('crypto'),
cryptiles = require('cryptiles'),
_ = require('lodash');
-
/**
*
* Setup ofuda with `options`.
@@ -94,23 +93,19 @@ Ofuda.prototype._locateHeadersByPrefix = function (request) {
};
/**
- * Locate a the value of a header via a case insensitive search, this is designed to
- * enable different clients I have observed using a either proper case or lower case
- * for header names.
+ * Lower case the header names.
*
* @param request
- * @param headerName
- * @return {*}
+ * @return {{}}
* @private
*/
-Ofuda.prototype._locateHeader = function (request, headerName) {
- var header = _.filter(_.keys(request.headers), function (key) {
- if (headerName.toLowerCase() === key.toLowerCase()) {
- return key;
- }
- }) || headerName; // this will either locate the header in the list OR just return the headerName
- return request.headers[header];
-};
+Ofuda.prototype._lowerCaseHeaders = function(request){
+
+ var _headers = {}
+ Object.keys(request.headers).forEach(function(key){_headers[key.toLowerCase()] = request.headers[key]})
+
+ return _headers;
+}
/**
* Assemble the canonical string from a request which will be signed.
@@ -120,14 +115,13 @@ Ofuda.prototype._locateHeader = function (request, headerName) {
*/
Ofuda.prototype._buildCanonicalStringFromRequest = function (request) {
- return _.union([request.method,
- this._locateHeader(request, 'Content-MD5'),
- this._locateHeader(request, 'Content-Type'),
- this._locateHeader(request, 'Date')],
+ var _headers = this._lowerCaseHeaders(request);
+
+ return _.union([request.method, _headers['content-md5'], _headers['content-type'], _headers['date']],
_.map(this._locateHeadersByPrefix(request),function (headerName) {
return headerName.toLowerCase() + ':' + request.headers[headerName];
}).sort(),
- request.path || request.url //caters for node http client or server use
+ request.path || request.url
).join('\n');
};
@@ -191,15 +185,15 @@ Ofuda.prototype.signHttpRequest = function (credentials, request, canonicalStrin
};
/**
- * Validate the hmac authorisation header in the supplied request using the auth callback to retrieve the credentials.
+ * Validate the HMAC authorisation header in the supplied request using the auth callback to retrieve the credentials.
*
* @param request
* @param authCallback
* @return {Boolean}
*/
Ofuda.prototype.validateHttpRequest = function (request, authCallback) {
- var authorization = this._locateHeader(request, 'Authorization');
+ var authorization = this._lowerCaseHeaders(request).authorization;
if (this.options.debug) {
console.log('authorization = ' + authorization);
View
@@ -91,7 +91,7 @@ describe('ofuda client', function () {
it('should generate a matching signature for the given string', function(){
- ofuda = new Ofuda({headerPrefix:'Amz', debug: true});
+ ofuda = new Ofuda({headerPrefix:'Amz', debug: false});
// accessKeyId: '44CF9590006BF252F707', accessKeySecret: 'OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV',
ofuda._generateHMACSignature(credentials, putCanonicalString).should.eql('jZNOcbfWmD/A/f3hSvVzXZjM2HU=');

0 comments on commit 07849a5

Please sign in to comment.