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

readable-tokens

v1.2.0

Published

Readable tokens for Node.js

Downloads

87

Readme

Readable Tokens

Inspired by GitHub Tokens, and Stripe, this library allows the ability to create random tokens that are "readable" by humans as well as verifiable.

Usage

Creating a random token

By default, the generate function will take a single argument representing the prefix of the token. A formatted token is then returned.

const { Crc32Token } = require('readable-tokens');

// generate a token
const token = await Crc32Token.generate('prefix'); // prefix_xxxxxxx

Creating a token from a UUID

Often our applications will use UUIDs internally to identify objects in databases. This library can be used to represent a UUID as a readable and verifiable token:

const { Crc32Token } = require('readable-tokens');

const token = await Crc32Token.generate('prefix', '8ece30ba-b1fc-4944-8758-75b20ebc1cc7'); // test_KNJYokHOindxbwRAd4MRNhPA6a5

NB: It's important to note that this library isn't hiding or making the UUID a secret, it's just a different format to represent the data that is encoded in a UUID.

Token Types

There are two types of tokens available in the library by default. A "plain" ReadableToken, which has no integrity checking, which means it is faster to generate, but at the trade-off that there is no ability to check the integrity of tokens without hitting a database or token store. This could be something worth thinking about when you're thinking of trying to reject tokens at the edge, or when you want to be able to use efficient secret scanning.

As inspired by GitHub tokens, and to provide a way to be able to do some quick offline validation of tokens, there is the Crc32Token, which integrates a CRC32 check value into the token value.

The token encoding is inspired by the GitHub tokens; they have a prefix, and then arbitrary binary data that is then encoded in base62, for a better developer experience (easy copy & paste).

Custom tokens

The library allows you to provide your own encoder if you wish to use a different encoding scheme or alphabet. There is also the ability to provide your own integrity checking logic if you don't want to use CRC32.

Here is an example of a token that uses a truncated HMAC for integrity checking and native Base64 for encoding:

const { createHmac, timingSafeEqual } = require('crypto');
const { ReadableTokenGenerator } = require('readable-tokens');

function truncatedHash(val) {
    const hash = createHmac('sha256', 'secret').update(val).digest();
    const offset = hash[hash.length - 1] & 0x0F;
    const truncated = (hash[offset] & 0x7F) << 24 |
        (hash[offset + 1] & 0xFF) << 16 |
        (hash[offset + 2] & 0xFF) <<  8 |
        (hash[offset + 3] & 0xFF);
    const buf = Buffer.alloc(4);
    buf.writeUInt32LE(truncated);
    return buf;
}

const customTokenType = new ReadableTokenGenerator({
    // encode as base64 using native buffer support
    encoder: {
        encode: (val) => Buffer.from(val).toString('base64').replace(/=+$/, ''),
        decode: (val) => Buffer.from(val, 'base64'),
    },
    integrity: {
        generate(val) {
            return Buffer.concat([val, truncatedHash(val)]);
        },
        check(val) {
            // everything up to the last 4 bytes is the raw data
            const payload = val.subarray(0, -4);
            const check = val.subarray(-4);
            if (timingSafeEqual(truncatedHash(val), check)) {
                // all good
                return payload;
            }
            throw new Error('HMAC did not validate');
        },
    },
});