GithubHelp home page GithubHelp logo

mokuteki225 / jwt Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 85 KB

Library created for education purposes to sign and decode JWT tokens using symmetric and asymmetric algorithms (HS256, RS256). Dependency free

License: MIT License

JavaScript 100.00%
asymmetric-cryptography hs256 jsonwebtoken jwt node rs256 symmetric-cryptography dependency-free

jwt's Introduction

@mokuteki/jwt

Zero dependency typescript friendly Node.js library designed to hash/decode JSON Web Tokens using symmetric/asymmetric algorithms

Installation

npm i @mokuteki/jwt

Quick start

  • Pick one hashing strategy out of existing ones
  • Pass the strategy to JSONWebToken constructor
  • Use created instance to generate/verify JWTs

HS256 Strategy

const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');

const strategy = new HS256Strategy({
  ttl: 10000,
  secret: 'YOUR_SECRET',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

const token = jwt.generate(payload);
const decoded = jwt.verify(token);

console.log(decoded); // { id: 1 }

RS256 Strategy

const { JSONWebToken, RS256Strategy } = require('@mokuteki/jwt');

const strategy = new RS256Strategy({
  ttl: 10000,
  publicKey: 'YOUR_PUBLIC_KEY',
  privateKey: 'YOUR_PRIVATE_KEY',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

const token = jwt.generate(payload);
const decoded = jwt.verify(token);

console.log(decoded); // { id: 1 }

Override predefined options

const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');

const strategy = new HS256Strategy({
  ttl: 10000,
  secret: 'YOUR_SECRET',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

/**
 * Override JWT ttl option
 */
const token = jwt.generate(payload, { ttl: 1000 });

setTimeout(() => {
  try {
    const decoded = jwt.verify(token);
  } catch (err) {
    // err instanceof JwtExpiredError
  }
}, 2000);
const { JSONWebToken, HS256Strategy } = require('@mokuteki/jwt');

const strategy = new HS256Strategy({
  ttl: 10000,
  secret: 'YOUR_SECRET',
});

const jwt = new JSONWebToken(strategy);
const payload = { id: 1 };

const token = jwt.generate(payload, { ttl: 20000, secret: 'NEW_SECRET' });

try {
  const decoded = jwt.verify(token);
} catch (err) {
  // err instanceof InvalidSignatureError
}

License

Licensed under MIT

jwt's People

Contributors

vkondratiuk482 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

jwt's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.