From 7ec136fec27a29338493745ae91ec110708a8a2a Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 17 Oct 2022 16:14:28 +0800 Subject: [PATCH] fix(superset-embedded-sdk): Buffer is not defined (#21641) --- superset-embedded-sdk/package-lock.json | 13 ++++++++++++- superset-embedded-sdk/package.json | 3 ++- superset-embedded-sdk/src/guestTokenRefresh.ts | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/superset-embedded-sdk/package-lock.json b/superset-embedded-sdk/package-lock.json index 35836b79bc175..f956ee27ac0b9 100644 --- a/superset-embedded-sdk/package-lock.json +++ b/superset-embedded-sdk/package-lock.json @@ -9,7 +9,8 @@ "version": "0.1.0-alpha.7", "license": "Apache-2.0", "dependencies": { - "@superset-ui/switchboard": "^0.18.26-0" + "@superset-ui/switchboard": "^0.18.26-0", + "jwt-decode": "^3.1.2" }, "devDependencies": { "@babel/cli": "^7.16.8", @@ -6495,6 +6496,11 @@ "node": ">=6" } }, + "node_modules/jwt-decode": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-3.1.2.tgz", + "integrity": "sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==" + }, "node_modules/kind-of": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", @@ -13042,6 +13048,11 @@ "minimist": "^1.2.5" } }, + "jwt-decode": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-3.1.2.tgz", + "integrity": "sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==" + }, "kind-of": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz", diff --git a/superset-embedded-sdk/package.json b/superset-embedded-sdk/package.json index 49debb4ad321f..fea75d7af58c9 100644 --- a/superset-embedded-sdk/package.json +++ b/superset-embedded-sdk/package.json @@ -33,7 +33,8 @@ "last 3 edge versions" ], "dependencies": { - "@superset-ui/switchboard": "^0.18.26-0" + "@superset-ui/switchboard": "^0.18.26-0", + "jwt-decode": "^3.1.2" }, "devDependencies": { "@babel/cli": "^7.16.8", diff --git a/superset-embedded-sdk/src/guestTokenRefresh.ts b/superset-embedded-sdk/src/guestTokenRefresh.ts index 214e91a1c33e4..101c4d9e93930 100644 --- a/superset-embedded-sdk/src/guestTokenRefresh.ts +++ b/superset-embedded-sdk/src/guestTokenRefresh.ts @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ +import jwt_decode from "jwt-decode"; export const REFRESH_TIMING_BUFFER_MS = 5000 // refresh guest token early to avoid failed superset requests export const MIN_REFRESH_WAIT_MS = 10000 // avoid blasting requests as fast as the cpu can handle @@ -23,7 +24,7 @@ export const DEFAULT_TOKEN_EXP_MS = 300000 // (5 min) used only when parsing gue // when do we refresh the guest token? export function getGuestTokenRefreshTiming(currentGuestToken: string) { - const parsedJwt = JSON.parse(Buffer.from(currentGuestToken.split('.')[1], 'base64').toString()); + const parsedJwt = jwt_decode>(currentGuestToken); // if exp is int, it is in seconds, but Date() takes milliseconds const exp = new Date(/[^0-9\.]/g.test(parsedJwt.exp) ? parsedJwt.exp : parseFloat(parsedJwt.exp) * 1000); const isValidDate = exp.toString() !== 'Invalid Date';