@alsanium/jwt

1.0.0-alpha.6 • Public • Published

@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("john.doe@example.com")::
  withAudience(["service1@example.com", "service2@example.com"])::
  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("john.doe@example.com") &&
      djwt::isIntendedFor("service1@example.com") &&
      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.

Package Sidebar

Install

npm i @alsanium/jwt

Weekly Downloads

6

Version

1.0.0-alpha.6

License

Unlicense

Last publish

Collaborators

  • fsenart