1 | {"version":3,"file":"jwt.cjs","sources":["../crypto/jwt.js"],"sourcesContent":["import * as error from '../error.js'\nimport * as buffer from '../buffer.js'\nimport * as string from '../string.js'\nimport * as json from '../json.js'\nimport * as ecdsa from '../crypto/ecdsa.js'\n\n/**\n * @param {Object} data\n */\nconst _stringify = data => buffer.toBase64UrlEncoded(string.encodeUtf8(json.stringify(data)))\n\n/**\n * @param {string} base64url\n */\nconst _parse = base64url => json.parse(string.decodeUtf8(buffer.fromBase64UrlEncoded(base64url)))\n\n/**\n * @param {CryptoKey} privateKey\n * @param {Object} payload\n */\nexport const encodeJwt = (privateKey, payload) => {\n const { name: algName, namedCurve: algCurve } = /** @type {any} */ (privateKey.algorithm)\n /* c8 ignore next 3 */\n if (algName !== 'ECDSA' || algCurve !== 'P-384') {\n error.unexpectedCase()\n }\n const header = {\n alg: 'ES384',\n typ: 'JWT'\n }\n const jwt = _stringify(header) + '.' + _stringify(payload)\n return ecdsa.sign(privateKey, string.encodeUtf8(jwt)).then(signature =>\n jwt + '.' + buffer.toBase64UrlEncoded(signature)\n )\n}\n\n/**\n * @param {CryptoKey} publicKey\n * @param {string} jwt\n */\nexport const verifyJwt = async (publicKey, jwt) => {\n const [headerBase64, payloadBase64, signatureBase64] = jwt.split('.')\n const verified = await ecdsa.verify(publicKey, buffer.fromBase64UrlEncoded(signatureBase64), string.encodeUtf8(headerBase64 + '.' + payloadBase64))\n /* c8 ignore next 3 */\n if (!verified) {\n throw new Error('Invalid JWT')\n }\n return {\n header: _parse(headerBase64),\n payload: _parse(payloadBase64)\n }\n}\n\n/**\n * Decode a jwt without verifying it. Probably a bad idea to use this. Only use if you know the jwt was already verified!\n *\n * @param {string} jwt\n */\nexport const unsafeDecode = jwt => {\n const [headerBase64, payloadBase64] = jwt.split('.')\n return {\n header: _parse(headerBase64),\n payload: _parse(payloadBase64)\n }\n}\n"],"names":["buffer.toBase64UrlEncoded","string.encodeUtf8","json.stringify","json.parse","string.decodeUtf8","buffer.fromBase64UrlE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