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 🙏

© 2024 – Pkg Stats / Ryan Hefner

granular-ac

v2.1.0

Published

Granular Access Control library for managing permissions.

Downloads

14

Readme

GranularAC - Granular Access Control Library for Node.js

GranularAC is a TypeScript library designed to manage granular access control in Node.js applications. It offers a flexible and customizable solution for defining and enforcing access control rules based on resources, actions, and scopes. By leveraging the concepts of Role-Based Access Control (RBAC), GranularAC allows you to implement fine-grained access control in your application.

Table of Contents

Why Use GranularAC?

GranularAC provides several benefits for managing access control in your Node.js application:

  1. Granularity: GranularAC allows you to define permissions at a granular level, specifying exactly which actions are permitted for each resource and scope combination. This granular approach gives you precise control over who can perform specific actions on specific resources.

  2. Flexibility: With GranularAC, you can easily define and customize access control rules to suit the requirements of your application. Whether you need to restrict access to certain resources, actions, or scopes, GranularAC offers the flexibility to tailor access control rules to your specific use case.

  3. Scalability: GranularAC is designed to scale with your application as it grows. Whether you're working on a small project or a large enterprise application, GranularAC can handle complex access control requirements and adapt to changes in your application over time.

  4. Security: By implementing access control rules with GranularAC, you can enhance the security of your application by ensuring that only authorized users have access to sensitive resources and actions. This helps protect your application from unauthorized access and potential security breaches.

  5. Ease of Use: GranularAC provides a simple and intuitive API for defining and verifying access control rules. With straightforward methods for creating permissions and checking permissions, GranularAC makes it easy to implement access control in your application.

Key Concepts

GranularAC is based on the principles of Role-Based Access Control (RBAC), which involves the following key concepts:

  • Resource: Any entity in your application that you want to control access to, such as a user, post, or document.

  • Action: A specific operation that can be performed on a resource, such as creating, reading, updating, or deleting.

  • Scope: The context or visibility of an action, which determines who can perform the action on the resource. Scopes can include "self" (referring to the current user), "others" (referring to other users), or custom scopes specific to your application's requirements.

  • Permission: A combination of an action, resource, and scope that defines whether a user is allowed to perform a specific action on a specific resource within a certain scope.

By defining permissions based on these concepts, GranularAC enables you to control access to various features and functionalities within your application with precision and control.

Permission String Format

The permission string format in GranularAC follows the pattern "action:resource:scope".

-> Action:Resource[:Scope?]

  > Action = "read";
  > Resource = "user";
  > Scope = ["others", "banned"];

Permission = "read:user"
Permission = "read:user:others"
Permission = "read:user:banned"

Together, these three components form a unique permission that defines whether a user is allowed to perform a particular action on a specific resource within a certain scope. The permission string format makes it easy to define and verify granular access permissions in a system.

Installation

You can install GranularAC via npm using the following command:

npm install granular-ac

Usage

GranularAC can be used to define and enforce access control rules in your application. Here's how you can use GranularAC in your Node.js application:

Creating Permissions

To create permissions, first instantiate the ARS (Action-Resource-Scope) class with the desired resource and actions. Then use the build method to generate permission strings for specific actions and scopes.

import { ARS } from "granular-ac";

// Define permissions for "post" resource
const postCredentials = new ARS("post", {
  create: ["public", "private", "others"],
  read: ["own", "others", "private"],
  update: ["own", "others", "private"],
  delete: ["own", "others", "private"],
} as const); // "as const" is used to enforce the type of the object

const permissions = postCredentials.build();

permissions.create.post._this; // 'create:post'
permissions.read.post.own; // 'read:post:own'

const updatePermissions = permissions.update;

updatePermissions.post.own; // 'update:post:own'
updatePermissions.post.others; // 'update:post:others'

Verifying Permissions

You can use the verify method to check if a user has the required permissions to perform a specific action on a resource within a certain scope.

const canRead = userCredentials.verify("read", "self", user.permissions);

Integrating with Express Middleware

GranularAC can be integrated with Express middleware to protect routes and endpoints in your application. By verifying permissions before allowing access to certain routes, you can ensure that only authorized users can perform certain actions.

import express from "express";
import { ARS } from "granular-ac";

const app = express();

// Define permissions for "post" resource
const postCredentials = new ARS("post", {
  create: ["public", "private", "others"],
  read: ["own", "others", "private"],
  update: ["own", "others", "private"],
  delete: ["own", "others", "private"],
} as const); // "as const" is used to enforce the type of the object

// Middleware to set user (may be user from database)
function setUser(req, res, next) {
  // building permissions list based on post
  const permissions = postCredentials.build();

  req.user = {
    id: 1,
    // add custom permissions
    permissions: [
      permissions.create.post._this,
      permissions.read.post.own,
      permissions.update.post.own,
      permissions.delete.post.own,
    ],
  };
}

// Middleware to verify permissions
function verifyPermissions(req, res, next) {
  const post = await database.findPost(req.params.postId);

  const scope = post.owner.id === req.user.id ? "own" : "others";

  if (postCredentials.verify("read", scope, req.user.permissions)) {
    next();
  } else {
    res.status(403).send("Unauthorized");
  }
}

// Route to read a post
app.get("/posts/:postId", setUser, verifyPermissions, (req, res) => {
  // Handle request
});

Contributing

Contributions to GranularAC are welcome! If you encounter any issues or have suggestions for improvements, feel free to submit issues and pull requests on the GitHub repository.

License

GranularAC is licensed under the MIT License. See the LICENSE file for details.

By providing a comprehensive access control solution based on RBAC principles, GranularAC empowers developers to implement robust security measures in their Node.js applications, ensuring data privacy and protection against unauthorized access.