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

hapi-api-secret-key

v1.1.0

Published

Protect your API tagged routes with a simple set of known keys. Useful for locking down useage from API managers

Downloads

21

Readme

Build Status

About

The use case that this HAPI plugin solves is simple: given a simple set of API keys, requests made to the routes marked as "api" should be protected with only the listed API keys.

This allows the API to be hosted behind an API Manager so that key rotation may occur.

Default Behavior

  1. API Secrets are usually loaded by the plugin with the following pseudo code:

    var secrets = [];
    var curr;
    while (curr = process.env['API_KEY_' + (secrets.length + 1)]) {
      secrets.push(curr);
    }
  2. Callee's are responsible for providing the secret either by the query string or HTTP-header named "api-key"

  3. Only the routes which have been decorated with "api" tag will be protected by this plugin.

In summary, the hapi-api-secret-key has two modes which is determined whether or not secrets are supplied to the plugin. These behaviors are documented as below.

| Secrets loaded? | Allow localhost access? | Allow remote access? | |-----------------|-------------------------|------------------------------------------------| | No | Yes | No | | Yes | No | Yes - only if supplied keys match a secret key |

Quick Start

Simply install the package.

npm install --save hapi-api-secret-key

Then configure your HAPI server to employ the plugin with the following configuration options

var server = new Hapi.Server();

server.register(
  // this is where you register all your HAPI plugins
  require('hapi-api-secret-key').plugin,
  function (err) {
    
    if (err) {
      throw err;
    }
    
    server.route({
      method: 'GET',
      path: '/protected/{name}',
      handler: function (request, reply) {
        reply({
          name: request.params.name
        });
      },
      config: {
        // we protect anything with the API tag by default
        tags: ['api']
      }
    });
    
    // below is standard start-up procedure of HAPI servers
    server.start(function(err) {
      
        if (err) {
            throw err;
        }

        console.log('Server running at:', server.info.uri);
      
    });
  }
);

Options?

With the above example, there are simple options which could be injected into the plugin, namely.

  • Secrets
  • Fetching the user supplied token/secret
  • Which routes to protect
var server = new Hapi.Server();

// by default, the plugin behaves as the options below
// however, you are able to selectively override the behavior
// of each option
var configOptions = {
  secrets: [
        process.env['API_KEY_1'],
        process.env['API_KEY_2']
        // and so on
    ],
  fetchUserApiKey: function (request) {
         return request.headers["api-key"] || request.query["api-key"];
      },
  shouldApplyApiFilter: function (request) {
        var tags = request.route.settings.tags;
        return tags && tags.indexOf('api') >= 0;
      }
};

server.register(
  // this is where you register all your HAPI plugins
  {
    register: require('hapi-api-secret-key').plugin,
    options: configOptions
  },
  function (err) {
    
    if (err) {
      throw err;
    }
    
    server.route({
      method: 'GET',
      path: '/protected/{name}',
      handler: function (request, reply) {
        reply({
          name: request.params.name
        });
      },
      config: {
        // we protect anything with the API tag by default
        tags: ['api']
      }
    });
    
    // below is standard start-up procedure of HAPI servers
    server.start(function(err) {
      
        if (err) {
            throw err;
        }

        console.log('Server running at:', server.info.uri);
      
    });
  }
);