1.1.0 • Published 3 years ago

express-iam v1.1.0

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

express-iam

npm Build Status codecov GitHub npm

Express Middleware for Identity and Access Management, this library enable you to manage the requests made to your express server.

Installation

$ npm install express-iam --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.
pathStringThe 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 express-iam what action will be applied on the permission, deny or allow.
[
  {
    "access_group":"admin",
    "permissions":[
      {
        "path":"*",
        "methods":"*",
        "action":"allow"
      }
    ]
  },
  {
    "access_group":"guest",
    "permissions":[
      {
        "path":"/foo",
        "methods":[
          "POST"
        ],
        "action":"allow"
      },
      {
        "path":"/foo2",
        "methods":[
          "POST",
          "UPDATE"
        ],
        "action":"deny"
      }
    ]
  }
]

config[type: function]

This methods loads the configuration to express-iam.

OptionDefaultDescription
access_controlArray \| FunctionThe access control array or function.
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.
const app = require('express');
const path = require('path');
const fs = require('fs');
const expressIAM = require('express-iam');

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

// Using access control from array
const  accessControlArray = [
  {
    "group":"admin",
    "permissions":[
      {
        "path":"*",
        "methods":"*",
        "action":"allow"
      }
    ]
  }
];

expressIAM.config({  
access_control:  accessControlArray,  
prefix:  '/api/v1'  });

authorize[type: function]

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

In an express based application:

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

app.use(expressIAM.authorize());

unless[type: function]

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

OptionTypeDescription
pathsString\|ArrayString or an array of string containing the path to be skipped. It also could be an array of object which is path 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, path will match against req.url instead of req.originalUrl. Please refer to express for the difference between req.url and req.originalUrl.
const express = require('express');
const app = express();

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

License

MIT