Skip to content

About

An authorization library that supports access control models like ACL, RBAC, ABAC in Node.js and Browser

Topics

Resources

Code of conduct

Security policy

Stars

2.9k stars

Watchers

19 watching

Forks

Repository files navigation

Apache Casbin (Incubating) — Node-Casbin

Apache Casbin (Incubating) is an effort undergoing incubation at the Apache Software Foundation (ASF), sponsored by the Apache Incubator PMC.

Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects.

While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Earlier package publications form the legacy Casbin community release history. Their presence in a registry does not establish Apache release approval. Apache releases require project and Incubator approval of the exact source distribution.

GitHub Actions Coverage Status Release NPM version NPM download install size Discord

News: still worry about how to write the correct node-casbin policy? Casbin online editor is coming to help!

casbin Logo

node-casbin is a powerful and efficient open-source access control library for Node.JS projects. It provides support for enforcing authorization based on various access control models.

All the languages supported by Casbin:

golang java nodejs php
Casbin jCasbin node-Casbin PHP-Casbin
production-ready production-ready production-ready production-ready
python dotnet c++ rust
PyCasbin Casbin.NET Casbin-CPP Casbin-RS
production-ready production-ready beta-test production-ready

Documentation

https://casbin.apache.org/docs/overview

Installation

# NPM
npm install apache-casbin --save

# Yarn
yarn add apache-casbin

Get started

New a node-casbin enforcer with a model file and a policy file, see Model section for details:

// For Node.js:
const { newEnforcer } = require('apache-casbin');
// For browser:
// import { newEnforcer } from 'apache-casbin';

const enforcer = await newEnforcer('basic_model.conf', 'basic_policy.csv');

Note: you can also initialize an enforcer with policy in DB instead of file, see Persistence section for details.

Add an enforcement hook into your code right before the access happens:

const sub = 'alice'; // the user that wants to access a resource.
const obj = 'data1'; // the resource that is going to be accessed.
const act = 'read'; // the operation that the user performs on the resource.

// Async:
const res = await enforcer.enforce(sub, obj, act);
// Sync:
// const res = enforcer.enforceSync(sub, obj, act);

if (res) {
  // permit alice to read data1
} else {
  // deny the request, show an error
}

Besides the static policy file, node-casbin also provides API for permission management at run-time. For example, You can get all the roles assigned to a user as below:

const roles = await enforcer.getRolesForUser('alice');

See Policy management APIs for more usage.

Policy management

Casbin provides two sets of APIs to manage permissions:

  • Management API: the primitive API that provides full support for Casbin policy management.
  • RBAC API: a more friendly API for RBAC. This API is a subset of Management API. The RBAC users could use this API to simplify the code.

Official Model

https://casbin.apache.org/docs/supported-models

Policy persistence

https://casbin.apache.org/docs/adapters

Policy consistence between multiple nodes

https://casbin.apache.org/docs/watchers

Role manager

https://casbin.apache.org/docs/role-managers

Contributors

This project exists thanks to all the people who contribute.

Star History

Star History Chart

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact us. PR is welcomed.

Runtime requirements

The runtime dependency supports Node.js 20 or 22 and later. Build and development tools require a current Node.js 20 LTS release or later.

Package-name migration

The next approved distribution uses apache-casbin in place of the historical npm package casbin. The API is unchanged by the name migration. After the approved package is published, update dependency declarations and imports to apache-casbin, regenerate your lockfile and check that adapters use the same core package instance. Existing casbin releases remain available; updating that old dependency does not automatically switch packages. Do not install both core identities in one application. Candidates are for review, not installation from npm.

About

An authorization library that supports access control models like ACL, RBAC, ABAC in Node.js and Browser

Topics

Resources

Code of conduct

Security policy

Stars

2.9k stars

Watchers

19 watching

Forks

Releases

Packages

Used by

Contributors

Languages