Skip to content
This repository was archived by the owner on Dec 1, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

});
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <info@boket.to>",
"repository": "git://github.com/boketto/hapi-auth-jsonwebtoken",
"main": "index",
Expand All @@ -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",
Expand Down
26 changes: 15 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Lab = require('lab');
var Hapi = require('hapi');
var Boom = require('boom');
var jwt = require('jsonwebtoken');


Expand Down Expand Up @@ -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');
Expand All @@ -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 });
Expand Down Expand Up @@ -93,29 +95,31 @@ 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 });

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();
});
});
});

Expand Down