Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@minddocdev/accesscontrol
Advanced tools
Many Role Based Access Control (RBAC) implementations differ, but the basics is widely adopted since it simulates real life role (job) assignments. But while data is getting more and more complex; you need to define policies on resources, subjects or even environments, this is called Attribute Based Access Control (ABAC).
ac.can(role).create(resource)
yarn install
yarn build
yarn test
yarn add @minddocdev/accesscontrol
To publish a new version of the package, firstly bump the version in the package.json
file,
then cut a new release on Github. This
will automatically initiate the publish
Github Action workflow and publish a new version to
Github Packages
import { AccessControl } from 'accesscontrol';
Define roles and grants one by one.
const ac = new AccessControl();
ac.grant('user') // define new or modify existing role. also takes an array.
.createOwn('video') // equivalent to .createOwn('video', ['*'])
.deleteOwn('video')
.readAny('video')
.grant('admin') // switch to another role without breaking the chain
.extend('user') // inherit role capabilities. also takes an array
.updateAny('video', ['title']) // explicitly defined attributes
.deleteAny('video');
const permission = ac.can('user').createOwn('video');
console.log(permission.granted); // —> true
console.log(permission.attributes); // —> ['*'] (all attributes)
permission = ac.can('admin').updateAny('video');
console.log(permission.granted); // —> true
console.log(permission.attributes); // —> ['title']
You can create/define roles simply by calling .grant(<role>)
or .deny(<role>)
methods on an AccessControl
instance.
// user role inherits viewer role permissions
ac.grant('user').extend('viewer');
// admin role inherits both user and editor role permissions
ac.grant('admin').extend(['user', 'editor']);
// both admin and superadmin roles inherit moderator permissions
ac.grant(['admin', 'superadmin']).extend('moderator');
// case #1
ac.grant('admin')
.extend('user') // assuming user role already exists
.grant('user')
.createOwn('video');
// case #2
ac.grant('user')
.createOwn('video')
.grant('admin')
.extend('user');
// below results the same for both cases
const permission = ac.can('admin').createOwn('video');
console.log(permission.granted); // true
Notes on inheritance:
ac.grant('user').extend('admin').grant('admin').extend('user')
will throw.ac.grant('baseRole').grant('role').extend('baseRole')
[CRUD][crud] operations are the actions you can perform on a resource. There are two action-attributes which define the possession of the resource: own and any.
For example, an admin
role can create
, read
, update
or delete
(CRUD) any account
resource. But a user
role might only read
or update
its own account
resource.
Action | Possession | |
---|---|---|
Create Read Update Delete | Own | The C|R|U|D action is (or not) to be performed on own resource(s) of the current subject. |
Any | The C|R|U|D action is (or not) to be performed on any resource(s); including own. |
ac.grant('role').readOwn('resource');
ac.deny('role').deleteAny('resource');
Note that own requires you to also check for the actual possession.
You can call .can(<role>).<action>(<resource>)
on an AccessControl
instance to check for granted permissions for a specific resource and action.
const permission = ac.can('user').readOwn('account');
permission.granted; // true
You can pass the grants directly to the AccessControl
constructor.
It accepts either an Object
:
// This is actually how the grants are maintained internally.
let grantsObject = {
admin: {
video: {
'create:any': ['*'],
'read:any': ['*'],
'update:any': ['*'],
'delete:any': ['*'],
},
},
user: {
video: {
'create:own': ['*'],
'read:own': ['*'],
'update:own': ['*'],
'delete:own': ['*'],
},
},
};
const ac = new AccessControl(grantsObject);
... or an Array
(useful when fetched from a database):
// grant list fetched from DB (to be converted to a valid grants object, internally)
let grantList = [
{ role: 'admin', resource: 'video', action: 'create:any', attributes: '*' },
{ role: 'admin', resource: 'video', action: 'read:any', attributes: '*' },
{ role: 'admin', resource: 'video', action: 'update:any', attributes: '*' },
{ role: 'admin', resource: 'video', action: 'delete:any', attributes: '*' },
{ role: 'user', resource: 'video', action: 'create:own', attributes: '*' },
{ role: 'user', resource: 'video', action: 'read:any', attributes: '*' },
{ role: 'user', resource: 'video', action: 'update:own', attributes: '*' },
{ role: 'user', resource: 'video', action: 'delete:own', attributes: '*' },
];
const ac = new AccessControl(grantList);
You can set grants any time...
const ac = new AccessControl();
ac.setGrants(grantsObject);
console.log(ac.getGrants());
Never commit directly to master, create a new branch and submit a pull request.
FAQs
Role and Attribute based Access Control
The npm package @minddocdev/accesscontrol receives a total of 0 weekly downloads. As such, @minddocdev/accesscontrol popularity was classified as not popular.
We found that @minddocdev/accesscontrol demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.