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

cabr

v0.0.8

Published

A module for handling RBAC-A in an express environment.

Downloads

3

Readme

cabr

Known Vulnerabilities

A module for handling RBAC-A in an express environment.

The CABR class provides rbac-a support for express/connect applications. By configuring a route to permission mapping, the request is intercepted based on the required permissions. Attributes defined in the RBAC service will be called before and after all middleware to perform further role validation or request/response filtering.

Documentation

The API documentation can be found at the github pages.

Installation

$ npm install --save cabr

Example usage

const express = require('express');
const rbac = require('rbac-a');
const CABR = require('cabr');
const app = express();

// init the rbac instance ...
const rbac = ...

const routes = {
	// every route, every HTTP method needs the awesome permission
	'.*': 'awesome',

	// the funky route, every HTTP method needs the 'awesome', yolo' and 'funky' permission
	'^\\/funky$': ['yolo', 'funky'],

	// every route, every HEAD request needs the 'clever' and 'smart' permission
	// plus the 'awesome' permission
	'.*': {HEAD: ['clever', 'smart']}, // or 'clever && smart'

	// every route, every COPY request needs the either the 'clever' or 'smart' permission
	// plus the 'awesome' permission
	'.*': {COPY: 'clever || smart']},

	// ALL HTTP methods for '/pets' will be checked with the 'pets.read'
	// permission and 'awesome' permissions
	'^\\/pets$': 'pets.read',

	// Custom config for '/cats', different HTTP methods
	// will apply different permissions
	'^\\/pets\\/cats$': {GET: 'pets.read', POST: 'cats.create', DELETE: ['pets.create', 'pets.delete']}
};

// init the cabr instance
const cabr = new CABR(rbac, {routes});


// use a custom user provider
const get = (req) => Promise.resolve(req.user);
cabr = new CABR(rbac, {routes, userProvider: {get}});

// register the express app - all request will be validated by
// the permissions defined in the route config
cabr.registerApp(app);

A request/response loop

A request is first validated against all matching permissions and attributes. To get the roles of the specific user, the options.userProvider.get method will be called with the current request object to get the identifier the registered rbac.provider can be queried with to get the role information for a user. Whut? Example:

// in the rbac mapping
{
	...
	 "users": {
		"1": ["writer"],
		"2": ["admin"]
	}
}

// assume this returns 1 or 2
const userProvider = req => req.user._id;

Of course you can use any other logic in your providers. You might also return a promise, resolving the username. If any permission validation fails, the options.unauthorizedHandler middleware will be called with the failing permission attached to the request object as rbacFailed. The registered attribute functions of the role will be called with the user/userId, the user role and an object consisting of the keys

  • req - the current request object, req.cabr is set to true

  • res - the current response object, res.cabr is undefined

  • permissions - the permissions applied for this route

    If any additional params are passed as an object to the guard middleware, these parameters will be available in the attribute validation function as well.

If any attribute function returns or resolves to a falsy value, the the options.unauthorizedHandler is called with the rbacFailed property of the request object set to the failing attribute name.

After that, all registered middlewares are applied. Then, all attribute functions of a role are called again with the user/userId, the user role and an object consisting of the keys

  • req - the current request object, req.cabr is undefined

  • res - the current response object, res.cabr is set to true

  • permissions - the permissions applied for this route

  • body - the response body, which may be mutated/filtered by the attribute functions

    Note that you should not dereference the request body, since this may cause errors. Note that this does only work for json responses, and if no response has been send already.

If any attribute validation fails, the options.unauthorizedHandler middleware will be called, with an error handler passed as the next function. Otherwise the mutated json response is send.