UNPKG

@gaonengwww/jose

Version:

JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes

92 lines (86 loc) 2.54 kB
// src/lib/invalid_key_input.ts function message(msg, actual, ...types) { types = types.filter(Boolean); if (types.length > 2) { const last = types.pop(); msg += `one of type ${types.join(", ")}, or ${last}.`; } else if (types.length === 2) { msg += `one of type ${types[0]} or ${types[1]}.`; } else { msg += `of type ${types[0]}.`; } if (actual == null) { msg += ` Received ${actual}`; } else if (typeof actual === "function" && actual.name) { msg += ` Received function ${actual.name}`; } else if (typeof actual === "object" && actual != null) { if (actual.constructor?.name) { msg += ` Received an instance of ${actual.constructor.name}`; } } return msg; } var invalid_key_input_default = (actual, ...types) => { return message("Key must be ", actual, ...types); }; // src/lib/buffer_utils.ts var encoder = new TextEncoder(); var decoder = new TextDecoder(); var MAX_INT32 = 2 ** 32; // src/lib/base64.ts function encodeBase64(input) { if (Uint8Array.prototype.toBase64) { return input.toBase64(); } const CHUNK_SIZE = 32768; const arr = []; for (let i = 0; i < input.length; i += CHUNK_SIZE) { arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE))); } return btoa(arr.join("")); } // src/util/base64url.ts function encode(input) { let unencoded = input; if (typeof unencoded === "string") { unencoded = encoder.encode(unencoded); } if (Uint8Array.prototype.toBase64) { return unencoded.toBase64({ alphabet: "base64url", omitPadding: true }); } return encodeBase64(unencoded).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_"); } // src/lib/is_key_like.ts function isCryptoKey(key) { return key?.[Symbol.toStringTag] === "CryptoKey"; } function isKeyObject(key) { return key?.[Symbol.toStringTag] === "KeyObject"; } // src/lib/key_to_jwk.ts async function keyToJWK(key) { if (isKeyObject(key)) { if (key.type === "secret") { key = key.export(); } else { return key.export({ format: "jwk" }); } } if (key instanceof Uint8Array) { return { kty: "oct", k: encode(key) }; } if (!isCryptoKey(key)) { throw new TypeError(invalid_key_input_default(key, "CryptoKey", "KeyObject", "Uint8Array")); } if (!key.extractable) { throw new TypeError("non-extractable CryptoKey cannot be exported as a JWK"); } const { ext, key_ops, alg, use, ...jwk } = await crypto.subtle.exportKey("jwk", key); return jwk; } export { keyToJWK as default };