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

cando

v0.2.0

Published

An authorization module

Downloads

3

Readme

CanDo

This module creates a dynamic handler for matching and authorizing routes.

In order, to perform an authorization for a given user, you must have access to the user, model, url, and method.

   +-------------------------+
   |       Request           |
   |                         |
   +----------+--------------+
              |
              |
   +----------+--------------+
   |                         |
   |                         |
   |     Authentication      |
   |                         |
   +-----------+-------------+
               |
               |
               |
   +-----------v-------------+
   |                         |
   |                         |
   |     Authorization       |
   |        (CanDo)          |
   +-----------+-------------+
               |
               |
   +-----------v-------------+
   |                         |
   |     app.router          |
   |                         |
   +-------------------------+
   

Here is a diagram of the middleware stack, the CanDo module can be placed between the Authentication and App.router. It needs the user, model, url, and method;

The concept behind the cando module is to give you the ability to create custom based authorization rules in a sharable javascript file that can be provided to both the server and the client. THis way our authorization rules are in one place and applied to the front-end to hide and show functionality and applied to the back-end to allow or deny actions.

Example:

var cando = require('cando');

cando
  .define('/api/users', function() {
    this.user.role === 'admin' ? this.allow() : this.deny();
    this.next();
  })
  .define('/api/users/:id', 'PUT', function() {
    this.user.id === this.model.user_id ? this.allow() : this.deny();
    this.next();
  });

app.use(...); //authentication
app.use(function(req, res, next) {
  // /api/:model/:id
  var id = req.url.split('/')[3];
  db.get(id, function(err, doc) {
    if (err) { return res.send(500, err); }
    req.model = doc;
    next();
  });
});

app.use(function(req, res, next) {
  var data = {
    user: req.session.user,
    model: req.model,
    req: req
  };
  
  cando.verify(data, function(err, authorized) {
    if (err) { return res.send(500, err); }
    if (!authorized) { return res.send(403, { status: 'Not Authorized' }); }
    console.log('authorized');
    next();
  });
});

Api

Cando

Is an authorization module that includes a micro-router for matching defined routes and providing authorization logic to confirm the user has the right to execute the requested api function.

There are two endpoints, #define and #verify.

define(pattern, [method], fn)

[define] takes a url pattern, a optional verb array, if null, then it is assumed you want to match against all verbs, and is the default verbs are GET, POST, PUT, DELETE. the last parameter is the function you want to be invoked on match in that function you will have access to the following:

  • this.user [object] if set
  • this.model [object] if set
  • this.url [string] - url of the request
  • this.method [string] - method of the request
  • this.params [object] if using colon patterns
  • this.set is the function you call to specify if the request is authorized or not. this.allow() or this.deny();
  • this.next to continue to the next match

verify(data, fn)

[verify] is the function that performs the match against all of your defined patterns. So you normally call this in the middleware stack or on the client before you want to execute your application code.


Browserfied version is in the root dir

  • cando.js

Thanks

  • mapleTree
  • NodeJS