-
Notifications
You must be signed in to change notification settings - Fork 2
/
djwt.js
387 lines (385 loc) · 10.9 KB
/
djwt.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// deno-fmt-ignore-file
// deno-lint-ignore-file
// This code was bundled using `deno bundle` and it's not recommended to edit it manually
const base64abc = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"+",
"/"
];
function encode(data) {
const uint8 = typeof data === "string" ? new TextEncoder().encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data);
let result = "", i;
const l = uint8.length;
for(i = 2; i < l; i += 3){
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4 | uint8[i - 1] >> 4];
result += base64abc[(uint8[i - 1] & 0x0f) << 2 | uint8[i] >> 6];
result += base64abc[uint8[i] & 0x3f];
}
if (i === l + 1) {
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4];
result += "==";
}
if (i === l) {
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4 | uint8[i - 1] >> 4];
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
result += "=";
}
return result;
}
function decode(b64) {
const binString = atob(b64);
const size = binString.length;
const bytes = new Uint8Array(size);
for(let i = 0; i < size; i++){
bytes[i] = binString.charCodeAt(i);
}
return bytes;
}
function addPaddingToBase64url(base64url) {
if (base64url.length % 4 === 2) return base64url + "==";
if (base64url.length % 4 === 3) return base64url + "=";
if (base64url.length % 4 === 1) {
throw new TypeError("Illegal base64url string!");
}
return base64url;
}
function convertBase64urlToBase64(b64url) {
if (!/^[-_A-Z0-9]*?={0,2}$/i.test(b64url)) {
throw new TypeError("Failed to decode base64url: invalid character");
}
return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
}
function convertBase64ToBase64url(b64) {
return b64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
}
function encode1(data) {
return convertBase64ToBase64url(encode(data));
}
function decode1(b64url) {
return decode(convertBase64urlToBase64(b64url));
}
const mod = {
encode: encode1,
decode: decode1
};
const encoder = new TextEncoder();
const decoder = new TextDecoder();
function isArray(input) {
return Array.isArray(input);
}
function isDefined(input) {
return input !== undefined;
}
function isNotNull(input) {
return input !== null;
}
function isNotNumber(input) {
return typeof input !== "number";
}
function isNotString(input) {
return typeof input !== "string";
}
function isNull(input) {
return input === null;
}
function isNumber(input) {
return typeof input === "number";
}
function isObject(input) {
return input !== null && typeof input === "object" && Array.isArray(input) === false;
}
function isString(input) {
return typeof input === "string";
}
function isUndefined(input) {
return input === undefined;
}
function isHashedKeyAlgorithm(algorithm) {
return isString(algorithm.hash?.name);
}
function isEcKeyAlgorithm(algorithm) {
return isString(algorithm.namedCurve);
}
function verify(alg, key) {
if (alg === "none") {
if (isNotNull(key)) {
throw new Error(`The alg '${alg}' does not allow a key.`);
} else return true;
} else {
if (!key) throw new Error(`The alg '${alg}' demands a key.`);
const keyAlgorithm = key.algorithm;
const algAlgorithm = getAlgorithm(alg);
if (keyAlgorithm.name === algAlgorithm.name) {
if (isHashedKeyAlgorithm(keyAlgorithm)) {
return keyAlgorithm.hash.name === algAlgorithm.hash.name;
} else if (isEcKeyAlgorithm(keyAlgorithm)) {
return keyAlgorithm.namedCurve === algAlgorithm.namedCurve;
}
}
return false;
}
}
function getAlgorithm(alg) {
switch(alg){
case "HS256":
return {
hash: {
name: "SHA-256"
},
name: "HMAC"
};
case "HS384":
return {
hash: {
name: "SHA-384"
},
name: "HMAC"
};
case "HS512":
return {
hash: {
name: "SHA-512"
},
name: "HMAC"
};
case "PS256":
return {
hash: {
name: "SHA-256"
},
name: "RSA-PSS",
saltLength: 256 >> 3
};
case "PS384":
return {
hash: {
name: "SHA-384"
},
name: "RSA-PSS",
saltLength: 384 >> 3
};
case "PS512":
return {
hash: {
name: "SHA-512"
},
name: "RSA-PSS",
saltLength: 512 >> 3
};
case "RS256":
return {
hash: {
name: "SHA-256"
},
name: "RSASSA-PKCS1-v1_5"
};
case "RS384":
return {
hash: {
name: "SHA-384"
},
name: "RSASSA-PKCS1-v1_5"
};
case "RS512":
return {
hash: {
name: "SHA-512"
},
name: "RSASSA-PKCS1-v1_5"
};
case "ES256":
return {
hash: {
name: "SHA-256"
},
name: "ECDSA",
namedCurve: "P-256"
};
case "ES384":
return {
hash: {
name: "SHA-384"
},
name: "ECDSA",
namedCurve: "P-384"
};
default:
throw new Error(`The jwt's alg '${alg}' is not supported.`);
}
}
async function verify1(signature, key, alg, signingInput) {
return isNull(key) ? signature.length === 0 : await crypto.subtle.verify(getAlgorithm(alg), key, signature, encoder.encode(signingInput));
}
async function create(alg, key, signingInput) {
return isNull(key) ? "" : mod.encode(new Uint8Array(await crypto.subtle.sign(getAlgorithm(alg), key, encoder.encode(signingInput))));
}
function isExpired(exp, leeway) {
return exp + leeway < Date.now() / 1000;
}
function isTooEarly(nbf, leeway) {
return nbf - leeway > Date.now() / 1000;
}
function is3Tuple(arr) {
return arr.length === 3;
}
function hasInvalidTimingClaims(...claimValues) {
return claimValues.some((claimValue)=>isDefined(claimValue) && isNotNumber(claimValue));
}
function validateTimingClaims(payload, { expLeeway =1 , nbfLeeway =1 } = {}) {
if (hasInvalidTimingClaims(payload.exp, payload.nbf)) {
throw new Error(`The jwt has an invalid 'exp' or 'nbf' claim.`);
}
if (isNumber(payload.exp) && isExpired(payload.exp, expLeeway)) {
throw RangeError("The jwt is expired.");
}
if (isNumber(payload.nbf) && isTooEarly(payload.nbf, nbfLeeway)) {
throw RangeError("The jwt is used too early.");
}
}
function hasValidAudClaim(claimValue) {
if (isUndefined(claimValue) || isString(claimValue)) return true;
else return isArray(claimValue) && claimValue.every(isString);
}
function validateAudClaim(aud, audience) {
if (hasValidAudClaim(aud)) {
if (isUndefined(aud)) {
throw new Error("The jwt has no 'aud' claim.");
}
const audArray = isString(aud) ? [
aud
] : aud;
const audienceArrayOrRegex = isString(audience) ? [
audience
] : audience;
if (!audArray.some((audString)=>isArray(audienceArrayOrRegex) ? audienceArrayOrRegex.includes(audString) : audienceArrayOrRegex.test(audString))) {
throw new Error("The identification with the value in the 'aud' claim has failed.");
}
} else {
throw new Error(`The jwt has an invalid 'aud' claim.`);
}
}
function decode2(jwt) {
try {
const arr = jwt.split(".").map(mod.decode).map((uint8Array, index)=>index === 0 || index === 1 ? JSON.parse(decoder.decode(uint8Array)) : uint8Array);
if (is3Tuple(arr)) return arr;
else throw new Error();
} catch {
throw Error("The serialization of the jwt is invalid.");
}
}
function validate([header, payload, signature], options) {
if (isNotString(header?.alg)) {
throw new Error(`The jwt's 'alg' header parameter value must be a string.`);
}
if (isObject(payload)) {
validateTimingClaims(payload, options);
if (isDefined(options?.audience)) {
validateAudClaim(payload.aud, options.audience);
}
return {
header,
payload,
signature
};
} else {
throw new Error(`The jwt claims set is not a JSON object.`);
}
}
async function verify2(jwt, key, options) {
const { header , payload , signature } = validate(decode2(jwt), options);
if (verify(header.alg, key)) {
if (!await verify1(signature, key, header.alg, jwt.slice(0, jwt.lastIndexOf(".")))) {
throw new Error("The jwt's signature does not match the verification signature.");
}
if (!(options?.predicates || []).every((predicate)=>predicate(payload))) {
throw new Error("The payload does not satisfy all passed predicates.");
}
return payload;
} else {
throw new Error(`The jwt's alg '${header.alg}' does not match the key's algorithm.`);
}
}
function createSigningInput(header, payload) {
return `${mod.encode(encoder.encode(JSON.stringify(header)))}.${mod.encode(encoder.encode(JSON.stringify(payload)))}`;
}
async function create1(header, payload, key) {
if (verify(header.alg, key)) {
const signingInput = createSigningInput(header, payload);
const signature = await create(header.alg, key, signingInput);
return `${signingInput}.${signature}`;
} else {
throw new Error(`The jwt's alg '${header.alg}' does not match the key's algorithm.`);
}
}
function getNumericDate(exp) {
return Math.round((exp instanceof Date ? exp.getTime() : Date.now() + exp * 1000) / 1000);
}
export { decode2 as decode };
export { validate as validate };
export { verify2 as verify };
export { create1 as create };
export { getNumericDate as getNumericDate };