From 27f392e07e3d3a22f876bd0ee350f192113e31d7 Mon Sep 17 00:00:00 2001 From: Aco Mitevski Date: Mon, 22 Dec 2014 16:49:22 +0100 Subject: [PATCH 1/2] add support for hapijs 8.0 --- README.md | 2 +- lib/index.js | 8 ++++---- package.json | 8 ++++---- test/index.js | 26 +++++++++++++++----------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b8bb37c..04217d1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ #### About plugin -JSON Web Token (JWT) authentication plugin for [Hapi 6.0](https://github.com/spumko/hapi) +JSON Web Token (JWT) authentication plugin for [Hapi 8.0](https://github.com/spumko/hapi) Based on original version of [hapi-auth-jwt by ryanfitz](https://github.com/ryanfitz/hapi-auth-jwt), modified to work with Hapi 6.0, and return some additional data for validateFunc (original token). The original token can be used for extra validation, i.e. check against redis to make sure token is valid. diff --git a/lib/index.js b/lib/index.js index 47d6032..5913c5e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -66,22 +66,22 @@ internals.implementation = function (server, options) { credentials = credentials || null; if (err) { - return reply(err, { credentials: credentials, log: { tags: ['auth', 'jwt'], data: err } }); + return reply(err, null, { credentials: credentials }); } if (!isValid) { - return reply(Boom.unauthorized('Invalid token', 'Bearer'), { credentials: credentials }); + return reply(Boom.unauthorized('Invalid token', 'Bearer'), null, { credentials: credentials }); } if (!credentials || typeof credentials !== 'object') { - return reply(Boom.badImplementation('Bad credentials object received for jwt auth validation'), { log: { tags: 'credentials' } }); + return reply(Boom.badImplementation('Bad credentials object received for jwt auth validation')); } // Authenticated - return reply(null, { credentials: credentials }); + return reply.continue({ credentials: credentials }); }); }); diff --git a/package.json b/package.json index b9450e2..70958fd 100644 --- a/package.json +++ b/package.json @@ -17,15 +17,15 @@ "node": "0.10.x" }, "dependencies": { - "boom": "2.x.x", - "hoek": "1.x.x", + "boom": "2.5.x", + "hoek": "^2.10.x", "jsonwebtoken": "^1.1.2" }, "peerDependencies": { - "hapi": ">=6.x.x" + "hapi": ">=8.x.x" }, "devDependencies": { - "hapi": "6.x.x", + "hapi": "8.x.x", "lab": "1.x.x", "grunt": "~0.4.2", "grunt-contrib-jshint": "~0.8.0", diff --git a/test/index.js b/test/index.js index 7045213..688e523 100644 --- a/test/index.js +++ b/test/index.js @@ -2,6 +2,7 @@ var Lab = require('lab'); var Hapi = require('hapi'); +var Boom = require('boom'); var jwt = require('jsonwebtoken'); @@ -32,7 +33,7 @@ describe('Token', function () { }); } else if (username === 'jane') { - return callback(Hapi.error.internal('boom')); + return callback(Boom.internal('boom')); } else if (username === 'invalid1') { return callback(null, true, 'bad'); @@ -56,10 +57,11 @@ describe('Token', function () { }); }; - var server = new Hapi.Server({ debug: false }); + var server = new Hapi.Server(); + server.connection(); before(function (done) { - server.pack.register(require('../'), function (err) { + server.register(require('../'), function (err) { expect(err).to.not.exist; server.auth.strategy('default', 'jwt', 'required', { key: privateKey, validateFunc: loadUser }); @@ -93,13 +95,15 @@ describe('Token', function () { it('returns decoded token when no validation function is set', function (done) { var handler = function (request, reply) { + console.log('authenticated', request.auth.isAuthenticated); expect(request.auth.isAuthenticated).to.equal(true); expect(request.auth.credentials).to.exist; reply('ok'); }; - var server = new Hapi.Server({ debug: false }); - server.pack.register(require('../'), function (err) { + var server = new Hapi.Server(); + server.connection(); + server.register(require('../'), function (err) { expect(err).to.not.exist; server.auth.strategy('default', 'jwt', 'required', { key: privateKey }); @@ -107,15 +111,15 @@ describe('Token', function () { server.route([ { method: 'POST', path: '/token', handler: handler, config: { auth: 'default' } } ]); - }); - var request = { method: 'POST', url: '/token', headers: { authorization: tokenHeader('john') } }; + var request = { method: 'POST', url: '/token', headers: { authorization: tokenHeader('john') } }; - server.inject(request, function (res) { + server.inject(request, function (res) { - expect(res.result).to.exist; - expect(res.result).to.equal('ok'); - done(); + expect(res.result).to.exist; + expect(res.result).to.equal('ok'); + done(); + }); }); }); From cb6bc2d4f68f99f8997d8ea38f0701c68f7f5bb8 Mon Sep 17 00:00:00 2001 From: Aco Mitevski Date: Tue, 23 Dec 2014 10:43:23 +0100 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 70958fd..12bba01 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hapi-auth-jsonwebtoken", "description": "JSON Web Token (JWT) authentication plugin", - "version": "0.2.1", + "version": "0.2.3", "author": "Boketto ", "repository": "git://github.com/boketto/hapi-auth-jsonwebtoken", "main": "index",