This commit is contained in:
nvms 2025-03-26 21:56:12 -04:00
parent fc2f2bea12
commit 910e19d690
2 changed files with 48 additions and 23 deletions

View File

@ -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 ```typescript
import { encode } from "@prsm/jwt"; import { encode } from "@prsm/jwt";
const payload = { // Create a token with standard claims
const token = encode(
{
sub: "user123",
iat: Date.now(), iat: Date.now(),
exp: Date.now() + 3600, 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 ```typescript
import { verify } from "@prsm/jwt"; 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"); // Check verification results
if (result.exp) throw new Error("token has expired"); if (!result.sig) {
if (!result.nbf) throw new Error("token is not yet valid") 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 ```typescript
import { decode } from "@prsm/jwt"; import { decode } from "@prsm/jwt";
const result = decode(token); const { header, payload, signature } = decode(token);
// { header: { alg: "HS256", typ: "JWT" }, payload: { iat: 123456789, exp: 123456789 }, signature: "..."
``` ```
## 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 |

View File

@ -8,10 +8,6 @@ A lightweight utility for parsing and converting time strings to milliseconds an
```bash ```bash
npm install @prsm/ms npm install @prsm/ms
# or
yarn add @prsm/ms
# or
pnpm add @prsm/ms
``` ```
## Usage ## Usage