UNPKG

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