Big Bang
This commit is contained in:
18
app/routing/Middleware.js
Normal file
18
app/routing/Middleware.js
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Global Middleware Definitions
|
||||
* -------------------------------------------------------------
|
||||
* These middleware are applied, in order, before every request that
|
||||
* Flitter handles, regardless of request type. Each middleware class
|
||||
* can be referenced using Flitter's global mw() function, but you can
|
||||
* also require() the class directly.
|
||||
*
|
||||
* Route-specific middleware should be specified in the corresponding
|
||||
* routes file.
|
||||
*/
|
||||
const Middleware = [
|
||||
|
||||
// mw('MiddlewareName'),
|
||||
|
||||
]
|
||||
|
||||
module.exports = exports = Middleware
|
||||
27
app/routing/middleware/HomeLogger.middleware.js
Normal file
27
app/routing/middleware/HomeLogger.middleware.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* HomeLogger Middleware
|
||||
* -------------------------------------------------------------
|
||||
* This is a sample middleware. It simply prints a console message when
|
||||
* the route that it is tied to is accessed. By default, it is called if
|
||||
* the '/' route is accessed. It can be injected in routes globally using
|
||||
* the global mw() function.
|
||||
*/
|
||||
class HomeLogger {
|
||||
|
||||
/*
|
||||
* Run the middleware test.
|
||||
* This method is required by all Flitter middleware.
|
||||
* It should either call the next function in the stack,
|
||||
* or it should handle the response accordingly.
|
||||
*/
|
||||
test(req, res, next){
|
||||
console.log("Home was accessed!")
|
||||
|
||||
/*
|
||||
* Call the next function in the stack.
|
||||
*/
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HomeLogger
|
||||
49
app/routing/middleware/auth/Permission.middleware.js
Normal file
49
app/routing/middleware/auth/Permission.middleware.js
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @module flitter-auth/deploy/routing/middleware/RequireAuth
|
||||
*/
|
||||
|
||||
/**
|
||||
* This middleware is provided by Flitter-auth. It will redirect the user
|
||||
* back to their previous location if the does not have the specified permission.
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
class Permission {
|
||||
|
||||
/**
|
||||
* Run the middleware's check. If an authenticated session exists and the user has the specified permission,
|
||||
* let the request continue. If an authenticated session doesn't exist, write the destination to the
|
||||
* session and redirect the user to the login page. If the permission doesn't exist, show a 401.
|
||||
* @param {Express/Request} req - the incoming Express request
|
||||
* @param {Express/Response} res - the corresponding Express response
|
||||
* @param {Function} next - Express handler stack callback. This should be called if the middleware check passed to allow the request to continue.
|
||||
* @param {string} permission - Name of the permission to require
|
||||
*/
|
||||
async test(req, res, next, permission){
|
||||
if ( req.session && req.session.auth && (req.session.auth.authenticated === true || req.session.auth.user) ){
|
||||
if ( req.session.auth.user.permissions && req.session.auth.user.permissions.includes(permission) ){
|
||||
next()
|
||||
}
|
||||
else if ( req.session.auth.user.role ){
|
||||
const Role = _flitter.model('auth:Role')
|
||||
const role = await Role.findOne({name: req.session.auth.user.role})
|
||||
|
||||
if ( role.permissions.includes(permission) ){
|
||||
next()
|
||||
}
|
||||
else {
|
||||
return _flitter.error(res, 401, {reason: 'Insufficient user permissions.'})
|
||||
}
|
||||
}
|
||||
else {
|
||||
return _flitter.error(res, 401, {reason: 'Insufficient user permissions.'})
|
||||
}
|
||||
}
|
||||
else {
|
||||
req.session.destination = req.originalUrl
|
||||
return res.redirect('/auth/login')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Permission
|
||||
35
app/routing/middleware/auth/RequireAuth.middleware.js
Normal file
35
app/routing/middleware/auth/RequireAuth.middleware.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @module flitter-auth/deploy/routing/middleware/RequireAuth
|
||||
*/
|
||||
|
||||
/**
|
||||
* This middleware is provided by Flitter-auth. It will redirect the user
|
||||
* back to their previous location if the does not contain a user object.
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
class RequireAuth {
|
||||
|
||||
/**
|
||||
* Run the middleware's check. If an authenticated session exists, let the request continue.
|
||||
* If an authenticated session doesn't exist, write the destination to the session and redirect
|
||||
* the user to the login page.
|
||||
* @param {Express/Request} req - the incoming Express request
|
||||
* @param {Express/Response} res - the corresponding Express response
|
||||
* @param {Function} next - Express handler stack callback. This should be called if the middleware check passed to allow the request to continue.
|
||||
*/
|
||||
test(req, res, next){
|
||||
if ( req.session && req.session.auth && (req.session.auth.authenticated === true || req.session.auth.user) ){
|
||||
/*
|
||||
* Call the next function in the stack.
|
||||
*/
|
||||
next()
|
||||
}
|
||||
else {
|
||||
req.session.destination = req.originalUrl
|
||||
return res.redirect('/auth/login')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequireAuth
|
||||
32
app/routing/middleware/auth/RequireGuest.middleware.js
Normal file
32
app/routing/middleware/auth/RequireGuest.middleware.js
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @module flitter-auth/deploy/routing/middleware/RequireGuest
|
||||
*/
|
||||
|
||||
/**
|
||||
* This middleware is provided by Flitter-auth. It will redirect the user
|
||||
* back to their previous location if the session contains the user object.
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
class RequireGuest {
|
||||
|
||||
/**
|
||||
* Run the middleware test. If an authenticated session exists, redirect the user to an error page.
|
||||
* Otherwise, allow the request to continue.
|
||||
* @param {Express/Request} req - the incoming Express request
|
||||
* @param {Express/Response} res - the corresponding Express response
|
||||
* @param {Function} next - The callback to continue the Express request handling stack. This is called if the middleware check passes.
|
||||
*/
|
||||
test(req, res, next){
|
||||
if ( req.session && req.session.auth && (req.session.auth.authenticated === true || req.session.auth.user) ){
|
||||
return _flitter.view(res, 'errors/requires_guest')
|
||||
}
|
||||
|
||||
/*
|
||||
* Call the next function in the stack.
|
||||
*/
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RequireGuest
|
||||
38
app/routing/middleware/auth/Role.middleware.js
Normal file
38
app/routing/middleware/auth/Role.middleware.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @module flitter-auth/deploy/routing/middleware/RequireAuth
|
||||
*/
|
||||
|
||||
/**
|
||||
* This middleware is provided by Flitter-auth. It will redirect the user
|
||||
* back to their previous location if the does not have the specified role.
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
class Role {
|
||||
|
||||
/**
|
||||
* Run the middleware's check. If an authenticated session exists and the user has the specified role,
|
||||
* let the request continue. If an authenticated session doesn't exist, write the destination to the
|
||||
* session and redirect the user to the login page. If the role doesn't exist, show a 401.
|
||||
* @param {Express/Request} req - the incoming Express request
|
||||
* @param {Express/Response} res - the corresponding Express response
|
||||
* @param {Function} next - Express handler stack callback. This should be called if the middleware check passed to allow the request to continue.
|
||||
* @param {string} role - Name of the role to require
|
||||
*/
|
||||
test(req, res, next, role){
|
||||
if ( req.session && req.session.auth && (req.session.auth.authenticated === true || req.session.auth.user) ){
|
||||
if ( req.session.auth.user.role && req.session.auth.user.role === role ){
|
||||
next()
|
||||
}
|
||||
else {
|
||||
return _flitter.error(res, 401, {reason: 'Insufficient user permissions.'})
|
||||
}
|
||||
}
|
||||
else {
|
||||
req.session.destination = req.originalUrl
|
||||
return res.redirect('/auth/login')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Role
|
||||
53
app/routing/routers/api/v1.routes.js
Normal file
53
app/routing/routers/api/v1.routes.js
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* v1 Routes
|
||||
* -------------------------------------------------------------
|
||||
* Put some description here!
|
||||
*/
|
||||
const v1 = {
|
||||
|
||||
/*
|
||||
* Define the prefix applied to each of these routes.
|
||||
* For example, if prefix is '/auth':
|
||||
* '/' becomes '/auth'
|
||||
* '/login' becomes '/auth/login'
|
||||
*/
|
||||
prefix: '/api/v1',
|
||||
|
||||
/*
|
||||
* Define middleware that should be applied to all
|
||||
* routes defined in this file. Middleware should be
|
||||
* included using Flitter's global mw() function, but
|
||||
* it can also be added directly using require().
|
||||
*/
|
||||
middleware: [
|
||||
// mw('Middleware Name'),
|
||||
],
|
||||
|
||||
/*
|
||||
* Define GET routes.
|
||||
* These routes are registered as GET methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
get: {
|
||||
// '/': [ controller('Controller_Name').handler_name ],
|
||||
},
|
||||
|
||||
/*
|
||||
* Define POST routes.
|
||||
* These routes are registered as POST methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
post: {
|
||||
'/out/:key': [ _flitter.controller('api:v1').new_out ],
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = v1
|
||||
52
app/routing/routers/auth.routes.js
Normal file
52
app/routing/routers/auth.routes.js
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @module flitter-auth/deploy/routing/routers/auth
|
||||
*/
|
||||
|
||||
/**
|
||||
* These are the route definitions for Flitter-auth.
|
||||
* @type {Object}
|
||||
*/
|
||||
module.exports = exports = {
|
||||
|
||||
/*
|
||||
* Define the prefix applied to each of these routes.
|
||||
* For example, if prefix is '/auth':
|
||||
* '/' becomes '/auth'
|
||||
* '/login' becomes '/auth/login'
|
||||
*/
|
||||
prefix: '/auth',
|
||||
|
||||
/*
|
||||
* Define GET routes.
|
||||
* These routes are registered as GET methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
get: {
|
||||
'/register': [ _flitter.mw('auth:RequireGuest'), _flitter.controller('Auth').register_get ],
|
||||
'/login': [ _flitter.mw('auth:RequireGuest'), _flitter.controller('Auth').login_get ],
|
||||
'/logout': [ _flitter.mw('auth:RequireAuth'), _flitter.controller('Auth').logout ],
|
||||
|
||||
/*
|
||||
* A placeholder dashboard.
|
||||
*/
|
||||
'/dash': [ _flitter.mw('auth:RequireAuth'), _flitter.controller('Auth').dash_get ]
|
||||
},
|
||||
|
||||
/*
|
||||
* Define POST routes.
|
||||
* These routes are registered as POST methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
post: {
|
||||
'/register': [ _flitter.mw('auth:RequireGuest'), _flitter.controller('Auth').register_post ],
|
||||
'/login': [ _flitter.mw('auth:RequireGuest'), _flitter.controller('Auth').login_post ],
|
||||
},
|
||||
}
|
||||
59
app/routing/routers/dash/v1.routes.js
Normal file
59
app/routing/routers/dash/v1.routes.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* v1 Routes
|
||||
* -------------------------------------------------------------
|
||||
* Put some description here!
|
||||
*/
|
||||
const v1 = {
|
||||
|
||||
/*
|
||||
* Define the prefix applied to each of these routes.
|
||||
* For example, if prefix is '/auth':
|
||||
* '/' becomes '/auth'
|
||||
* '/login' becomes '/auth/login'
|
||||
*/
|
||||
prefix: '/dash/v1',
|
||||
|
||||
/*
|
||||
* Define middleware that should be applied to all
|
||||
* routes defined in this file. Middleware should be
|
||||
* included using Flitter's global mw() function, but
|
||||
* it can also be added directly using require().
|
||||
*/
|
||||
middleware: [
|
||||
// mw('Middleware Name'),
|
||||
_flitter.mw('auth:RequireAuth')
|
||||
],
|
||||
|
||||
/*
|
||||
* Define GET routes.
|
||||
* These routes are registered as GET methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
get: {
|
||||
// '/': [ controller('Controller_Name').handler_name ],
|
||||
'/': [ _flitter.controller('dash:v1').main ],
|
||||
|
||||
'/project/new': [ _flitter.controller('dash:v1').new_project_show ],
|
||||
'/project/view/:id': [ _flitter.controller('dash:v1').project_view ],
|
||||
'/out/view/:id': [ _flitter.controller('dash:v1').out_view ],
|
||||
},
|
||||
|
||||
/*
|
||||
* Define POST routes.
|
||||
* These routes are registered as POST methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
post: {
|
||||
'/project/new': [ _flitter.controller('dash:v1').new_project_do ],
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = v1
|
||||
54
app/routing/routers/index.routes.js
Normal file
54
app/routing/routers/index.routes.js
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Index Routes
|
||||
* -------------------------------------------------------------
|
||||
* This is a sample routes file. Routes and their handlers should be
|
||||
* defined here, but no logic should occur.
|
||||
*/
|
||||
const index = {
|
||||
|
||||
/*
|
||||
* Define the prefix applied to each of these routes.
|
||||
* For example, if prefix is '/auth':
|
||||
* '/' becomes '/auth'
|
||||
* '/login' becomes '/auth/login'
|
||||
*/
|
||||
prefix: '/',
|
||||
|
||||
/*
|
||||
* Define middleware that should be applied to all
|
||||
* routes defined in this file. Middleware should be
|
||||
* included using Flitter's global mw() function, but
|
||||
* it can also be added directly using require().
|
||||
*/
|
||||
middleware: [
|
||||
// _flitter.mw('HomeLogger'),
|
||||
],
|
||||
|
||||
/*
|
||||
* Define GET routes.
|
||||
* These routes are registered as GET methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
get: {
|
||||
'/': [ _flitter.controller('Home').welcome ],
|
||||
},
|
||||
|
||||
/*
|
||||
* Define POST routes.
|
||||
* These routes are registered as POST methods.
|
||||
* Handlers for these routes should be specified as
|
||||
* an array of functions that are applied in order.
|
||||
*
|
||||
* mw() calls apply Flitter middleware
|
||||
* controller() calls get methods in Flitter controllers
|
||||
*/
|
||||
post: {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
module.exports = index
|
||||
Reference in New Issue
Block a user