Socket
Socket
Sign inDemoInstall

readable-tokens

Package Overview
Dependencies
3
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    readable-tokens

Readable tokens for Node.js


Version published
Weekly downloads
81
increased by1057.14%
Maintainers
1
Install size
184 kB
Created
Weekly downloads
 

Changelog

Source

1.1.0 (2024-04-25)

Features

  • allow consumers to pass in their own PRNG (4e5ccad)

Bug Fixes

  • ensure prng returns a promise (38d9be5)

Readme

Source

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');
        },
    },
});

Keywords

FAQs

Last updated on 25 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc