Skip to content

LeighSteiner/klen-secure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 

Repository files navigation

klen-secure

klen-secure is an npm module to create customizable backend route security, free of most specific backend dependencies (does not specifically require Express, PostgreSQL, or Sequelize, for example). It is dependent on Passport, in order to make use of the request user object after log in.

Your authorization functions are kept as private properties scoped so that the main functions of the module can use them, but they cannot be overwritten or manipulated elsewhere in your application.

The module primarily produces two pieces of middleware. The first, checkAuthorizations, cycles through all of the functions on your authObject and attaches a "clearance" property to the request's user, which has a array of all clearance levels to which a particular user has access.

The second, authFailLogger, has two purposes. It should be passed as middleware into any route (see Usage examples below) which requires a particular clearance level to access. authFailLogger handles both preventing and allowing access appropriately, based on a user’s clearances, and creates a log of userIds which make any particular bad request, allowing you to see which users are attempting to access protected areas of your application.

There is also a secondary function, singleRouteSecure. With checkAuthorizations, all of your authFunctions (which are likely to be DB queries, and therefore slow) are run once, and only once, and thereafter, only the clearances array is checked, which is much more efficient. However, if your application has very cases where it will be checking clearance level, using the singleRoute middleware might be a superior option, as it only queries the DB at the absolute last possible moment.

Set up

The authMaster (the export from klen-secure) takes a reference to a model on which to run the authentication checks, an object containing your custom authorization functions (which defaults, for illustration purposes, to a series of Sequelize calls to a PostgreSQL DB), and a boolean (default set to false) which allows access to the getAuthFailLog function (as opposed to the viewAuthFailLog). Once required and initialized, you can secure as many of your routes as you need.

Usage

  1. npm install --save klen-secure
  2. In the appropriate file:
const authMaster = require('klen-secure')();
// You MUST invoke authMaster in order to access its functionality
  1. Create your authenticator instance, with the model on which to authenticate, your authFuncs object, and the logViewBool:
const userAuthenticator = new authMaster(User, authObject, true);
  1. After login, use the checkAuthorizations middleware to attach clearances to your user:
router.use(userAuthenticator.checkAuthorizations());
  1. Secure your routes!
router.get('/ModsOnly', userAuthenticator.authFailLogger('isMod'), (req, res,next) => {
  res.send('Welcome to the Mod Page!');
});

About

npm back end route security using node and express

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published