( token: string, options?: JwtDecodeOptions, )
| 62 | ): T; |
| 63 | export function jwtDecode<T = JwtPayload>(token: string, options?: JwtDecodeOptions): T; |
| 64 | export function jwtDecode<T = JwtHeader | JwtPayload>( |
| 65 | token: string, |
| 66 | options?: JwtDecodeOptions, |
| 67 | ): T { |
| 68 | if (typeof token !== "string") { |
| 69 | throw new InvalidTokenError("Invalid token specified: must be a string"); |
| 70 | } |
| 71 | |
| 72 | options ||= {}; |
| 73 | |
| 74 | const pos = options.header === true ? 0 : 1; |
| 75 | const part = token.split(".")[pos]; |
| 76 | |
| 77 | if (typeof part !== "string") { |
| 78 | throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`); |
| 79 | } |
| 80 | |
| 81 | let decoded: string; |
| 82 | try { |
| 83 | decoded = base64UrlDecode(part); |
| 84 | } catch (e) { |
| 85 | throw new InvalidTokenError( |
| 86 | `Invalid token specified: invalid base64 for part #${pos + 1} (${(e as Error).message})`, |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | try { |
| 91 | return JSON.parse(decoded) as T; |
| 92 | } catch (e) { |
| 93 | throw new InvalidTokenError( |
| 94 | `Invalid token specified: invalid json for part #${pos + 1} (${(e as Error).message})`, |
| 95 | ); |
| 96 | } |
| 97 | } |
no test coverage detected
searching dependent graphs…