Registry / security / fastify-casbin-rest

fastify-casbin-rest

JSON →
library4.0.0jsnpmunverified

A Fastify plugin that integrates Casbin authorization using a RESTful model. As of version 4.0.0, it builds on top of fastify-casbin to enforce access control based on subject, object, and action extracted from HTTP requests. It provides route-level enabling, customizable extractors, and hooks for allow/deny actions. Released by NearForm, it is actively maintained with TypeScript definitions included. Unlike generic Casbin adapters, this plugin is opinionated for REST APIs and tightly couples with Fastify's lifecycle for minimal boilerplate.

npm install fastify-casbin-rest
INSTALL
IMPORT
SIG · FASTIFY-CASBIN-RES
F
fastify-casbin-rest
securityjavascriptv4.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import fastifyCasbinRest from 'fastify-casbin-rest'
import { fastifyCasbinRest } from 'fastify-casbin-rest'
This package exports a default plugin function for Fastify's register.
default (require)
const fastifyCasbinRest = require('fastify-casbin-rest')
const { default: fastifyCasbinRest } = require('fastify-casbin-rest')
CJS users must use default require; named destructuring does not work.
type FastifyCasbinRestOptions
import type { FastifyCasbinRestOptions } from 'fastify-casbin-rest'
import { FastifyCasbinRestOptions } from 'fastify-casbin-rest'
Type-only import to avoid runtime inclusion; the interface is exported for TypeScript users.

Shows registering fastify-casbin and fastify-casbin-rest with custom extractors and a route protected by Casbin REST authorization.

import fastify from 'fastify'; import fastifyCasbin from 'fastify-casbin'; import fastifyCasbinRest from 'fastify-casbin-rest'; const app = fastify(); await app.register(fastifyCasbin, { model: 'path/to/model.conf', adapter: 'path/to/policy.csv' }); await app.register(fastifyCasbinRest, { getSub: (req) => req.user, getObj: (req) => req.url, getAct: (req) => req.method, onDeny: (reply) => reply.code(403).send('Forbidden') }); app.get('/resource', { config: { casbin: { rest: true } }, handler: (req, reply) => reply.send('protected') }); await app.listen({ port: 3000 });
Debug
Known issues
breakingVersion 2.0.0 changed the route option from casbin.rest.enabled to casbin.rest
fix
Use casbin.rest: true or casbin.rest: { ... } instead of casbin.rest.enabled: true.
affects: <2.0.0
gotchaThe plugin must be registered after fastify-casbin, otherwise it will throw an error.
fix
Ensure fastify-casbin is registered before fastify-casbin-rest in your plugin chain.
affects: >=1.0.0
gotchaIf getDom is not provided, the dom variable will be undefined; Casbin enforcer may reject if policy expects a dom.
fix
Provide getDom in plugin options or route options if your Casbin model uses domains.
affects: >=1.0.0
deprecatedThe onDeny and onAllow callbacks are synchronous; async support may be added in a future major.
fix
Use synchronous callbacks only.
affects: <5.0.0
Errors
Common errors & fixes
Error: Cannot find module 'fastify-casbin'
fastify-casbin peer dependency not installed
fix
npm install fastify-casbin
TypeError: Cannot read properties of undefined (reading 'enforce')
fastify-casbin not registered before fastify-casbin-rest
fix
Ensure app.register(fastifyCasbin, ...) is called before app.register(fastifyCasbinRest, ...)
Error: route option casbin.rest must be a boolean or an object
Using string or number value for casbin.rest
fix
Set casbin.rest to true or an object with extraction functions.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies
casbinrequiredCore authorization library for policy enforcement
fastify-casbinrequiredPeer plugin that decorates Fastify with Casbin instance
Agent activity
14 hits · last 30 days
node
14
Resources
fastify-casbin-rest — npm install fastify-casbin-rest · libregistry