New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

access-weaver

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

access-weaver

A lightweight and flexible access control middleware for Node.js and Express, supporting policy-based authorization

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

Access Weaver 🧵

Access Weaver is a lightweight, declarative authorization middleware for Express.js.
It helps you weave access control policies into your routes using simple, rule-based flows.

✨ Features

  • 🔑 Declarative rules with allow / deny effects.

  • 🎯 Resource-based access with wildcards (*).

  • Express.js middleware with simple setup.

  • 🧩 Flexible targets (static or dynamic from req).

  • 🧪 Easy to test with SuperTest and Jest.

📦 Installation

npm install access-weaver

or

yarn add access-weaver

⚡ Quick Start

1. Define Policies

Policies are collections of flow rules:

const allowInvoiceRead = [
    {
        effect: "allow",
        action: "invoice.read",
        target: "invoice:*",
    },
];

const denyInvoiceRead = [
    {
        effect: "deny",
        action: "invoice.read",
        target: "invoice:*",
    },
];

2. Attach Policies to Users

Attach policies to the req.user object:

app.use((req, _res, next) => {
        req.user = {
            id: "u1",
            
            // Use like this
            policies: allowInvoiceRead,
        };
        next();
    }
);

3. Protect Routes

Use the authorize middleware:

import express from "express";
import {authorize} from "access-weaver";

const app = express();
app.get(
    "/invoice/:id",
    authorize({
        action: "invoice.read",
        target: (req) => `invoice:${req.params.id}`, // -> always do this when getting single data so that if any policy restrict someone it would work automatically
    }),
    (req, res) => {
        res.json({data: `Invoice data for ${req.params.id}`});
    }
);

4. More Examples

Delete a User

app.delete(
    "/users/delete/:id",
    authorize({
        action: "users.delete",
        target: (req) => `users:${req.params.id}`,
    }),
    (req, res) => {
        res.json({data: `User deleted with id ${req.params.id}`});
    }
);

Read All Users

app.get(
    "/users",
    authorize({
        action: "users.read",
        target: () => `users:*`,
    }),
    (req, res) => {
        res.json({data: [{id: 1}, {id: 2}, {id: 3}]});
    }
);

Read Single User

app.get(
    "/users/:id",
    authorize({
        action: "users.read",
        target: (req) => `users:${req.params.id}`,
    }),
    (req, res) => {
        res.json({data: {userId: req.params.id}});
    }
);

🔒 Example Policies

Policy NameActionTargetEffectDescription
allowInvoiceReadinvoice.readinvoice:*allowCan read all invoices
denyInvoiceReadinvoice.readinvoice:*denyCannot read invoices
allowInvoiceUpdateinvoice.updateinvoice:*allowCan update invoices
allowUserDeleteWithSpecificIdusers.deleteusers:101allowCan delete only user with ID 101
allowDeleteAnyUserusers.deleteusers:*allowCan delete any user
allowUserGetAllusers.readusers:*allowCan read all users
allowSingleUserReadusers.readusers:101allowCan only read user with ID 101

🧪 Testing

import request from "supertest";

it("should allow invoice read when policy permits", async () => {
    const app = makeApp(allowInvoiceRead);
    const res = await request(app).get("/invoice/123");
    expect(res.status).toBe(200);
    expect(res.body).toEqual({data: "Invoice data for 123"});
});

🤝 Contributing

PRs and issues are welcome! 🎉
If you find a bug or want a feature, open an issue.

📜 License

MIT © 2025

Keywords

authorization

FAQs

Package last updated on 06 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts