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

zd-capability-param

v2.0.3

Published

Library to create, validate and decode capability url tokens.

Downloads

9

Readme

zd-capability-param

Library to create, validate and decode capability url tokens.

Build Status

Known Vulnerabilities Coverage Status

You know, this "click on this link to validate your email account" or "click to recover your account".
Generated tokens are:

  • Difficult to guess/tamper
  • Limited lifetime
  • Valid URL

Difficult to guess/tamper

Because TTL + payload is signed with HMAC.

Limited lifetime

The link includes an expiration date. This expiration date is included in the HMAC signature.

Secret rotation

The HMAC secret could be easily rotated by just "pushing" more secrets. Each secret will be tested if the TTL looks good. For this reason it is important to set a low maxSecrets number (i.e 5).
Older secrets will get discarded.

Valid URL

Token is kept short enough by encoding it as Base64. This Base64 string is also URL-encoded to prevent breaking urls i.e by adding &. .

Install

$ npm install zd-capability-param

Usage

Encode/decode

// create a token valid for 48 hours
const lifetime = 2 * 24 * 60; // 48 HOURS
const param = capabilityParam.create('secret', {lifetime});
const toEncode = {user: 'alice@localhost'};
// encode
const token = param.encode(toEncode);
console.log('token', token);
// prints
// token 27376691.eyJ1c2VyIjoiYWxpY2VAbG9jYWxob3N0In0%3D.7cf77z2h%2Bz0cR%2Brc2hVO5L8cV7Q05pfanwMxqHTM9Qc%3D

// decode
let result;
try{
	result = param.decode(token);
}catch(e){
	if(e instanceof ExpiredCapabilityError){
	// capability param has expired i.e link is too old
		console.log(`token expired at ${e.expired}`);
		return;
	}
	if(e instanceof InvalidCapabilityError){
		// capability param has expired i.e link is too old
		console.log(`capability is not valid: "${e.capabilityParam}"`);
		return;
	}
}
console.log('user', result.user);
// prints user alice@localhost

Check express example

API

capabilityParam.create(secret, [options])

  • #secret Could be an string or an array of strings
  • #options Optional
    • lifetime Token lifetime in minutes. Default is 48 hours.
    • algorithm A hash algoritm. Default sha256.
    • maxSecrets Max number of secrets to keep. See secret rotation. Default is 5.
  • Returns {encode(payload), decode(token), pushSecret(secret)}

The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are 'sha256', 'sha512', etc. On recent releases of OpenSSL, openssl list -digest-algorithms (openssl list-message-digest-algorithms for older versions of OpenSSL) will display the available digest algorithms.

Secret rotation

// Provide a secret set on creation
const secrets = ['currentSecret','oldSecret'];
const param = capabilityParam.create(secrets, {maxSecrets:2});
// Add a new secret
param.pushSecret('new current secret');
// Because maxSecrets === 2 'oldSecret' is no longer valid.
// Valid secrets are 'new current secret'  and 'currentSecret'
// New tokens are signed with 'new current secret'

Security warning

The payload is signed with HMAC and encoded to Base64 but IT IS NOT ENCRYPTED. Anyone can "see" the contents of the payload by decoding the string.