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

@wizeline/access-decision-manager-express

v0.5.2

Published

An implementation of the Voter pattern for determining access rights for express

Downloads

23

Readme

lerna build

Access Decision Manager - Express.

Determines whether or not an entity is entitled to perform a specific action, for example, request a particular URI or modify a specific object. It relies on Voters, all voters are called each time you use isGranted() method.

The Access Decision Manager takes the responses from all voters and makes the final verdict (to allow or deny access to the resource) according to the strategy defined in the application.

It recognizes several strategies:

affirmative (default)

grant access as soon as there is one voter granting access;

unanimous

only grant access if none of the voters has denied access;

[custom]

(any strategy can be defined and passed as an argument)

Implementing Voters

Voters are to determine access rights The use of Voters to verify permissions is based on this model. Voters are called to grant access to a resource or an action.

Adding a Custom Voter.

To create a Voter, use the Voter interface:

export interface Voter {
 supports: (attribute: any, subject: any, context: any) => boolean;
 voteOnAttribute: (
   attribute: any,
   subject: any,
   user: any,
   context: any,
 ) => boolean | Promise<boolean>;
}

Here's a detailed description of the two abstract methods:

supports(attribute, subject, context) When isGranted() is called, the first argument is passed here as attribute (e.g. ROLE_USER, edit) and the second argument (if any) is passed as subject (e.g. null, a Post object). Your job is to determine if your voter should vote on the attribute/subject combination. if you return true, voteOnAttribute() will be called. Otherwise, your voter is done: some other voters should process this. The third argument, context is to help you to determine access rights, the access decision manager takes this argument when is created

voteOnAttribue(attribute, subject, user, context) If you return true from supports, then this method is called. Your job is simple: return true to allow access and false to deny access. The user and context properties (if any) can be useful.

For example, the following voter checks attributes related to an admin:

Suppose you have a Private object, and you need to decide whether or not the current user can edit the resource. The only users that can get that resource are the ones who are admins; also, you know that the users who are admins have a roles property, which includes admin. So, it would be best if you created a new voter that validates what actions supports in this case GET_PRIVATE. And that the voteOnAttribute validates that the user is an admin.

This voter can be named admin.voter.ts since it is the one which determines access for admin users.

const supportedAttributes = [
 "GET_PRIVATE"
];

const adminVoter = {
 supports(attribute): boolean {
   return supportedAttributes.includes(attribute);
 },
 voteOnAttribute(attribute, subject, user): boolean {
   // Do the logic here to validate determine if has permissions.
   return (
     user &&
     user.roles &&
     user.roles.includes('admin')
   );
 }
}

export default adminVoter;

Then we can have a public resource that can be view for everyone, in this case we need specified the attributes and the logic to validate, since we know this is public we are returning a true.

const supportedAttributes = [
 'GET_PUBLIC'
];

const publicVoter = {
 supports(attribute): boolean {
   return supportedAttributes.includes(attribute);
 },
 voteOnAttribute(attribute, subject, user): boolean {
   // Do the logic here to validate determine if has permissions.
   return true;
 }
}

export default publicVoter;

Then let's add our voters to our express application, here we are assuming that we have the data of our user in req.user

import AccessDecisionManagerProvider, { Voter } from '@wizeline/access-decision-manager-express';
import express from 'express'
import publicVoter from '../path/to/voters';
import privateVoter from '../path/to/voters';

const voters: Voter[] = [
    publicVoter,
    privateVoter
];


const app: express.Application = express();


app.use(AccessDecisionManagerProvider((req) => req.user, voters({})));

Now add the middleware in the routers with their attributes


import { isGrantedMiddleware } from '@wizeline/access-decision-manager-express';

///....

app.get('/publicResource',
  isGrantedMiddleware('GET_PUBLIC'),
  /// ...someController.getAll,
);


app.get('/privateResource',
  isGrantedMiddleware('GET_PRIVATE'),
  /// ...someAnotherController.getAll,
);