jwt-decode.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (function (factory) {
  2. typeof define === 'function' && define.amd ? define(factory) :
  3. factory();
  4. }((function () { 'use strict';
  5. /**
  6. * The code was extracted from:
  7. * https://github.com/davidchambers/Base64.js
  8. */
  9. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  10. function InvalidCharacterError(message) {
  11. this.message = message;
  12. }
  13. InvalidCharacterError.prototype = new Error();
  14. InvalidCharacterError.prototype.name = "InvalidCharacterError";
  15. function polyfill(input) {
  16. var str = String(input).replace(/=+$/, "");
  17. if (str.length % 4 == 1) {
  18. throw new InvalidCharacterError(
  19. "'atob' failed: The string to be decoded is not correctly encoded."
  20. );
  21. }
  22. for (
  23. // initialize result and counters
  24. var bc = 0, bs, buffer, idx = 0, output = "";
  25. // get next character
  26. (buffer = str.charAt(idx++));
  27. // character found in table? initialize bit storage and add its ascii value;
  28. ~buffer &&
  29. ((bs = bc % 4 ? bs * 64 + buffer : buffer),
  30. // and if not first of each 4 characters,
  31. // convert the first 8 bits to one ascii character
  32. bc++ % 4) ?
  33. (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6)))) :
  34. 0
  35. ) {
  36. // try to find character in table (0-63, not found => -1)
  37. buffer = chars.indexOf(buffer);
  38. }
  39. return output;
  40. }
  41. var atob = (typeof window !== "undefined" &&
  42. window.atob &&
  43. window.atob.bind(window)) ||
  44. polyfill;
  45. function b64DecodeUnicode(str) {
  46. return decodeURIComponent(
  47. atob(str).replace(/(.)/g, function(m, p) {
  48. var code = p.charCodeAt(0).toString(16).toUpperCase();
  49. if (code.length < 2) {
  50. code = "0" + code;
  51. }
  52. return "%" + code;
  53. })
  54. );
  55. }
  56. function base64_url_decode(str) {
  57. var output = str.replace(/-/g, "+").replace(/_/g, "/");
  58. switch (output.length % 4) {
  59. case 0:
  60. break;
  61. case 2:
  62. output += "==";
  63. break;
  64. case 3:
  65. output += "=";
  66. break;
  67. default:
  68. throw "Illegal base64url string!";
  69. }
  70. try {
  71. return b64DecodeUnicode(output);
  72. } catch (err) {
  73. return atob(output);
  74. }
  75. }
  76. function InvalidTokenError(message) {
  77. this.message = message;
  78. }
  79. InvalidTokenError.prototype = new Error();
  80. InvalidTokenError.prototype.name = "InvalidTokenError";
  81. function jwtDecode(token, options) {
  82. if (typeof token !== "string") {
  83. throw new InvalidTokenError("Invalid token specified");
  84. }
  85. options = options || {};
  86. var pos = options.header === true ? 0 : 1;
  87. try {
  88. return JSON.parse(base64_url_decode(token.split(".")[pos]));
  89. } catch (e) {
  90. throw new InvalidTokenError("Invalid token specified: " + e.message);
  91. }
  92. }
  93. /*
  94. * Expose the function on the window object
  95. */
  96. //use amd or just through the window object.
  97. if (window) {
  98. if (typeof window.define == "function" && window.define.amd) {
  99. window.define("jwt_decode", function() {
  100. return jwtDecode;
  101. });
  102. } else if (window) {
  103. window.jwt_decode = jwtDecode;
  104. }
  105. }
  106. })));
  107. //# sourceMappingURL=jwt-decode.js.map