Code Monkey home page Code Monkey logo

express-restricted's Introduction

express-restricted

npm npm NPM Travis

express-restricted is a simple Node.js package for Express.js middleware to restrict access to API endpoints with the use of JSON Web Tokens.

Installation

Installation is done through npm:

$ npm i express-restricted

or if you use yarn:

$ yarn add express-restricted

Options

  • config - Configuration object contains properties used to target where in the req object should the middleware look for data.

    • reqProp - REQUIRED, String - first child of req object (body, headers, ...)
    • childProp - OPTIONAL, String - child of reqProp (Authorization, ...)
    • identifier - REQUIRED, String - A property of JWT payload used to identify access rights to the endpoint.
    • jwtKey - REQUIRED, String - containing the secret for HMAC algorithms. Used to generate the JSON Web Token. The decoded payload of the token is added to the request object as decoded property.

    Example:

    const config = {
      reqProp: 'headers',
      childProp: 'authorization',
      identifier: 'user_type',
      jwtKey: 'ThereIsNoSecret'
    };
  • allow - REQUIRED, String or Array of Strings or an empty Array - Used to list identifier values, which are allowed to access the endpoint. An empty array will make the endpoint accessible to any identifier value. The JWT verification still has to pass.

    Example:

    const allow = ['admin', 'maintainer'];

Usage

Restrict access to an endpoint

const express = require('express');
const restricted = require('express-restricted');

const router = express.Router();

const config = {
  reqProp: 'headers',
  childProp: 'authorization',
  identifier: 'user_type',
  jwtKey: 'ThereIsNoSecret'
};

const allow = {
  all: [], // any identifier value has access
  staff: ['receptionist'],
  admins: ['super admin', 'admin']
};

router.get('/', restricted(config, allow.public), (req, res) => {
  res.json({ msg: 'Router GET /' });
});

router.get(
  '/:id/cool/:cool_id',
  restricted(config, allow.admins),
  (req, res) => {
    res.json({ msg: 'Router GET /:id/cool/:cool_id' });
  }
);

router.post('/', restricted(config, allow.staff), (req, res) => {
  res.json({ msg: 'Router POST /:id' });
});

server.listen(9000);

Restrict access to all endpoints in a route

const express = require('express');
const restricted = require('express-restricted');

const router = express.Router();

const config = {
  reqProp: 'headers',
  childProp: 'authorization',
  identifier: 'user_type',
  jwtKey: 'ThereIsNoSecret'
};

const admins: ['super admin', 'admin'];

// Restricts all router endpoints to admins only
router.use(restricted(config, admins));

router.get('/', (req, res) => {
  res.json({ msg: 'Router GET /' });
});

router.get(
  '/:id/cool/:cool_id',
  (req, res) => {
    res.json({ msg: 'Router GET /:id/cool/:cool_id' });
  }
);

router.post('/', (req, res) => {
  res.json({ msg: 'Router POST /:id' });
});

server.listen(9000);

License

MIT

Author

Pavol

express-restricted's People

Contributors

pav0l avatar

Watchers

James Cloos avatar

express-restricted's Issues

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.