1.0.20 • Published 7 months ago

@shgysk8zer0/jwk-utils v1.0.20

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@shgysk8zer0/jwk-utils

Use JWK and JWTs using the Crypto API JWT Compatible

CodeQL Node CI Lint Code Base

GitHub license GitHub last commit GitHub release GitHub Sponsors

npm node-current npm bundle size gzipped npm

GitHub followers GitHub forks GitHub stars Twitter Follow

Donate using Liberapay


Installation

npm i @shgysk8zer0/jwk-utils

About

This library provides JWK and JWT support via the crypto API.

Supported Algorithms

  • RS256
  • RS384
  • RS512
  • ES256
  • ES384
  • ES512
  • HS256
  • HS384
  • HS512
  • PS256
  • PS384
  • PS512
  • EdDSA

!Note EdDSA is currently experimental in Node.js and is only suported in Safari. See Browser Compatibility on MDN.

Not Supported

  • ES256K

Example

import { generateJWK, createJWT, verifyJWT } from '@shgysk8zer0/jwt-jwk';

// Generate a JWK pair
const { publicKey, privateKey } = await generateJWK();

// JWTs use Unix timestamps - seconds, not ms.
const now = Math.floor(Date.now() / 1000);
// Create a JWT
const token = await createJWT({
  iss: 'Some issuer',
  sub: 'The Subject',
  iat: now,
  exp: now + 60,
  nbf: now,
  jti: crypto.randomUUID(),
  scope: 'api',
  entitlements: ['db:read'],
}, privateKey);

// Verify the JWT
const verifiedPayload = await verifyJWT(token, publicKey, { entitlements: ['db:read'] });

Limitations

Due to using JWKs and public/private keys, this currently does not support algorithms not suppported by crypto.subtle.

!Note Polyfills, especially for Unit8Array.fromBase64() & Uint8Array.prototype.toBase64() are required. They are provided by @shgysk8zer0/polyfills, which is imported in the main package (@shgysk8zer0/jwk-utils). However, for compatibility with client-side usage and to avoid conflicts, it is not imported by direct imports.

1.0.19

7 months ago

1.0.18

7 months ago

1.0.17

7 months ago

1.0.20

7 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago