npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

klen-secure

v1.1.7

Published

a package for securing back end routes

Downloads

17

Readme

klen-secure

klen-secure is an npm module to create customizeable backend route security, free of most specific backend dependencies (does not specifically require express, or postgres 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 users 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 options, 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 PostgresDB), 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 required.

---------- ! 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
  3. create your authenticator instance, with the model on which to authenticate, your authFuncs object and the logViewBool const userAuthenticator = new authMaster(User, authObject, true);
  4. after login, use the checkAuthorizations middleware to attach clearances to your user: router.use(userAuthenticator.checkAuthorizations())
  5. secure your routes! router.get('/ModsOnly', userAuthenticator.authFailLogger('isMod'), (req, res,next) => { res.send('Welcome to the Mod Page!');})