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: ':'
})
hemera.ready(() => {
})
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'
}
}, function(req, cb) {
console.log(this.user$)
cb(null, true)
})
hemera.add({
topic: 'math'
, cmd: 'subtract'
, auth$: {
roles: ['admin', 'manager']
}
}, function(req, cb) {
console.log(this.user$)
cb(null, true)
})
hemera.add({
topic: 'math'
, cmd: 'divide'
, auth$: {
roles: []
}
}, function(req, cb) {
console.log(this.user$)
cb(null, true)
})
Calling w/ roles
Adding a user
object 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']
}
}
}, function(err, res) {
console.log(res)
})
hemera.act({
topic: 'math'
, cmd: 'subtract'
, meta$: {
user: {
roles: ['grunt']
}
}
}, function(err, res) {
console.log(err.code)
})
hemera.act({
topic: 'math'
, cmd: 'divide'
, meta$: {
user: {
superuser: true
}
}
}, 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'
}
}, 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'
}
}, function(req, cb) {
console.log(this.user$)
cb(null, true)
})
hemera.add({
topic: 'internal'
, cmd: 'dangerous'
, auth$: {
permissions: ' '
}
}, 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)
})