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

cybertoken

v2.1.0

Published

A token format for APIs inspired by the GitHub's API token format.

Downloads

41

Readme

cybertoken CI CD npm badge

A token format inspired by GitHub's new API token format. Think of it as a standardized password format with some handy properties. Intended for API token and password generation.

What is this?

GitHub changed their token format a while back and explained the reasons in an insightful blog post[^1].

tl;dr:

  1. Having a defined format for secrets is good for automated secret scanning.
  2. Putting some prefix to a secret makes debugging easier when looking at some random token.
  3. Using _ instead of - is better for double-click text selection: abc_def vs abc-def.
  4. Using Base62[^2] instead of Base64 results in less encoding errors when passing the token somewhere.
  5. CRC32 checksum at the end of the token for faster offline syntactic check of a token (used as a heuristic, not for security).

These properties make this token format suitable for things like API tokens, which is what cybertoken is.

Usage

Install:

npm install cybertoken
import { createTokenGenerator } from "cybertoken";

const apiTokenGenerator = createTokenGenerator({
  prefixWithoutUnderscore: "contoso",
});

const token = apiTokenGenerator.generateToken();
// Securely hash `token` and save it in your database. Return `token` to the user once.

console.log(token);
// contoso_IvN2xEuHUDjQ3PNQgZkILWc7GUxpmThDD410NQxJalD5GjY

Bonus

You can also use cybertokens as passwords. If you use GitHub, you can set up a custom secret scanning pattern that will prevent anyone from pushing anything that looks like a cybertoken that your org uses.

For example, use this pattern for Cybertokens with the prefix contosopass:

contosopass_[0-9A-Za-z]{10,}

CLI Usage

The npm package also offers a command-line utility to generate tokens.

npm install -g cybertoken

Usage:

cybertoken <prefix>

# example:
cybertoken test
test_KOK5QxQr4GBGk97X8Ij5ZnyZO24kMIEiW1LdUYIqEj7A5Nv

You can also provide the token prefix via an env variable:

CYBERTOKEN_PREFIX=foo cybertoken

To make thing easier, you can put this in your .bashrc:

echo "export CYBERTOKEN_PREFIX=foo" >> ~/.bashrc

...and now you can just use cybertoken!

Anatomy

This is what a cybertoken consists of:

  prefix      base62(n-bytes + v + crc32(n-bytes + v))
┌────────┐ ┌──────────────────────────────────────────────┐
myapitoken_161YNJQOaoFoWLaoXeMVHYyrSLhGPOjSBYBtxY6V3GqFFZKV

Explanation:

  • n-bytes are n random bytes, but at least 21 (default: 22), generated by a cryptographically secure random source
  • v is a single byte containing the version of the cybertoken format used (currently, only 0x00)
  • The bytes are concatenated with the version and their CRC32 sum before base62 encoding
  • base62 alphabet used: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

[^1]: GitHub's blog post covering this: https://github.blog/2021-04-05-behind-githubs-new-authentication-token-formats [^2]: More info in Base62: https://en.wikipedia.org/wiki/Base62