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

@astronautlabs/jwt

v1.1.0

Published

An isomorphic JWT library (works in browser and Node.js)

Downloads

97

Readme

@/jwt

This library is Alpha quality in the 0.0.x series (no automatic updates by semver). Please take caution if you choose to use it, and do not use it in production. We welcome PRs for fixes, features and general improvements.

A simple isomorphic JWT library (works in browser and Node.js) with support for signing and verifying JWTs using a number of common algorithms.

Installation

npm install @astronautlabs/jwt

Usage

Common Options

  • now -- Specify the UNIX wall clock time to use when enforcing exp claims. When not specified, Date.now() is used.
  • algorithm -- The signature algorithm to use. See Supported Algorithms for the options.
  • secretOrKey -- The key to use for the operation. When using asymmetric algorithms (like RS256, ES256, etc) you should pass public keys for validate() and private keys for encode()

Signing

    async encode(claims: any, options: EncodeOptions): Promise<Token>

Remarks

Returns a Token object with the given claims, and signed by the credentials specified in options (see algorithm and secretOrKey).

Example

import { JWT } from '@astronautlabs/jwt';

try {
    let token = await JWT.encode({ sub: 123 }, { algorithm: 'HS256', secretOrKey: 'stuff' });
    console.dir(token); // => { string: 'eY...', claims: { sub: ..., ... } }
} catch (e) {
    console.error('Failed to validate token: ');
    console.error(e);
}

Validation

JWT.validate(string : string, options: DecodeOptions): Promise<Token>

Returns a Token object if the given string is a valid (and trusted) JWT. If validation fails, throws an Error.

Types of errors JWT.validate() can throw:

  • Algorithm mismatch: If the token header's alg claim does not match the configured algorithm (options.algorithm)
  • Signature mismatch: If the signature does not match
  • Expiration: If the token's exp claim is not acceptable according to policy
import { JWT } from '@astronautlabs/jwt';

try {
    let token = await JWT.validate(`eY...`, { algorithm: 'HS256', secretOrKey: 'stuff' });
    console.dir(token); // => { string: 'eY...', claims: { sub: ..., ... } }
} catch (e) {
    console.error('Failed to validate token: ');
    console.error(e);
}

Expiration

You can configure a policy for built-in validation of the exp claim when validating tokens. To do so, specify { validate: { exp: "(policyName)" }} within the options passed to JWT.validate().

The available policies are:

  • when-present (default) -- When a token has an exp claim, fail validation if the token is expired
  • force -- Require tokens to have a valid (fresh) exp claim
  • ignore -- Ignore exp even when it is present (it will still be available on token.claims).

You can override the current time by providing options.now. Consider using this instead of options.validate.exp = 'ignore'.

Options

  • validate

Supported Platforms

  • Browser/Web using WebCrypto
  • Node.js

Supported Algorithms

  • HS256
  • HS384
  • HS512
  • RS256
  • RS512
  • ES256