Giter Club home page Giter Club logo

acl-sentinel's Introduction

acl-sentinel

Simple and configurable ACL for Node.js with multi roles

How to use

  1. In your auth method please add to JWT token:
  • iss - this is a string identifier for the version of API or rules or any else
  • roles[] - this is an array with user roles, only one if it happens like that
                let payload = {
                  id: userId,
                  iat: atNow,
                  exp: expire,
                  iss: 'v1',
                  userRole: ["admin", "user"]
              };

Here is we save in JWT for userId access for rule version v1 with roles admin or user.

  1. In you auth middleware, please import
const acl = require('./acl');
const rules = require('./acl-rules');

and after validation JTW token, please add some like that

        if (!acl.isAllow(req,"v1",rules)){
            res.status(401).json({ message: 'Not authorized', status: false  });
            return;
        }
  1. Create a acl-rules.json file, (of course you can add it as an object or import thru environment). Example:
{
  "v1": {
    "allowed": {
      "/user": {
        "get": {
          "roles": ["admin", "user"]
        }
      },
      "/users/:userId": {
        "put": {
          "roles": ["admin"]
        }
      },
      "*": {
        "get": {
          "roles": ["admin", "user", "guest"]
        },
        "post": {
          "roles": ["admin"]
        },
        "*": {
          "roles": ["admin"]
        }
      }
    }
  }
}

Where * is mean any of.

How it works

We are trying to find rules for the route. If we have no then get a default route. After trying to find a rule for the method. If we have no then get a default method. If no rule is found then return false.

So now we try to find if any roles of user have in the rules. If we have it then returns true if not return false.

acl-sentinel's People

Contributors

hooke72 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.