From 20f687d3d18d8c2915cb9d28ed2725bb7f1b6820 Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Thu, 6 Nov 2025 18:59:28 -0800 Subject: [PATCH] getTokenData URL-safe base64 encoding and missing padding --- src/ims.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ims.js b/src/ims.js index c04d8be..cac4da6 100644 --- a/src/ims.js +++ b/src/ims.js @@ -211,10 +211,15 @@ async function _toTokenResult (apiResponse) { * * @param {string} token The token to decode and extract the token value from * @returns {object} The decoded token payload data without header and signature + * @throws {Error} If the token is invalid or cannot be decoded */ function getTokenData (token) { const [, payload] = token.split('.', 3) - return JSON.parse(Buffer.from(payload, 'base64')) + const base64 = payload.replace(/-/g, '+').replace(/_/g, '/') + // add padding if necessary + const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4) + const decoded = Buffer.from(padded, 'base64').toString('utf-8') + return JSON.parse(decoded) } /**