From 910e19d690579c4d045145f8dfc85b2914c684c5 Mon Sep 17 00:00:00 2001 From: nvms Date: Wed, 26 Mar 2025 21:56:12 -0400 Subject: [PATCH] README --- packages/jwt/README.md | 67 ++++++++++++++++++++++++++++++------------ packages/ms/README.md | 4 --- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/packages/jwt/README.md b/packages/jwt/README.md index b37beb3..42523d6 100644 --- a/packages/jwt/README.md +++ b/packages/jwt/README.md @@ -1,43 +1,72 @@ -# jwt +# @prsm/jwt -A package for encoding, decoding, and verifying JWTs. +[![NPM version](https://img.shields.io/npm/v/@prsm/jwt?color=a1b858&label=)](https://www.npmjs.com/package/@prsm/jwt) -# Installation +A lightweight JWT implementation for encoding, decoding, and verifying JSON Web Tokens. -`npm install @prsm/jwt` +## Installation -## Encoding +```bash +npm install @prsm/jwt +``` + +## Usage + +### Encoding Tokens ```typescript import { encode } from "@prsm/jwt"; -const payload = { - iat: Date.now(), - exp: Date.now() + 3600, -}; +// Create a token with standard claims +const token = encode( + { + sub: "user123", + iat: Date.now(), + exp: Date.now() + 3600000, // 1 hour from now + }, + "your-secret-key" +); -const token = encode(payload, process.env.SIGNING_KEY); +// Specify algorithm (default is HS256) +const rsaToken = encode(payload, privateKey, "RS256"); ``` -## Verifying +### Verifying Tokens ```typescript import { verify } from "@prsm/jwt"; -const result = verify(token, process.env.SIGNING_KEY); +const result = verify(token, "your-secret-key"); -if (!result.sig) throw new Error("signature verification failed"); -if (result.exp) throw new Error("token has expired"); -if (!result.nbf) throw new Error("token is not yet valid") +// Check verification results +if (!result.sig) { + console.error("Invalid signature"); +} -// token payload is available at result.decoded.payload +if (result.exp) { + console.error("Token expired"); +} + +// Access the decoded payload +const { sub, iat } = result.decoded.payload; ``` -## Decoding +### Decoding Without Verification ```typescript import { decode } from "@prsm/jwt"; -const result = decode(token); -// { header: { alg: "HS256", typ: "JWT" }, payload: { iat: 123456789, exp: 123456789 }, signature: "..." +const { header, payload, signature } = decode(token); ``` + +## Supported Algorithms + +| Algorithm | Description | +|-----------|--------------------------------| +| HS256 | HMAC with SHA-256 (default) | +| HS384 | HMAC with SHA-384 | +| HS512 | HMAC with SHA-512 | +| RS256 | RSA Signature with SHA-256 | +| RS384 | RSA Signature with SHA-384 | +| RS512 | RSA Signature with SHA-512 | +| ES256 | ECDSA Signature with SHA-256 | diff --git a/packages/ms/README.md b/packages/ms/README.md index d1226db..cdf3a38 100644 --- a/packages/ms/README.md +++ b/packages/ms/README.md @@ -8,10 +8,6 @@ A lightweight utility for parsing and converting time strings to milliseconds an ```bash npm install @prsm/ms -# or -yarn add @prsm/ms -# or -pnpm add @prsm/ms ``` ## Usage