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

auth-policy

v1.0.2

Published

Authorization Policy Builder

Downloads

43

Readme

auth-policy

A minimal authorization policy builder which defines if a viewer can perform an action on an entity. The Policy can be defined in a declarative manner and can be consumed at various layers of any application.

Usage

yarn add auth-policy
import Policy from 'auth-policy'

// create a new policy
const userPolicy = new Policy();

// register concern
userPolicy.register('update', ({ viewer, entity: user, value }) => {
  if(viewer.role === 'Admin') return true;
    
  if(viewer.id === user.id) {
    if(value.role === 'Admin') return false;
      
    return true;
  }
    
  return false;
});

// verify authorization
userPolicy.can(viewer).perform(':update').having(value).on(user);

Documentation

Name | Description ------------ | ------------- viewer| The user for whom the authorization is being verified. action| A string which defines the action to be performed by the viewer. entity| The object against which the action is to be performed. value| The value associated with the action.

Concerns

Every policy has multiple concerns, each of which maps to an action performed by the viewer and defines if the viewer is authorized to perfom that certain action. Concerns are added to a policy using the register function.

import Policy from 'auth-policy';

const userPolicy = new Policy();

// registering a single concern
// associated action = ':read'
userPolicy.register('read', ({ viewer }) => !!viewer);

// registering multiple concerns with same authorization policy
// associated actions = ':update', ':delete'
userPolicy.register(['update', 'delete'], ({ viewer, entity }) => 
  viewer.role === 'Admin' || viewer.id === entity.id
);

Child Policies

Any policy can have multiple child policies which can be included using the include function. It is recommended to have a single root level policy and nest all the other entity level policies inside it.

A policy can be included in two ways, either by passing a prebuilt instance of Policy or using a callback function which receives a fresh instance of Policy in the argument that can be used to define the concerns inside the function. Policies can be deeply nested as much as you need.

import Policy from 'auth-policy';

const postPolicy = new Policy();
// associated action = ':read'
postPolicy.register('read', ({ viewer, entity }) => 
  entity.isPublished || viewer.id === entity.publisher_id
);

const policy = new Policy();

// including a prebuilt policy
// available actions = 'post:read'
policy.include('post', postPolicy);

// using a callback function to define a new policy
// accociated actions = 'user:read', 'user:email:update', 'user:phone_number:update'
policy.include('user', p => {
  p.register('read', ({ viewer }) => !!viewer);
  
  // include another set of nested policies at once
  p.include(['email', 'phone_number'], p => {
    p.register('update', ({ viewer, entity: user }) => viewer.id === user.id);
  });
});

Authorization

Once the policy is defined we can simply use the can function chain to verify the access to the viewer for a certain action.

import Policy from 'auth-policy';

const policy = new Policy();

policy.include('invite', p => {
  p.register('read', () => true);
  p.register('update', ({ viewer, entity: invite, value }) => {
    if(viewer.id === invite.organiser_id) return true;
      
    if(viewer.id === invite.user_id) {
      if(invite.status === 'Requested' && value.status === 'Accepted')
        return false;
      
      return true;
    } 
      
    return false;
  });
});

const viewer = { id: 1 };
const organiser = { id: 2 };
const invite = { user_id: 1, organiser_id: 2, status: 'Requested' };

policy.can(viewer).perform('invite:read').on(invite); // true

const updatedValue = { status: 'Accepted' };

/* pass value using `having` function if
 * there is any value associated with the action. */
policy.can(viewer).perform('invite:update').having(updatedValue).on(invite) // false

policy.can(organiser).perform('invite:update').having(updatedValue).on(invite) // true