UNPKG

@mysten/sui

Version:
1 lines 4.76 kB
{"version":3,"file":"jwt-decode.mjs","names":[],"sources":["../../src/zklogin/jwt-decode.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n// oxlint-disable check-tag-names\n\n/** Copied from https://github.com/auth0/jwt-decode/blob/3d372e9875ff673228a9f2d9df74e84690842a9c/lib/index.ts */\n\n/**\nThe MIT License (MIT)\n\nCopyright (c) 2015 Auth0, Inc. <support@auth0.com> (http://auth0.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n */\n\nexport interface JwtDecodeOptions {\n\theader?: boolean;\n}\n\nexport interface JwtHeader {\n\ttyp?: string;\n\talg?: string;\n\tkid?: string;\n}\n\nexport interface JwtPayload {\n\tiss?: string;\n\tsub?: string;\n\taud?: string[] | string;\n\texp?: number;\n\tnbf?: number;\n\tiat?: number;\n\tjti?: string;\n}\n\nexport class InvalidTokenError extends Error {}\n\nInvalidTokenError.prototype.name = 'InvalidTokenError';\n\nfunction b64DecodeUnicode(str: string) {\n\treturn decodeURIComponent(\n\t\tatob(str).replace(/(.)/g, (_m, p) => {\n\t\t\tlet code = (p as string).charCodeAt(0).toString(16).toUpperCase();\n\t\t\tif (code.length < 2) {\n\t\t\t\tcode = '0' + code;\n\t\t\t}\n\t\t\treturn '%' + code;\n\t\t}),\n\t);\n}\n\nfunction base64UrlDecode(str: string) {\n\tlet output = str.replace(/-/g, '+').replace(/_/g, '/');\n\tswitch (output.length % 4) {\n\t\tcase 0:\n\t\t\tbreak;\n\t\tcase 2:\n\t\t\toutput += '==';\n\t\t\tbreak;\n\t\tcase 3:\n\t\t\toutput += '=';\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new Error('base64 string is not of the correct length');\n\t}\n\n\ttry {\n\t\treturn b64DecodeUnicode(output);\n\t} catch {\n\t\treturn atob(output);\n\t}\n}\n\nexport function jwtDecode<T = JwtHeader>(\n\ttoken: string,\n\toptions: JwtDecodeOptions & { header: true },\n): T;\nexport function jwtDecode<T = JwtPayload>(token: string, options?: JwtDecodeOptions): T;\nexport function jwtDecode<T = JwtHeader | JwtPayload>(\n\ttoken: string,\n\toptions?: JwtDecodeOptions,\n): T {\n\tif (typeof token !== 'string') {\n\t\tthrow new InvalidTokenError('Invalid token specified: must be a string');\n\t}\n\n\toptions ||= {};\n\n\tconst pos = options.header === true ? 0 : 1;\n\tconst part = token.split('.')[pos];\n\n\tif (typeof part !== 'string') {\n\t\tthrow new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);\n\t}\n\n\tlet decoded: string;\n\ttry {\n\t\tdecoded = base64UrlDecode(part);\n\t} catch (e) {\n\t\tthrow new InvalidTokenError(\n\t\t\t`Invalid token specified: invalid base64 for part #${pos + 1} (${(e as Error).message})`,\n\t\t);\n\t}\n\n\ttry {\n\t\treturn JSON.parse(decoded) as T;\n\t} catch (e) {\n\t\tthrow new InvalidTokenError(\n\t\t\t`Invalid token specified: invalid json for part #${pos + 1} (${(e as Error).message})`,\n\t\t);\n\t}\n}\n"],"mappings":";AAkDA,IAAa,oBAAb,cAAuC,MAAM;AAE7C,kBAAkB,UAAU,OAAO;AAEnC,SAAS,iBAAiB,KAAa;AACtC,QAAO,mBACN,KAAK,IAAI,CAAC,QAAQ,SAAS,IAAI,MAAM;EACpC,IAAI,OAAQ,EAAa,WAAW,EAAE,CAAC,SAAS,GAAG,CAAC,aAAa;AACjE,MAAI,KAAK,SAAS,EACjB,QAAO,MAAM;AAEd,SAAO,MAAM;GACZ,CACF;;AAGF,SAAS,gBAAgB,KAAa;CACrC,IAAI,SAAS,IAAI,QAAQ,MAAM,IAAI,CAAC,QAAQ,MAAM,IAAI;AACtD,SAAQ,OAAO,SAAS,GAAxB;EACC,KAAK,EACJ;EACD,KAAK;AACJ,aAAU;AACV;EACD,KAAK;AACJ,aAAU;AACV;EACD,QACC,OAAM,IAAI,MAAM,6CAA6C;;AAG/D,KAAI;AACH,SAAO,iBAAiB,OAAO;SACxB;AACP,SAAO,KAAK,OAAO;;;AASrB,SAAgB,UACf,OACA,SACI;AACJ,KAAI,OAAO,UAAU,SACpB,OAAM,IAAI,kBAAkB,4CAA4C;AAGzE,aAAY,EAAE;CAEd,MAAM,MAAM,QAAQ,WAAW,OAAO,IAAI;CAC1C,MAAM,OAAO,MAAM,MAAM,IAAI,CAAC;AAE9B,KAAI,OAAO,SAAS,SACnB,OAAM,IAAI,kBAAkB,0CAA0C,MAAM,IAAI;CAGjF,IAAI;AACJ,KAAI;AACH,YAAU,gBAAgB,KAAK;UACvB,GAAG;AACX,QAAM,IAAI,kBACT,qDAAqD,MAAM,EAAE,IAAK,EAAY,QAAQ,GACtF;;AAGF,KAAI;AACH,SAAO,KAAK,MAAM,QAAQ;UAClB,GAAG;AACX,QAAM,IAAI,kBACT,mDAAmD,MAAM,EAAE,IAAK,EAAY,QAAQ,GACpF"}