npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-rbac-kit

v1.0.0

Published

Enterprise RBAC Authorization Engine for Node.js

Readme

node-rbac-kit

Enterprise-Grade Role-Based Access Control (RBAC) Engine for Node.js.

node-rbac-kit is a fast, framework-agnostic, TypeScript-first authorization library. It supports hierarchical roles, dynamic context rules, wildcards, multi-tenant SaaS applications, validation, graph export, and caching (In-Memory and Redis) with native adapters for Express and NestJS.


Features

  • Core RBAC: Declarative role and permission maps.
  • Hierarchical Inheritance: Graph-based role resolution with cycle detection.
  • Wildcard Matchers: Glob-like namespace permissions (e.g., loan.* matches loan.approve and loan.create).
  • Dynamic Rules (ABAC): Context-aware authorization (e.g., loan officer can approve only if user and loan branches match).
  • Multi-Tenant SaaS: Complete role, rule, and cache isolation between tenants out of the box.
  • Diagnostics & Explanations: Returns detailed denial reasons, required roles, and missing permissions instead of simple booleans.
  • High-Performance Cache: Memory and Redis caching adapters with automatic invalidation.
  • Visual Graph Exporter: Generate permission graphs/trees for auditing or dashboard UI.
  • Diagnostic Validator: Detect duplicate permissions, unused permissions, circular paths, and missing parent roles.
  • Framework Integrations: Custom NestJS Decorators/Guards and Express middleware.

Installation

npm install node-rbac-kit

Quick Start

import { PermissionEngine, MemoryCacheAdapter } from 'node-rbac-kit';

// 1. Initialize Engine
const engine = new PermissionEngine({
  cache: new MemoryCacheAdapter(300), // cache results for 5 mins
  auditLogger: (event) => {
    console.log(`[RBAC AUDIT] ${event.type} - User: ${event.userId} - Action: ${event.action} - Decision: ${event.decision}`);
  }
});

// 2. Define Roles and Inheritance
engine.defineRole({
  name: 'Officer',
  permissions: ['loan.read']
});

engine.defineRole({
  name: 'Manager',
  inherits: ['Officer'], // Inherits loan.read
  permissions: ['loan.update', 'loan.approve']
});

engine.defineRole({
  name: 'Admin',
  inherits: ['Manager'], // Inherits Officer + Manager permissions
  permissions: ['loan.*', 'report.download'] // Wildcards supported
});

// 3. Define Dynamic context rule
engine.defineRule({
  action: 'loan.approve',
  check: (user, context) => {
    // User can approve only if they belong to the loan's branch
    return user.branchId === context.branchId;
  }
});

// 4. Perform Checks
const checkResult = await engine.check({
  user: { id: 'usr-1', roles: ['Officer'], branchId: 'north-branch' },
  action: 'loan.approve',
  context: { branchId: 'north-branch' }
});

console.log(checkResult.allowed); // false (Officer lacks static permission to approve loans)
console.log(checkResult.explanation);
/*
Output:
{
  decision: 'DENY',
  reason: 'User is missing required permission: "loan.approve".',
  missingPermission: 'loan.approve',
  requiredRoles: ['Manager', 'Admin'],
  currentRoles: ['Officer']
}
*/

Advanced Capabilities

Multi-Tenant SaaS Isolation

Scope roles, permissions, and caches dynamically per tenant.

const engine = new PermissionEngine({
  tenantResolver: (context) => context.tenantId
});

// Define same role name with different structures for different tenants
engine.defineRole({ name: 'Admin', permissions: ['loan.create'] }, 'TenantA');
engine.defineRole({ name: 'Admin', permissions: ['loan.read'] }, 'TenantB');

Redis Caching with Failure Resiliency

If Redis goes down, node-rbac-kit continues to resolve authorization requests dynamically without throwing application-breaking exceptions.

import Redis from 'ioredis';
import { PermissionEngine, RedisCacheAdapter } from 'node-rbac-kit';

const redisClient = new Redis('redis://localhost:6379');
const cache = new RedisCacheAdapter(redisClient, {
  defaultTtlSeconds: 600,
  prefix: 'my-app-rbac'
});

const engine = new PermissionEngine({ cache });

Visual Graph Generation

Exposes structural JSON trees representing your hierarchy—perfect for building visual admin panels.

const graph = engine.getPermissionGraph();
console.log(JSON.stringify(graph, null, 2));

Diagnostics Validator

Scans your system for logical configuration errors.

const result = engine.validate(['loan.read', 'loan.write', 'report.download']);
console.log(result.isValid); // true or false
console.log(result.issues);
// Detects: Circular hierarchies, duplicate permissions, unused permissions, missing parents.

Integrations

Express Middleware

Attach permission checks directly to your routes:

import express from 'express';
import { requirePermission } from 'node-rbac-kit';

const app = express();

app.get('/loans/:id', 
  requirePermission(engine, 'loan.read', {
    getUser: (req) => req.session.user, // Customize user extraction
    getContext: (req) => ({ tenantId: req.headers['x-tenant-id'] }) // Custom context
  }),
  (req, res) => {
    res.send('Loan Details');
  }
);

NestJS Decorators & Guard

Secure controller endpoints using declarative metadata:

import { Controller, Get, UseGuards } from '@nestjs/common';
import { RBACGuard, Permission, HasRole } from 'node-rbac-kit';

@Controller('loans')
@UseGuards(RBACGuard)
export class LoanController {

  @Get()
  @Permission('loan.read')
  findAll() {
    return 'Loans list';
  }

  @Get('admin')
  @HasRole('Admin')
  adminPanel() {
    return 'Admin dashboard';
  }
}

Register the engine as a NestJS provider:

import { Module } from '@nestjs/common';
import { PermissionEngine, RBACGuard } from 'node-rbac-kit';

@Module({
  providers: [
    {
      provide: PermissionEngine,
      useValue: new PermissionEngine()
    },
    {
      provide: 'NESTJS_RBAC_OPTIONS',
      useValue: {
        getUser: (req) => req.user,
        getContext: (req) => ({ tenantId: req.headers['x-tenant-id'] })
      }
    },
    RBACGuard
  ],
  exports: [PermissionEngine]
})
export class SecurityModule {}

License

Custom Non-Commercial (See LICENSE file for details)