auth-scope

Create authorization scope from collection of roles and/or permissions.

npm install auth-scope
3 downloads in the last day
6 downloads in the last week
17 downloads in the last month

auth-scope

Create a scope from collection of roles and/or permissions.

Originally made for use with oauth2orize and the scope parameter, but could be adapted to different scenarios.

Installation

Node

npm install auth-scope

Browser

component install alexmingoia/auth-scope

Example

var Permission = require('auth-permission')
  , Role = require('auth-role')
  , Scope = require('auth-scope');

// Specify any number of roles or permissions
var roles = new Scope([
  Role('api')
    .allow(Permission('read profile'))
    .allow(Permission('read post')),
  Permission('create account'),
  Permission('update billing')
]);

// Get scope permissions
var permissions = scope.permissions();

JSON.stringify(permissions);
// => ['read profile', 'read post', 'create account']

// Create new scope by narrowing current scope.
var restricted = scope.narrow(['api', 'create account']));

API

new Scope(collection)

Create a new scope from collection of permissions and roles.

scope.find(query)

Find permissions or roles in scope.

Array|String|Scope query array of role or permission names, another scope, or a string of comma separated role or permission names.

Returns array of roles or permissions

scope.narrow(query)

Create a new scope by narrowing existing scope.

Array|String|Scope query array of role or permission names, another scope, or a string of comma separated role or permission names.

Returns new scope.

scope.match(query)

Array|String|Scope query array of role or permission names, another scope, or a string of comma separated role or permission names.

Returns boolean.

scope.has(name)

Check if scope has given role or permission.

Returns boolean.

scope.roles()

Returns array of scope roles.

scope.permissions()

Returns scope permissions, including those in scope roles.

scope.names()

scope.toJSON()

Returns array of scope role or permission names.

Tests

Tests are written with mocha and should.js using BDD-style assertions.

Run them using npm:

npm test

MIT Licensed

npm loves you