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

rhebok

v0.1.11

Published

Easy to use hierarchical role based access control (RBAC) with support for asynchronous authorization.

Downloads

26

Readme

Rhebok - A hierarchical, asynchronous role based access control (RBAC) module

Build Status

Overview

This module works by defining a hierarchy of roles and capabilities and checking to see if a role possesses a certain capability. This allows you to determine whether a user should have access to a given resource or operation. The module is written in Typescript and compiles to commonjs.

This module is in beta. The API could change prior to 1.0 release.

Changelog

  • 0.2.0: Instead of extending the Cap class, you should now create a class that implements ICap. See the example.spec.ts file for implementation details.

Install

npm install rhebok

Sample Code

Code samples demonstrating the usage of this module are available here.

Objects

Role

Accepts name and params.

Important The Role name must be unique within your role hierarchy. To ensure that you haven't used a role name twice, you can chain the validate() method to the end of your topmost role. This will run a recursive check and throw an error if a duplicate role name has been found. While you do not have to call this method, keep in mind that accidentally re-using a role name could result in a user gaining elevated access.

const SimpleRoleSchema = new Role(
	 'logged-out',
	{
	    caps: [
	      new HasCap('user:create')
	    ],
	    children: [
		    new Role(
			    'logged-in',
			    {
				    caps: [
					    new HasCap('event:create'),
					    new ConditionalCap(
						    'event:update',
						    {
							    if: async (context: any) => {
									return (context.eventOwnerId === context.userId);
								}
							}
					    )
				    ]
				}
			)
		]
	}
 ).validate();

Methods

Can

Accepts a role name, capability name and a context object. The context object is passed into any ConditionalCap to help it decide if the role should pass the capability check. This method returns a promise which resolves to true if the role has the capability.

SimpleRoleSchema.can(
	'logged-in',
	'event:update', 
	{ 
		userId: 5, 
		eventOwnerId: 6
	}
);

HasCap

Accepts name. If a role has this capability then the role is deemed to be authorized.

new HasCap('event:create')

ConditionalCap

Accepts a name and params. If a role has this capability, then the function assigned to the if attribute of params is called. The if function should return a promise. If that promise resolves with true, then the role is deemed authorized.

new ConditionalCap(
    'event:update',
    {
	    if: async (context: any) => {
			return (context.eventOwnerId === context.userId);
		}
	}
)

Extending Roles and Capabilities

Extending Roles

You may wish to extend the Role object in your application with a thin wrapper. This will provide two benefits:

  1. You can use types for your roles and the context parameter which gives you the benefits of type checking in Typescript.
  2. You can encapsulate logic for asynchronously retrieving a users role. This is especially helpful when the users role depends on the authorization context (i.e. the users role depends on the particular object they are trying to access).

The examples folder shows how to extend roles and capabilities with custom typings.