node-abac
Node.js Attributes Based Access Control library
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 = ;
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 = 'path/to/policy.json';const Abac = '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 = 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:
To enable globally
const Abac = myPolicy true;
Alternatively this global flag can be overwritten on individual enforcement calls by using the fourth parameter verbose_error
.
const Abac = myPolicy; // verbose_errors globally offconst mySubject = {};const myResource = {}; ... const result = Abac; // 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.
Calling getRuleAttributes('can-be-admin-of-group')
will return our required fields to fulfill the rule.
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; // 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; // 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