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

express-authz

v1.0.1

Published

Authorization middleware for express

Downloads

8

Readme

Authz

rules and policy based authorization middleware

The expreess-authz package is available on npm.

$ npm install authz

This middleware package is meant to ride alongside authentication (such as PassportJS) and provide route specific access authorization based on rules that you provide. The rules are arbitrary, and can be tied into whatever data model or access system you care to use.

Quick start

Import authorization

var authz = require('express-authz');

Set up rules for authorizations

authorization.addRule('allAccess', function() {
    return true;
});
authorization.addRule('noAccess', function(req) {
    return false;
});

The function you provide should return something truthy to allow access, or false otherwise. The function will be passed in the req object from express, and you can operate on anything annotated onto that object.

authorization.addRule('applicationOwner', function(req) { 
    // PROTIP: when comparing BSONid from Mongoose, do comparisons with
    // .equals(), not == or ===
    return (req.application.owner.equals(req.user._id));
});

authorization.addRule('gmailAccount', function(req) {
    // email address of user contains renasar.com
    return (req.user.email.search('gmail.com') > 0);
});

Set up policies that are sets of rules

Once you've added rules, you create policies, referencing the names of the rules you provided earlier. Note that a "falsey" return from an rule check will not immediately error and fail, only fall through to the next check.

authorization.addPolicy('applicationOwner', ['applicationOwner', 'renasarian']);
authorization.addPolicy('nobody', ['noAccess']);

Utilize the middleware

When enforcing, the middleware will process the rules in the order that you provide, short-circuiting to allow access if any of the rules returns true. If none of the rules returns true, then the middleware will return an annotated Error using next() down the middleware chain. It is expecting you to have an error handler somewhere in that chain that will return a response that you prefer based on the authorization failure.

var express = require('express'),
    router = express.Router(),
    authz = require('express-authz');

router.route('/some/path/:Id')
    .get(authz.enforcePolicy('applicationOwner'), function (req, res, next) {
        ... your route logic here ...
    });

The middleware can be used in Connect/Express apps in your route definitions, as generic middleware, etc.

handling unauthorized access

If the authorization fails, the middleware will pass down an Error annotated with a 'status' of 403 down the middleware chain. Configure or create an error handler to send a response based on this error:

app.use(function(err, req, res, next) {
  if (err.status) {
    res.send(err.status)
  } else {
    next(err)
  }
});

Authentication

This package is for adding authorization over the top of authentication. For a full featured authentication package, see PassportJS.

tests

npm test

Current Status