UNPKG

@mysten/sui

Version:

Sui TypeScript API(Work in Progress)

86 lines (85 loc) 2.59 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var jwt_decode_exports = {}; __export(jwt_decode_exports, { InvalidTokenError: () => InvalidTokenError, jwtDecode: () => jwtDecode }); module.exports = __toCommonJS(jwt_decode_exports); class InvalidTokenError extends Error { } InvalidTokenError.prototype.name = "InvalidTokenError"; function b64DecodeUnicode(str) { return decodeURIComponent( atob(str).replace(/(.)/g, (_m, p) => { let code = p.charCodeAt(0).toString(16).toUpperCase(); if (code.length < 2) { code = "0" + code; } return "%" + code; }) ); } function base64UrlDecode(str) { let output = str.replace(/-/g, "+").replace(/_/g, "/"); switch (output.length % 4) { case 0: break; case 2: output += "=="; break; case 3: output += "="; break; default: throw new Error("base64 string is not of the correct length"); } try { return b64DecodeUnicode(output); } catch (err) { return atob(output); } } function jwtDecode(token, options) { if (typeof token !== "string") { throw new InvalidTokenError("Invalid token specified: must be a string"); } options || (options = {}); const pos = options.header === true ? 0 : 1; const part = token.split(".")[pos]; if (typeof part !== "string") { throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`); } let decoded; try { decoded = base64UrlDecode(part); } catch (e) { throw new InvalidTokenError( `Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})` ); } try { return JSON.parse(decoded); } catch (e) { throw new InvalidTokenError( `Invalid token specified: invalid json for part #${pos + 1} (${e.message})` ); } } //# sourceMappingURL=jwt-decode.js.map