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

@bencmbrook/prefixed-api-key

v4.0.0

Published

Generate prefixed API-keys

Downloads

4

Readme

Generate Prefixed API Keys

This is a fork of seamapi/prefixed-api-key. Seam-style API Keys have many advantages:

  • Double clicking the api key selects the entire api key
  • The alphabet is standard across languages thanks to the base58 RFC and its usage in cryptocurrencies
  • They are shorter than hex and base32 api keys
  • They have prefixes allowing secret scanning by github
  • They have a hashed component so the server doesn't need to store the api key (reducing attack surface)
  • They have unhashed Key IDs which can be mutually used by the server and key bearer/customer to identify the api key
  • They default to roughly the same number of entropy bits as UUIDv4

The Format

API keys look like this:

myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG

Let's break down each component of the API key...

myapp ..._...  BRTRKFsL ..._...  51FwqftsmMDHHbJAMEXXHCgG
^              ^                 ^
Prefix         Key ID            Secret
  • The Prefix is used to identify the company or service creating the API Key. This is very helpful in secret scanning.
  • The Key ID is stored by both the server and the key bearer/customer, it can be used to identify an API key in logs or displayed on a customer's dashboard. An apiKey can be blocklisted by its Key ID.
  • The Secret is how we authenticate this key. The secret is never stored on the server, but a hash of it is stored on the server. When we receive an incoming request, we search our database for key_id and hash(secret).

Getting Started

import { generateAPIKey } from '@bencmbrook/prefixed-api-key';

const key = await generateAPIKey({ keyPrefix: 'myapp' });

// Store the key.secretHash and key.keyId in your database and give
// key.apiKey to your customer.

console.log(key);
/*
{
  keyId: 'BRTRKFsL',
  secretHash: 'd70d981d87b449c107327c2a2afbf00d4b58070d6ba571aac35d7ea3e7c79f37',
  apiKey: 'myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG'
}
*/

Utility Methods

import {
  checkAPIKey,
  extractKeyId,
  extractSecret,
  getAPIKeyObject,
  hashSecret,
} from '@bencmbrook/prefixed-api-key';

hashSecret('51FwqftsmMDHHbJAMEXXHCgG');
// "d70d981d87b449c107327c2a2afbf00d4b58070d6ba571aac35d7ea3e7c79f37"

extractSecret('myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG');
// "51FwqftsmMDHHbJAMEXXHCgG"

extractKeyId('myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG');
// "BRTRKFsL"

getAPIKeyObject('myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG');
/*
{
  keyId: 'BRTRKFsL',
  secretHash: 'd70d981d87b449c107327c2a2afbf00d4b58070d6ba571aac35d7ea3e7c79f37',
  apiKey: 'myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG'
}
*/

checkAPIKey(
  'myapp_BRTRKFsL_51FwqftsmMDHHbJAMEXXHCgG',
  'd70d981d87b449c107327c2a2afbf00d4b58070d6ba571aac35d7ea3e7c79f37',
);
// true