Coder Social home page Coder Social logo

lykmapipo / express-authorization Goto Github PK

View Code? Open in Web Editor NEW

This project forked from support-dot-com/express-authorization

0.0 2.0 0.0 148 KB

An express/connect middleware module for enforcing an Apache Shiro inspired authorization system.

License: MIT License

express-authorization's Introduction

express-authorization

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:*"]

app.get('/restricted',
  authorization.ensureRequest.isPermitted("restricted:view"),
  function(req, res) {
    ...
  });

Installation

$ npm install express-authorization

Features

  • 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

Philosophy

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.

Documentation

Permission Wildcard Expressions

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

Permission Query

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.

authorization
  .considerSubject(user)
  .isPermitted("express:coding")
authorization
  .considerPermissions("source:edit", "express:*")
  .isPermitted("express:coding")

From subject or permission list -> claim -> isPermitted

authorization.considerSubject | authorization.considerPermissions -> 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 Middleware

express-authorization uses a fluent API to generate express middleware for enforcing permissions.

authorization.ensureRequest.isPermitted("restricted:view")

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.

authorization.ensureRequest
  .withPermissions(function (req, res, done) { done(["identity:*"]); })
  .isPermitted("identity:edit");

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.

Browser

The dist directory includes a version of the permission query API that can be used in your web pages. See dist/authorization.js.

License

MIT License

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.

express-authorization's People

Contributors

charleseads avatar

Watchers

James Cloos avatar lally elias avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.