From ed5e9d4158146940a4ad2a51e9d6eba6d01b8f1d Mon Sep 17 00:00:00 2001 From: Adam Bretz Date: Mon, 29 Sep 2014 11:06:21 -0400 Subject: [PATCH] Removed outer describe. --- test/bewit.js | 266 +++++++-------- test/hawk.js | 924 +++++++++++++++++++++++++------------------------- 2 files changed, 591 insertions(+), 599 deletions(-) diff --git a/test/bewit.js b/test/bewit.js index cef7cea..7c1c755 100755 --- a/test/bewit.js +++ b/test/bewit.js @@ -14,202 +14,198 @@ var internals = {}; var expect = Lab.expect; var before = Lab.before; -var describe = Lab.experiment; var it = Lab.test; -describe('Bewit', function () { - - var credentials = { - 'john': { - cred: { - id: 'john', - key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', - algorithm: 'sha256' - } - }, - 'jane': { - err: Hapi.error.internal('boom') +var credentials = { + 'john': { + cred: { + id: 'john', + key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', + algorithm: 'sha256' } - }; + }, + 'jane': { + err: Hapi.error.internal('boom') + } +}; - var getCredentials = function (id, callback) { +var getCredentials = function (id, callback) { - if (credentials[id]) { - return callback(credentials[id].err, credentials[id].cred); - } - else { - return callback(null, null); - } - }; + if (credentials[id]) { + return callback(credentials[id].err, credentials[id].cred); + } + else { + return callback(null, null); + } +}; - var getBewit = function (id, path) { +var getBewit = function (id, path) { - if (credentials[id] && credentials[id].cred) { - return Hawk.uri.getBewit('http://example.com:8080' + path, { credentials: credentials[id].cred, ttlSec: 60 }); - } - else { - return ''; - } - }; + if (credentials[id] && credentials[id].cred) { + return Hawk.uri.getBewit('http://example.com:8080' + path, { credentials: credentials[id].cred, ttlSec: 60 }); + } + else { + return ''; + } +}; - var bewitHandler = function (request, reply) { +var bewitHandler = function (request, reply) { - reply('Success'); - }; + reply('Success'); +}; - var server = new Hapi.Server(); - before(function (done) { +var server = new Hapi.Server(); +before(function (done) { - server.pack.register(require('../'), function (err) { + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; + expect(err).to.not.exist; - server.auth.strategy('default', 'bewit', true, { getCredentialsFunc: getCredentials }); + server.auth.strategy('default', 'bewit', true, { getCredentialsFunc: getCredentials }); - server.route([ - { method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } }, - { method: 'GET', path: '/bewitOptional', handler: bewitHandler, config: { auth: { mode: 'optional', strategy: 'default' } } }, - { method: 'GET', path: '/bewitScope', handler: bewitHandler, config: { auth: { scope: 'x', strategy: 'default' } } }, - { method: 'GET', path: '/bewitTos', handler: bewitHandler, config: { auth: { tos: '2.0.0', strategy: 'default' } } } - ]); + server.route([ + { method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } }, + { method: 'GET', path: '/bewitOptional', handler: bewitHandler, config: { auth: { mode: 'optional', strategy: 'default' } } }, + { method: 'GET', path: '/bewitScope', handler: bewitHandler, config: { auth: { scope: 'x', strategy: 'default' } } }, + { method: 'GET', path: '/bewitTos', handler: bewitHandler, config: { auth: { tos: '2.0.0', strategy: 'default' } } } + ]); - done(); - }); + done(); }); +}); - it('returns a reply on successful auth', function (done) { +it('returns a reply on successful auth', function (done) { - var bewit = getBewit('john', '/bewit'); - server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) { + var bewit = getBewit('john', '/bewit'); + server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) { - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.result).to.equal('Success'); + done(); }); +}); - it('returns an error reply on failed optional auth', function (done) { +it('returns an error reply on failed optional auth', function (done) { - var bewit = getBewit('john', '/abc'); - server.inject('http://example.com:8080/bewitOptional?bewit=' + bewit, function (res) { + var bewit = getBewit('john', '/abc'); + server.inject('http://example.com:8080/bewitOptional?bewit=' + bewit, function (res) { - expect(res.statusCode).to.equal(401); - done(); - }); + expect(res.statusCode).to.equal(401); + done(); }); +}); - it('returns an error on bad bewit', function (done) { +it('returns an error on bad bewit', function (done) { - var bewit = getBewit('john', '/abc'); - server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) { + var bewit = getBewit('john', '/abc'); + server.inject('http://example.com:8080/bewit?bewit=' + bewit, function (res) { - expect(res.statusCode).to.equal(401); - done(); - }); + expect(res.statusCode).to.equal(401); + done(); }); +}); - it('returns an error on bad bewit format', function (done) { +it('returns an error on bad bewit format', function (done) { - server.inject('http://example.com:8080/bewit?bewit=junk', function (res) { + server.inject('http://example.com:8080/bewit?bewit=junk', function (res) { - expect(res.statusCode).to.equal(400); - done(); - }); + expect(res.statusCode).to.equal(400); + done(); }); +}); - it('returns an error on insufficient tos', function (done) { +it('returns an error on insufficient tos', function (done) { - var bewit = getBewit('john', '/bewitTos'); - server.inject('http://example.com:8080/bewitTos?bewit=' + bewit, function (res) { + var bewit = getBewit('john', '/bewitTos'); + server.inject('http://example.com:8080/bewitTos?bewit=' + bewit, function (res) { - expect(res.statusCode).to.equal(403); - done(); - }); + expect(res.statusCode).to.equal(403); + done(); }); +}); - it('returns an error on insufficient scope', function (done) { +it('returns an error on insufficient scope', function (done) { - var bewit = getBewit('john', '/bewitScope'); - server.inject('http://example.com:8080/bewitScope?bewit=' + bewit, function (res) { + var bewit = getBewit('john', '/bewitScope'); + server.inject('http://example.com:8080/bewitScope?bewit=' + bewit, function (res) { - expect(res.statusCode).to.equal(403); - done(); - }); + expect(res.statusCode).to.equal(403); + done(); }); +}); - it('returns a reply on successful auth when using a custom host header key', function (done) { +it('returns a reply on successful auth when using a custom host header key', function (done) { - var bewit = getBewit('john', '/bewit'); - var request = { method: 'GET', url: '/bewit?bewit=' + bewit, headers: { custom: 'example.com:8080' } }; + var bewit = getBewit('john', '/bewit'); + var request = { method: 'GET', url: '/bewit?bewit=' + bewit, headers: { custom: 'example.com:8080' } }; - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; + expect(err).to.not.exist; - server.auth.strategy('default', 'bewit', { - getCredentialsFunc: getCredentials, - hawk: { - hostHeaderName: 'custom' - } - }); + server.auth.strategy('default', 'bewit', { + getCredentialsFunc: getCredentials, + hawk: { + hostHeaderName: 'custom' + } + }); - server.route({ method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } }); + server.route({ method: 'GET', path: '/bewit', handler: bewitHandler, config: { auth: 'default' } }); - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('cannot add a route that has payload validation required', function (done) { +it('cannot add a route that has payload validation required', function (done) { - var fn = function () { + var fn = function () { - server.route({ method: 'POST', - path: '/bewitPayload', - handler: bewitHandler, - config: { auth: { mode: 'required', strategy: 'default', payload: 'required' }, - payload: { output: 'stream', parse: false } } - }); - }; + server.route({ method: 'POST', + path: '/bewitPayload', + handler: bewitHandler, + config: { auth: { mode: 'required', strategy: 'default', payload: 'required' }, + payload: { output: 'stream', parse: false } } + }); + }; - expect(fn).to.throw(Error); - done(); - }); + expect(fn).to.throw(Error); + done(); +}); - it('cannot add a route that has payload validation as optional', function (done) { +it('cannot add a route that has payload validation as optional', function (done) { - var fn = function () { + var fn = function () { - server.route({ method: 'POST', - path: '/bewitPayload', - handler: bewitHandler, - config: { auth: { mode: 'required', strategy: 'default', payload: 'optional' }, - payload: { output: 'stream', parse: false } } - }); - }; + server.route({ method: 'POST', + path: '/bewitPayload', + handler: bewitHandler, + config: { auth: { mode: 'required', strategy: 'default', payload: 'optional' }, + payload: { output: 'stream', parse: false } } + }); + }; - expect(fn).to.throw(Error); - done(); - }); + expect(fn).to.throw(Error); + done(); +}); - it('can add a route that has payload validation as none', function (done) { +it('can add a route that has payload validation as none', function (done) { - var fn = function () { + var fn = function () { - server.route({ method: 'POST', - path: '/bewitPayload', - handler: bewitHandler, - config: { auth: { mode: 'required', strategy: 'default', payload: false }, - payload: { output: 'stream', parse: false } } - }); - }; + server.route({ method: 'POST', + path: '/bewitPayload', + handler: bewitHandler, + config: { auth: { mode: 'required', strategy: 'default', payload: false }, + payload: { output: 'stream', parse: false } } + }); + }; - expect(fn).to.not.throw(Error); - done(); - }); + expect(fn).to.not.throw(Error); + done(); }); diff --git a/test/hawk.js b/test/hawk.js index f8c6f29..a5e44f5 100755 --- a/test/hawk.js +++ b/test/hawk.js @@ -16,666 +16,662 @@ var internals = {}; // Test shortcuts var expect = Lab.expect; -var describe = Lab.experiment; var it = Lab.test; -describe('Hawk', function () { - - var credentials = { - 'john': { - cred: { - id: 'john', - key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', - algorithm: 'sha256' - } - }, - 'jane': { - err: Boom.internal('boom') - }, - 'joan': { - cred: { - id: 'joan', - key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', - algorithm: 'sha256' - } - } - }; - - var getCredentials = function (id, callback) { - - if (credentials[id]) { - return callback(credentials[id].err, credentials[id].cred); +var credentials = { + 'john': { + cred: { + id: 'john', + key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', + algorithm: 'sha256' } - else { - return callback(null, null); + }, + 'jane': { + err: Boom.internal('boom') + }, + 'joan': { + cred: { + id: 'joan', + key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn', + algorithm: 'sha256' } - }; - - var hawkHeader = function (id, path) { - - if (credentials[id] && credentials[id].cred) { - return Hawk.client.header('http://example.com:8080' + path, 'POST', { credentials: credentials[id].cred }); - } - else { - return ''; - } - }; - - it('returns a reply on successful auth', function (done) { - - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { - - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawk', - handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default' } } - ); - - var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: hawkHeader('john', '/hawk').field } }; - server.inject(request, function (res) { - - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + } +}; + +var getCredentials = function (id, callback) { + + if (credentials[id]) { + return callback(credentials[id].err, credentials[id].cred); + } + else { + return callback(null, null); + } +}; + +var hawkHeader = function (id, path) { + + if (credentials[id] && credentials[id].cred) { + return Hawk.client.header('http://example.com:8080' + path, 'POST', { credentials: credentials[id].cred }); + } + else { + return ''; + } +}; + +it('returns a reply on successful auth', function (done) { + + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { + + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawk', + handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default' } } + ); + + var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: hawkHeader('john', '/hawk').field } }; + server.inject(request, function (res) { + + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns a reply on failed optional auth', function (done) { +it('returns a reply on failed optional auth', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkOptional', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'optional', strategy: 'default' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkOptional', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'optional', strategy: 'default' } } } + ); - var request = { method: 'POST', url: 'http://example.com:8080/hawkOptional' }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawkOptional' }; + server.inject(request, function (res) { - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('includes authorization header in response when the response is a stream', function (done) { - - var hawkStreamHandler = function (request, reply) { +it('includes authorization header in response when the response is a stream', function (done) { - var TestStream = function () { + var hawkStreamHandler = function (request, reply) { - Stream.Readable.call(this); - }; + var TestStream = function () { - Hoek.inherits(TestStream, Stream.Readable); + Stream.Readable.call(this); + }; - TestStream.prototype._read = function (size) { + Hoek.inherits(TestStream, Stream.Readable); - var self = this; + TestStream.prototype._read = function (size) { - if (this.isDone) { - return; - } - this.isDone = true; + var self = this; - setTimeout(function () { + if (this.isDone) { + return; + } + this.isDone = true; - self.push('hi'); - }, 2); + setTimeout(function () { - setTimeout(function () { + self.push('hi'); + }, 2); - self.push(null); - }, 5); - }; + setTimeout(function () { - var stream = new TestStream(); - reply(stream); + self.push(null); + }, 5); }; - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var stream = new TestStream(); + reply(stream); + }; + + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkStream', handler: hawkStreamHandler, config: { auth: 'default' } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkStream', handler: hawkStreamHandler, config: { auth: 'default' } }); - var authHeader = hawkHeader('john', '/hawkStream'); - var request = { method: 'POST', url: 'http://example.com:8080/hawkStream', headers: { authorization: authHeader.field } }; - server.inject(request, function (res) { + var authHeader = hawkHeader('john', '/hawkStream'); + var request = { method: 'POST', url: 'http://example.com:8080/hawkStream', headers: { authorization: authHeader.field } }; + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.headers['server-authorization']).to.contain('Hawk'); + expect(res.statusCode).to.equal(200); + expect(res.headers['server-authorization']).to.contain('Hawk'); - var options = { - payload: res.payload - }; + var options = { + payload: res.payload + }; - getCredentials('john', function (err, cred) { + getCredentials('john', function (err, cred) { - var header = Hawk.server.header(cred, authHeader.artifacts, options); - expect(header).to.equal(res.headers['server-authorization']); - done(); - }); + var header = Hawk.server.header(cred, authHeader.artifacts, options); + expect(header).to.equal(res.headers['server-authorization']); + done(); }); }); }); +}); - it('includes valid authorization header in response when the response is text', function (done) { +it('includes valid authorization header in response when the response is text', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawk', - handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default' } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawk', + handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default' } } + ); - var authHeader = hawkHeader('john', '/hawk'); - var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: authHeader.field } }; + var authHeader = hawkHeader('john', '/hawk'); + var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: authHeader.field } }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.headers['server-authorization']).to.contain('Hawk'); + expect(res.statusCode).to.equal(200); + expect(res.headers['server-authorization']).to.contain('Hawk'); - var options = { - payload: res.payload, - contentType: res.headers['content-type'] - }; + var options = { + payload: res.payload, + contentType: res.headers['content-type'] + }; - getCredentials('john', function (err, cred) { + getCredentials('john', function (err, cred) { - var header = Hawk.server.header(cred, authHeader.artifacts, options); - expect(header).to.equal(res.headers['server-authorization']); + var header = Hawk.server.header(cred, authHeader.artifacts, options); + expect(header).to.equal(res.headers['server-authorization']); - done(); - }); + done(); }); }); }); +}); - it('includes valid authorization header in response when the request fails validation', function (done) { +it('includes valid authorization header in response when the request fails validation', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkValidate', - handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default', validate: { query: {} } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkValidate', + handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default', validate: { query: {} } } } + ); - var authHeader = hawkHeader('john', '/hawkValidate?a=1'); - var request = { method: 'POST', url: 'http://example.com:8080/hawkValidate?a=1', headers: { authorization: authHeader.field } }; - server.inject(request, function (res) { + var authHeader = hawkHeader('john', '/hawkValidate?a=1'); + var request = { method: 'POST', url: 'http://example.com:8080/hawkValidate?a=1', headers: { authorization: authHeader.field } }; + server.inject(request, function (res) { - expect(res.headers['server-authorization']).to.exist; - expect(res.headers['server-authorization']).to.contain('Hawk'); - expect(res.statusCode).to.equal(400); + expect(res.headers['server-authorization']).to.exist; + expect(res.headers['server-authorization']).to.contain('Hawk'); + expect(res.statusCode).to.equal(400); - var options = { - payload: res.payload, - contentType: res.headers['content-type'] - }; + var options = { + payload: res.payload, + contentType: res.headers['content-type'] + }; - getCredentials('john', function (err, cred) { + getCredentials('john', function (err, cred) { - authHeader.artifacts.credentials = cred; - var header = Hawk.server.header(cred, authHeader.artifacts, options); - expect(header).to.equal(res.headers['server-authorization']); + authHeader.artifacts.credentials = cred; + var header = Hawk.server.header(cred, authHeader.artifacts, options); + expect(header).to.equal(res.headers['server-authorization']); - done(); - }); + done(); }); }); }); +}); - it('does not include authorization header in response when the response is an error', function (done) { +it('does not include authorization header in response when the response is an error', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkError', - handler: function (request, reply) { reply(new Error()); }, - config: { auth: 'default' } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkError', + handler: function (request, reply) { reply(new Error()); }, + config: { auth: 'default' } } + ); - var request = { method: 'POST', url: 'http://example.com:8080/hawkError', headers: { authorization: hawkHeader('john', '/hawkError').field } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawkError', headers: { authorization: hawkHeader('john', '/hawkError').field } }; + server.inject(request, function (res) { - expect(res.statusCode).to.equal(500); - expect(res.headers.authorization).to.not.exist; - done(); - }); + expect(res.statusCode).to.equal(500); + expect(res.headers.authorization).to.not.exist; + done(); }); }); +}); - it('returns an error on bad auth header', function (done) { +it('returns an error on bad auth header', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', - path: '/hawk', - handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default' } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', + path: '/hawk', + handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default' } }); - var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: hawkHeader('john', 'abcd').field } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: hawkHeader('john', 'abcd').field } }; + server.inject(request, function (res) { - expect(res.result).to.exist; - expect(res.statusCode).to.equal(401); - done(); - }); + expect(res.result).to.exist; + expect(res.statusCode).to.equal(401); + done(); }); }); +}); - it('returns an error on bad header format', function (done) { +it('returns an error on bad header format', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawk', handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default' } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawk', handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default' } }); - var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: 'junk' } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: 'junk' } }; + server.inject(request, function (res) { - expect(res.result).to.exist; - expect(res.statusCode).to.equal(401); - done(); - }); + expect(res.result).to.exist; + expect(res.statusCode).to.equal(401); + done(); }); }); +}); - it('returns an error on bad scheme', function (done) { +it('returns an error on bad scheme', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawk', handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default' } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawk', handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default' } }); - var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: 'junk something' } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawk', headers: { authorization: 'junk something' } }; + server.inject(request, function (res) { - expect(res.result).to.exist; - expect(res.statusCode).to.equal(401); - done(); - }); + expect(res.result).to.exist; + expect(res.statusCode).to.equal(401); + done(); }); }); +}); - it('returns an error on insufficient tos', function (done) { +it('returns an error on insufficient tos', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkTos', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { tos: '2.0.0', strategy: 'default' } } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkTos', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { tos: '2.0.0', strategy: 'default' } } }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkTos', headers: { authorization: hawkHeader('john', '/hawkTos').field } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawkTos', headers: { authorization: hawkHeader('john', '/hawkTos').field } }; + server.inject(request, function (res) { - expect(res.statusCode).to.equal(403); - done(); - }); + expect(res.statusCode).to.equal(403); + done(); }); }); +}); - it('returns an error on insufficient scope', function (done) { +it('returns an error on insufficient scope', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkScope', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { scope: 'x', strategy: 'default' } } - }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkScope', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { scope: 'x', strategy: 'default' } } + }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkScope', payload: 'something', headers: { authorization: hawkHeader('john', '/hawkScope').field } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: 'http://example.com:8080/hawkScope', payload: 'something', headers: { authorization: hawkHeader('john', '/hawkScope').field } }; + server.inject(request, function (res) { - expect(res.statusCode).to.equal(403); - done(); - }); + expect(res.statusCode).to.equal(403); + done(); }); }); +}); - it('returns a reply on successful auth when using a custom host header key', function (done) { +it('returns a reply on successful auth when using a custom host header key', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; + expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { - getCredentialsFunc: getCredentials, - hawk: { - hostHeaderName: 'custom' - } - }); + server.auth.strategy('default', 'hawk', { + getCredentialsFunc: getCredentials, + hawk: { + hostHeaderName: 'custom' + } + }); - server.route({ method: 'POST', path: '/hawk', - handler: function (request, reply) { reply('Success'); }, - config: { auth: 'default' } } - ); + server.route({ method: 'POST', path: '/hawk', + handler: function (request, reply) { reply('Success'); }, + config: { auth: 'default' } } + ); - var request = { method: 'POST', url: '/hawk', headers: { authorization: hawkHeader('john', '/hawk').field, custom: 'example.com:8080' } }; - server.inject(request, function (res) { + var request = { method: 'POST', url: '/hawk', headers: { authorization: hawkHeader('john', '/hawk').field, custom: 'example.com:8080' } }; + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns a reply on successful auth and payload validation', function (done) { - - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { - - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayload', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); - - var payload = 'application text formatted payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred, payload: payload, contentType: 'text/plain' }); - var request = { - method: 'POST', - url: 'http://example.com:8080/hawkPayload', - headers: { authorization: authHeader.field, 'content-type': 'text/plain' }, - payload: payload, - simulate: { split: true } - }; +it('returns a reply on successful auth and payload validation', function (done) { + + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { + + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayload', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); + + var payload = 'application text formatted payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred, payload: payload, contentType: 'text/plain' }); + var request = { + method: 'POST', + url: 'http://example.com:8080/hawkPayload', + headers: { authorization: authHeader.field, 'content-type': 'text/plain' }, + payload: payload, + simulate: { split: true } + }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns an error with payload validation when the payload is tampered with', function (done) { +it('returns an error with payload validation when the payload is tampered with', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayload', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayload', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } }); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); - payload += 'HACKED'; - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayload', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); + payload += 'HACKED'; + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayload', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(401); - expect(res.result.message).to.equal('Payload is invalid'); - done(); - }); + expect(res.statusCode).to.equal(401); + expect(res.result.message).to.equal('Payload is invalid'); + done(); }); }); +}); - it('returns an error with payload validation when the payload is absent', function (done) { +it('returns an error with payload validation when the payload is absent', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayload', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } }); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayload', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } }); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); - payload = ''; - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayload', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); + payload = ''; + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayload', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(401); - expect(res.result.message).to.equal('Payload is invalid'); - done(); - }); + expect(res.statusCode).to.equal(401); + expect(res.result.message).to.equal('Payload is invalid'); + done(); }); }); +}); - it('returns an error with payload validation when the payload is tampered with and the route has optional validation', function (done) { +it('returns an error with payload validation when the payload is tampered with and the route has optional validation', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayloadOptional', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'optional', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayloadOptional', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'optional', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadOptional', 'POST', { credentials: credentials.john.cred, payload: payload }); - payload += 'HACKED'; - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadOptional', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadOptional', 'POST', { credentials: credentials.john.cred, payload: payload }); + payload += 'HACKED'; + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadOptional', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(401); - expect(res.result.message).to.equal('Payload is invalid'); - done(); - }); + expect(res.statusCode).to.equal(401); + expect(res.result.message).to.equal('Payload is invalid'); + done(); }); }); +}); - it('returns a reply on successful auth and payload validation when validation is optional', function (done) { +it('returns a reply on successful auth and payload validation when validation is optional', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayloadOptional', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'optional', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayloadOptional', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'optional', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadOptional', 'POST', { credentials: credentials.john.cred, payload: payload }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadOptional', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadOptional', 'POST', { credentials: credentials.john.cred, payload: payload }); + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadOptional', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.result).to.exist; - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.result).to.exist; + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns a reply on successful auth when payload validation is optional and no payload hash exists', function (done) { +it('returns a reply on successful auth when payload validation is optional and no payload hash exists', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayloadOptional', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'optional', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayloadOptional', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'optional', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadOptional', 'POST', { credentials: credentials.john.cred }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadOptional', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadOptional', 'POST', { credentials: credentials.john.cred }); + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadOptional', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.result).to.exist; - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.result).to.exist; + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns a reply on successful auth and when payload validation is disabled', function (done) { +it('returns a reply on successful auth and when payload validation is disabled', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayloadNone', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: false, strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayloadNone', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: false, strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadNone', 'POST', { credentials: credentials.john.cred, payload: payload }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadNone', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadNone', 'POST', { credentials: credentials.john.cred, payload: payload }); + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadNone', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns a reply on successful auth when the payload is tampered with and the route has disabled validation', function (done) { +it('returns a reply on successful auth when the payload is tampered with and the route has disabled validation', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayloadNone', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: false, strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayloadNone', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: false, strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadNone', 'POST', { credentials: credentials.john.cred, payload: payload }); - payload += 'HACKED'; - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadNone', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayloadNone', 'POST', { credentials: credentials.john.cred, payload: payload }); + payload += 'HACKED'; + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayloadNone', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns a reply on successful auth when auth is optional and when payload validation is required', function (done) { +it('returns a reply on successful auth when auth is optional and when payload validation is required', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkOptionalPayload', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'optional', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkOptionalPayload', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'optional', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkOptionalPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkOptionalPayload', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkOptionalPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); + var request = { method: 'POST', url: 'http://example.com:8080/hawkOptionalPayload', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(200); - expect(res.result).to.equal('Success'); - done(); - }); + expect(res.statusCode).to.equal(200); + expect(res.result).to.equal('Success'); + done(); }); }); +}); - it('returns an error with payload validation when the payload is tampered with and the route has optional auth', function (done) { +it('returns an error with payload validation when the payload is tampered with and the route has optional auth', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkOptionalPayload', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'optional', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkOptionalPayload', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'optional', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkOptionalPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); - payload += 'HACKED'; - var request = { method: 'POST', url: 'http://example.com:8080/hawkOptionalPayload', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkOptionalPayload', 'POST', { credentials: credentials.john.cred, payload: payload }); + payload += 'HACKED'; + var request = { method: 'POST', url: 'http://example.com:8080/hawkOptionalPayload', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(401); - expect(res.result.message).to.equal('Payload is invalid'); - done(); - }); + expect(res.statusCode).to.equal(401); + expect(res.result.message).to.equal('Payload is invalid'); + done(); }); }); +}); - it('returns an error with payload validation when the payload hash is not included and payload validation is required', function (done) { +it('returns an error with payload validation when the payload hash is not included and payload validation is required', function (done) { - var server = new Hapi.Server(); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.pack.register(require('../'), function (err) { - expect(err).to.not.exist; - server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); - server.route({ method: 'POST', path: '/hawkPayload', - handler: function (request, reply) { reply('Success'); }, - config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } - ); + expect(err).to.not.exist; + server.auth.strategy('default', 'hawk', { getCredentialsFunc: getCredentials }); + server.route({ method: 'POST', path: '/hawkPayload', + handler: function (request, reply) { reply('Success'); }, + config: { auth: { mode: 'required', payload: 'required', strategy: 'default' }, payload: { override: 'text/plain' } } } + ); - var payload = 'Here is my payload'; - var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred }); - var request = { method: 'POST', url: 'http://example.com:8080/hawkPayload', headers: { authorization: authHeader.field }, payload: payload }; + var payload = 'Here is my payload'; + var authHeader = Hawk.client.header('http://example.com:8080/hawkPayload', 'POST', { credentials: credentials.john.cred }); + var request = { method: 'POST', url: 'http://example.com:8080/hawkPayload', headers: { authorization: authHeader.field }, payload: payload }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.statusCode).to.equal(401); - expect(res.result.message).to.equal('Missing payload authentication'); - done(); - }); + expect(res.statusCode).to.equal(401); + expect(res.result.message).to.equal('Missing payload authentication'); + done(); }); }); });