weapp-jwt.js 2.9 KB

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