diff --git a/jwtScheme.js b/jwtScheme.js index de79cc9..6c0de45 100644 --- a/jwtScheme.js +++ b/jwtScheme.js @@ -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'); @@ -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 @@ -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'); @@ -81,9 +80,11 @@ async function jwtAuthentication(request, h) { } } -export function scheme(server, options) { +function scheme(server, options) { return { api: { settings: 5 }, authenticate: jwtAuthentication }; -} \ No newline at end of file +} + +module.exports = scheme; \ No newline at end of file diff --git a/test/jwtAuthentication.js b/test/jwtAuthentication.js index 652c7eb..adc5fa9 100644 --- a/test/jwtAuthentication.js +++ b/test/jwtAuthentication.js @@ -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 () => {}) \ No newline at end of file + 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); + }); +}); \ No newline at end of file