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

@alsanium/jwt

v1.0.0-alpha.6

Published

JSON Web Token implementation for Node.js.

Downloads

22

Readme

@alsanium/jwt

NPM version Dependencies status Dev dependencies status Travis CI status Codecov status

JSON Web Tokens are an open and industry standard method for representing claims securely between two parties. This package allows you to forge, encode, decode, sign, verify, validate and inspect JWT.

Usage

To compact token

import ms from "ms";
import uuid from "uuid";
import { token, encode } from "@alsanium/jwt/core";
import { HS256 } from "@alsanium/jwt/signature";
import {
  withAlgorithm, withId, withIssuer, withSubject, withAudience, withPayload,
  withIssueTime, withActivationTime, withExpirationTime
} from "@alsanium/jwt/operator"

// construct
let ijwt = token()::
  withAlgorithm("HS256")::
  withId(uuid.v4())::
  withIssuer("http://example.com")::
  withSubject("[email protected]")::
  withAudience(["[email protected]", "[email protected]"])::
  withPayload("name", "John Doe")::
  withPayload("admin", true)::
  withIssueTime(Date.now())::
  withActivationTime(Date.now() + ms("1 hour"))::
  withExpirationTime(Date.now() + ms("1 day"));

// encode
let ejwt = encode(ijwt);

// sign
let sjwt = HS256.sign(ejwt, "secret");

From compact token

import ms from "ms";
import { decode } from "@alsanium/jwt/core";
import { HS256 } from "@alsanium/jwt/signature";
import {
  isIssuedBy, isSubmittedBy, isIntendedFor,
  isFresh, isActive, isLive
} from "@alsanium/jwt/validator";
import { payload } from "@alsanium/jwt/operator";

// verify
if (!HS256.verify(sjwt, "secret")) {
  throw new Error("invalid token.");
}

// decode
let djwt = decode(sjwt);

// validate
const LEEWAY  = ms("2 minutes"),
      MAX_AGE = ms("10 days");

if (!(djwt::isIssuedBy("http://example.com") &&
      djwt::isSubmittedBy("[email protected]") &&
      djwt::isIntendedFor("[email protected]") &&
      djwt::isFresh(MAX_AGE, LEEWAY) &&
      djwt::isActive(LEEWAY) &&
      djwt::isLive(LEEWAY)
   )) {
  throw new Error("invalid token.");
}

// inspect
console.log(`name: ${djwt::payload("name")}`);
console.log(`admin: ${djwt::payload("admin")}`);

Installation

  • Install @alsanium/jwt

    npm install --save @alsanium/jwt@^1.0.0-alpha
  • Install ms for convenience

    npm install --save ms
  • Install uuid for convenience

    npm install --save uuid

Dependencies

| Name | Version | | ------------------------------------ | ------- | | immutable | 3.7.6 |

  • This package is written with tomorrow's JavaScript syntax and uses Babel for transpilation. As current Node.js versions do not fully support new ECMAScript specifications, you may need the polyfill provided by Babel.

  • This package uses heavily immutable collections. Immutable package provides persistent immutable List, Stack, Map, OrderedMap, Set, OrderedSet and Record.

Alternatives

JWT.IO provides a (not exhaustive) list of libraries for token signing and verification in different languages.

Critical vulnerabilities exist in some JSON Web Token libraries with asymmetric keys.

API

The full list of available methods along with their documentations and examples are available in the API Reference documentation.

Help

Contributing

You are invited to contribute new features, fixes, or updates, large or small; we are always thrilled to receive pull requests, and do our best to process them as fast as we can.

Before you start to code, we recommend discussing your plans through a GitHub issue, especially for more ambitious contributions. This gives other contributors a chance to point you in the right direction, give you feedback on your design, and help you find out if someone else is working on the same thing.

Thank you, contributors!

License

This is free and unencumbered software released into the public domain.

You can check out the full license here or here.

About

This package is created and maintained by @fsenart.
The names and logos for Alsanium are trademarks of Alsanium, S.A.S.