Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"last 3 edge versions"
],
"dependencies": {
"@superset-ui/switchboard": "^0.20.3"
"@superset-ui/switchboard": "^0.20.3",
"jwt-decode": "^4.0.0"
},
"devDependencies": {
"@babel/cli": "^7.16.8",
Expand Down
5 changes: 3 additions & 2 deletions src/guestTokenRefresh.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { jwtDecode } 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
export const DEFAULT_TOKEN_EXP_MS = 300000 // (5 min) used only when parsing guest token exp fails

// when do we refresh the guest token?
export function getGuestTokenRefreshTiming(currentGuestToken: string) {
// atob converts a string from base64 https://developer.mozilla.org/en-US/docs/Web/API/atob
const parsedJwt = JSON.parse(atob(currentGuestToken.split('.')[1]).toString());
const parsedJwt = jwtDecode<Record<string, any>>(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';
Expand Down