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

request-authorization

v1.1.11

Published

Node module for signing and authorizing requests

Downloads

113

Readme

request-authorization

Node module for signing and authorizing requests.

Usage

Firstly the module will need to be initialized with schemes and there associated clients. This only needs to be done once preferably on application start.

More schemes are defined below in the scheme options section.


var requestAuthorization = require('request-authorization');

var schemes = [
    {
        scheme: 'HMAC-SHA256',
        alias: 'scheme-one', // NOT REQUIRED
        useTimestamp: true,
        clients: [
            {
                clientId: 'clientOne',
                password: 'aGVsbG93b3JsZA==' // base64 encoded password
            }
        ]
    },
    {
        scheme: 'RSA',
        useTimestamp: true,
        timestampValidationWindowInSeconds: 60,
        clients: [
            {
                clientId: 'clientTwo',
                relativeOrAbsolutePathToPublicKey: './public.pem',
                relativeOrAbsolutePathToPrivateKey: './private.key'
            }
        ]
    },
    {
        scheme: 'RSA',
        useTimestamp: true,
        timestampValidationWindowInSeconds: 60,
        clients: [
            {
                clientId: 'clientThree',
                publicKey: process.env.PUBLIC_KEY,
                privateKey: process.env.PRIVATE_KEY
            }
        ]
    }
];

requestAuthorization.init(schemes);

Generate authororization header

The generateAuthorizationHeader function can be used to generate authorization headers. The function accepts an options argument and data as a string to be used for the signature.

var requestAuthorization = require('request-authorization');

var options = {
    schemeName: 'HMAC-SHA256',
    clientId: 'clientOne'
};

var data = req.originalUrl + (req.body ? JSON.stringify(req.body) : '{}');
var header = requestAuthorization.generateAuthorizationHeader(options, data);

The generated authorization header would look like the following:

HMAC-SHA256 clientId=clientOne;timestamp=2015-11-11T13:41:09.430Z;signature=cCqTvX6CZDv1N00QUP1lsvzSO6SFawQHz1bTHCeBnyA=

If an alias was defined for the scheme then the alias name would be used instead of the scheme name in the generated header.

Custom-Alias-Name clientId=clientOne;timestamp=2015-11-11T13:41:09.430Z;signature=cCqTvX6CZDv1N00QUP1lsvzSO6SFawQHz1bTHCeBnyA=

isAuthorized

The isAuthorized function can be used to authorize a request. The function accepts the 'authorization' header and request data as a string.

var requestAuthorization = require('request-authorization');

var data = "{ firstName: 'john' }";
var authorizationHeader = 'HMAC-SHA256 clientId=clientOne;timestamp=2015-11-05T12:12:35.675Z;signature=8+OIZQiZBqdBx5CGzVyMMfNhXPbhz2szJX2WqWrun5U=';

var authorized = requestAuthorization.isAuthorized(authorizationHeader, data);

console.log('authorization scheme name:', authorized.schemeName);
console.log('authorization client id:', authorized.clientId);

if (authorized.result) {
    console.log('You are allowed in');
}
else {
    console.log('Denied');
    console.log('authorization error', authorized.error);
}

authorization

Express authorization middleware is available and can be used in the following way.

var requestAuthorization = require('request-authorization');
var express = require('express');
var router = express.Router();

router.get('/', requestAuthorization.authorized(getData), function(req, res) {
	res.status(200).send('You got in');

	console.log(req.requestAuthorizationIsAuthorizedResult.schemeName);
	console.log(req.requestAuthorizationIsAuthorizedResult.clientId);
	console.log(req.requestAuthorizationIsAuthorizedResult.result);
	console.log(req.requestAuthorizationIsAuthorizedResult.error);
});

function getData(req) {
    return req.originalUrl + (req.body ? JSON.stringify(req.body) : '{}');
}

// a route could also be used that does not make use of a get data function
router.get('/', requestAuthorization.authorized(), function(req, res) {
	res.status(200).send('You got in');
});

Scheme options

When initialising the module multiple schemes can be provided, each scheme can also have multiple clients each having different names, base64 encoded passwords, public and private keys for signature generation.

The schemes currently available are:

  • HMAC-SHA256
  • HMAC-SHA512
  • HMAC-MD5
  • RSA

alias

If an alias is defined this will be used in the first part of the header.

var headerExample = 'aliasName clientId=clientOne;timestamp=2015-11-11T13:41:09.430Z;signature=cCqTvX6CZDv1N00QUP1lsvzSO6SFawQHz1bTHCeBnyA='

useTimestamp

If the userTimestamp option is defined and set to true the iso string date format will be used in the signature and header.

var headerExample = 'HMAC-SHA256 clientId=clientOne;timestamp=2015-11-11T13:41:09.430Z;signature=cCqTvX6CZDv1N00QUP1lsvzSO6SFawQHz1bTHCeBnyA='

var isoFormatDate = new Date().toISOString();

timestampValidationWindowInSeconds

If useTimestamp is defined and the timestampValidationWindowInSeconds is defined then the difference between the timestamp and the server dateTime will be checked and if it is outside of the configured window the request would not be authorized.

var requestAuthorization = require('request-authorization');

var schemes = [
    {
        scheme: 'HMAC-SHA256',
        alias: 'Alias-Name', // NOT REQUIRED
        useTimestamp: true,
        timestampValidationWindowInSeconds: 60,
        clients: [
            {
                clientId: 'clientOne',
                password: 'aGVsbG93b3JsZA==' // base64 encoded password
            }
        ]
    }
];

requestAuthorization.init(schemes);

Installation

With npm do

$ npm install request-authorization --save

License

(MIT)

Copyright (c) 2015 Lee Crowe

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.