Skip to content

Commit

Permalink
Assignment4
Browse files Browse the repository at this point in the history
* added hapi-auth-bearer-token
* auth strategy is registered in it's own plugin './authtoken.js'
* all routes must have valid token to be accessed
  - currently only one route exists.
* adjusted project values to reflect assignment4
* tests adjusted to posses valid token.
  • Loading branch information
zoe-1 committed Dec 26, 2017
1 parent 7cd9e61 commit 697dee8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* lesson1
* lesson2
* lesson3
* lesson4
29 changes: 29 additions & 0 deletions lib/authtoken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

// Declare internals

const internals = {};


const defaultValidateFunc = (request, token) => {

// Put database query to authenticate the token is valid here.

return {
isValid: token === '12345678',
credentials: { token }
};
};

exports.plugin = {
name: 'authtoken',
version: '1.0.0',
description: 'register hapi-auth-bearer-token strategy.',
register: function (server, options) {

server.auth.strategy('default', 'bearer-access-token', {
validate: defaultValidateFunc
});
server.auth.default('default');
}
};
10 changes: 8 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

const Hapi = require('hapi');
const Hoek = require('hoek');
const HapiAuthBearerToken = require('hapi-auth-bearer-token');
const AuthToken = require('./authtoken');

// Load custom plugins

Expand All @@ -13,7 +15,9 @@ const Version = require('./version');
const internals = {};

internals.customPlugins = [
{ plugin: Version, options: { message: 'lesson3' } }
{ plugin: HapiAuthBearerToken, options: {} },
{ plugin: AuthToken, options: {} },
{ plugin: Version, options: { message: 'lesson4' } }
];

internals.init = async (serverOptions) => {
Expand All @@ -24,8 +28,10 @@ internals.init = async (serverOptions) => {

const server = new Hapi.Server(serverOptions);

await server.register(internals.customPlugins, { once: true }); // Our custom plugins register only once.
await server.register(internals.customPlugins, { once: true }); // These plugins register only once.

await server.start();

return server;
}
catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ exports.plugin = {
version: '1.0.0',
register: function (server, options) {

// Use below to test requests to the live server.
// curl -H "Authorization: Bearer 12345678" -X GET http://localhost:8000/version

const version = function (request, h) {

return 'version ' + Package.version + ' ' + options.message;
Expand Down
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "university-v1.0.0",
"version": "1.0.3",
"version": "1.0.4",
"description": "rewrite of the university",
"main": "lib/index.js",
"directories": {
Expand All @@ -27,7 +27,8 @@
},
"homepage": "https://github.com/zoe-1/university-rewrite#readme",
"dependencies": {
"hapi": "17.x.x"
"hapi": "17.x.x",
"hapi-auth-bearer-token": "6.x.x"
},
"devDependencies": {
"code": "5.x.x",
Expand Down
6 changes: 4 additions & 2 deletions test/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ describe('/version', () => {

expect(server).to.be.an.object();

const res = await server.inject('/version');
const request = { method: 'GET', url: '/version', headers: { authorization: 'Bearer 12345678' } };

expect(res.result).to.equal('version 1.0.3 lesson3');
const res = await server.inject(request);

expect(res.result).to.equal('version 1.0.4 lesson4');
await server.stop();
});
});

0 comments on commit 697dee8

Please sign in to comment.