Check permissions using Shiro-like strings, put in a trie.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
test
.gitignore
.travis.yml
README.md
bower.json
index.d.ts
index.js
karma.conf.js
package-lock.json
package.json

README.md

NPM version Build Status Coverage Dependency StatusGreenkeeper badge

Check permissions using Shiro-like strings, put in a trie.

Module for handling permissions in an Apache Shiro-like style. Permissions are stored in a Trie which makes it super performant and able to make additional queries apart from a simple permission check: it is also possible to return a list of sub-rights. For example, if you have permissions to access resources with id 1 and 2, you can simply ask which ids are accessable using a customized Shiro syntax.

Install

node.js

$ npm install --save shiro-trie

web / frontend

$ bower install --save shiro-trie

Getting Started

node.js

var shiroTrie = require('shiro-trie');

web / frontend

Using the shiro-trie plugin in your web-app is pretty simple, too. First, you should include the script file to your HTML-file:

<script type="text/javascript" src="bower_components/shiro-trie/dist/shiro-trie.js" />

Usage

var shiroTrie = require('shiro-trie');

var account1 = shiroTrie.newTrie();

account1.add([
  'printer:xpc5000:print',
  'printer:xpc4000:*',
  'nas:timeCapsule,fritzbox:read'
]);

account1.check('printer:xpc4000:configure'); // true
account1.check('nas:timeCapsule:write'); // false

account1.permissions('printer:?'); // ['xpc5000', 'xpc4000']
account1.permissions('nas:$:?'); // ['read']

Defining permissions

See Understanding Permissions in Apache Shiro for a short introduction to Shiro Syntax. Basically, you can describe a permission hierarchy using : as separator. Example:

printer:xpc5000:print

You may define multiple alternatives for a level using , as separator. For example:

nas:timeCapsule,fritzbox:read is the same as nas:timeCapsule:read plus nas:fritzbox:read.

You may also use the wildcard character * to grant all permissions:

printer:*:print grants printing on any printer.

At the end, a wildcard may be omitted. Example:

printer:xpc5000 is the same as printer:xpc5000:*.

The function for adding one or multiple permissions is .add(…). You may set one string, a list of strings or array(s) of strings. It returns the same ShiroTrie instance for chainability.

Note that ? is no special character for single-character-wildcard, as opposed to some other Shiro libraries.

Checking permissions

You should always check for explicit permissions (no wildcard * or alternative , characters). For example:

printer:xpc4000:print

or

nas:timeCapsule:write

The function for checking a permission is .check(string). It returns true or false.

Getting available permissions

This is an extension to the original Apache Shiro syntax and functionality that is enabled by using a Trie instead of regular expressions for permission checking.

Given the example above, you may want to show a list of printers the user has access to (=any sub-permission). In traditional Shiro, you will have to take the whole list and whitelist each single object using a separate permission-check call to find out if there are permissions or not.

This module has a special method with a slightly different syntax to achieve exactly that: getting objects an account has permissions to.

The syntax is basically the same as for checking permissions, but introduces two new special characters: $ and ?. You perform a normal check, but you can swap a single part of the query with ?. This means “give me all that can stand there”. For example:

The permission nas:timeCapsule,fritzbox:read can be queried with nas:? which will return ['timeCapsule', 'fritzbox']. In the same manner, nas:?:write would return a list with all NAS devices where the write permission is available.

$ is a special character for “any”. For example:

nas:$:? would return a list of rights the user has on any NAS device in the example above – where each is only included once. Example:

Available permissions: nas:timeCapsule:read,write, nas:fritzbox:read,reboot. The query nas:$:? would return ['read', 'write', 'reboot'].

The function for checking available permissions is .permissions(string). It returns an Array of available permission Strings. The string to check may only contain one ? character. Also note that nas:? is the same query as nas:?:$ (would return ['timeCapsule', 'fritzbox']).

API

Initialization

var shiroTrie = require('shiro-trie');

shiroTrie.newTrie(); / shiroTrie.new();

Returns a new ShiroTrie instance.

var account1 = shiroTrie.newTrie();

Instance methods

add(string[, string])

Adds a new permission. Multiple permission strings can be added at once, either as argument list or as array. Even multiple array may be used as arguments. Returns the same instance for chaining.

Permission strings may contain special characters :, *, , but not $ or ?.

account1.add([
  'printer:xpc5000:print',
  'printer:xpc4000:*',
  'nas:timeCapsule,fritzbox:read'
]);

check(string)

Checks if a single permission is allowed. No special characters apart from :, , and * are allowed. If the permission string contains , characters, all variants are tested and the result is only true if all permissions are allowed. Returns Boolean.

account1.check('printer:xpc4000:configure'); // true
account1.check('nas:timeCapsule:write'); // false

permissions(string)

Retrieves a list of available permissions at a certain position in the permission Trie. Expects a permission string containing ?. Additionally, the any operator $ can be used. Returns Array.

account1.permissions('printer:?'); // ['xpc5000', 'xpc4000']
account1.permissions('nas:$:?'); // ['read']

reset()

Empties the Trie and returns it. New permissions can be added using add(…) afterwards.

Tests

Tests can be executed with Mocha:

$ mocha -R spec

Current Test Coverage:

Coverage

It can be checked with istanbul:

$ istanbul cover _mocha -- -R spec

Known issues

  • add(…) and permissions(…) and one case in _check is implemented recursive which is probably not ideal

Changelog

0.4.8

  • removed lodash dependency in readme
  • applied patches

0.4.7

  • fixed typings returntype of constructor

0.4.6

  • fixed querying for permissions a:b:*:c with query a:?:$:d resulting in b instead of [].

0.4.5

  • fixed a permission where an explicit star at the end would not overpower previous more fine-granular permissions

0.4.3 and 0.4.4

  • fixed querying for permissions a:*:b with query a:c:? not resulting in b.

0.4.2

  • fixed a bug where a star permission would be overwritten

0.4.1

  • fixed a bug where a permission a:*:c would overwrite a:b:c,d so that a:b:d would return false

0.4.0

  • added typescript typings

0.3.13

  • dependency update

0.3.12

  • permission queries now have an implicit $ at the end

0.3.11

  • permission query now supports multiple $

0.3.10

  • proper handling of $ after ? in permission query

0.3.9

  • dependency update

0.3.8

  • dependency update

0.3.7

  • dependency update

0.3.6

  • do not put uniq into Array.prototype

0.3.5

  • removed dependency lodash. shiro-trie is super slim now.

0.3.4

  • dependency update

0.3.3

  • dependency update

0.3.2

  • dependency update

0.3.1

  • dependency update

0.3.0

  • support for , in check(…)

0.2.1

  • bug fixes

0.2.0

  • Renamed package to shiro-trie
  • Added bower support

0.1.1

  • Updated dependencies

0.1.0

  • Initial Release

License

MIT © entrecode GmbH