Skip to content

Commit 0f43741

Browse files
committed
fix: Use jwtDecode to parse the token
1 parent b6e9a35 commit 0f43741

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

package-lock.json

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"last 3 edge versions"
3636
],
3737
"dependencies": {
38-
"@superset-ui/switchboard": "^0.20.3"
38+
"@superset-ui/switchboard": "^0.20.3",
39+
"jwt-decode": "^4.0.0"
3940
},
4041
"devDependencies": {
4142
"@babel/cli": "^7.16.8",

src/guestTokenRefresh.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { jwtDecode } from "jwt-decode";
2+
13
export const REFRESH_TIMING_BUFFER_MS = 5000 // refresh guest token early to avoid failed superset requests
24
export const MIN_REFRESH_WAIT_MS = 10000 // avoid blasting requests as fast as the cpu can handle
35
export const DEFAULT_TOKEN_EXP_MS = 300000 // (5 min) used only when parsing guest token exp fails
46

57
// when do we refresh the guest token?
68
export function getGuestTokenRefreshTiming(currentGuestToken: string) {
7-
// atob converts a string from base64 https://developer.mozilla.org/en-US/docs/Web/API/atob
8-
const parsedJwt = JSON.parse(atob(currentGuestToken.split('.')[1]).toString());
9+
const parsedJwt = jwtDecode<Record<string, any>>(currentGuestToken);
910
// if exp is int, it is in seconds, but Date() takes milliseconds
1011
const exp = new Date(/[^0-9\.]/g.test(parsedJwt.exp) ? parsedJwt.exp : parseFloat(parsedJwt.exp) * 1000);
1112
const isValidDate = exp.toString() !== 'Invalid Date';

0 commit comments

Comments
 (0)