Skip to content

Commit 11902f8

Browse files
committed
feat(utils): validate jwt
1 parent 552b640 commit 11902f8

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/utils/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,16 @@ export const downloadFile = (data: string, mimeType: 'text/csv', fileName: strin
105105
/**
106106
* Parse a Jitbug JWT and return its expiry and user id from the payload.
107107
*/
108-
export const parseJitbugJsonWebToken = (jwt: string): TokenPayload => {
109-
const payload = JSON.parse(window.atob(jwt.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
108+
export const parseJitbugJsonWebToken = (jwt: string): TokenPayload | void => {
109+
const [header, payload, signature] = jwt.split('.');
110110

111-
return { expires: payload.exp * 1000, uid: JSON.parse(payload.uid) };
111+
if (!header || !payload || !signature) {
112+
return;
113+
}
114+
115+
const payloadContent = JSON.parse(window.atob(payload.replace(/-/g, '+').replace(/_/g, '/')));
116+
117+
return { expires: payloadContent.exp * 1000, uid: JSON.parse(payloadContent.uid) };
112118
};
113119

114120
export interface TokenPayload {

0 commit comments

Comments
 (0)