From 197f6fbbcd12ac1371140d299db2665c45098acc Mon Sep 17 00:00:00 2001 From: Darren Liew Date: Sun, 4 Aug 2024 14:46:38 +0800 Subject: [PATCH 1/2] added js module and typescript support --- index.js | 6 +++--- index.mjs | 2 ++ package.json | 4 +++- test.mjs | 36 ++++++++++++++++++++++++++++++++++++ types/index.d.ts | 30 ++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 index.mjs create mode 100644 test.mjs create mode 100644 types/index.d.ts diff --git a/index.js b/index.js index a5f79cb..253808b 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ -const fs = require('fs') -const path = require('path') -const crypto = require('crypto') +const fs = require('node:fs') +const path = require('node:path') +const crypto = require('node:crypto') const ecdsa = require('ecdsa-sig-formatter') const SEP = '.' diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..258cb52 --- /dev/null +++ b/index.mjs @@ -0,0 +1,2 @@ +const pJWT = require('./index') +export default pJWT diff --git a/package.json b/package.json index 6217b86..21f41fe 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,10 @@ { "name": "pico-jwt", - "version": "0.3.3", + "version": "0.4.0", "description": "A pico-sized JWT library", + "type": "commonjs", "main": "index.js", + "module": "index.mjs", "scripts": { "test": "DEBUG=1 node test.js", "lint": "eslint .", diff --git a/test.mjs b/test.mjs new file mode 100644 index 0000000..5f8710e --- /dev/null +++ b/test.mjs @@ -0,0 +1,36 @@ +// import module +import { fileURLToPath } from 'node:url' +import { dirname } from 'node:path' +import pJWT from './index.js' + +const dir = dirname(fileURLToPath(import.meta.url)) + +// instantiate the module +const jwt = new pJWT('RS256') + +// add key files asynchronous (absolute path only) +jwt.addKeys(dir + '/test.key', dir + '/test.key.pub', () => { + console.log('loaded') + + const rawHeader = { + kid: 'custom-header-key-id' + } + const rawPayload = { + iss: 'pico', + aud: 'world' + } + + // create jwt with payload + const token = jwt.create(rawPayload, rawHeader) + + // get header of jwt + const header = jwt.header(token) // or pJWT.prototype.header(token) + console.log('jwt header extract:', rawHeader.kid === header.kid) + + // get payload of jwt + const payload = jwt.payload(token) // or pJWT.prototype.payload(token) + console.log('jwt payload extract:', rawPayload.aud === payload.aud) + + // verify jwt + console.log('jwt validation:', jwt.verify(token)) +}) diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..2f61ad3 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,30 @@ +/// + +declare module 'pico-jwt' { + import { KeyObject } from 'crypto'; + + type Callback = (err: Error | null, key?: string | Buffer | undefined) => void; + type Algorithm = 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512' | 'PS256' | 'PS384' | 'PS512'; + + interface JWTHeader { + typ?: string; + alg: Algorithm; + [key: string]: any; + } + + interface JWTPayload { + [key: string]: any; + } + + class JWT { + constructor(algo: Algorithm, secret: string | Buffer, key?: string | Buffer); + addKeys(secret: string | Buffer, key?: string | Buffer, cb?: Callback): void; + create(payload: JWTPayload, header?: Partial): string; + header(jwt: string): JWTHeader | undefined; + payload(jwt: string): JWTPayload | undefined; + verify(jwt: string): boolean; + } + + export = JWT; +} + From d546691aad2fb64868a8998e75e193f8c4af1984 Mon Sep 17 00:00:00 2001 From: Darren Liew Date: Sun, 4 Aug 2024 14:53:34 +0800 Subject: [PATCH 2/2] updated readme --- README.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b4e038b..c3d8577 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,23 @@ ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm Please note that PSXXX only works on Node 6.12+ (excluding 7.x). ## Test -`npm test` + +### Feature Test +```bash +npm test +``` + +### ESM Test +```bash +node test.mjs +``` ## Example ```javascript // import module -const pJWT = require('pico-jwt') +import pJWT from 'pico-jwt' +// supported commonjs require as well +// const pJWT = require('pico-jwt') // instantiate the module const jwt = new pJWT('HS256', 'secretKey')