Socket
Socket
Sign inDemoInstall

shiro-trie

Package Overview
Dependencies
0
Maintainers
4
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    shiro-trie

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


Version published
Weekly downloads
3.5K
increased by5.28%
Maintainers
4
Install size
50.5 kB
Created
Weekly downloads
 

Changelog

Source

<small>0.4.10 (2021-01-13)</small>

  • fix: order of permissions irrelevant (1260fcd), closes #147
  • chore: new changelog (48e9c65)
  • chore: updated travis.yml (cd45e20)
  • chore(package): update coveralls to version 3.0.8 (b27be6b), closes #142
  • chore(package): update coveralls to version 3.0.9 (b71921d), closes #142
  • chore(package): update karma to version 4.0.0 (6d995b1)
  • chore(package): update lockfile package-lock.json (c418016)
  • chore(package): update lockfile package-lock.json (8a8d4c4)
  • chore(package): update lockfile package-lock.json (f396e88)
  • chore(package): update package-lock.json (53f91bf)
  • chore(package): Bump http-proxy from 1.17.0 to 1.18.1 (002effa)
  • test: restored node 8 support (d71deb7)

Readme

Source

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

see CHANGELOG.md

License

MIT © entrecode GmbH

Keywords

FAQs

Last updated on 13 Jan 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc