UNPKG

@gaonengwww/jose

Version:

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

88 lines (83 loc) 2.34 kB
// src/lib/buffer_utils.ts var encoder = new TextEncoder(); var decoder = new TextDecoder(); var MAX_INT32 = 2 ** 32; // src/lib/base64.ts function decodeBase64(encoded) { if (Uint8Array.fromBase64) { return Uint8Array.fromBase64(encoded); } const binary = atob(encoded); const bytes = new Uint8Array(binary.length); for (let i = 0; i < binary.length; i++) { bytes[i] = binary.charCodeAt(i); } return bytes; } // src/util/base64url.ts function decode(input) { if (Uint8Array.fromBase64) { return Uint8Array.fromBase64(typeof input === "string" ? input : decoder.decode(input), { alphabet: "base64url" }); } let encoded = input; if (encoded instanceof Uint8Array) { encoded = decoder.decode(encoded); } encoded = encoded.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, ""); try { return decodeBase64(encoded); } catch { throw new TypeError("The input to be decoded is not correctly encoded."); } } // src/lib/is_object.ts function isObjectLike(value) { return typeof value === "object" && value !== null; } var is_object_default = (input) => { if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") { return false; } if (Object.getPrototypeOf(input) === null) { return true; } let proto = input; while (Object.getPrototypeOf(proto) !== null) { proto = Object.getPrototypeOf(proto); } return Object.getPrototypeOf(input) === proto; }; // src/util/decode_protected_header.ts function decodeProtectedHeader(token) { let protectedB64u; if (typeof token === "string") { const parts = token.split("."); if (parts.length === 3 || parts.length === 5) { ; [protectedB64u] = parts; } } else if (typeof token === "object" && token) { if ("protected" in token) { protectedB64u = token.protected; } else { throw new TypeError("Token does not contain a Protected Header"); } } try { if (typeof protectedB64u !== "string" || !protectedB64u) { throw new Error(); } const result = JSON.parse(decoder.decode(decode(protectedB64u))); if (!is_object_default(result)) { throw new Error(); } return result; } catch { throw new TypeError("Invalid Token or Protected Header formatting"); } } export { decodeProtectedHeader };