From 2e67ba8744f719b7d0a28b47eff1c5824588008b Mon Sep 17 00:00:00 2001 From: noremacsim Date: Tue, 29 Nov 2022 14:29:57 +0000 Subject: [PATCH 1/4] Requested Path Fix --- .prettierignore | 4 +++- App/middleware/auth.js | 5 ++--- Core/driftyCore.js | 2 +- Core/index.js | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.prettierignore b/.prettierignore index dd7c792..8802aab 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,4 +4,6 @@ package-lock.json package.json postman.json readme.md -SECURITY.md \ No newline at end of file +SECURITY.md +CODE_OF_CONDUCT.md +CONTRIBUTING.md \ No newline at end of file diff --git a/App/middleware/auth.js b/App/middleware/auth.js index 2f708bd..f9fb4cd 100755 --- a/App/middleware/auth.js +++ b/App/middleware/auth.js @@ -7,7 +7,7 @@ const { Group, TwoFactorAuthentication, } = require(path.join(__dirname, '../../Core/models/')); -const {CustomRoutes} = require("../../Core"); +const {CustomRoutes} = require("../../Core/"); dotenv.config(); @@ -39,8 +39,7 @@ async function middle(request, h) { let requestedPath = '/'; const checkRequestedPath = obj => obj.path === request?.route?.path; - - if (CustomRoutes.some(checkRequestedPath)) { + if (CustomRoutes.CustomRoutes.some(checkRequestedPath)) { requestedPath = request?.route?.path; } diff --git a/Core/driftyCore.js b/Core/driftyCore.js index e37a9f8..565e06c 100644 --- a/Core/driftyCore.js +++ b/Core/driftyCore.js @@ -51,7 +51,7 @@ const init = async (type) => { } // Build Server Routes - server.route(CustomRoutes); + server.route(CustomRoutes.CustomRoutes); // Build View Handler to render templates server.views({ diff --git a/Core/index.js b/Core/index.js index 8d31259..199bc76 100755 --- a/Core/index.js +++ b/Core/index.js @@ -1,4 +1,4 @@ -module.exports = require('./routerSetup.js'); +module.exports.CustomRoutes = require('./routerSetup.js'); module.exports.Models = require('./models'); module.exports.driftyCore = require('./driftyCore.js'); module.exports.middleware = require('./middleware'); From 0f2bd6e40b0a304339ca123bca8cead96bfb37f4 Mon Sep 17 00:00:00 2001 From: noremacsim Date: Tue, 29 Nov 2022 19:14:20 +0000 Subject: [PATCH 2/4] Updated Moved Core Features to Core, Fixed Few Bugs --- App/controllers/companyController.js | 2 + App/controllers/feedController.js | 2 + App/controllers/friendsController.js | 2 + App/controllers/groupController.js | 2 + App/controllers/homeController.js | 2 + App/controllers/publicController.js | 2 + App/controllers/userController.js | 9 ++ App/routes/friends.js | 2 +- App/routes/home.js | 4 +- App/routes/posts.js | 2 +- App/routes/user.js | 68 +------- App/views/error/404.html | 145 ++++++------------ Core/controllers/index.js | 14 ++ Core/driftyCore.js | 7 +- {App => Core}/helpers/ejsHelper.js | 3 +- Core/helpers/index.js | 23 +++ Core/helpers/paths.js | 19 +++ Core/index.js | 8 +- {App => Core}/middleware/2faCheck.js | 0 {App => Core}/middleware/apiAuth.js | 0 {App => Core}/middleware/auth.js | 13 +- Core/middleware/index.js | 12 +- .../models/TwoFactorAuthentication.js | 0 {App => Core}/models/authtoken.js | 0 {App => Core}/models/companys.js | 0 {App => Core}/models/groups.js | 0 Core/models/index.js | 13 +- Core/models/routeMethods.js | 12 ++ Core/models/routeMiddleware.js | 20 +++ Core/models/routes.js | 46 ++++++ {App => Core}/models/users.js | 2 +- {App => Core}/plugins/fullView.js | 0 {App => Core}/plugins/hapiView.js | 6 +- Core/plugins/index.js | 12 +- {App => Core}/routes/company.js | 28 ++-- {App => Core}/routes/error.js | 7 + {App => Core}/routes/group.js | 15 +- Core/routes/index.js | 23 +++ Core/routes/user.js | 65 ++++++++ bin/build-routes.js | 3 + 40 files changed, 382 insertions(+), 211 deletions(-) create mode 100644 Core/controllers/index.js rename {App => Core}/helpers/ejsHelper.js (72%) mode change 100755 => 100644 create mode 100644 Core/helpers/index.js create mode 100644 Core/helpers/paths.js rename {App => Core}/middleware/2faCheck.js (100%) rename {App => Core}/middleware/apiAuth.js (100%) rename {App => Core}/middleware/auth.js (86%) mode change 100755 => 100644 rename {App => Core}/models/TwoFactorAuthentication.js (100%) rename {App => Core}/models/authtoken.js (100%) mode change 100755 => 100644 rename {App => Core}/models/companys.js (100%) rename {App => Core}/models/groups.js (100%) create mode 100644 Core/models/routeMethods.js create mode 100644 Core/models/routeMiddleware.js create mode 100644 Core/models/routes.js rename {App => Core}/models/users.js (98%) mode change 100755 => 100644 rename {App => Core}/plugins/fullView.js (100%) mode change 100755 => 100644 rename {App => Core}/plugins/hapiView.js (83%) mode change 100755 => 100644 rename {App => Core}/routes/company.js (58%) rename {App => Core}/routes/error.js (63%) rename {App => Core}/routes/group.js (55%) create mode 100644 Core/routes/index.js create mode 100644 Core/routes/user.js create mode 100644 bin/build-routes.js diff --git a/App/controllers/companyController.js b/App/controllers/companyController.js index 999e348..61748d5 100644 --- a/App/controllers/companyController.js +++ b/App/controllers/companyController.js @@ -7,6 +7,8 @@ const {ValidationError} = require('sequelize'); const Boom = require('boom'); module.exports = { + name: 'company', + newCompany: async (request, h) => { if (!request.payload.name) { throw Boom.badRequest('Company Name Required'); diff --git a/App/controllers/feedController.js b/App/controllers/feedController.js index b9bac75..3372708 100644 --- a/App/controllers/feedController.js +++ b/App/controllers/feedController.js @@ -3,6 +3,8 @@ const {sequelize} = require(path.join(__dirname, '../../Core/models/')); const {QueryTypes} = require('sequelize'); module.exports = { + name: 'feed', + posts: async (request, h, raw = false) => { // We should group the users from company then group then firends to get posts // If a user is part of a company/group we should return global posts in that scope diff --git a/App/controllers/friendsController.js b/App/controllers/friendsController.js index 8ff4f28..70266da 100644 --- a/App/controllers/friendsController.js +++ b/App/controllers/friendsController.js @@ -7,6 +7,8 @@ const {ValidationError, QueryTypes} = require('sequelize'); const Boom = require('boom'); module.exports = { + name: 'friends', + newRequest: async (request, h) => { if (!request.payload.friend_ID) { throw Boom.badRequest('We need a friend_Id'); diff --git a/App/controllers/groupController.js b/App/controllers/groupController.js index 4f0aecc..fd08cd3 100644 --- a/App/controllers/groupController.js +++ b/App/controllers/groupController.js @@ -4,6 +4,8 @@ const {ValidationError} = require('sequelize'); const Boom = require('boom'); module.exports = { + name: 'group', + newGroup: async (request, h) => { if (!request.payload.name) { throw Boom.badRequest('Group Name Required'); diff --git a/App/controllers/homeController.js b/App/controllers/homeController.js index 5df5986..162e1c2 100644 --- a/App/controllers/homeController.js +++ b/App/controllers/homeController.js @@ -2,6 +2,8 @@ const path = require('path'); const Feed = require(path.join(__dirname, '../controllers/feedController')); module.exports = { + name: 'home', + main: async (request, h) => { const posts = await Feed.posts(request, h, true); return h.simsView('home/main', {posts: posts ?? {}}, request); diff --git a/App/controllers/publicController.js b/App/controllers/publicController.js index c702eb8..3fa40d9 100644 --- a/App/controllers/publicController.js +++ b/App/controllers/publicController.js @@ -1,4 +1,6 @@ module.exports = { + name: 'public', + home: async (request, h) => { return h.fullView('welcome', null); }, diff --git a/App/controllers/userController.js b/App/controllers/userController.js index 6495613..d3d96a2 100755 --- a/App/controllers/userController.js +++ b/App/controllers/userController.js @@ -11,7 +11,16 @@ const {ValidationError} = require('sequelize'); const Boom = require('boom'); module.exports = { + name: 'user', + signinView: async (request, h) => { + + if (request.state.twoFAPassed === true && + request.state.isLoggedIn === true + ) { + return h.redirect(`/`); + } + if ( request.state.twoFAPassed === false && request.state.isLoggedIn === true diff --git a/App/routes/friends.js b/App/routes/friends.js index bbbd27d..00ab414 100644 --- a/App/routes/friends.js +++ b/App/routes/friends.js @@ -3,7 +3,7 @@ const Friends = require(path.join( __dirname, '../controllers/friendsController' )); -const {middleware} = require(path.join(__dirname, '../../Core/middleware')); +const middleware = require(path.join(__dirname, '../../Core/middleware')); module.exports = [ { diff --git a/App/routes/home.js b/App/routes/home.js index 42926c8..a842904 100644 --- a/App/routes/home.js +++ b/App/routes/home.js @@ -1,6 +1,6 @@ const path = require('path'); const Home = require(path.join(__dirname, '../controllers/homeController')); -const {middleware} = require(path.join(__dirname, '../../Core/middleware')); +const {Middleware} = require(path.join(__dirname, '../../Core/')); module.exports = [ { @@ -8,7 +8,7 @@ module.exports = [ path: '/', config: { handler: async function (request, h) { - const connect = await middleware.auth(request, h); + const connect = await Middleware.auth(request, h); if (connect === 'passed') { return Home.main(request, h); } diff --git a/App/routes/posts.js b/App/routes/posts.js index bb9875c..ba165c2 100644 --- a/App/routes/posts.js +++ b/App/routes/posts.js @@ -1,6 +1,6 @@ const path = require('path'); const Feed = require(path.join(__dirname, '../controllers/feedController')); -const {middleware} = require(path.join(__dirname, '../../Core/middleware')); +const middleware = require(path.join(__dirname, '../../Core/middleware')); module.exports = [ { diff --git a/App/routes/user.js b/App/routes/user.js index 0543857..b0b165e 100755 --- a/App/routes/user.js +++ b/App/routes/user.js @@ -1,72 +1,12 @@ const path = require('path'); -const User = require(path.join(__dirname, '../controllers/userController')); -const {middleware} = require(path.join(__dirname, '../../Core/middleware')); +const {Controllers} = require(path.join(__dirname, '../../Core/')); +const middleware = require(path.join(__dirname, '../../Core/middleware')); module.exports = [ - { - method: 'POST', - path: '/user/login', - handler: User.login, - config: { - description: 'Login User', - }, - }, - { - method: 'POST', - path: '/user/logout', - handler: User.logout, - config: { - description: 'Logout User', - }, - }, - { - method: 'POST', - path: '/user/register', - handler: User.register, - config: { - description: 'Register User', - }, - }, - { - method: 'GET', - path: '/user', - handler: User.details, - config: { - pre: [{method: middleware.apiAuth}], - description: 'User Details', - }, - }, - { - method: 'POST', - path: '/user/update', - handler: User.update, - config: { - pre: [{method: middleware.apiAuth}], - description: 'Update User', - }, - }, - { - method: 'GET', - path: '/user/2fa/new', - handler: User.new2Fa, - config: { - pre: [{method: middleware.apiAuth}], - description: 'User Details', - }, - }, - { - method: 'POST', - path: '/user/2fa/validate', - handler: User.enable2Fa, - config: { - pre: [{method: middleware.twofaCheck}], - description: 'User Details', - }, - }, { method: 'GET', path: '/user/login', - handler: User.signinView, + handler: Controllers.user.signinView, config: { description: 'Login User', }, @@ -78,7 +18,7 @@ module.exports = [ handler: async function (request, h) { const connect = await middleware.auth(request, h); if (connect === 'passed') { - return User.settingsView(request, h); + return Controllers.user.settingsView(request, h); } return connect; }, diff --git a/App/views/error/404.html b/App/views/error/404.html index 83cb926..2afcb11 100644 --- a/App/views/error/404.html +++ b/App/views/error/404.html @@ -1,106 +1,59 @@ - -
-
-

- 4 - 0 - 4 -

-

PAGE NOT FOUND

- -
-
+ + + +
+

404 Page Not Found

+

The page you requested was not found.

+ + \ No newline at end of file diff --git a/Core/controllers/index.js b/Core/controllers/index.js new file mode 100644 index 0000000..5b8e1b8 --- /dev/null +++ b/Core/controllers/index.js @@ -0,0 +1,14 @@ +const fs = require('fs'); +const path = require('path'); + +let controller; +let Controllers = []; + +fs.readdirSync(path.join(__dirname, '../../App/controllers/')) + .filter((file) => file !== 'index.js') + .forEach((file) => { + controller = require(path.join(__dirname + '../../../App/controllers/', file)); + Controllers[controller.name] = controller; + }); + +module.exports = Controllers; diff --git a/Core/driftyCore.js b/Core/driftyCore.js index 565e06c..f0b4f72 100644 --- a/Core/driftyCore.js +++ b/Core/driftyCore.js @@ -1,7 +1,6 @@ const Hapi = require('@hapi/hapi'); const path = require('path'); -const {CustomRoutes, Models} = require(path.join(__dirname, './')); -const {plugins} = require(path.join(__dirname, './plugins')); +const {Routes, Models, Plugins} = require(path.join(__dirname, './')); const cookies = require(path.join(__dirname, '../App/config/cookies.js')); const PORT = process.env.PORT || 4101; @@ -46,12 +45,12 @@ const init = async (type) => { // Register Custom plugins // eslint-disable-next-line - for (let [customPluginName, customPlugin] of Object.entries(plugins)) { + for (let [customPluginName, customPlugin] of Object.entries(Plugins)) { await server.register(customPlugin); } // Build Server Routes - server.route(CustomRoutes.CustomRoutes); + server.route(Routes); // Build View Handler to render templates server.views({ diff --git a/App/helpers/ejsHelper.js b/Core/helpers/ejsHelper.js old mode 100755 new mode 100644 similarity index 72% rename from App/helpers/ejsHelper.js rename to Core/helpers/ejsHelper.js index 1639c9e..a517ece --- a/App/helpers/ejsHelper.js +++ b/Core/helpers/ejsHelper.js @@ -1,4 +1,5 @@ -module.exports = { +module.exports.name = 'ejsHelpers'; +module.exports.functions = { isLoggedIn: function (request) { return request.state.isLoggedIn; }, diff --git a/Core/helpers/index.js b/Core/helpers/index.js new file mode 100644 index 0000000..dd11fbe --- /dev/null +++ b/Core/helpers/index.js @@ -0,0 +1,23 @@ +const path = require('path'); +const fs = require('fs'); + +let type; +const helpers = {}; + +// Core Plugins +fs.readdirSync(__dirname + '/') + .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') + .forEach((file) => { + type = require(path.join(__dirname + '/', file)); + helpers[type.name] = type.functions; + }); + +// Custom Plugins +fs.readdirSync(__dirname + '/../../App/helpers/') + .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') + .forEach((file) => { + type = require(path.join(__dirname + '/../../App/helpers/', file)); + helpers[type.name] = type.functions; + }); + +module.exports = helpers; diff --git a/Core/helpers/paths.js b/Core/helpers/paths.js new file mode 100644 index 0000000..8c938fd --- /dev/null +++ b/Core/helpers/paths.js @@ -0,0 +1,19 @@ +module.exports.name = 'path'; +module.exports.functions = { + exists: function (request, h) { + // Check if path exists in routes + let requestedPath = '/'; + const checkRequestedPath = obj => obj.path === request?.route?.path; + h.request.server._core.router.routes.forEach((element) => { + if (element.routes.some(checkRequestedPath)) { + requestedPath = request?.route?.path; + } + }) + + if (requestedPath === '/{any*}') { + requestedPath = '/'; + } + + return request?.route?.path ? requestedPath : false; + }, +}; diff --git a/Core/index.js b/Core/index.js index 199bc76..5abc12d 100755 --- a/Core/index.js +++ b/Core/index.js @@ -1,4 +1,8 @@ -module.exports.CustomRoutes = require('./routerSetup.js'); module.exports.Models = require('./models'); +module.exports.Helpers = require('./helpers'); +module.exports.Plugins = require('./plugins'); +module.exports.Middleware = require('./middleware'); +module.exports.Controllers = require('./controllers'); +module.exports.Routes = require('./routes'); module.exports.driftyCore = require('./driftyCore.js'); -module.exports.middleware = require('./middleware'); +console.log('loaded'); \ No newline at end of file diff --git a/App/middleware/2faCheck.js b/Core/middleware/2faCheck.js similarity index 100% rename from App/middleware/2faCheck.js rename to Core/middleware/2faCheck.js diff --git a/App/middleware/apiAuth.js b/Core/middleware/apiAuth.js similarity index 100% rename from App/middleware/apiAuth.js rename to Core/middleware/apiAuth.js diff --git a/App/middleware/auth.js b/Core/middleware/auth.js old mode 100755 new mode 100644 similarity index 86% rename from App/middleware/auth.js rename to Core/middleware/auth.js index f9fb4cd..bc79f1d --- a/App/middleware/auth.js +++ b/Core/middleware/auth.js @@ -7,7 +7,7 @@ const { Group, TwoFactorAuthentication, } = require(path.join(__dirname, '../../Core/models/')); -const {CustomRoutes} = require("../../Core/"); +const {Helpers} = require(path.join(__dirname, `../../Core/`)); dotenv.config(); @@ -31,18 +31,15 @@ async function middle(request, h) { token = request.auth.credentials; } + // Check if path exists in routes + let requestedPath = Helpers.path.exists(request, h); + if (token && userAgent) { const authToken = await AuthToken.findOne({ where: {token, userAgent}, include: User, }); - let requestedPath = '/'; - const checkRequestedPath = obj => obj.path === request?.route?.path; - if (CustomRoutes.CustomRoutes.some(checkRequestedPath)) { - requestedPath = request?.route?.path; - } - if (!authToken) { h.unstate('jwt'); h.unstate('isLoggedIn'); @@ -79,7 +76,7 @@ async function middle(request, h) { h.unstate('jwt'); h.unstate('isLoggedIn'); h.unstate('twoFAPassed'); - return h.redirect(`/user/login?path=${request?.route?.path}`); + return h.redirect(`/user/login?path=${requestedPath}`); } } diff --git a/Core/middleware/index.js b/Core/middleware/index.js index 9ca2bb4..93a8e4d 100755 --- a/Core/middleware/index.js +++ b/Core/middleware/index.js @@ -3,6 +3,16 @@ const fs = require('fs'); let type; const middleware = {}; + +//Core MiddleWare +fs.readdirSync(__dirname + '/') + .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') + .forEach((file) => { + type = require(path.join(__dirname + '/', file)); + middleware[type.name] = type.function; + }); + +// Custom MiddleWare fs.readdirSync(__dirname + '/../../App/middleware/') .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') .forEach((file) => { @@ -10,4 +20,4 @@ fs.readdirSync(__dirname + '/../../App/middleware/') middleware[type.name] = type.function; }); -module.exports = {middleware}; +module.exports = middleware; diff --git a/App/models/TwoFactorAuthentication.js b/Core/models/TwoFactorAuthentication.js similarity index 100% rename from App/models/TwoFactorAuthentication.js rename to Core/models/TwoFactorAuthentication.js diff --git a/App/models/authtoken.js b/Core/models/authtoken.js old mode 100755 new mode 100644 similarity index 100% rename from App/models/authtoken.js rename to Core/models/authtoken.js diff --git a/App/models/companys.js b/Core/models/companys.js similarity index 100% rename from App/models/companys.js rename to Core/models/companys.js diff --git a/App/models/groups.js b/Core/models/groups.js similarity index 100% rename from App/models/groups.js rename to Core/models/groups.js diff --git a/Core/models/index.js b/Core/models/index.js index eb310ff..d9ca837 100644 --- a/Core/models/index.js +++ b/Core/models/index.js @@ -25,7 +25,18 @@ try { const db = {}; -// reate Models +// Create Core Models +fs.readdirSync(__dirname + '/') + .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') + .forEach((file) => { + const model = require(path.join( + __dirname + '/', + file + ))(sequelize, Sequelize.DataTypes); + db[model.name] = model; + }); + +// Create Custom Models fs.readdirSync(__dirname + '/../../App/models/') .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') .forEach((file) => { diff --git a/Core/models/routeMethods.js b/Core/models/routeMethods.js new file mode 100644 index 0000000..4c78708 --- /dev/null +++ b/Core/models/routeMethods.js @@ -0,0 +1,12 @@ +module.exports = (sequelize, DataTypes) => { + + const Drifty_Routes_Method = sequelize.define('Drifty_Routes_Method', { + type: DataTypes.ENUM('GET', 'POST', 'PUT'), + }); + + Drifty_Routes_Method.associate = function (models) { + Drifty_Routes_Method.belongsTo(models.Drifty_Routes); + }; + + return Drifty_Routes_Method; +}; diff --git a/Core/models/routeMiddleware.js b/Core/models/routeMiddleware.js new file mode 100644 index 0000000..4b9346f --- /dev/null +++ b/Core/models/routeMiddleware.js @@ -0,0 +1,20 @@ +module.exports = (sequelize, DataTypes) => { + const Drifty_Routes_Middleware = sequelize.define('Drifty_Routes_Middleware', { + handler: { + type: DataTypes.STRING, + unique: false, + allowNull: false, + validate: { + notNull: { + msg: 'Requires a handler', + }, + }, + }, + }); + + Drifty_Routes_Middleware.associate = function (models) { + Drifty_Routes_Middleware.belongsTo(models.Drifty_Routes); + }; + + return Drifty_Routes_Middleware; +}; diff --git a/Core/models/routes.js b/Core/models/routes.js new file mode 100644 index 0000000..1766ca5 --- /dev/null +++ b/Core/models/routes.js @@ -0,0 +1,46 @@ +module.exports = (sequelize, DataTypes) => { + const Drifty_Routes = sequelize.define('Drifty_Routes', { + name: { + type: DataTypes.STRING, + unique: true, + allowNull: false, + validate: { + notNull: { + msg: 'Requires a name', + }, + }, + }, + path: { + type: DataTypes.STRING, + unique: true, + allowNull: false, + validate: { + notNull: { + msg: 'Requires a path', + }, + }, + }, + handler: { + type: DataTypes.STRING, + unique: false, + allowNull: false, + validate: { + notNull: { + msg: 'Requires a handler', + }, + }, + }, + description: { + type: DataTypes.STRING, + unique: false, + allowNull: true, + }, + }); + + Drifty_Routes.associate = function (models) { + Drifty_Routes.hasMany(models.Drifty_Routes_Method); + Drifty_Routes.hasMany(models.Drifty_Routes_Middleware); + }; + + return Drifty_Routes; +}; diff --git a/App/models/users.js b/Core/models/users.js old mode 100755 new mode 100644 similarity index 98% rename from App/models/users.js rename to Core/models/users.js index 87a26fa..ed6e53a --- a/App/models/users.js +++ b/Core/models/users.js @@ -47,7 +47,7 @@ module.exports = (sequelize, DataTypes) => { User.associate = function (models) { User.hasMany(models.AuthToken); - User.hasMany(models.Posts); + //User.hasMany(models.Posts); User.hasOne(models.TwoFactorAuthentication); User.belongsToMany(models.Company, {through: 'company_users'}); User.belongsToMany(models.Group, {through: 'group_users'}); diff --git a/App/plugins/fullView.js b/Core/plugins/fullView.js old mode 100755 new mode 100644 similarity index 100% rename from App/plugins/fullView.js rename to Core/plugins/fullView.js diff --git a/App/plugins/hapiView.js b/Core/plugins/hapiView.js old mode 100755 new mode 100644 similarity index 83% rename from App/plugins/hapiView.js rename to Core/plugins/hapiView.js index 72c0759..6abd105 --- a/App/plugins/hapiView.js +++ b/Core/plugins/hapiView.js @@ -1,4 +1,5 @@ const path = require('path'); +const {Helpers} = require(path.join(__dirname, `../`)); const simsView = { name: 'simsView', @@ -15,10 +16,7 @@ const simsView = { request = {}; } - viewOptions['helpers'] = require(path.join( - __dirname, - `../helpers/ejsHelper` - )); + viewOptions['helpers'] = Helpers.ejsHelpers; viewOptions['request'] = request; return this.view(view, viewOptions); diff --git a/Core/plugins/index.js b/Core/plugins/index.js index 82a859f..d5a1df7 100755 --- a/Core/plugins/index.js +++ b/Core/plugins/index.js @@ -3,6 +3,16 @@ const fs = require('fs'); let type; const plugins = {}; + +// Core Plugins +fs.readdirSync(__dirname + '/') + .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') + .forEach((file) => { + type = require(path.join(__dirname + '/', file)); + plugins[type.name] = type.object; + }); + +// Custom Plugins fs.readdirSync(__dirname + '/../../App/plugins/') .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') .forEach((file) => { @@ -10,4 +20,4 @@ fs.readdirSync(__dirname + '/../../App/plugins/') plugins[type.name] = type.object; }); -module.exports = {plugins}; +module.exports = plugins; diff --git a/App/routes/company.js b/Core/routes/company.js similarity index 58% rename from App/routes/company.js rename to Core/routes/company.js index fcf54f3..341a0ea 100644 --- a/App/routes/company.js +++ b/Core/routes/company.js @@ -1,53 +1,49 @@ const path = require('path'); -const Company = require(path.join( - __dirname, - '../controllers/companyController' -)); -const {middleware} = require(path.join(__dirname, '../../Core/middleware')); - +const {Controllers, Middleware} = require(path.join(__dirname, '../')); +console.log(Controllers); module.exports = [ { method: 'POST', path: '/company/new', - handler: Company.newCompany, + handler: Controllers.company.newCompany, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Create New Company', }, }, { method: 'POST', path: '/company/user/add', - handler: Company.addUser, + handler: Controllers.company.addUser, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Assign User to a Company', }, }, { method: 'POST', path: '/company/user/remove', - handler: Company.removeUser, + handler: Controllers.company.removeUser, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Remove a user from Company', }, }, { method: 'POST', path: '/company/group/add', - handler: Company.addGroup, + handler: Controllers.company.addGroup, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Assign Group to a Company', }, }, { method: 'POST', path: '/company/group/remove', - handler: Company.removeGroup, + handler: Controllers.company.removeGroup, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Remove a group from Company', }, }, diff --git a/App/routes/error.js b/Core/routes/error.js similarity index 63% rename from App/routes/error.js rename to Core/routes/error.js index 4bb8e3c..83015ac 100644 --- a/App/routes/error.js +++ b/Core/routes/error.js @@ -1,4 +1,5 @@ const Boom = require('boom'); +const pathHelpers = require("../helpers/paths"); module.exports = [ { @@ -7,6 +8,12 @@ module.exports = [ handler: (request, h) => { const accept = request.headers.accept; + const requestedPath = pathHelpers.exists(request, h) + if (requestedPath) + { + return h.redirect(`/user/login?path=${requestedPath}`); + } + if (accept && accept.match(/json/)) { return Boom.notFound('This resource isn’t available.'); } diff --git a/App/routes/group.js b/Core/routes/group.js similarity index 55% rename from App/routes/group.js rename to Core/routes/group.js index 38cb87e..115c7aa 100644 --- a/App/routes/group.js +++ b/Core/routes/group.js @@ -1,32 +1,31 @@ const path = require('path'); -const Group = require(path.join(__dirname, '../controllers/groupController')); -const {middleware} = require(path.join(__dirname, '../../Core/middleware')); +const {Controllers, Middleware} = require(path.join(__dirname, '../')); module.exports = [ { method: 'POST', path: '/group/new', - handler: Group.newGroup, + handler: Controllers.group.newGroup, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Create New Group', }, }, { method: 'POST', path: '/group/user/add', - handler: Group.addUser, + handler: Controllers.group.addUser, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Assign User to a Group', }, }, { method: 'POST', path: '/group/user/remove', - handler: Group.removeUser, + handler: Controllers.group.removeUser, config: { - pre: [{method: middleware.apiAuth}], + pre: [{method: Middleware.apiAuth}], description: 'Remove a user from Group', }, }, diff --git a/Core/routes/index.js b/Core/routes/index.js new file mode 100644 index 0000000..c2da38b --- /dev/null +++ b/Core/routes/index.js @@ -0,0 +1,23 @@ +const fs = require('fs'); +const path = require('path'); + +let CustomRoutes = []; +// Core Routes +fs.readdirSync(path.join(__dirname, '/')) + .filter((file) => file !== 'index.js') + .forEach((file) => { + CustomRoutes = CustomRoutes.concat( + require(path.join(__dirname, `/${file}`)) + ); + }); + +// Custom Routes +fs.readdirSync(path.join(__dirname, '../../App/routes/')) + .filter((file) => file !== 'index.js') + .forEach((file) => { + CustomRoutes = CustomRoutes.concat( + require(path.join(__dirname, `../../App/routes/${file}`)) + ); + }); + +module.exports = CustomRoutes; diff --git a/Core/routes/user.js b/Core/routes/user.js new file mode 100644 index 0000000..92fd356 --- /dev/null +++ b/Core/routes/user.js @@ -0,0 +1,65 @@ +const path = require('path'); +const {Controllers, Middleware} = require(path.join(__dirname, '../')); + +module.exports = [ + { + method: 'POST', + path: '/user/login', + handler: Controllers.user.login, + config: { + description: 'Login User', + }, + }, + { + method: 'POST', + path: '/user/logout', + handler: Controllers.user.logout, + config: { + description: 'Logout User', + }, + }, + { + method: 'POST', + path: '/user/register', + handler: Controllers.user.register, + config: { + description: 'Register User', + }, + }, + { + method: 'GET', + path: '/user', + handler: Controllers.user.details, + config: { + pre: [{method: Middleware.apiAuth}], + description: 'User Details', + }, + }, + { + method: 'POST', + path: '/user/update', + handler: Controllers.user.update, + config: { + pre: [{method: Middleware.apiAuth}], + description: 'Update User', + }, + }, + { + method: 'GET', + path: '/user/2fa/new', + handler: Controllers.user.new2Fa, + config: { + pre: [{method: Middleware.apiAuth}], + description: 'User Details', + }, + }, + { + method: 'POST', + path: '/user/2fa/validate', + handler: Controllers.user.enable2Fa, + config: { + pre: [{method: Middleware.twofaCheck}], + description: 'User Details', + }, + }, +]; diff --git a/bin/build-routes.js b/bin/build-routes.js new file mode 100644 index 0000000..b5d389d --- /dev/null +++ b/bin/build-routes.js @@ -0,0 +1,3 @@ +// In here we will build custom routes from the DB. +// This will be needed as we will start to save our routes within the DB instead of a file. +// Our routes I think.... are defined before db let me check...... \ No newline at end of file From bb2dd0b1bf8fb3282094a96ae2e5a2db7dbb303e Mon Sep 17 00:00:00 2001 From: noremacsim Date: Tue, 29 Nov 2022 19:15:14 +0000 Subject: [PATCH 3/4] Fix Formatting --- App/controllers/userController.js | 4 +- App/views/error/404.html | 113 ++++++++++++++++-------------- Core/controllers/index.js | 5 +- Core/helpers/paths.js | 4 +- Core/index.js | 2 +- Core/models/index.js | 8 +-- Core/models/routeMethods.js | 1 - Core/models/routeMiddleware.js | 23 +++--- Core/routes/error.js | 7 +- bin/build-routes.js | 2 +- 10 files changed, 90 insertions(+), 79 deletions(-) diff --git a/App/controllers/userController.js b/App/controllers/userController.js index d3d96a2..0ad90ea 100755 --- a/App/controllers/userController.js +++ b/App/controllers/userController.js @@ -14,8 +14,8 @@ module.exports = { name: 'user', signinView: async (request, h) => { - - if (request.state.twoFAPassed === true && + if ( + request.state.twoFAPassed === true && request.state.isLoggedIn === true ) { return h.redirect(`/`); diff --git a/App/views/error/404.html b/App/views/error/404.html index 2afcb11..8a151b9 100644 --- a/App/views/error/404.html +++ b/App/views/error/404.html @@ -1,59 +1,66 @@ - - - 404 Page Not Found - - - -
-

404 Page Not Found

-

The page you requested was not found.

- - \ No newline at end of file + p { + margin: 12px 15px 12px 15px; + } + + + +
+

404 Page Not Found

+

The page you requested was not found.

+
+ + diff --git a/Core/controllers/index.js b/Core/controllers/index.js index 5b8e1b8..20f076c 100644 --- a/Core/controllers/index.js +++ b/Core/controllers/index.js @@ -7,7 +7,10 @@ let Controllers = []; fs.readdirSync(path.join(__dirname, '../../App/controllers/')) .filter((file) => file !== 'index.js') .forEach((file) => { - controller = require(path.join(__dirname + '../../../App/controllers/', file)); + controller = require(path.join( + __dirname + '../../../App/controllers/', + file + )); Controllers[controller.name] = controller; }); diff --git a/Core/helpers/paths.js b/Core/helpers/paths.js index 8c938fd..c2b1b1e 100644 --- a/Core/helpers/paths.js +++ b/Core/helpers/paths.js @@ -3,12 +3,12 @@ module.exports.functions = { exists: function (request, h) { // Check if path exists in routes let requestedPath = '/'; - const checkRequestedPath = obj => obj.path === request?.route?.path; + const checkRequestedPath = (obj) => obj.path === request?.route?.path; h.request.server._core.router.routes.forEach((element) => { if (element.routes.some(checkRequestedPath)) { requestedPath = request?.route?.path; } - }) + }); if (requestedPath === '/{any*}') { requestedPath = '/'; diff --git a/Core/index.js b/Core/index.js index 5abc12d..5eb2c99 100755 --- a/Core/index.js +++ b/Core/index.js @@ -5,4 +5,4 @@ module.exports.Middleware = require('./middleware'); module.exports.Controllers = require('./controllers'); module.exports.Routes = require('./routes'); module.exports.driftyCore = require('./driftyCore.js'); -console.log('loaded'); \ No newline at end of file +console.log('loaded'); diff --git a/Core/models/index.js b/Core/models/index.js index d9ca837..c577ef6 100644 --- a/Core/models/index.js +++ b/Core/models/index.js @@ -29,10 +29,10 @@ const db = {}; fs.readdirSync(__dirname + '/') .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js') .forEach((file) => { - const model = require(path.join( - __dirname + '/', - file - ))(sequelize, Sequelize.DataTypes); + const model = require(path.join(__dirname + '/', file))( + sequelize, + Sequelize.DataTypes + ); db[model.name] = model; }); diff --git a/Core/models/routeMethods.js b/Core/models/routeMethods.js index 4c78708..2c2f864 100644 --- a/Core/models/routeMethods.js +++ b/Core/models/routeMethods.js @@ -1,5 +1,4 @@ module.exports = (sequelize, DataTypes) => { - const Drifty_Routes_Method = sequelize.define('Drifty_Routes_Method', { type: DataTypes.ENUM('GET', 'POST', 'PUT'), }); diff --git a/Core/models/routeMiddleware.js b/Core/models/routeMiddleware.js index 4b9346f..1eedf28 100644 --- a/Core/models/routeMiddleware.js +++ b/Core/models/routeMiddleware.js @@ -1,16 +1,19 @@ module.exports = (sequelize, DataTypes) => { - const Drifty_Routes_Middleware = sequelize.define('Drifty_Routes_Middleware', { - handler: { - type: DataTypes.STRING, - unique: false, - allowNull: false, - validate: { - notNull: { - msg: 'Requires a handler', + const Drifty_Routes_Middleware = sequelize.define( + 'Drifty_Routes_Middleware', + { + handler: { + type: DataTypes.STRING, + unique: false, + allowNull: false, + validate: { + notNull: { + msg: 'Requires a handler', + }, }, }, - }, - }); + } + ); Drifty_Routes_Middleware.associate = function (models) { Drifty_Routes_Middleware.belongsTo(models.Drifty_Routes); diff --git a/Core/routes/error.js b/Core/routes/error.js index 83015ac..d59ace6 100644 --- a/Core/routes/error.js +++ b/Core/routes/error.js @@ -1,5 +1,5 @@ const Boom = require('boom'); -const pathHelpers = require("../helpers/paths"); +const pathHelpers = require('../helpers/paths'); module.exports = [ { @@ -8,9 +8,8 @@ module.exports = [ handler: (request, h) => { const accept = request.headers.accept; - const requestedPath = pathHelpers.exists(request, h) - if (requestedPath) - { + const requestedPath = pathHelpers.exists(request, h); + if (requestedPath) { return h.redirect(`/user/login?path=${requestedPath}`); } diff --git a/bin/build-routes.js b/bin/build-routes.js index b5d389d..0f75ae5 100644 --- a/bin/build-routes.js +++ b/bin/build-routes.js @@ -1,3 +1,3 @@ // In here we will build custom routes from the DB. // This will be needed as we will start to save our routes within the DB instead of a file. -// Our routes I think.... are defined before db let me check...... \ No newline at end of file +// Our routes I think.... are defined before db let me check...... From 35319f05a18f22a116e0bcaa2ea0e6ddf2565c0a Mon Sep 17 00:00:00 2001 From: codefactor-io Date: Tue, 29 Nov 2022 19:16:38 +0000 Subject: [PATCH 4/4] [CodeFactor] Apply fixes --- App/views/error/404.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/App/views/error/404.html b/App/views/error/404.html index 8a151b9..905eb90 100644 --- a/App/views/error/404.html +++ b/App/views/error/404.html @@ -31,8 +31,8 @@ border-bottom: 1px solid #d0d0d0; font-size: 19px; font-weight: normal; - margin: 0 0 14px 0; - padding: 14px 15px 10px 15px; + margin: 0 0 14px; + padding: 14px 15px 10px; } code { @@ -42,8 +42,8 @@ border: 1px solid #d0d0d0; color: #002166; display: block; - margin: 14px 0 14px 0; - padding: 12px 10px 12px 10px; + margin: 14px 0; + padding: 12px 10px; } #container { @@ -53,7 +53,7 @@ } p { - margin: 12px 15px 12px 15px; + margin: 12px 15px; }