You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Powerful access control with a dead simple API. Build any access control scheme
you need by allowing maps of arbitrary keys and values called contexts.
Simple — just two API methods.
Powerful — flexible enough to build any API scheme.
The call to assert returns false because the properties in the context
asserted do not match any allowed context. However, if we add a matching role
property:
varapp=require('express')();varaccess=require('context-access');// Allow users with manager or admin role to POST to /usersaccess.allow({path: '/users',method: [['GET','POST']]role: [['manager','admin']],});// Route middlewarevarauthorize=function(req,res,next){varcontext={role: req.session.role,// adminpath: req.path,// /usersmethod: req.method// POST};if(access.assert(context)){returnnext();}else{res.send(403,'You must be an admin to do this!');}};// Use route middlewareapp.post('/users',authorize,function(req,res){// ...});
API
exports.allow(context)
Allow a given context when asserted.
exports.assert(context)
Assert a given context. Returns true or false if it is allowed or denied.
If there's no definition for a key in the given context, then it is ignored.
Browser support
Firefox, Chrome, Safari, IE9+
Tests
Tests are written with mocha and
should using BDD-style assertions.