Skip to content

Commit

Permalink
add jwt scheme/strategy test...
Browse files Browse the repository at this point in the history
ref #52
  • Loading branch information
maxgrossman committed May 30, 2019
1 parent bc05b54 commit ff700db
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
23 changes: 12 additions & 11 deletions jwtScheme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const config = require('../../config')[process.env.NODE_ENV || 'development'];
const db = require('../../connection');
const config = require('./config')[process.env.NODE_ENV || 'development'];
const db = require('./connection');
const Boom = require('@hapi/boom');
const jwt = require('jsonwebtoken');
const dayjs = require('dayjs');
Expand All @@ -19,11 +19,9 @@ function outOfDate(timestamp) {
* @param {string} authorizationHeader HTTP authorization header that includes JWT
* @return {object} parsed JWT that includes session id, user id, and user name
*/
function getToken(authorizationHeader) {
return authorizationHeader.length
? jwt.verify(authorizationHeader.replace('Bearer ', ''), config.jwt)
: authorizationHeader;
}
// function getToken(authorizationHeader) {

// }

/**
* Ensures JWT provided in authorization header is valid
Expand Down Expand Up @@ -65,11 +63,12 @@ function isAuthorized(token) {

async function jwtAuthentication(request, h) {
try {
let token = getToken(request.headers.authorization);
if (!token.length) {
let authorizationHeader = request.headers.authorization;
if (!authorizationHeader || !authorizationHeader.length) {
throw Boom.unauthorized(null, 'Custom');
}

let token = jwt.verify(authorizationHeader.replace('Bearer ', ''), config.jwt);
let authorized = await isAuthorized(token);
if (!authorized) {
throw Boom.unauthorized(null, 'Custom');
Expand All @@ -81,9 +80,11 @@ async function jwtAuthentication(request, h) {
}
}

export function scheme(server, options) {
function scheme(server, options) {
return {
api: { settings: 5 },
authenticate: jwtAuthentication
};
}
}

module.exports = scheme;
57 changes: 46 additions & 11 deletions test/jwtAuthentication.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
'use strict';

const server = require('./server');
const Boom = require('@hapi/boom');
const jwt = require('jsonwebtoken');
const seedData = require('../testData/seeds');
const config = require('../config')[process.env.NODE_ENV || 'testing'];
const mergeDefaults = require('./mergeDefaults');

const testRoute = {
method: 'GET',
path: '/',
handler: async function (request, h) {
try {
await request.server.auth.verify(request);
const chai = require('chai');
const expect = chai.expect;

} catch (err) {
return Boom.unauthorize()
const authTestRoute = {
method: 'GET',
config: {
auth: 'default',
handler: async function (request, h) {
try {
return request.auth.isAuthenticated;
} catch (err) {
return Boom.unauthorized();
}
}
}
},
path: '/authTest'
}


before(async () => await server.liftOff());
before(async () => await server.liftOff(authTestRoute));
describe('post', () => {
it ('replies 200 if provided valid signed JWT', async () => {})
it ('replies 200 when provided valid JWT', async () => {
const request = mergeDefaults({
method: 'GET',
headers: { Authorization: `Bearer ${seedData.fakeToken}` },
url: '/authTest'
});

const r = await server.inject(request);
expect(r.statusCode).to.eql(200);
expect(r.result).to.eql(true); // result === isAuthenticated bool...
});

it ('replies 401 when provided invalid or no JWT', async() => {
let request = mergeDefaults({
method: 'GET',
headers: { Authorization: 'Bearer blimBlam' },
url: '/authTest'
});
let r = await server.inject(request);
expect(r.statusCode).to.eql(401);

request = mergeDefaults({ method: 'GET', url: '/authTest' });
r = await server.inject(request);
expect(r.statusCode).to.eql(401);
});
});

0 comments on commit ff700db

Please sign in to comment.