-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
228 lines (184 loc) · 5.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
(function () {
var exports = (typeof module !== 'undefined' && module.exports) || window;
var cryptoSubtle = (window.crypto && crypto.subtle) ||
(window.crypto && crypto.webkitSubtle) ||
(window.msCrypto && window.msCrypto.Subtle);
if (!cryptoSubtle) {
throw new Error('crypto.subtle not found');
}
// Adapted from https://chromium.googlesource.com/chromium/blink/+/master/LayoutTests/crypto/subtle/hmac/sign-verify.html
var Base64URL = {
stringify: function (a) {
var base64string = btoa(String.fromCharCode.apply(0, a));
return base64string.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
},
parse: function (s) {
s = s.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '');
return new Uint8Array(Array.prototype.map.call(atob(s), function (c) { return c.charCodeAt(0); }));
}
};
function isString(s) {
return typeof s === 'string';
}
function utf8ToUint8Array(str) {
var chars = [];
str = window.btoa(unescape(encodeURIComponent(str)));
return Base64URL.parse(str);
}
function isFunction(fn) {
return typeof fn === 'function';
}
function isObject(arg) {
return arg !== null && typeof arg === 'object';
}
exports.verifyJWT = function signJWT(token, secret, alg, cb) {
if (!isFunction(cb)) {
throw new Error('cb must be a function');
}
if (!isString(token)) {
return cb(new Error('token must be a string'));
}
if (!isString(secret)) {
return cb(new Error('secret must be a string'));
}
if (!isString(alg)) {
return cb(new Error('alg must be a string'));
}
var tokenParts = token.split('.');
if (tokenParts.length !== 3) {
return cb(new Error('token must have 3 parts'));
}
var algorithms = {
HS256: {
name: 'HMAC',
hash: {
name: 'SHA-256'
}
}
};
var importAlgorithm = algorithms[alg];
if (!importAlgorithm) {
return cb(new Error('algorithm not found'));
}
// TODO Test utf8ToUint8Array function
var keyData = utf8ToUint8Array(secret);
cryptoSubtle.importKey(
'raw',
keyData,
importAlgorithm,
false,
['sign']
).then(function (key) {
var partialToken = tokenParts.slice(0,2).join('.');
var signaturePart = tokenParts[2];
// TODO Test utf8ToUint8Array function
var messageAsUint8Array = utf8ToUint8Array(partialToken);
// TODO Test utf8ToUint8Array function
var signatureAsUint8Array = utf8ToUint8Array(signaturePart);
cryptoSubtle.sign(
importAlgorithm.name,
key,
messageAsUint8Array
).then(function (res) {
// TODO Test
var resBase64 = Base64URL.stringify(new Uint8Array(res));
// TODO Time comparison
cb(null, resBase64 === signaturePart);
}, cb);
}, cb);
};
exports.signJWT = function signJWT(payload, secret, alg, cb) {
if (!isFunction(cb)) {
throw new Error('cb must be a function');
}
if (!isObject(payload)) {
return cb(new Error('payload must be an object'));
}
if (!isString(secret)) {
return cb(new Error('secret must be a string'));
}
if (!isString(alg)) {
return cb(new Error('alg must be a string'));
}
var algorithms = {
HS256: {
name: 'HMAC',
hash: {
name: 'SHA-256'
}
}
};
var importAlgorithm = algorithms[alg];
if (!importAlgorithm) {
return cb(new Error('algorithm not found'));
}
var payloadAsJSON;
try {
payloadAsJSON = JSON.stringify(payload);
} catch (err) {
return cb(err);
}
var header = {alg: alg, typ: 'JWT'};
var headerAsJSON = JSON.stringify(header);
var partialToken = Base64URL.stringify(utf8ToUint8Array(headerAsJSON)) + '.' +
Base64URL.stringify(utf8ToUint8Array(payloadAsJSON));
// TODO Test utf8ToUint8Array function
var keyData = utf8ToUint8Array(secret);
cryptoSubtle.importKey(
'raw',
keyData,
importAlgorithm,
false,
['sign']
).then(function (key) {
var characters = payloadAsJSON.split('');
var it = utf8ToUint8Array(payloadAsJSON).entries();
var i = 0;
var result = [];
var current;
while (!(current = it.next()).done) {
result.push([current.value[1], characters[i]]);
i++;
}
// TODO Test utf8ToUint8Array function
var messageAsUint8Array = utf8ToUint8Array(partialToken);
cryptoSubtle.sign(
importAlgorithm.name,
key,
messageAsUint8Array
).then(function (signature) {
// TODO Test
var signatureAsBase64 = Base64URL.stringify(new Uint8Array(signature));
var token = partialToken + '.' + signatureAsBase64;
cb(null, token);
}, cb);
}, cb);
};
exports.decodeJWT = function (token) {
var output = token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
switch (output.length % 4) {
case 0:
break;
case 2:
output += '==';
break;
case 3:
output += '=';
break;
default:
throw 'Illegal base64url string!';
}
// TODO Use shim or document incomplete browsers
var result = window.atob(output);
try{
return decodeURIComponent(escape(result));
} catch (err) {
return result;
}
};
exports.parseJWT = function (token) {
// TODO: Handle when decodeJWT fails.
// TODO: Handle when JSON.parse fails.
return JSON.parse(exports.decodeJWT(token));
};
}());