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

node-abac

v0.0.7

Published

Node.js Attributes Based Access Control library

Downloads

38

Readme

node-abac

Node.js Attributes Based Access Control library

npm version npm npm

This library is designed to help implement the concept of Attribute Based Access Control (ABAC) in your Node.js applications using simple JSON policies.

For more information on ABAC consider reading the NIST Specification Guide.

Inspired by php-abac

Installation

Install the latest stable release with the npm command-line tool:

$ npm install node-abac

Configuration

Import the library

const NodeAbac = require('node-abac');

Policies

Policies can be read from one or more files. Both JSON and YAML structures are supported and can be mixed if desired.

const Abac = new NodeAbac('path/to/policy.json');
const Abac = new NodeAbac(['path/to/policy.json', 'another/path/policy2.yml']);

Or passed directly in as a JavaScript object

const myPolicy = {
    attributes: {
        user: {
            hasDrivingLicense: "Possesses driving license"
        }
    },
    rules: {
        "can-drive": {
            attributes: {
                "user.hasDrivingLicense": {
                    comparison_type: "boolean",
                    comparison: "booleanAnd",
                    value: true
                }
            }
        }
    }
};

const Abac = new NodeAbac(myPolicy);

Error messages

Descriptive enforcement errors can be toggled on or off globally using a second parameter to NodeAbac. By default they're disabled so failed enforcements will simply return false. For example:

{
  "msg": "can-be-admin DENIED",
  "errors": [
    {
      "msg": "numeric value 'Times banned' failed to pass isLesserThanEqualTo",
      "expected": 1,
      "actual": 2
    },
    {
      "msg": "array value 'Origin' failed to pass isIn",
      "expected": "FR|DE|IT|L|GB|P|ES|NL|B",
      "actual": "PL"
    }
  ]
}

To enable globally

const Abac = new NodeAbac(myPolicy, true);

Alternatively this global flag can be overwritten on individual enforcement calls by using the fourth parameter verbose_error.

const Abac = new NodeAbac(myPolicy); // verbose_errors globally off
const mySubject = {};
const myResource = {};

...

const result = Abac.enforce('my-rule', mySubject, myResource, true); // verbose_error on for this call only

Usage

This library simply consists of two functions to integrate ABAC into your application.

getRuleAttributes

getRuleAttributes is used to inspect a rule for its required objects and their fields. The response is typically used to make further requests to a Policy Information Point (PIP).

Given the policy with a rule can-be-admin-of-group: for a user to become admin of a group they must me active, over 21 years old, been banned no more than once and be in the group that they're attempting to administer.

{
    "attributes": {
        "user": {
            "active": "Active",
            "banCount": "Times banned",
            "dob": "Date of birth",
            "group": "Group ID"
        },
        "group": {
            "id": "Group ID"
        }
    },
    "rules": {
        "can-be-admin-of-group": {
            "attributes": {
                "user.active": {
                    "comparison_type": "boolean",
                    "comparison": "boolAnd",
                    "value": true
                },
                "user.dob": {
                    "comparison_type": "datetime",
                    "comparison": "isLessRecentThan",
                    "value": "-21Y"
                },
                "user.banCount": {
                    "comparison_type": "numeric",
                    "comparison": "isLesserThanEqualTo",
                    "value": 1
                },
                "user.group": {
                    "comparison_target": "group",
                    "comparison_type": "numeric",
                    "comparison": "isStrictlyEqual",
                    "field": "id"
                }
            }
        }
    }
}

Calling getRuleAttributes('can-be-admin-of-group') will return our required fields to fulfill the rule.

{
    "user": [
        "active",
        "dob",
        "banCount",
        "group"
    ],
    "group": [
        "id"
    ]
}

This tells us our subject is user and the resource is group.

enforce

enforce is the point at which a permit or deny decision is made. It must be called with a rule name and subject and can optionally accept a resource that the rule is protecting.

To continue the above example consider the scenario where we have acquired our attribute data and want to check the user can administer the group.

let subject = {
    user: {
        active: true,
        dob: '1991-05-12',
        banCount: 0,
        group: 12
    }
};
let resource = {
    group: {
        id: 12
    }
};

const permit = Abac.enforce('can-be-admin-of-group', subject, resource); // returns true

subject = {
    user: {
        active: true,
        dob: '2006-05-12', // too young
        banCount: 4, // banned too many times
        group: 12
    }
};
resource = {
    group: {
        id: 12
    }
};

const deny = Abac.enforce('can-be-admin-of-group', subject, resource); // returns false || error message

Comparisons

For more information on comparison usage please refer to the dedicated comparisons documentation.

Coming Soon

  • Additional documentation and examples
  • Testing
  • Additional datatype operations