1.0.1 • Published 2 years ago

node-express-rbac v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

node-express-rbac

npm workflow codecov GitHub npm

Express Middleware for Role Based Access Control, this library enable you to manage the requests made to your express server.

Installation

$ npm install node-express-rbac --save

Use

First step is to create your access control, it could be stored in a database, file or a simple array, the structure should follow the below example.

Definition of Access Control

OptionDefaultDescription
access_groupStringThe access group with name.
permissionsArrayArray of permissions that defined to an access group, to allow or deny.
resourceStringThe route that the permission will be applied. Use * to include all routes or sub-routes. e.g. /foo/*.
methodsString \| ArrayThe methods that the permission will be applied. Use * to include all methods.
actionStringThis property tells node-express-rbac what action will be applied on the permission, deny or allow.

Example

[
  {
    "access_group":"admin",
    "permissions":[
      {
        "resource":"*",
        "methods":"*",
        "action":"allow"
      }
    ]
  },
  {
    "access_group":"guest",
    "permissions":[
      {
        "resource":"/foo",
        "methods":["POST"],
        "action":"deny"
      },
    ]
  }
]

config[type: function]

This methods loads the configuration to node-express-rbac.

OptionDefaultDescription
access_controlArrayThe access control array.
access_group_search_pathStringThe path in request object where access group resides.
custom_messageStringThe custom message when user is denied.
default_access_groupStringThe default access_group to be assigned if no role defined.
prefixStringThe base URL of your api. e.g. api/v1.

Example

const app = require('express');
const path = require('path');
const fs = require('fs');
const expressRBAC = require('node-express-rbac');

// Using access control from file
const accessControlFile =  fs.readFileSync(
  path.join(__dirname,  './access-control/access-control.json'));
  
expressRBAC.config({
	prefix:  '/api/v1',
	access_control:  accessControlFile,
});

authorize[type: function]

This methods is the middleware to node-express-rbac manage your requests.

In an express based application:

Example

const express = require('express');
const app = express();

app.use(expressRBAC.authorize());

unless[type: function]

By default, node-express-rbac will block any route that does not have access control defined. This method allows you to create exceptions for routes that did not use node-express-rbac.

OptionTypeDescription
resourcesString\|ArrayString or an array of string containing the resource to be skipped. It also could be an array of object which is resource and methods key-pairs.
methodsString\|ArrayString or an array of string containing the methods to be skipped.
useOriginalUrlBooleanIt could be true or false, default is true. if false, resource will match against req.url instead of req.originalUrl. Please refer to express for the difference between req.url and req.originalUrl.

Example

const express = require('express');
const app = express();

app.use(expressRBAC.authorize().unless({ resources: ['/foo'] }));

License

MIT