Skip to content

IsaiahTek/AuthZ

Repository files navigation

AuthZ β€” The Multi-Flavor Authorization SDK

npm version CI status MIT License TypeScript Dual format

A production-grade, highly optimized authorization framework for Node.js.
Combines the power of RBAC (Role-Based Access Control) and PBAC (Policy-Based Access Control)
into a unified engine with sub-microsecond evaluation latency.


✨ Core Principles

  1. Performance by Default β€” Static role resolution (O(1)) and indexed policy evaluation.
  2. Framework Freedom β€” Core logic is fully isolated; adapters for Express, Fastify, and NestJS provide native integration without coupling.
  3. Audit First β€” Built-in lifecycle hooks (onPreAuth / onPostAuth) for real-time security logging.
  4. Developer Experience β€” Fluent policy builders, deep debug mode, and rich TypeScript inference.

πŸ“¦ Packages

Package Purpose Version
@vynelix/authz-core Core Engine β€” RBAC + PBAC logic npm
@vynelix/authz-express Middleware for Express.js npm
@vynelix/authz-fastify Plugin + Decorator for Fastify npm
@vynelix/authz-nestjs Module + Guard for NestJS npm
@vynelix/authz-node Vanilla Node.js wrapper npm

πŸš€ Quick Start

1. Install

# Core (required)
npm install @vynelix/authz-core

# Pick your framework adapter
npm install @vynelix/authz-express   # Express
npm install @vynelix/authz-fastify   # Fastify
npm install @vynelix/authz-nestjs    # NestJS
npm install @vynelix/authz-node      # Vanilla Node.js / Lambda

2. Create your engine

import { createAuthz, policy } from '@vynelix/authz-core';

const authz = createAuthz({
  // RBAC β€” hierarchical roles resolved at build time (O(1) lookups)
  roles: {
    guest:  { can: ['post.read'] },
    user:   { inherits: ['guest'], can: ['post.create'] },
    editor: { inherits: ['user'],  can: ['post.update'] },
    admin:  { can: ['*'] },
  },

  // PBAC β€” fine-grained policies with function or JSON conditions
  policies: [
    policy('post.update')
      .on('post')
      .when((ctx) => ctx.user.id === ctx.resource?.ownerId) // only own posts
      .build(),

    policy('post.delete')
      .deny()
      .when((ctx) => ctx.resource?.locked === true) // hard lock even for admin
      .build(),
  ],
});

3. Authorize a request

const allowed = await authz.can({
  user:     { id: 'usr_1', roles: ['editor'] },
  action:   'post.update',
  resource: { type: 'post', id: 'p42', ownerId: 'usr_1' },
});
// => true (editor role + user owns the post)

4. Use with your framework

Express

import { authorize } from '@vynelix/authz-express';

app.put('/posts/:id',
  authorize(authz, {
    action:   'post.update',
    resource: (req) => ({ type: 'post', id: req.params.id, ownerId: req.post.ownerId }),
  }),
  (req, res) => res.json({ ok: true })
);

Fastify

import { fastifyAuthz } from '@vynelix/authz-fastify';

fastify.register(fastifyAuthz, { engine: authz });

fastify.put('/posts/:id', {
  preHandler: fastify.authorize({ action: 'post.update' })
}, handler);

NestJS

@Module({ imports: [AuthzModule.forRoot({ engine: authz })] })
export class AppModule {}

@Controller('posts')
@UseGuards(AuthzGuard)
export class PostController {
  @Put(':id')
  @Authorize({ action: 'post.update', resource: 'post' })
  update() { ... }
}

πŸ” Debug Mode

Every adapter supports debug: true which returns a full Decision object:

const decision = await authz.can(ctx, { debug: true });
// {
//   allowed: false,
//   reason: "PBAC Denied (Condition mismatch): ...",
//   matchedPolicies: ["post.update"],
//   failedConditions: ["Condition failed for policy: post.update"]
// }

πŸ“Š Feature Parity Matrix

Feature Core Express Fastify NestJS Node
RBAC / PBAC Engine βœ… βœ… βœ… βœ… βœ…
Per-Request Caching βœ… βœ… βœ… βœ… βœ…
Audit Hooks (Pre/Post) βœ… βœ… βœ… βœ… ❌
Debug Mode βœ… βœ… βœ… βœ… βœ…
Decorator Support ❌ ❌ βœ… βœ… ❌
Global Middleware ❌ βœ… βœ… βœ… ❌

⚑ Benchmarks

Evaluated on a standard Linux environment using tinybench.

Scenario Ops/sec Avg Latency
RBAC Only (100-level hierarchy) ~438,000 2.27Β΅s
PBAC Only (100 indexed policies) ~312,000 3.19Β΅s
Combined (No Cache) ~237,000 4.20Β΅s
Combined + Cache Hit ~716,000 1.39Β΅s

See performance_report.md for full methodology.


πŸ†š Why AuthZ?

Feature AuthZ CASL Oso
RBAC Hierarchy βœ… ⚠️ βœ…
PBAC / Attributes βœ… βœ… βœ…
Deny Overrides Allow βœ… ❌ βœ…
Serializable JSON Policies βœ… βœ… ❌
O(1) Role Lookup βœ… ❌ ❌
Per-Request Cache βœ… ❌ ❌
Framework Adapters βœ… ⚠️ ⚠️

πŸ“š Documentation

Guide Description
Getting Started Installation, first engine, first check
RBAC Guide Roles, inheritance, wildcards
PBAC Guide Policies, conditions, deny rules
Custom Engine Implementing AuthorizationEngine

πŸ§ͺ Examples

Working runnable apps in authz-examples/:

  • Express β€” RBAC + PBAC + audit hooks
  • Fastify β€” Plugin + preHandler decorator
  • NestJS β€” Module + Guard + @Authorize decorator

πŸ—ΊοΈ Roadmap

  • Redis Cache Provider β€” Shared cache for multi-instance deployments
  • AuthZ Dashboard β€” Visual policy editor and real-time audit viewer
  • Rust/WASM Core β€” WASM-based engine for maximum performance

🀝 Contributing

See CONTRIBUTING.md for how to set up the monorepo, run tests, and submit pull requests.


License

MIT Β© 2026 Engr., Isaiah Pius

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors