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

axios-adapter-hmac

v0.1.1

Published

Adapter for HMAC signing axios requests

Downloads

55

Readme

axios-adapter-hmac

Build Status Coverage Status

Sign axios HTTP requests with a customizable HMAC signature

Installation

npm install axios-adapter-hmac --save

Usage

const axios = require("axios");
const hmacAdapter = require("axios-adapter-hmac");

const config = {
    //options detailed below
};
const adapter = hmacAdapter(axios, config);

// the request will automatcally be signed
axios
    .get("https://example.com", { adapter })
    .then(response => console.log(response))
    .catch(error => console.log(error));

This also works (so you only have to define the adapter once):

const axios = require("axios");
const hmacAdapter = require("axios-adapter-hmac");

const config = {
    // ...
};
const adapter = hmacAdapter(axios, config);
const instance = axios.create({ adapter });

instance
    .get("https://example.com")
    .then(response => console.log(response))
    .catch(error => console.log(error));

Configure

The default options are:

{
    scheme: require("hmac-scheme-plain"),
    algorithm: "sha256",
    signatureEncoding: "hex",
    signedHeaders: ["content-type", "date", "host"],
}

algorithm

The hashing algorithm (e.g., sha256, md5, etc.) to use when generating hashes. See Node.js' crypto documentation for information on available algorithms.

signatureEncoding

The encoding to use when generating hash digests. Valid options are hex, latin1, and base64.

signedHeaders

The headers that will be included when creating the HMAC signature. Only these headers will be used when creating the signature; other headers included may be modified enroute without invaldating the signature.

Must be an array; can be empty.

Schemes

The adapter gains flexibility through pluggable schemes. The scheme does most of the hard work and is responsible for generating the HMAC signature and adding the requisite headers to the request.

A default scheme is included (hmac-scheme-plain), which follows the spec defined in cmawhorter/hmmac.

The default scheme expects two keys, publicKey and privateKey, which you must pass into the config object via the schemeConfig key:

const config = {
    // ...other options
    schemeConfig: {
        privateKey: "your private key",
        publicKey: "your public key",
    },
};

If you want to use the default scheme (likely in combination with cmawhorter/hmmac), you do not need to specify the scheme. Note, however, that the schemeConfig is still required;

Using Your Own Scheme

If you would like to create your own scheme (for example, to sign requests in the AWS4 style), you may do so. Simply require the scheme and pass it to the scheme key of the config. For example:

const axios = require("axios");
const customScheme = require("custom-scheme");
const hmacAdapter = require("axios-adapter-hmac");

const config = {
    scheme: customScheme
};

const adapter = hmacAdapter(axios, config);

Your scheme must implement a sign function, which should directly modify the request object it is given. The sign function will receive the following object as its first argument when it is called:

{
    request,
    schemeConfig,
    signedHeaders,
    hash,
    hmac,
}

request

The axios request object.

schemeConfig

The value of schemeConfig as defined in the adapter config. Use this to pass options like public and private keys to the scheme.

signedHeaders

An array of headers to be included when creating the HMAC signature.

hash

A function that takes a string of data and returns a hash digest using the encoding specified in the adapter config.

hmac

A function that takes a string of data and returns an HMAC digest using the encoding and algorithms specified in the adapter config.