npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-express-authorization

v1.0.2

Published

Scopes based authorization middleware for express applications.

Downloads

9

Readme

Simple Express Authorization -

Scopes based authorization middleware. Ideal for app express or derivative such as express-gateway

node npm mocha chai supertest nyc Coverage Status Build Status

Installation

$ npm i simple-express-authorization

Usage the simple-express-authorization

When there is a single setting

const app = require('express')
const guard = require('simple-express-authorization')

const settings = {
    responseCaseError: {
        code: 403,
        message: "FORBIDDEN",
        description: "Authorization failed due to insufficient permissions.",
        redirect_link: "/auth"
    },
    logicalStrategy: 'AND',
    flowStrategy: "NEXTWITHERROR"
};

guard.config(settings)

app.get('/users', guard.check(['users:read', 'users:readAll']), () => {
    return [];
}))

app.get('/users/:userId', guard.check(['users:read']), () => {
    return {};
}))
...

When there are local settings

const app = require('express')
const guard = require('simple-express-authorization')

const settingsGetAll = {
    responseCaseError: {
        code: 403,
        message: "FORBIDDEN",
        description: "Authorization failed due to insufficient permissions.",
        redirect_link: "/auth"
    },
    logicalStrategy: 'AND',
    flowStrategy: "NEXTWITHERROR"
};

const settingsGet = {
    responseCaseError: {
        code: 403,
        message: "FORBIDDEN",
        description: "Authorization failed due to insufficient permissions.",
        redirect_link: "/auth"
    },
    logicalStrategy: 'AND',
    flowStrategy: "RETURNRESPONSE"
};

guard.config(options)

app.get('/users', guard.check(['users:read', 'users:readAll'],settingsGetAll), () => {
    return [];
}))

app.get('/users/:userId', guard.check(['users:read'],settingsGet), () => {
    return {};
}))
...

Possibles settings

settings = {
    /** Specific where we find user scopes
    * By default we use -> req.user.scope
    * Observation: 
    *      - userScopesLocation is a string
    *      - req.user.scope is expected to be of type Array.
    * 
    * When informed "a.b.c" we use -> req['a']['b']['c']
    */
    userScopesLocation: "DEFAULT",

    /** Specifies the logical strategy used to evaluate user scopes
    * By default we use -> OR
    * Observation: 
    *      - logicalStrategy is a string
    *      - We currently only support "OR" and "AND".
    */
    logicalStrategy: "OR",

    /** Specifies the return object if the user does not have the expected scopes.
     *  responseCaseError is the content returned in the response body when flowStrategy
     *  is not modified, or when it is set to the default value "RETURNRESPONSE"
     */
    responseCaseError: {
        code: 403,
        message: "FORBIDDEN",
        description: "Authorization failed due to insufficient permissions.",
        redirect_link: "/auth"
    },

    /** Specifies the flow strategy used when the user does not have the expected scopes
     * By default we use -> RETURNRESPONSE
     * Observation: 
     *      - flowStrategy is a string
     *      - "RETURNRESPONSE"-> When the user does not have the required scopes,
     *      the object responseCaseError is returned.
     *      - "NEXTWITHERROR"-> When the user does not have the required scopes,
     *      the next() function is called passing the responseCaseError object.
     *      -  We currently only support "RETURNRESPONSE" and "NEXTWITHERROR".
     */
    flowStrategy: "RETURNRESPONSE"
}

Running tests

Unitary tests

Run npm run test:unit to execute the unit tests.

Integration tests

Run npm run test:integration to execute the integration tests.

Coverage tests

Run npm run test:coverage to execute the coverage tests.