(token: string)
| 1038 | }; |
| 1039 | |
| 1040 | const decodeJwtPayload = (token: string): Readonly<Record<string, unknown>> | null => { |
| 1041 | const payload = token.split(".")[1]; |
| 1042 | if (!payload) return null; |
| 1043 | if (!/^[A-Za-z0-9_-]+$/.test(payload) || payload.length % 4 === 1) return null; |
| 1044 | const base64 = payload.replaceAll("-", "+").replaceAll("_", "/"); |
| 1045 | const padded = base64.padEnd(base64.length + ((4 - (base64.length % 4)) % 4), "="); |
| 1046 | // atob yields latin1 code units; JWT payloads are UTF-8 bytes, so re-decode |
| 1047 | // them properly or non-ASCII claim values (accented emails, names) garble. |
| 1048 | const utf8 = new TextDecoder().decode( |
| 1049 | Uint8Array.from(globalThis.atob(padded), (char) => char.charCodeAt(0)), |
| 1050 | ); |
| 1051 | const decoded = decodeJwtClaims(utf8); |
| 1052 | return Option.isSome(decoded) ? decoded.value : null; |
| 1053 | }; |
| 1054 | |
| 1055 | export const idTokenIdentityLabel = (idToken: string | undefined): string | undefined => { |
| 1056 | if (!idToken) return undefined; |
no test coverage detected