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

node-permissions-client

v1.0.1

Published

Node library for the Guardian's permissions service

Downloads

1

Readme

Node library for the Guardian's permissions service

Dependencies

Permissions are applied on a user email, you must include an authentication library such as panda.

The client requires node 4.3 or newer.

Installation

npm install --save node-permissions-client

Usage

const permissions = require('node-permissions-client')({
	app: 'application name, should match what is in permissions model',
	s3Bucket: 'permissions-cache-s3-bucket',
	s3BucketPrefix: 'STAGE',
	s3PermissionsFile: 'permissions.json',
	s3Region: 'eu-west-1',
	// Optional values:
	sendStatus: true, // If `true` the middleware responds with status `403`. If false it forwards the error.
	logger: console, // or any other object implementing .info .warn .error
	s3Client: null, // instance of AWS.S3 in case you want to override the default
	updateInterval: 60, // seconds, how often should the cache update
	onUpdate: function () {} // callback to be notified after the cache gets updated
});

app.post('/api/sensitive/action', permissions('permission_name'), function (req, res) {
	// Your code here
});

Advanced usage

Custom logger

If your application is using a custom logger like winston or bunyan you can use the following code, by default it logs to the console.

const permissionClient = require('node-permissions-client');
const bunyan = require('bunyan');
const winston = require('winston');

const bunyanLogger = bunyan.createLogger({
	level: 'warn'
});
const winstonLogger = new (winston.Logger)({
	level: 'warn',
	transports: []
});
const permissions = permissionClient({
	logger: bunyanLogger,
	// or
	logger: winstonLogger
});

Multiple permissions

You can leverage express middleware to authorize an endpoint against multiple permissions. The user must be authorized with both permissions.

const permissions = require('node-permissions-client')({
	app: 'application',
	s3Bucket: 'bucket',
	s3BucketPrefix: 'STAGE',
	s3PermissionsFile: 'permissions.json'
});

app.post(
	'/api/sensitive/action',
	permissions('permission_one'),
	permissions('permission_two'),
	function (req, res) {
		// Your code here
	}
);

Error handling

By default the middleware returns a 403 error if the user is not authorized.

You can customize the error response setting sendStatus: false

const permissionsClient = require('node-permissions-client');
const permissions = permissionsClient({
	app: 'application',
	s3Bucket: 'bucket',
	s3BucketPrefix: 'STAGE',
	s3PermissionsFile: 'permissions.json',
	sendStatus: false
});

app.post('/api/sensitive/action', permissions('deny'), (req, res) => {});

app.use(function (err, req, res, next) {
	if (err instanceof permissionClient.Unauthorized) {
		res.send(403, 'Custom response');
	} else {
		next(err);
	}
});

Cross account usage

If your application runs a different account from permissions, you can use this code

const parmissionClient = require('node-permissions-client');
const AWS = require('aws-sdk');

const chain = new AWS.CredentialProviderChain();
chain.providers = [
	// use a profile for local development
	new AWS.SharedIniFileCredentials({ profile: 'permission' }),
	// and STS Assume Role for your instances
	new AWS.TemporaryCredentials({ RoleArn: 'arn:...' })
];

const permissions = client({
	app: 'application',
	s3Bucket: 'bucket',
	s3BucketPrefix: 'STAGE',
	s3PermissionsFile: 'permissions.json',
	s3Client: new AWS.S3({
		credentials: null,
		region: 'eu-west-1',
		credentialProvider: chain
	})
});

Contributing

Local development

  • Clone the repo
  • npm test
  • Run tests on change with nodemon --exec "tap test"

Publishing

  • Update version package.json
  • npm publish