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

@allstar/hemera-acl

v4.1.0

Published

Access control pluging for hemera applications

Downloads

9

Readme

Hemera ACL

Hemera pluging adding Access control via coarse based roles, or granular permissions

Installation

$ npm install @allstar/hemera-acl

Usage

Basic Setup

const Hemera = require('nats-hemera')
const nats = require('nats')
const Acl = require('@allstar/hemera-acl')

const nc = nats.connect({
  servers: ['nats://0.0.0.0:4222']
})

const hmera = new Hemera(nc)
hemera.use(acl, {
  separator: ':' // default
})

hemera.ready(() => {
  // go!
})

Roles

For coarse control, handlers can define a roles array (or single string) specifying the roles that are allowed

hemera.add({
  topic: 'math'
, cmd: 'add'
, auth$: {
    roles: 'admin' // only admin access
  }
}, function(req, cb) {
  // admin user
  console.log(this.user$)
  cb(null, true)
})

hemera.add({
  topic: 'math'
, cmd: 'subtract'
, auth$: {
    roles: ['admin', 'manager'] // admin OR manager access
  }
}, function(req, cb) {
  // admin user
  console.log(this.user$)
  cb(null, true)
})

hemera.add({
  topic: 'math'
, cmd: 'divide'
, auth$: {
    roles: [] // no roles - superuser access only
  }
}, function(req, cb) {
  // admin user
  console.log(this.user$)
  cb(null, true)
})
Calling w/ roles

Adding a userobject to the hemera metadata with the roles array key populated to pass acl validation. Request that do not pass authorization will return an error with the code EAUTH. Users with the special key superuser set tot true will always pass authorization

hemera.act({
  topic: 'math'
, cmd: 'add'
, meta$: {
    user: {
      roles: ['salesman', 'admin'] // passes authorization for the add handler
    }
  }
}, function(err, res) {
  console.log(res)
})

hemera.act({
  topic: 'math'
, cmd: 'subtract'
, meta$: {
    user: {
      roles: ['grunt'] // fails validatin for the subtract handler
    }
  }
}, function(err, res) {
  console.log(err.code) // EAUTH
})

hemera.act({
  topic: 'math'
, cmd: 'divide'
, meta$: {
    user: {
      superuser: true  // always authorized
    }
  }
}, function(err, res) {
  console.log('success!')
})

Permissions

Permissions allow you to specify an arbitry level of granularity from 1 level to as deep as you wish to go. Giving the user object a nested permissions object will dictate what a user has access to. The terminal key in the object tree should be a boolean value (true or false). The absence of a key is equivelent to false

const user = {
  roles: ['grunt']
, permissions: {
    auth: {
      user: {
        create: true
      , read: true
      , update: true
      , delete: false
      }
    }
  , blog: {
      post: {
        create: true
      , update: true
      , read: true
      , delete: true
      }
    , tag: {
        read: true
      }
    }
  }
}

To mark a handler with the desired permission include a permissions key to the auth object.

hemera.add({
  topic: 'blog'
, cmd: 'post'
, auth$: {
    permissions: 'blog:post:create' // need to permission to create blog posts
  }
}, function(req, cb) {
  console.log(this.user$)
  cb(null, true)
})

hemera.add({
  topic: 'user'
, cmd: 'delete'
, auth$: {
    permissions: 'one:very:very:specific:perm:only:few:people:have' // very granular permission
  }
}, function(req, cb) {
  console.log(this.user$)
  cb(null, true)
})

hemera.add({
  topic: 'internal'
, cmd: 'dangerous'
, auth$: {
    permissions: ' '  // super user only access (empty string)
  }
}, function(req, cb) {
  console.log(this.user$)
  cb(null, true)
})
Calling with permissions
hemera.act({
  topic: 'math'
, cmd: 'add'
, meta$: {
    user: {
      permissions: {
        auth: {
          user: {
            create: true
          }
        }
      , one: {
          very: {
            very: {
              specific: {
                perm: {
                  only: {
                    few: {
                      people: {
                        have: true
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}, function(err, res) {
  console.log(res)
})