This library is designed to help implement the concept of Attribute Based Access Control (ABAC) in your Node.js applications
using simple JSON
or YAML
policies.
For more information on ABAC consider reading the NIST Specification Guide.
Inspired by php-abac
Install the latest stable release with the npm command-line tool:
$ npm install node-abac
Import the library
const NodeAbac = require('node-abac');
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": {
/* NOTE: 'comparison_type' value must be lowercase e.g. 'boolean' vs 'isStrictlyEqual: Boolean' */
comparison_type: "boolean",
comparison: "booleanAnd",
value: true
}
}
}
}
};
const Abac = new NodeAbac(myPolicy);
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
This library simply consists of two functions to integrate ABAC into your application.
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
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
For more information on comparison usage please refer to the dedicated comparisons documentation.
- Additional documentation and examples
- Testing
- Additional datatype operations