MCPcopy Index your code
hub / github.com/apache/casbin-node-casbin

github.com/apache/casbin-node-casbin @v5.51.1

repository ↗ · DeepWiki ↗ · release v5.51.1 ↗ · + Follow
527 symbols 1,240 edges 63 files 204 documented · 39% updated 10d agov5.51.1 · 2026-06-25★ 2,9034 open issues
README

Node-Casbin

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

```shell script

NPM

npm install casbin --save

Yarn

yarn add casbin


## Get started

New a `node-casbin` enforcer with a model file and a policy file, see [Model](#official-model) section for details:

```node.js
// For Node.js:
const { newEnforcer } = require('casbin');
// For browser:
// import { newEnforcer } from '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.

  • https://github.com/apache/casbin-node-casbin/issues
  • https://discord.gg/S5UjpzGZjN

Extension points exported contracts — how you extend this code

Adapter (Interface)
(no doc) [5 implementers]
src/persist/adapter.ts
ConfigInterface (Interface)
(no doc) [4 implementers]
src/config.ts
RoleManager (Interface)
(no doc) [2 implementers]
src/rbac/roleManager.ts
Logger (Interface)
(no doc) [2 implementers]
src/log/logger.ts
EffectorStream (Interface)
(no doc) [2 implementers]
src/effect/effectorStream.ts
SpyAdapter (Interface)
(no doc)
test/managementAPI_self.test.ts
IPolicyParser (Interface)
(no doc) [5 implementers]
src/persist/helper.ts
Effector (Interface)
(no doc) [2 implementers]
src/effect/effector.ts

Core symbols most depended-on inside this repo

newEnforcer
called by 106
src/enforcer.ts
get
called by 104
src/config.ts
addDef
called by 90
src/model/model.ts
enforceSync
called by 64
src/coreEnforcer.ts
hasPolicy
called by 53
src/model/model.ts
toString
called by 48
src/rbac/defaultRoleManager.ts
set
called by 30
src/config.ts
enforce
called by 28
src/coreEnforcer.ts

Shape

Method 370
Function 77
Class 62
Interface 14
Enum 4

Languages

TypeScript100%

Modules by API surface

src/managementEnforcer.ts58 symbols
src/syncedEnforcer.ts46 symbols
src/coreEnforcer.ts45 symbols
src/model/model.ts37 symbols
src/rbac/defaultRoleManager.ts33 symbols
src/enforcer.ts33 symbols
src/config.ts24 symbols
src/util/builtinOperators.ts21 symbols
src/internalEnforcer.ts19 symbols
src/util/util.ts18 symbols
src/persist/helper.ts16 symbols
test/model.test.ts14 symbols

Dependencies from manifests, versioned

@casbin/expression-eval5.3.0 · 1×
@semantic-release/commit-analyzer8.0.1 · 1×
@semantic-release/npm7.1.3 · 1×
@semantic-release/release-notes-generator9.0.3 · 1×
@types/jest26.0.20 · 1×
@types/node10.5.3 · 1×
@types/picomatch2.2.2 · 1×
@typescript-eslint/eslint-plugin4.17.0 · 1×
await-lock2.0.1 · 1×
buffer6.0.3 · 1×

For agents

$ claude mcp add casbin-node-casbin \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact