jwt.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  2. const b64re = /^(?:[A-Za-z\d+/]{4})*?(?:[A-Za-z\d+/]{2}(?:==)?|[A-Za-z\d+/]{3}=?)?$/;
  3. export function weBtoa(string) {
  4. string = String(string);
  5. let bitmap, a, b, c, result = "", i = 0, rest = string.length % 3;
  6. for (; i < string.length;) {
  7. if ((a = string.charCodeAt(i++)) > 255 ||
  8. (b = string.charCodeAt(i++)) > 255 ||
  9. (c = string.charCodeAt(i++)) > 255) {
  10. throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
  11. }
  12. bitmap = (a << 16) | (b << 8) | c;
  13. result += b64.charAt((bitmap >> 18) & 63)
  14. + b64.charAt((bitmap >> 12) & 63)
  15. + b64.charAt((bitmap >> 6) & 63)
  16. + b64.charAt(bitmap & 63);
  17. }
  18. return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
  19. }
  20. export function weAtob(string) {
  21. string = String(string).replace(/[\t\n\f\r ]+/g, "");
  22. if (!b64re.test(string)) {
  23. throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
  24. }
  25. string += "==".slice(2 - (string.length & 3));
  26. let bitmap, result = "", r1, r2, i = 0;
  27. for (; i < string.length;) {
  28. bitmap = (b64.indexOf(string.charAt(i++)) << 18) |
  29. (b64.indexOf(string.charAt(i++)) << 12) |
  30. ((r1 = b64.indexOf(string.charAt(i++))) << 6) |
  31. (r2 = b64.indexOf(string.charAt(i++)));
  32. result += r1 === 64
  33. ? String.fromCharCode((bitmap >> 16) & 255)
  34. : r2 === 64
  35. ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
  36. : String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
  37. }
  38. return result;
  39. }
  40. function b64DecodeUnicode(str) {
  41. return decodeURIComponent(
  42. weAtob(str).replace(/(.)/g, (p) => {
  43. let code = p.charCodeAt(0).toString(16).toUpperCase();
  44. if (code.length < 2) code = "0" + code;
  45. return "%" + code;
  46. })
  47. );
  48. }
  49. function base64_url_decode(str) {
  50. let output = str.replace(/-/g, "+").replace(/_/g, "/");
  51. switch (output.length % 4) {
  52. case 0: break;
  53. case 2: output += "=="; break;
  54. case 3: output += "="; break;
  55. default: throw new Error("Illegal base64url string!");
  56. }
  57. try {
  58. return b64DecodeUnicode(output);
  59. } catch (err) {
  60. return weAtob(output);
  61. }
  62. }
  63. export default function jwt(token, options = {}) {
  64. if (typeof token !== "string") {
  65. throw new Error("Invalid token specified");
  66. }
  67. const pos = options.header === true ? 0 : 1;
  68. try {
  69. return JSON.parse(base64_url_decode(token.split(".")[pos]));
  70. } catch (e) {
  71. throw new Error("Invalid token specified: " + e.message);
  72. }
  73. }