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

kane-internal-token

v0.2.1

Published

Koa middleware to restrict access to internal APIs using a shared secret

Downloads

13

Readme

Kane Internal Token

Build Status

This is a simple Koa middleware which only purpose is to intercept the request to see if it was coming from another internal API.

Requirements

You need to execute node version 6 at least.

Installation

# npm
npm install --save kane-internal-token
# yarn
yarn add kane-internal-token

Usage

This package will look for a variable that contains the shared secret in the following order:

  • the request.header object (by default x-internal-token)
  • the request.query object (by default INTERNAL_TOKEN)

It will then use the found value and compare it with the local variable (named INTERNAL_TOKEN by default) stored as an environment variable (remember that this value must be protected and never stored on a public or unsafe location).

const Koa = require("koa");
const {internalToken} = require("kane-internal-token");

const app = new Koa();
app.use(internalToken());

This will add the boolean "isInternal" in production only (using NODE_ENV) indicating if the shared secret is matched:

if (ctx.state.isInternal) {
    // do stuff
}

By default if both tokens mismatch an error response is returned. You can prevent that using the options parameter:

// check the `defaults` object in index.js
const opts = {
    wrongTokenAsError: false
};
app.use(internalToken(opts));

There's also an utility function to retrieve the token value as an header object to pass on a request to an internal API:

// it could be any HTTP client
const axios = require("axios");
const {internalHeader} = require("kane-internal-token");

const header = internalHeader();
const instance = axios.create({
  baseURL: "https://internal-api.example.com/",
  headers: header
});

Linting

Made using eslint. To enforce rules to be applied, use yarn lint:fix.

Testing

Invoke yarn test.

Contributing

First, install the dependencies using yarn:

yarn install --frozen-lockfile

Verify that your project is configured correctly by launching tests:

yarn test

Before you start coding make sure that you've read our CONTRIBUTING guide!