@alsanium/jwt
v1.0.0-alpha.6
Published
JSON Web Token implementation for Node.js.
Downloads
18
Maintainers
Readme
@alsanium/jwt
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 conveniencenpm install --save ms
Install
uuid
for conveniencenpm 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
- RFC 7519 is the reference documentation about JSON Web Token. For more advanced details about how JSON Web Token works internally and in which ecosystem it exists, see also RFC 7515, RFC 7516, RFC 7517 and RFC 7518.
- JWT.IO also provides a gentle introduction to JSON Web Token.
- To learn more about the list of available JSON Web Token signing algorithms along with standard methods to generate signing keys, see also the Cryptographic Key Generation documentation.
- If you have any problems with, or questions about this package, please contact us through a GitHub issue.
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.
About
This package is created and maintained by @fsenart.
The names and logos for Alsanium are trademarks of Alsanium, S.A.S.