Skip to content
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: 2 additions & 0 deletions .env-sample
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SMTP_PASSWORD=secret
COOKIE_SECRET=secret
JWT_SECRET=secret
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const config = {
$default: 9000
}
},
authStrategy: 'jwt',
authAttempts: {
forIp: 50,
forIpAndUser: 7
Expand All @@ -30,6 +31,11 @@ const config = {
production: process.env.COOKIE_SECRET,
$default: '!k3yb04rdK4tz~4qu4~k3yb04rdd0gz!'
},
jwtSecret: {
$filter: 'env',
production: process.env.JWT_SECRET,
$default: 'aStrongJwtSecret-#mgtfYK@QuRV8VMM7T>WfN4;^fMVr)y'
},
hapiMongoModels: {
mongodb: {
uri: {
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ const composeOptions = {
relativeTo: __dirname
};


module.exports = Glue.compose.bind(Glue, Manifest.get('/'), composeOptions);
3 changes: 3 additions & 0 deletions manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const manifest = {
labels: ['web']
}],
registrations: [
{
plugin: 'hapi-auth-jwt2'
},
{
plugin: 'hapi-auth-basic'
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"handlebars": "4.x.x",
"hapi": "15.x.x",
"hapi-auth-basic": "4.x.x",
"hapi-auth-jwt2": "^7.1.3",
"hapi-mongo-models": "6.x.x",
"hoek": "4.x.x",
"inert": "4.x.x",
"jade": "1.x.x",
"joi": "9.x.x",
"jsonwebtoken": "^7.1.9",
"lout": "9.x.x",
"mongo-models": "1.x.x",
"mongodb": "2.x.x",
Expand Down
1 change: 0 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Composer((err, server) => {
}

server.start(() => {

console.log('Started the plot device on port ' + server.info.port);
});
});
23 changes: 12 additions & 11 deletions server/api/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Async = require('async');
const AuthPlugin = require('../auth');
const Boom = require('boom');
const Joi = require('joi');
const Config = require('../../config');


const internals = {};
Expand All @@ -20,7 +21,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -57,7 +58,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
}
},
Expand All @@ -84,7 +85,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/my',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'account'
}
},
Expand Down Expand Up @@ -114,7 +115,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -144,7 +145,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -187,7 +188,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/my',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'account'
},
validate: {
Expand Down Expand Up @@ -229,7 +230,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}/user',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -340,7 +341,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}/user',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [{
Expand Down Expand Up @@ -425,7 +426,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}/notes',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -467,7 +468,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}/status',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -528,7 +529,7 @@ internals.applyRoutes = function (server, next) {
path: '/accounts/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [
Expand Down
13 changes: 7 additions & 6 deletions server/api/admin-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const AuthPlugin = require('../auth');
const Boom = require('boom');
const Joi = require('joi');
const Config = require('../../config');


const internals = {};
Expand All @@ -17,7 +18,7 @@ internals.applyRoutes = function (server, next) {
path: '/admin-groups',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -57,7 +58,7 @@ internals.applyRoutes = function (server, next) {
path: '/admin-groups/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [
Expand Down Expand Up @@ -87,7 +88,7 @@ internals.applyRoutes = function (server, next) {
path: '/admin-groups',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -120,7 +121,7 @@ internals.applyRoutes = function (server, next) {
path: '/admin-groups/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -165,7 +166,7 @@ internals.applyRoutes = function (server, next) {
path: '/admin-groups/{id}/permissions',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -206,7 +207,7 @@ internals.applyRoutes = function (server, next) {
path: '/admin-groups/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down
19 changes: 10 additions & 9 deletions server/api/admins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Async = require('async');
const AuthPlugin = require('../auth');
const Boom = require('boom');
const Joi = require('joi');
const Config = require('../../config');


const internals = {};
Expand All @@ -19,7 +20,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -59,7 +60,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [
Expand Down Expand Up @@ -89,7 +90,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -122,7 +123,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -171,7 +172,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}/permissions',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -212,7 +213,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}/groups',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -253,7 +254,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}/user',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -370,7 +371,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}/user',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -463,7 +464,7 @@ internals.applyRoutes = function (server, next) {
path: '/admins/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [
Expand Down
7 changes: 4 additions & 3 deletions server/api/auth-attempts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const AuthPlugin = require('../auth');
const Boom = require('boom');
const Joi = require('joi');
const Config = require('../../config');


const internals = {};
Expand All @@ -17,7 +18,7 @@ internals.applyRoutes = function (server, next) {
path: '/auth-attempts',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
validate: {
Expand Down Expand Up @@ -57,7 +58,7 @@ internals.applyRoutes = function (server, next) {
path: '/auth-attempts/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [
Expand Down Expand Up @@ -87,7 +88,7 @@ internals.applyRoutes = function (server, next) {
path: '/auth-attempts/{id}',
config: {
auth: {
strategy: 'simple',
strategy: Config.get('/authStrategy'),
scope: 'admin'
},
pre: [
Expand Down
14 changes: 11 additions & 3 deletions server/api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Bcrypt = require('bcrypt');
const Boom = require('boom');
const Config = require('../../config');
const Joi = require('joi');

const createToken = require('../utils/token');

const internals = {};

Expand Down Expand Up @@ -98,9 +98,16 @@ internals.applyRoutes = function (server, next) {
}]
},
handler: function (request, reply) {

const credentials = request.pre.session._id.toString() + ':' + request.pre.session.key;
const authHeader = 'Basic ' + new Buffer(credentials).toString('base64');
const token = createToken(request.pre.user, request.pre.session);

let authHeader = '';

if( Config.get('authStrategy') === 'simple') {
authHeader = 'Basic ' + new Buffer(credentials).toString('base64');
} else {
authHeader = 'Bearer ' + token;
}

reply({
user: {
Expand All @@ -109,6 +116,7 @@ internals.applyRoutes = function (server, next) {
email: request.pre.user.email,
roles: request.pre.user.roles
},
id_token: token,
session: request.pre.session,
authHeader
});
Expand Down
Loading