@ingestkorea/util-jsonwebtoken
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

@ingestkorea/util-jsonwebtoken

npm (scoped) NPM downloads

Description

INGESTKOREA Utility JsonWebToken Handler for Node.js.

Installing

npm install @ingestkorea/util-jsonwebtoken

Getting Started

Pre-requisites

  • Use TypeScript v4.x
  • Includes the TypeScript definitions for node.
    npm install -D @types/node # save dev mode

Support Algorithms

  • ES256 (ECDSA using P-256 curve and SHA-256 hash algorithm)

Create KeyPair

import { writeFileSync } from 'node:fs';
import { createJsonWebKeyEC256 } from '@ingestkorea/util-jsonwebtoken';
(async () => {
  const dist = __dirname + '/' + 'keyInfo.json';
  const keyInfo = await createJsonWebKeyEC256();
  writeFileSync(dist, JSON.stringify(keyInfo, null, 2), 'utf-8');
  console.log({ dist, keyInfo });
  // {
  //   dist: DIST_FILE_PATH,
  //   keyInfo: KEY_INFO
  // }
})();

Import

import {
  JsonWebTokenClient,
  CreateTokenHandlerInput,
  VerifyTokenHandlerInput
} from '@ingestkorea/util-jsonwebtoken';
import { publicKey, privateKey } from './keyInfo.json';

Usage

CreateToken

  • Initiate client with configuration.(sign, privateKey)
// a client can be shared by different CreateTokenHandlerInput params.
const client = new JsonWebTokenClient({
  mode: 'sign',
  credentials: {
    privateKey: privateKey
  },
  options: { // optional
    issuer: 'hello-world.com', // default your hostname
    expiresIn: 3600 * 3, // default 3600
    serviceName: 'sample-servie' // default "USER_NAME token service"
  }
});

(async () => {
  try {
    let params: CreateTokenHandlerInput = {
      userName: 'John Doe', // sample
      isAdmin: true // sample
    };
    let data = await client.create(params);
    console.log(data)
} catch (err) {
    console.log(err);
  };
})();

VerifyToken

  • Initiate client with configuration.(verify, publicKey)
// a client can be shared by different VerifyTokenHandlerInput params.
let client = new JsonWebTokenClient({
  mode: 'verify',
  credentials: {
    publicKey: publicKey
  }
});

(async () => {
  let inputToken = 'xxx.yyy.zzz';
  let params: VerifyTokenHandlerInput = {
    token: inputToken
  };
  try {
    let data = await client.verify(params);
    console.log(data)
  } catch (err) {
    console.log(err);
  };
})();

License

This Utility is distributed under the MIT License, see LICENSE for more information.

Readme

Keywords

Package Sidebar

Install

npm i @ingestkorea/util-jsonwebtoken

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

47.7 kB

Total Files

45

Last publish

Collaborators

  • ingestkorea