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

session-id-token

v1.2.0

Published

An utility library for generating digitally signed and base64 encoded session token based on cryptographically random session ID

Downloads

56

Readme

Session ID Token

An utility library for generating digitally signed and base64 encoded session token based on cryptographically random session ID

What Is a Session Token?

A session token is just a string, but there are two common options for what this string should contain and how it should be formatted

JSON Web Token (JWT)

JWT is a JSON object containing several fields that are digitally-signed, base64-encoded, and optionally encrypted if the embedded data contains sensitive information. It embeds all session state data into the token itself. If you have the token, you can decode it to get the data

Pros:

  • No need for a shared session store

Cons:

  • Irrevocable: since there is no shared session store on the server-side, we can't revoke it
  • Much larger on the wire: since JWT embeds all of the session state data, as opposed to just a session ID, they are noticeably larger. This adds size overhead to every request
  • Must be encrypted if they contain sensitive info: JWT often needs to be encrypted and decrypted as well, which not only increases the size, but also adds time overhead for the decryption on every request

If you are interested in this session token option, check out this library: https://github.com/auth0/node-jsonwebtoken

Digitally Signed Session ID

The second option for session token is digitally signed and base64 encoded session token based on cryptographically random session ID. It serves as a key to unlock our session store which stores our actual session state data

  • Digitally signed: The token not only contains this cryptographically random session ID, but also a digital signature that the server can use to verify that the ID hasn't been modified since it was signed. Typically the token is a combination of the first 32 bytes of the random session ID and the remaining bytes for the digital signature
  • Base64 encoded: a technique for translating binary bytes that might be well beyond the ASCII range into distinct ASCII characters. Cryptographically random IDs and digital signatures are both binary data, but HTTP wants plain text, so we need to encode the binary data into base64 text
  • Cryptographically random: Simple random number generator generates random numbers that are actually predictable; Cryptographic random number generator generates absolutely unpredictable random numbers

Pros:

  • Completely opaque: Since the session ID is just random bytes, it reveals nothing about the user or the actual session data
  • Efficient: since the token is just a key to data stored on the server-side, it is relatively small and thus doesn't add a lot of overhead to request/response messages
  • Secure: the digital signature ensures that attackers can't modify the session token
  • Easily revocable: when the user signs out, or when an administrator wants to end the session, we can simply delete the session data in the session store. If someone tries to use that token again, the server won't find any data in the session store
  • Easily expireable: if we use a session store like Redis, we can set an expiry time on the key, after which Redis will automatically delete session data

Cons:

  • Requires round-trip between your web server and the session store on each request containing a valid token
  • The session store can become a single point of failure

Usage

This package has 0 dependency :)

import { generateSessionToken, validateSessionToken } from 'session-id-token';

const SIGNING_KEY = 'My secret';
const sessionToken = generateSessionToken(SIGNING_KEY); // returns string
const isValid = validateSessionToken(sessionToken); // returns boolean