All files / blong-gogo/src jose.ts

83.39% Statements 241/289
56.66% Branches 34/60
100% Functions 15/15
83.39% Lines 241/289

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 2901x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x                     4x 4x 1x 1x 1x 1x 124x                   124x 124x 124x 124x 124x 124x 124x                 124x 124x 1x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 1x 60x 60x 60x 60x 60x   60x 60x 60x 60x 60x 60x 1x 29x 29x 29x 29x 29x 29x 29x 29x   29x   29x 29x 29x 29x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x               29x               29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1x 29x 29x 29x 29x 29x 29x 29x   29x 29x   29x 29x 1x 29x 29x 29x 29x 29x 29x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 1x 25x 25x 25x 25x 25x 25x 25x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 3x 3x 3x 13x 13x 13x 13x 13x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x     13x 25x 25x 25x 13x 4x 13x 13x 13x  
import {
    CompactEncrypt,
    CompactSign,
    FlattenedEncrypt,
    FlattenedSign,
    GeneralEncrypt,
    GeneralSign,
    calculateJwkThumbprint,
    compactDecrypt,
    compactVerify,
    exportJWK,
    flattenedDecrypt,
    generalDecrypt,
    generateKeyPair,
    importJWK,
    type CompactDecryptResult,
    type FlattenedDecryptResult,
    type FlattenedJWE,
    type FlattenedJWS,
    type GeneralDecryptResult,
    type GeneralJWE,
    type GeneralJWS,
    type JWEHeaderParameters,
    type JWK,
} from 'jose';
 
type KeyLike = CryptoKey | {type: string};
 
/**
 * Spec for a JWK key that can be:
 * - A static JWK object
 * - `{generate: {alg, crv?, use?, ...}}` — dynamically generate a key pair on startup.
 *   Additional properties (e.g. `use`, `kid`) are merged into the final JWK, taking precedence
 *   over the generated key material where keys overlap (except `alg`, which is always taken from the spec).
 * - `{env: 'ENV_VAR_NAME'}` — load a JSON-serialized JWK from an environment variable
 */
export type KeySpec =
    | JWK
    | KeyLike
    | Uint8Array
    | {
          generate: {
              alg: string;
              crv?: string;
              modulusLength?: number;
              use?: string;
              [k: string]: unknown;
          };
      }
    | {env: string};
 
async function resolveKeySpec(
    spec: KeySpec | undefined,
): Promise<JWK | KeyLike | Uint8Array | undefined> {
    if (!spec) return undefined;
    if ('generate' in spec) {
        const {alg, crv, modulusLength, ...rest} = spec.generate;
        const {privateKey} = await generateKeyPair(alg, {
            ...(crv && {crv}),
            ...(typeof modulusLength === 'number' && {modulusLength}),
            extractable: true,
        });
        const jwk = await exportJWK(privateKey);
        return {...jwk, alg, ...rest};
    }
    if ('env' in spec) {
        const value = process.env[spec.env];
        if (!value) return undefined;
        try {
            return JSON.parse(value) as JWK;
        } catch {
            throw new Error(
                `Gateway key env var "${spec.env}" is set but does not contain valid JSON`,
            );
        }
    }
    return spec as JWK | KeyLike | Uint8Array;
}
 
const isBrowser: boolean = typeof window !== 'undefined' && typeof window.document !== 'undefined';
 
const isKey = async (o: unknown): Promise<boolean> => {
    if (isBrowser) {
        return (
            typeof o === 'object' &&
            o !== null &&
            typeof (o as Record<string, unknown>).extractable === 'boolean' &&
            typeof (o as Record<string, unknown>)?.algorithm === 'object' &&
            typeof ((o as Record<string, unknown>)?.algorithm as Record<string, unknown>)?.name ===
                'string' &&
            typeof (o as Record<string, unknown>).type === 'string'
        );
    } else {
        const {
            types: {isKeyObject, isCryptoKey},
        } = (await import('node:util')).default;
        const {KeyObject} = (await import('node:crypto')).default;
        return isKeyObject
            ? isKeyObject(o)
            : KeyObject
              ? o instanceof KeyObject
              : isCryptoKey
                ? isCryptoKey(o)
                : typeof o === 'object' &&
                  o !== null &&
                  (o as Record<string, unknown>).constructor !== Object &&
                  typeof (o as Record<string, unknown>).type === 'string';
    }
};
 
async function importKey(
    jwk: JWK | KeyLike | Uint8Array,
): Promise<{key: KeyLike | Uint8Array; alg: string}> {
    const is = await isKey(jwk);
    const {alg} = is ? await exportKey(jwk as KeyLike | Uint8Array) : (jwk as JWK);
 
    return {
        key: is ? (jwk as KeyLike | Uint8Array) : await importJWK(jwk as JWK, alg ?? ''),
        alg: alg ?? '',
    };
}
 
async function exportKey(
    key: KeyLike | Uint8Array | JWK,
    returnPrivate: boolean = false,
): Promise<JWK> {
    const jwk: JWK = (await isKey(key))
        ? await exportJWK(key as KeyLike | Uint8Array)
        : (key as JWK);
    if (!jwk.kid) jwk.kid = ((await calculateJwkThumbprint(jwk)) as string) ?? undefined;
    if (returnPrivate) return jwk;
    const {d, p, q, dp, dq, qi, ...publicJwk} = jwk; // eslint-disable-line @typescript-eslint/no-unused-vars
    return publicJwk;
}
 
async function sign(
    message: object,
    {key, alg}: {key: KeyLike | Uint8Array; alg: string},
    options: {serialization?: unknown},
): Promise<FlattenedJWS | GeneralJWS | string> {
    const payload = Buffer.isBuffer(message) ? message : Buffer.from(JSON.stringify(message));
    switch (options?.serialization) {
        case 'general':
            return new GeneralSign(payload).addSignature(key).setProtectedHeader({alg}).sign();
        case 'flattened':
            return new FlattenedSign(payload).setProtectedHeader({alg}).sign(key);
        default:
            return new CompactSign(payload).setProtectedHeader({alg}).sign(key);
    }
}
 
function encrypt(
    jws: string | Buffer,
    {key, alg}: {key: KeyLike | Uint8Array; alg: string},
    protectedHeader: object,
    unprotectedHeader: JWEHeaderParameters,
    options: {serialization?: unknown},
): Promise<string | FlattenedJWE | GeneralJWE> {
    switch (options?.serialization) {
        case 'compact':
            return new CompactEncrypt(Buffer.from(jws))
                .setProtectedHeader({
                    alg,
                    enc: 'A128CBC-HS256',
                    ...protectedHeader,
                })
                .encrypt(key);
        case 'flattened':
            return new FlattenedEncrypt(Buffer.from(jws))
                .setProtectedHeader({
                    alg,
                    enc: 'A128CBC-HS256',
                    ...protectedHeader,
                })
                .encrypt(key);
        default:
            return new GeneralEncrypt(Buffer.from(jws))
                .setProtectedHeader({
                    alg,
                    enc: 'A128CBC-HS256',
                    ...protectedHeader,
                })
                .setSharedUnprotectedHeader(unprotectedHeader)
                .addRecipient(key)
                .encrypt();
    }
}
 
async function decrypt(
    jwe: string | GeneralJWE,
    {key}: {key: KeyLike | Uint8Array},
    options?: {complete?: unknown},
): Promise<Uint8Array | CompactDecryptResult | GeneralDecryptResult | FlattenedDecryptResult> {
    const {plaintext, protectedHeader} =
        typeof jwe === 'string'
            ? await compactDecrypt(jwe, key)
            : jwe.recipients
              ? await generalDecrypt(jwe, key)
              : await flattenedDecrypt(jwe, key);
    return options?.complete ? {plaintext, protectedHeader} : plaintext;
}
 
async function verify(
    plaintext: string | Uint8Array,
    {key}: {key: KeyLike | Uint8Array},
): Promise<object> {
    return JSON.parse(new TextDecoder().decode((await compactVerify(plaintext, key)).payload));
}
 
async function signEncrypt(
    message: Parameters<typeof sign>[0],
    mlsk: Parameters<typeof sign>[1],
    mlekPub?: Parameters<typeof encrypt>[1],
    protectedHeader?: Parameters<typeof encrypt>[2],
    unprotectedHeader?: Parameters<typeof encrypt>[3],
    options?: {encrypt?: Parameters<typeof encrypt>[4]; sign?: Parameters<typeof sign>[2]},
): ReturnType<typeof encrypt> {
    return encrypt(
        (await sign(message, mlsk, options?.sign as Parameters<typeof sign>[2])) as string,
        mlekPub!,
        protectedHeader!,
        unprotectedHeader!,
        options?.encrypt as Parameters<typeof encrypt>[4],
    );
}
 
async function decryptVerify(
    message: Parameters<typeof decrypt>[0],
    mlskPub: Parameters<typeof verify>[1],
    mlek?: Parameters<typeof decrypt>[1],
): ReturnType<typeof verify> {
    return verify((await decrypt(message, mlek!)) as Uint8Array, mlskPub);
}
 
export default async function jose({sign, encrypt}: {sign?: KeySpec; encrypt?: KeySpec}): Promise<{
    keys: {sign: JWK; encrypt: JWK};
    signEncrypt: (
        msg: Parameters<typeof signEncrypt>[0],
        key: Parameters<typeof importKey>[0],
        protectedHeader?: Parameters<typeof signEncrypt>[3],
        unprotectedHeader?: Parameters<typeof signEncrypt>[4],
        options?: Parameters<typeof signEncrypt>[5],
    ) => unknown;
    decryptVerify: (
        msg: Parameters<typeof decryptVerify>[0],
        key: Parameters<typeof importKey>[0],
    ) => unknown;
    decrypt: (
        msg: string | GeneralJWE,
        options: unknown,
    ) => ReturnType<typeof decrypt> | typeof msg;
    verify: (plaintext: string | Uint8Array, key: KeyLike | Uint8Array) => Promise<object>;
}> {
    const resolvedSign = await resolveKeySpec(sign);
    const resolvedEncrypt = await resolveKeySpec(encrypt);
    const mlsk = resolvedSign && (await importKey(resolvedSign));
    const mlek = resolvedEncrypt && (await importKey(resolvedEncrypt));
    return {
        keys: {
            sign: (resolvedSign && ((await exportKey(resolvedSign)) as JWK)) || ({} as JWK),
            encrypt:
                (resolvedEncrypt && ((await exportKey(resolvedEncrypt)) as JWK)) || ({} as JWK),
        },
        signEncrypt: async (
            msg: Parameters<typeof signEncrypt>[0],
            key: Parameters<typeof importKey>[0],
            protectedHeader?: Parameters<typeof signEncrypt>[3],
            unprotectedHeader?: Parameters<typeof signEncrypt>[4],
            options?: Parameters<typeof signEncrypt>[5],
        ) =>
            mlsk
                ? signEncrypt(
                      msg,
                      mlsk,
                      await importKey(await exportKey(key as JWK)),
                      protectedHeader!,
                      unprotectedHeader!,
                      options,
                  )
                : msg,
        decryptVerify: async (
            msg: Parameters<typeof decryptVerify>[0],
            key: Parameters<typeof importKey>[0],
        ) => (mlek ? decryptVerify(msg, await importKey(await exportKey(key as JWK)), mlek!) : msg),
        decrypt: (msg, options) =>
            mlek ? decrypt(msg, mlek!, options as {complete?: unknown} | undefined) : msg,
        verify: async (msg, key) => verify(msg, await importKey(key)),
    };
}