An authorization system for connect and express applications inspired by Apache's Shiro project.
An express/connect middleware module for enforcing an Apache Shiro inspired authorization system.
var express = require'express';var authorization = require'express-authorization';var app = express;// Consider an authenticated user in the express session:// req.session.user.permissions = ["restricted:*"]appget'/restricted'authorizationensureRequestisPermitted"restricted:view";
$ npm install express-authorization
- Easy integration into your express or connect based code
- Permission claim evaluation in the browser
- Expressive permissions inspired by Apache Shiro
- Customizable to fit your application needs
- High test coverage
In express-authorization, a permission is a statement that defines access to an explicit activity, behavior or action. Subjects, usually website users, are assigned permissions to enable access to sets of activities, behaviors, or actions. To practically assign sets of permissions to subjects, express-authorization supports a wildcard enabled permission statement syntax that closely follows the syntax used by Apache Shiro. A collection of permissions assigned to a subject are compiled by the system into a regular expression referred to as a claim. Claims are then queried regarding whether or not they permit permissions that gate activities, behaviors, or actions.
express-authorization uses the Apache Shiro wildcard permission syntax directly.
- Permission statements are composed of from parts delimited by colons (:).
- Permission parts can contain lists delimited by commas (,).
- Wildcard ? and * can be used to match one or more chataters within an expression part.
- Examples: system:* | activity:create,update,delete
Permissions statements are strings that can be specified in parameter lists that may include arrays of composited permissions. A subject, usually a user, is expected to be represented by an object with a permissions property refering to either a single permission or an array of permissions.
In the permission query API, a permission source is placed under consideration and compiled into a claim that is queries to confirm permitted permissions.
authorizationconsiderSubject | authorizationconsiderPermissions -> claim
The considerSubject and considerPermissions methods are used to generate a claim object. A claim object is litterly a regular expression that matches any permissions that are permitted by the subject, user, or permission list under consideration. In addition, an isPermitted method is exposed on the claim for checking if one or more permissions are supported. See the consider test cases in the code for more examples.
express-authorization uses a fluent API to generate express middleware for enforcing permissions.
To generate an express middleware, you write a call chain starting with a reference to authenticate.ensureRequest and ending in a call to isPermitted. The call to isPermitted returns a connect/express compliant middleware function.
By default, ensureRequest sources permissions from the session through references to session.user.permissions or session.permissions. To consider alternative permission sources, withSubject or withPermissions callbacks (asynchronous or immediate) are used.
ensureRequest redirects to /login by default when a request is denied. redirectTo can be used to specify an alternate redirect url. onDenied can be used to prived a custom response function.
All of these options (withSubject, withPermissions, redirectTo, and onDenied) can be set either through chained API calls, or on the ensureRequest.options object. When set on the global authorization.ensureRequest.options, these establish new defaults.
A custom new authorization.EnsureRequest() object can be constructed for use if required.
The dist directory includes a version of the permission query API that can be used in your web pages. See dist/authorization.js.
Copyright (c) 2013 Support.com, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.