Permalink
Browse files

misc

  • Loading branch information...
1 parent f483ad6 commit 9de9c2e68c25179b85815afad83b1800b721e51e @hueniverse committed Mar 30, 2013
Showing with 732 additions and 732 deletions.
  1. +604 −604 README.md
  2. +23 −23 example/usage.js
  3. +2 −2 lib/client.js
  4. +7 −7 lib/crypto.js
  5. +24 −24 lib/utils.js
  6. +4 −4 test/client.js
  7. +6 −6 test/crypto.js
  8. +42 −42 test/index.js
  9. +6 −6 test/server.js
  10. +14 −14 test/uri.js
View
1,208 README.md

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -7,71 +7,71 @@ var Hawk = require('../lib');
// Declare internals
-var internals = {
- credentials: {
- dh37fgj492je: {
+var internals = {
+ credentials: {
+ dh37fgj492je: {
id: 'dh37fgj492je', // Required by Hawk.client.header
key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
algorithm: 'sha256',
- user: 'Steve'
- }
- }
+ user: 'Steve'
+ }
+ }
};
// Credentials lookup function
-var credentialsFunc = function (id, callback) {
+var credentialsFunc = function (id, callback) {
- return callback(null, internals.credentials[id]);
+ return callback(null, internals.credentials[id]);
};
// Create HTTP server
-var handler = function (req, res) {
+var handler = function (req, res) {
- Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
+ Hawk.server.authenticate(req, credentialsFunc, {}, function (err, credentials, artifacts) {
var payload = (!err ? 'Hello ' + credentials.user + ' ' + artifacts.ext : 'Shoosh!');
- var headers = {
- 'Content-Type': 'text/plain',
- 'Server-Authorization': Hawk.server.header(artifacts, { payload: payload, contentType: 'text/plain' })
+ var headers = {
+ 'Content-Type': 'text/plain',
+ 'Server-Authorization': Hawk.server.header(artifacts, { payload: payload, contentType: 'text/plain' })
};
res.writeHead(!err ? 200 : 401, headers);
- res.end(payload);
- });
+ res.end(payload);
+ });
};
Http.createServer(handler).listen(8000, '127.0.0.1');
// Send unauthenticated request
-Request('http://127.0.0.1:8000/resource/1?b=1&a=2', function (error, response, body) {
+Request('http://127.0.0.1:8000/resource/1?b=1&a=2', function (error, response, body) {
- console.log(response.statusCode + ': ' + body);
+ console.log(response.statusCode + ': ' + body);
});
// Send authenticated request
var header = Hawk.client.header('http://127.0.0.1:8000/resource/1?b=1&a=2', 'GET', { credentials: internals.credentials.dh37fgj492je, ext: 'and welcome!' });
-var options = {
+var options = {
uri: 'http://127.0.0.1:8000/resource/1?b=1&a=2',
method: 'GET',
- headers: {
- authorization: header.field
- }
+ headers: {
+ authorization: header.field
+ }
};
-Request(options, function (error, response, body) {
+Request(options, function (error, response, body) {
var isValid = Hawk.client.authenticate(response, header.artifacts, { payload: body });
console.log(response.statusCode + ': ' + body + (isValid ? ' (valid)' : ' (invalid)'));
- process.exit(0);
+ process.exit(0);
});
View
@@ -190,8 +190,8 @@ exports.authenticate = function (res, artifacts, options) {
return true;
}
- if (!attributes.hash) {
- return false;
+ if (!attributes.hash) {
+ return false;
}
var calculatedHash = Crypto.calculateHash(options.payload, artifacts.credentials.algorithm, res.headers['content-type']);
View
@@ -63,15 +63,15 @@ exports.generateNormalizedString = function (type, options) {
options.port + '\n' +
(options.hash || '') + '\n';
- if (options.ext) {
- normalized += options.ext.replace('\\', '\\\\').replace('\n', '\\n');
+ if (options.ext) {
+ normalized += options.ext.replace('\\', '\\\\').replace('\n', '\\n');
}
normalized += '\n';
- if (options.app) {
- normalized += options.app + '\n' +
- (options.dlg || '') + '\n';
+ if (options.app) {
+ normalized += options.app + '\n' +
+ (options.dlg || '') + '\n';
}
return normalized;
@@ -89,10 +89,10 @@ exports.calculateHash = function (payload, algorithm, contentType) {
};
-exports.calculateTsMac = function (ts, credentials) {
+exports.calculateTsMac = function (ts, credentials) {
var hash = Crypto.createHash(credentials.algorithm);
hash.update('hawk.' + exports.headerVersion + '.ts\n' + ts + '\n');
- return hash.digest('base64');
+ return hash.digest('base64');
};
View
@@ -61,10 +61,10 @@ exports.parseHost = function (req, hostHeaderName) {
// Parse Content-Type header content
-exports.parseContentType = function (header) {
+exports.parseContentType = function (header) {
- if (!header) {
- return '';
+ if (!header) {
+ return '';
}
return header.split(';')[0].trim().toLowerCase();
@@ -107,27 +107,27 @@ exports.now = function () {
// Parse Hawk HTTP Authorization header
-exports.parseAuthorizationHeader = function (header, keys) {
+exports.parseAuthorizationHeader = function (header, keys) {
keys = keys || ['id', 'ts', 'nonce', 'hash', 'ext', 'mac', 'app', 'dlg'];
- if (!header) {
- return Boom.unauthorized(null, 'Hawk');
+ if (!header) {
+ return Boom.unauthorized(null, 'Hawk');
}
var headerParts = header.match(/^(\w+)(?:\s+(.*))?$/); // Header: scheme[ something]
- if (!headerParts) {
- return Boom.badRequest('Invalid header syntax');
+ if (!headerParts) {
+ return Boom.badRequest('Invalid header syntax');
}
var scheme = headerParts[1];
- if (scheme.toLowerCase() !== 'hawk') {
- return Boom.unauthorized(null, 'Hawk');
+ if (scheme.toLowerCase() !== 'hawk') {
+ return Boom.unauthorized(null, 'Hawk');
}
var attributesString = headerParts[2];
- if (!attributesString) {
- return Boom.badRequest('Invalid header syntax');
+ if (!attributesString) {
+ return Boom.badRequest('Invalid header syntax');
}
var attributes = {};
@@ -136,32 +136,32 @@ exports.parseAuthorizationHeader = function (header, keys) {
// Check valid attribute names
- if (keys.indexOf($1) === -1) {
+ if (keys.indexOf($1) === -1) {
errorMessage = 'Unknown attribute: ' + $1;
- return;
+ return;
}
// Allowed attribute value characters: !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
- if ($2.match(/^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~]+$/) === null) {
+ if ($2.match(/^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~]+$/) === null) {
errorMessage = 'Bad attribute value: ' + $1;
- return;
+ return;
}
// Check for duplicates
- if (attributes.hasOwnProperty($1)) {
+ if (attributes.hasOwnProperty($1)) {
errorMessage = 'Duplicate attribute: ' + $1;
- return;
+ return;
}
attributes[$1] = $2;
- return '';
+ return '';
});
- if (verify !== '') {
- return Boom.badRequest(errorMessage || 'Bad header format');
- }
-
- return attributes;
+ if (verify !== '') {
+ return Boom.badRequest(errorMessage || 'Bad header format');
+ }
+
+ return attributes;
};
View
@@ -51,17 +51,17 @@ describe('Hawk', function () {
done();
});
- it('should return a valid authorization header (no ext)', function (done) {
+ it('should return a valid authorization header (no ext)', function (done) {
- var credentials = {
+ var credentials = {
id: '123456',
key: '2983d45yun89q',
- algorithm: 'sha256'
+ algorithm: 'sha256'
};
var header = Hawk.client.header('https://example.net/somewhere/over/the/rainbow', 'POST', { credentials: credentials, timestamp: 1353809207, nonce: 'Ygvqdz', payload: 'something to write about', contentType: 'text/plain' }).field;
expect(header).to.equal('Hawk id="123456", ts="1353809207", nonce="Ygvqdz", hash="2QfCt3GuY9HQnHWyWD3wX68ZOKbynqlfYmuO2ZBRqtY=", mac="HTgtd0jPI6E4izx8e4OHdO36q00xFCU0FolNq3RiCYs="');
- done();
+ done();
});
it('should return an empty authorization header on missing options', function (done) {
View
@@ -1,6 +1,6 @@
// Load modules
-var Lab = require('lab');
+var Lab = require('lab');
var Hawk = require('../lib');
@@ -11,11 +11,11 @@ var internals = {};
// Test shortcuts
-var expect = Lab.expect;
-var before = Lab.before;
-var after = Lab.after;
-var describe = Lab.experiment;
-var it = Lab.test;
+var expect = Lab.expect;
+var before = Lab.before;
+var after = Lab.after;
+var describe = Lab.experiment;
+var it = Lab.test;
describe('Hawk', function () {
Oops, something went wrong.

0 comments on commit 9de9c2e

Please sign in to comment.