From 73ab93d1f19d5c5960183bf428710ea14ff583c1 Mon Sep 17 00:00:00 2001 From: Michael Oliver Date: Fri, 24 May 2024 10:39:51 -0700 Subject: [PATCH] Add comments to parseJwt implementation --- src/client/plugin/lib.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/client/plugin/lib.ts b/src/client/plugin/lib.ts index d744917..a1c5d08 100644 --- a/src/client/plugin/lib.ts +++ b/src/client/plugin/lib.ts @@ -15,12 +15,19 @@ export function parseCookies(cookieStr: string) { ) } +// This parseJWT implementation is taken from https://stackoverflow.com/a/38552302/1935971 export function parseJwt(token: string) { + // JWT has 3 parts separated by ".", the payload is the base64url-encoded part in the middle const base64Url = token.split(".")[1] + // base64url replaced '+' and '/' with '-' and '_', so we undo it here const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/") const jsonPayload = decodeURIComponent( atob(base64) .split("") + // atob decoded the base64 string, but multi-byte characters (emojis for example) + // are not decoded properly. For example, "🍀" looks like "ð\x9F\x8D\x80". The next + // line converts bytes into URI-percent-encoded format, for example "%20" for space. + // Lastly, the decodeURIComponent wrapping this can correctly get a UTF-8 string. .map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)) .join(""), )