This library is a TypeScript implementation of Policy-Based Access Control (PBAC) inspired by node-pbac. It allows you to evaluate policies and permissions based on defined rules and conditions, making it easy to manage access to resources in your application.
To install the PBAC library, use the following command:
npm install --save @moonlightjs/pbac
First, import the PBAC class and related interfaces:
import PBAC, { PBACPolicy, PBACEvaluateOptionsParams } from '@moonlightjs/pbac';
Next, define your policies:
const examplePolicy: PBACPolicy = {
Version: '1.0.0',
Statement: [
{
Effect: 'Allow',
Action: ['read'],
Resource: ['resource:${user.id}'],
Principal: { user: ['user1', 'user2'] },
Condition: {
'StringEquals': { 'user:id': ['user1', 'user2'] },
},
},
],
};
Create a new PBAC instance with the defined policies:
const pbac = new PBAC(examplePolicy);
Evaluate access based on the given options:
const evaluateOptions: PBACEvaluateOptionsParams = {
action: 'read',
resource: 'resource:user1',
principal: { user: 'user1' },
context: {
user: {
id: 'user1',
},
},
};
const accessGranted = pbac.evaluate(evaluateOptions);
console.log('Access granted:', accessGranted);
This library includes unit tests using Jest. To run the tests, execute the following command:
npm test
This library is a TypeScript adaptation of the node-pbac project. A big thank you to the original author and contributors for their work on the initial implementation.