MCPcopy
hub / github.com/auth0/jwt-decode / jwtDecode

Function jwtDecode

lib/index.ts:64–97  ·  view source on GitHub ↗
(
  token: string,
  options?: JwtDecodeOptions,
)

Source from the content-addressed store, hash-verified

62): T;
63export function jwtDecode<T = JwtPayload>(token: string, options?: JwtDecodeOptions): T;
64export 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}

Callers 1

index.test.tsFile · 0.85

Calls 1

base64UrlDecodeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…