@sourceregistry/node-jwt 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -138,6 +138,7 @@ Autodetection fails for unsupported keys:
138
138
  * Support **x5c/x5t** (X.509 cert chain + SHA-1 thumbprint)
139
139
  * Normalize **JWKS** with auto-generated `kid` and `x5t`
140
140
  * Resolve keys from **JWKS** by `kid` for verification
141
+ * Load remote **JWKS** with caching via `JWKS.fromWeb()`
141
142
 
142
143
  ### 🔹 Example: JWKS Key Selection
143
144
 
@@ -151,6 +152,45 @@ const jwks = JWKS.normalize({ keys: [jwk] });
151
152
  // Retrieve key by kid
152
153
  const keyObject = JWKS.toKeyObject(jwks, jwk.kid);
153
154
  ```
155
+
156
+ ### 🔹 Example: Remote JWKS (`fromWeb`)
157
+
158
+ ```ts
159
+ import { JWKS } from '@sourceregistry/node-jwt';
160
+
161
+ const jwks = await JWKS.fromWeb('https://issuer.example', {
162
+ ttl: 60_000,
163
+ timeoutMs: 2_000
164
+ });
165
+
166
+ const jwk = await jwks.key('my-kid');
167
+ const keys = await jwks.list();
168
+ const rsaKeys = await jwks.find({ kty: 'RSA' });
169
+ const firstSigKey = await jwks.findFirst({ use: 'sig' });
170
+
171
+ // Force refresh
172
+ await jwks.reload();
173
+
174
+ // Access cached JWKS snapshot
175
+ const current = jwks.export();
176
+ ```
177
+
178
+ `fromWeb()` options:
179
+
180
+ * `fetch` — custom fetch implementation (for runtimes/framework adapters)
181
+ * `ttl` — cache TTL in ms (`0` disables automatic refresh)
182
+ * `timeoutMs` — network timeout in ms
183
+ * `endpointOverride` — custom endpoint (absolute or relative)
184
+ * `overrideEndpointCheck` — skip automatic `/.well-known/jwks.json` append
185
+ * `cache` — custom cache backend with `{ get(key), set(key, value) }`
186
+
187
+ ### 🔹 Local Examples
188
+
189
+ See runnable examples in:
190
+
191
+ * `examples/jwt.example.ts`
192
+ * `examples/jwks.example.ts`
193
+
154
194
  ---
155
195
 
156
196
  ## 🛡️ Security Features
@@ -166,6 +206,42 @@ const keyObject = JWKS.toKeyObject(jwks, jwk.kid);
166
206
 
167
207
  ---
168
208
 
209
+ ## 🔏 ECDSA Signature Format: DER vs JOSE (New)
210
+
211
+ For **ECDSA** algorithms (`ES256`, `ES384`, `ES512`, `ES256K`) there are two common signature encodings:
212
+
213
+ - **DER** (ASN.1) — what Node.js produces by default
214
+ - **JOSE** (`r || s` raw signature) — required by the JWT/JWS spec and used by systems like **VAPID/Web Push (WNS)**
215
+
216
+ ### Default behavior
217
+ By default, this library outputs **DER** signatures for `ES*` algorithms to match Node.js/OpenSSL defaults.
218
+
219
+ ### Enable JOSE output
220
+ To generate spec-compliant JOSE ECDSA signatures, set:
221
+
222
+ - `signatureFormat: "jose"` in `sign()`
223
+
224
+ ```ts
225
+ import { sign, verify } from "@sourceregistry/node-jwt";
226
+
227
+ const token = sign(
228
+ { sub: "123", iat: Math.floor(Date.now() / 1000) },
229
+ ecPrivateKey,
230
+ { alg: "ES256", signatureFormat: "jose" }
231
+ );
232
+
233
+ // Verify JOSE-signed token
234
+ const result = verify(token, ecPublicKey, { signatureFormat: "jose" });
235
+ ````
236
+
237
+ ### Auto-detect verification (optional)
238
+
239
+ If enabled in your version, `verify()` can also validate JOSE ECDSA signatures without specifying `signatureFormat` (it will try DER first, then JOSE).
240
+ If you want strict behavior, pass `signatureFormat: "der"` or `signatureFormat: "jose"` explicitly.
241
+
242
+ > 💡 For VAPID/Web Push (e.g. Windows WNS endpoints), you typically need `ES256` with `signatureFormat: "jose"`.
243
+
244
+
169
245
  ## 📚 API Reference
170
246
 
171
247
  ### `sign(payload, secret, options?)`
@@ -197,7 +273,7 @@ Decode a JWT without verification (**unsafe**).
197
273
 
198
274
  ## 🧪 Testing
199
275
 
200
- * 100% branch coverage
276
+ * High branch coverage
201
277
  * All algorithms + autodetection paths
202
278
  * All failure modes
203
279
  * Sync + Promise APIs