Extracted from patitas-peludas onto the object plugin contract. Core is a type import only, so installing the plugin never drags in a second core or a version warning; a bad key still fails boot, which the contract tests now pin. The extraction moves from jose 5 to jose 6, and that is not a version bump: jose 6 refuses a private CryptoKey in jwtVerify, where jose 5 accepted one and derived the public half itself. The original keeps a single key per signer and verifies with the key it signed with, which works on 5 and throws "CryptoKey instances must be of type public" on 6 — for every asymmetric algorithm, so ES*, RS*, PS* and EdDSA alike. Symmetric keys are unaffected, which is exactly why one HS256 test would have shipped it. importJWKKey now imports the key twice for an asymmetric pair: as given, to sign with, and with the private fields stripped, to verify with. A symmetric key has no public half, so it keeps one secret for both. The round-trip is covered per algorithm family rather than once, because one algorithm is not coverage — it is the gap the defect lived in. The get() helper in the contract tests is generic in its body: Response.json() is typed Promise<unknown>, so reaching into the payload did not typecheck. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
60 lines
2.5 KiB
TypeScript
60 lines
2.5 KiB
TypeScript
import { readFile } from 'node:fs/promises';
|
|
|
|
import { importJWK, type JWK } from 'jose';
|
|
|
|
import type { ImportedJWK, KeyMaterial } from './types.ts';
|
|
|
|
/**
|
|
* Reads a JWK from disk.
|
|
*
|
|
* `readFile` + `JSON.parse` rather than `await import(path)`: a dynamic import of a JSON file is cached
|
|
* by the module registry for the life of the process, resolves differently under CommonJS and ESM, and
|
|
* puts private key material into a module cache anything in the process can read.
|
|
*/
|
|
async function readJWK(filePath: string): Promise<JWK> {
|
|
return JSON.parse(await readFile(filePath, 'utf8')) as JWK;
|
|
}
|
|
|
|
const PRIVATE_FIELDS = new Set(['d', 'p', 'q', 'dp', 'dq', 'qi', 'oth', 'key_ops']);
|
|
|
|
/** Whether the key is symmetric, in which case one secret both signs and verifies. */
|
|
function isSymmetric(jwk: JWK): boolean {
|
|
return jwk.kty === 'oct' || 'k' in jwk;
|
|
}
|
|
|
|
/** The same JWK with every private field removed. */
|
|
function publicHalf(jwk: JWK): JWK {
|
|
return Object.fromEntries(Object.entries(jwk).filter(([field]) => !PRIVATE_FIELDS.has(field))) as JWK;
|
|
}
|
|
|
|
/**
|
|
* Reads and imports a JWK file, keeping the algorithm and key id it declares.
|
|
*
|
|
* Imports the key **twice** for an asymmetric pair: once as given, to sign with, and once with the
|
|
* private fields stripped, to verify with. That is not redundancy — jose 6 rejects a private
|
|
* `CryptoKey` in `jwtVerify` with `CryptoKey instances must be of type "public"`, where jose 5
|
|
* accepted one and derived the public half itself. A signer that kept a single key therefore signed
|
|
* fine and threw on every verification, for every asymmetric algorithm, which is all of ES*, RS*,
|
|
* PS* and EdDSA.
|
|
*
|
|
* A symmetric key has no public half to derive, so it verifies with the same secret it signs with.
|
|
*/
|
|
export async function importJWKKey(filePath: string): Promise<ImportedJWK> {
|
|
const jwk = await readJWK(filePath);
|
|
const key: KeyMaterial = await importJWK(jwk, jwk.alg);
|
|
const verifyKey: KeyMaterial = isSymmetric(jwk) ? key : await importJWK(publicHalf(jwk), jwk.alg);
|
|
|
|
return { key, verifyKey, alg: jwk.alg, kid: jwk.kid };
|
|
}
|
|
|
|
/** Strips every private field, so the result is safe to serve at a JWKS endpoint. */
|
|
export async function readPublicJWK(filePath: string): Promise<JWK> {
|
|
const jwk = await readJWK(filePath);
|
|
|
|
if (isSymmetric(jwk)) {
|
|
throw new Error('A JWKS endpoint requires an asymmetric key; a symmetric JWK has no public half to publish.');
|
|
}
|
|
|
|
return { ...publicHalf(jwk), use: 'sig', key_ops: ['verify'] };
|
|
}
|