@versini/auth-common 3.1.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +40 -1
- package/dist/index.js +134 -124
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -94,4 +94,43 @@ type GetToken = {
|
|
|
94
94
|
};
|
|
95
95
|
declare const getToken: ({ headers, body, clientId }: GetToken) => string;
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
type ScopesGrants = {
|
|
98
|
+
[key: string]: string[];
|
|
99
|
+
} | string[];
|
|
100
|
+
/**
|
|
101
|
+
* Checks if the given token grants the required scopes.
|
|
102
|
+
*
|
|
103
|
+
* This function verifies the provided token and extracts its payload.
|
|
104
|
+
* It then checks if the token contains the required scopes. The scopes can be provided
|
|
105
|
+
* either as an array of strings or as a map of string arrays. When the scopes are provided
|
|
106
|
+
* as a map, the function checks if the token contains at least one of the scopes in each
|
|
107
|
+
* of the map's values (OR operation).
|
|
108
|
+
*
|
|
109
|
+
*
|
|
110
|
+
* @async
|
|
111
|
+
* @function isGranted
|
|
112
|
+
* @param {string} token - The token to be verified and checked for scopes.
|
|
113
|
+
* @param {ScopesGrants} scopes - The required scopes. This can be an array of strings
|
|
114
|
+
* representing the scopes or a map where the keys are strings
|
|
115
|
+
* and the values are arrays of strings representing the scopes.
|
|
116
|
+
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating whether the
|
|
117
|
+
* token grants the required scopes.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* Example with an array of scopes (AND operation)
|
|
121
|
+
* const scopesArray = ["read", "write"];
|
|
122
|
+
* const res = isGranted(token, scopesArray);
|
|
123
|
+
* console.log(res); // true only if the token has both "read" and "write" scopes
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* Example with a map of scopes (OR operation)
|
|
127
|
+
* const scopesMap = {
|
|
128
|
+
* "admin": ["read", "write"],
|
|
129
|
+
* "user": ["read"]
|
|
130
|
+
* };
|
|
131
|
+
* const res = isGranted(token, scopesMap);
|
|
132
|
+
* console.log(res); // true if the token has either "read" and "write" scopes or "read" scope
|
|
133
|
+
*/
|
|
134
|
+
declare const isGranted: (token: string, scopes: ScopesGrants) => Promise<boolean>;
|
|
135
|
+
|
|
136
|
+
export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, type ScopesGrants, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, isGranted, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/auth-common v3.
|
|
2
|
+
@versini/auth-common v3.3.0
|
|
3
3
|
© 2024 gizmette.com
|
|
4
4
|
*/
|
|
5
5
|
try {
|
|
6
6
|
window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
|
|
7
|
-
version: "3.
|
|
8
|
-
buildTime: "07/21/2024
|
|
7
|
+
version: "3.3.0",
|
|
8
|
+
buildTime: "07/21/2024 07:33 PM EDT",
|
|
9
9
|
homepage: "https://github.com/aversini/auth-client",
|
|
10
10
|
license: "MIT"
|
|
11
11
|
});
|
|
12
12
|
} catch {
|
|
13
13
|
}
|
|
14
|
-
const
|
|
14
|
+
const je = {
|
|
15
15
|
ID_TOKEN: "id_token",
|
|
16
16
|
ACCESS_TOKEN: "token",
|
|
17
17
|
ID_AND_ACCESS_TOKEN: "id_token token",
|
|
18
18
|
CODE: "code",
|
|
19
19
|
REFRESH_TOKEN: "refresh_token",
|
|
20
20
|
PASSKEY: "passkey"
|
|
21
|
-
},
|
|
21
|
+
}, et = {
|
|
22
22
|
CLIENT_ID: "X-Auth-ClientId"
|
|
23
23
|
}, ne = {
|
|
24
24
|
ACCESS_TOKEN: "access_token"
|
|
25
|
-
},
|
|
25
|
+
}, I = {
|
|
26
26
|
ALG: "RS256",
|
|
27
27
|
USER_ID_KEY: "sub",
|
|
28
28
|
TOKEN_ID_KEY: "__raw",
|
|
@@ -41,17 +41,17 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
|
|
|
41
41
|
l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
|
|
42
42
|
sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
|
|
43
43
|
awIDAQAB
|
|
44
|
-
-----END PUBLIC KEY-----`,
|
|
44
|
+
-----END PUBLIC KEY-----`, tt = {
|
|
45
45
|
ACCESS: "5m",
|
|
46
46
|
ID: "90d",
|
|
47
47
|
REFRESH: "90d"
|
|
48
|
-
},
|
|
48
|
+
}, rt = {
|
|
49
49
|
AUTHENTICATE: "authenticate",
|
|
50
50
|
CODE: "code",
|
|
51
51
|
LOGOUT: "logout"
|
|
52
|
-
},
|
|
52
|
+
}, O = crypto, G = (e) => e instanceof CryptoKey, T = new TextEncoder(), C = new TextDecoder();
|
|
53
53
|
function oe(...e) {
|
|
54
|
-
const t = e.reduce((a, { length:
|
|
54
|
+
const t = e.reduce((a, { length: o }) => a + o, 0), r = new Uint8Array(t);
|
|
55
55
|
let n = 0;
|
|
56
56
|
for (const a of e)
|
|
57
57
|
r.set(a, n), n += a.length;
|
|
@@ -142,7 +142,7 @@ function m(e, t = "algorithm.name") {
|
|
|
142
142
|
function v(e, t) {
|
|
143
143
|
return e.name === t;
|
|
144
144
|
}
|
|
145
|
-
function
|
|
145
|
+
function W(e) {
|
|
146
146
|
return parseInt(e.name.slice(4), 10);
|
|
147
147
|
}
|
|
148
148
|
function de(e) {
|
|
@@ -175,7 +175,7 @@ function le(e, t, ...r) {
|
|
|
175
175
|
if (!v(e.algorithm, "HMAC"))
|
|
176
176
|
throw m("HMAC");
|
|
177
177
|
const n = parseInt(t.slice(2), 10);
|
|
178
|
-
if (
|
|
178
|
+
if (W(e.algorithm.hash) !== n)
|
|
179
179
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
180
180
|
break;
|
|
181
181
|
}
|
|
@@ -185,7 +185,7 @@ function le(e, t, ...r) {
|
|
|
185
185
|
if (!v(e.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
186
186
|
throw m("RSASSA-PKCS1-v1_5");
|
|
187
187
|
const n = parseInt(t.slice(2), 10);
|
|
188
|
-
if (
|
|
188
|
+
if (W(e.algorithm.hash) !== n)
|
|
189
189
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
190
190
|
break;
|
|
191
191
|
}
|
|
@@ -195,7 +195,7 @@ function le(e, t, ...r) {
|
|
|
195
195
|
if (!v(e.algorithm, "RSA-PSS"))
|
|
196
196
|
throw m("RSA-PSS");
|
|
197
197
|
const n = parseInt(t.slice(2), 10);
|
|
198
|
-
if (
|
|
198
|
+
if (W(e.algorithm.hash) !== n)
|
|
199
199
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
200
200
|
break;
|
|
201
201
|
}
|
|
@@ -219,7 +219,7 @@ function le(e, t, ...r) {
|
|
|
219
219
|
}
|
|
220
220
|
ue(e, r);
|
|
221
221
|
}
|
|
222
|
-
function
|
|
222
|
+
function q(e, t, ...r) {
|
|
223
223
|
var n;
|
|
224
224
|
if (r.length > 2) {
|
|
225
225
|
const a = r.pop();
|
|
@@ -227,11 +227,11 @@ function z(e, t, ...r) {
|
|
|
227
227
|
} else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
|
|
228
228
|
return t == null ? e += ` Received ${t}` : typeof t == "function" && t.name ? e += ` Received function ${t.name}` : typeof t == "object" && t != null && (n = t.constructor) != null && n.name && (e += ` Received an instance of ${t.constructor.name}`), e;
|
|
229
229
|
}
|
|
230
|
-
const L = (e, ...t) =>
|
|
231
|
-
function
|
|
232
|
-
return
|
|
230
|
+
const L = (e, ...t) => q("Key must be ", e, ...t);
|
|
231
|
+
function z(e, t, ...r) {
|
|
232
|
+
return q(`Key for the ${e} algorithm must be `, t, ...r);
|
|
233
233
|
}
|
|
234
|
-
const X = (e) =>
|
|
234
|
+
const X = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"], fe = (...e) => {
|
|
235
235
|
const t = e.filter(Boolean);
|
|
236
236
|
if (t.length === 0 || t.length === 1)
|
|
237
237
|
return !0;
|
|
@@ -242,10 +242,10 @@ const X = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
|
|
|
242
242
|
r = new Set(a);
|
|
243
243
|
continue;
|
|
244
244
|
}
|
|
245
|
-
for (const
|
|
246
|
-
if (r.has(
|
|
245
|
+
for (const o of a) {
|
|
246
|
+
if (r.has(o))
|
|
247
247
|
return !1;
|
|
248
|
-
r.add(
|
|
248
|
+
r.add(o);
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
251
|
return !0;
|
|
@@ -253,7 +253,7 @@ const X = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
|
|
|
253
253
|
function he(e) {
|
|
254
254
|
return typeof e == "object" && e !== null;
|
|
255
255
|
}
|
|
256
|
-
function
|
|
256
|
+
function K(e) {
|
|
257
257
|
if (!he(e) || Object.prototype.toString.call(e) !== "[object Object]")
|
|
258
258
|
return !1;
|
|
259
259
|
if (Object.getPrototypeOf(e) === null)
|
|
@@ -350,25 +350,25 @@ const Se = async (e) => {
|
|
|
350
350
|
e.ext ?? !1,
|
|
351
351
|
e.key_ops ?? r
|
|
352
352
|
], a = { ...e };
|
|
353
|
-
return delete a.alg, delete a.use,
|
|
353
|
+
return delete a.alg, delete a.use, O.subtle.importKey("jwk", a, ...n);
|
|
354
354
|
}, Q = (e) => b(e);
|
|
355
|
-
let
|
|
355
|
+
let J, D;
|
|
356
356
|
const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j = async (e, t, r, n) => {
|
|
357
357
|
let a = e.get(t);
|
|
358
358
|
if (a != null && a[n])
|
|
359
359
|
return a[n];
|
|
360
|
-
const
|
|
361
|
-
return a ? a[n] =
|
|
360
|
+
const o = await Se({ ...r, alg: n });
|
|
361
|
+
return a ? a[n] = o : e.set(t, { [n]: o }), o;
|
|
362
362
|
}, ye = (e, t) => {
|
|
363
363
|
if (Z(e)) {
|
|
364
364
|
let r = e.export({ format: "jwk" });
|
|
365
|
-
return delete r.d, delete r.dp, delete r.dq, delete r.p, delete r.q, delete r.qi, r.k ? Q(r.k) : (
|
|
365
|
+
return delete r.d, delete r.dp, delete r.dq, delete r.p, delete r.q, delete r.qi, r.k ? Q(r.k) : (D || (D = /* @__PURE__ */ new WeakMap()), j(D, e, r, t));
|
|
366
366
|
}
|
|
367
367
|
return e;
|
|
368
368
|
}, Ee = (e, t) => {
|
|
369
369
|
if (Z(e)) {
|
|
370
370
|
let r = e.export({ format: "jwk" });
|
|
371
|
-
return r.k ? Q(r.k) : (
|
|
371
|
+
return r.k ? Q(r.k) : (J || (J = /* @__PURE__ */ new WeakMap()), j(J, e, r, t));
|
|
372
372
|
}
|
|
373
373
|
return e;
|
|
374
374
|
}, we = { normalizePublicKey: ye, normalizePrivateKey: Ee }, E = (e, t, r = 0) => {
|
|
@@ -377,8 +377,8 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
377
377
|
if (n === -1)
|
|
378
378
|
return !1;
|
|
379
379
|
const a = e.subarray(n, n + t.length);
|
|
380
|
-
return a.length !== t.length ? !1 : a.every((
|
|
381
|
-
},
|
|
380
|
+
return a.length !== t.length ? !1 : a.every((o, i) => o === t[i]) || E(e, t, n + 1);
|
|
381
|
+
}, k = (e) => {
|
|
382
382
|
switch (!0) {
|
|
383
383
|
case E(e, [42, 134, 72, 206, 61, 3, 1, 7]):
|
|
384
384
|
return "P-256";
|
|
@@ -398,74 +398,74 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
398
398
|
throw new w("Invalid or unsupported EC Key Curve or OKP Key Sub Type");
|
|
399
399
|
}
|
|
400
400
|
}, ge = async (e, t, r, n, a) => {
|
|
401
|
-
let
|
|
401
|
+
let o, i;
|
|
402
402
|
const c = new Uint8Array(atob(r.replace(e, "")).split("").map((s) => s.charCodeAt(0)));
|
|
403
403
|
switch (n) {
|
|
404
404
|
case "PS256":
|
|
405
405
|
case "PS384":
|
|
406
406
|
case "PS512":
|
|
407
|
-
|
|
407
|
+
o = { name: "RSA-PSS", hash: `SHA-${n.slice(-3)}` }, i = ["verify"];
|
|
408
408
|
break;
|
|
409
409
|
case "RS256":
|
|
410
410
|
case "RS384":
|
|
411
411
|
case "RS512":
|
|
412
|
-
|
|
412
|
+
o = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${n.slice(-3)}` }, i = ["verify"];
|
|
413
413
|
break;
|
|
414
414
|
case "RSA-OAEP":
|
|
415
415
|
case "RSA-OAEP-256":
|
|
416
416
|
case "RSA-OAEP-384":
|
|
417
417
|
case "RSA-OAEP-512":
|
|
418
|
-
|
|
418
|
+
o = {
|
|
419
419
|
name: "RSA-OAEP",
|
|
420
420
|
hash: `SHA-${parseInt(n.slice(-3), 10) || 1}`
|
|
421
|
-
},
|
|
421
|
+
}, i = ["encrypt", "wrapKey"];
|
|
422
422
|
break;
|
|
423
423
|
case "ES256":
|
|
424
|
-
|
|
424
|
+
o = { name: "ECDSA", namedCurve: "P-256" }, i = ["verify"];
|
|
425
425
|
break;
|
|
426
426
|
case "ES384":
|
|
427
|
-
|
|
427
|
+
o = { name: "ECDSA", namedCurve: "P-384" }, i = ["verify"];
|
|
428
428
|
break;
|
|
429
429
|
case "ES512":
|
|
430
|
-
|
|
430
|
+
o = { name: "ECDSA", namedCurve: "P-521" }, i = ["verify"];
|
|
431
431
|
break;
|
|
432
432
|
case "ECDH-ES":
|
|
433
433
|
case "ECDH-ES+A128KW":
|
|
434
434
|
case "ECDH-ES+A192KW":
|
|
435
435
|
case "ECDH-ES+A256KW": {
|
|
436
|
-
const s =
|
|
437
|
-
|
|
436
|
+
const s = k(c);
|
|
437
|
+
o = s.startsWith("P-") ? { name: "ECDH", namedCurve: s } : { name: s }, i = [];
|
|
438
438
|
break;
|
|
439
439
|
}
|
|
440
440
|
case "EdDSA":
|
|
441
|
-
|
|
441
|
+
o = { name: k(c) }, i = ["verify"];
|
|
442
442
|
break;
|
|
443
443
|
default:
|
|
444
444
|
throw new w('Invalid or unsupported "alg" (Algorithm) value');
|
|
445
445
|
}
|
|
446
|
-
return
|
|
446
|
+
return O.subtle.importKey(t, c, o, !1, i);
|
|
447
447
|
}, Ae = (e, t, r) => ge(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
|
|
448
448
|
async function be(e, t, r) {
|
|
449
449
|
if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
|
|
450
450
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
451
451
|
return Ae(e, t);
|
|
452
452
|
}
|
|
453
|
-
const
|
|
453
|
+
const P = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
|
|
454
454
|
if (!(t instanceof Uint8Array)) {
|
|
455
455
|
if (!X(t))
|
|
456
|
-
throw new TypeError(
|
|
456
|
+
throw new TypeError(z(e, t, ...R, "Uint8Array"));
|
|
457
457
|
if (t.type !== "secret")
|
|
458
|
-
throw new TypeError(`${
|
|
458
|
+
throw new TypeError(`${P(t)} instances for symmetric algorithms must be of type "secret"`);
|
|
459
459
|
}
|
|
460
460
|
}, Te = (e, t, r) => {
|
|
461
461
|
if (!X(t))
|
|
462
|
-
throw new TypeError(
|
|
462
|
+
throw new TypeError(z(e, t, ...R));
|
|
463
463
|
if (t.type === "secret")
|
|
464
|
-
throw new TypeError(`${
|
|
464
|
+
throw new TypeError(`${P(t)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
465
465
|
if (t.algorithm && r === "verify" && t.type === "private")
|
|
466
|
-
throw new TypeError(`${
|
|
466
|
+
throw new TypeError(`${P(t)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
467
467
|
if (t.algorithm && r === "encrypt" && t.type === "private")
|
|
468
|
-
throw new TypeError(`${
|
|
468
|
+
throw new TypeError(`${P(t)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
469
469
|
}, ve = (e, t, r) => {
|
|
470
470
|
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : Te(e, t, r);
|
|
471
471
|
};
|
|
@@ -474,27 +474,27 @@ function _e(e, t, r, n, a) {
|
|
|
474
474
|
throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
475
475
|
if (!n || n.crit === void 0)
|
|
476
476
|
return /* @__PURE__ */ new Set();
|
|
477
|
-
if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((
|
|
477
|
+
if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((i) => typeof i != "string" || i.length === 0))
|
|
478
478
|
throw new e('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
|
|
479
|
-
let
|
|
480
|
-
r !== void 0 ?
|
|
481
|
-
for (const
|
|
482
|
-
if (!
|
|
483
|
-
throw new w(`Extension Header Parameter "${
|
|
484
|
-
if (a[
|
|
485
|
-
throw new e(`Extension Header Parameter "${
|
|
486
|
-
if (
|
|
487
|
-
throw new e(`Extension Header Parameter "${
|
|
479
|
+
let o;
|
|
480
|
+
r !== void 0 ? o = new Map([...Object.entries(r), ...t.entries()]) : o = t;
|
|
481
|
+
for (const i of n.crit) {
|
|
482
|
+
if (!o.has(i))
|
|
483
|
+
throw new w(`Extension Header Parameter "${i}" is not recognized`);
|
|
484
|
+
if (a[i] === void 0)
|
|
485
|
+
throw new e(`Extension Header Parameter "${i}" is missing`);
|
|
486
|
+
if (o.get(i) && n[i] === void 0)
|
|
487
|
+
throw new e(`Extension Header Parameter "${i}" MUST be integrity protected`);
|
|
488
488
|
}
|
|
489
489
|
return new Set(n.crit);
|
|
490
490
|
}
|
|
491
|
-
const
|
|
491
|
+
const Pe = (e, t) => {
|
|
492
492
|
if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
|
|
493
493
|
throw new TypeError(`"${e}" option must be an array of strings`);
|
|
494
494
|
if (t)
|
|
495
495
|
return new Set(t);
|
|
496
496
|
};
|
|
497
|
-
function
|
|
497
|
+
function Ie(e, t) {
|
|
498
498
|
const r = `SHA-${e.slice(-3)}`;
|
|
499
499
|
switch (e) {
|
|
500
500
|
case "HS256":
|
|
@@ -519,28 +519,28 @@ function Re(e, t) {
|
|
|
519
519
|
throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
|
|
520
520
|
}
|
|
521
521
|
}
|
|
522
|
-
async function
|
|
523
|
-
if (t = await we.normalizePublicKey(t, e),
|
|
522
|
+
async function Re(e, t, r) {
|
|
523
|
+
if (t = await we.normalizePublicKey(t, e), G(t))
|
|
524
524
|
return le(t, e, r), t;
|
|
525
525
|
if (t instanceof Uint8Array) {
|
|
526
526
|
if (!e.startsWith("HS"))
|
|
527
527
|
throw new TypeError(L(t, ...R));
|
|
528
|
-
return
|
|
528
|
+
return O.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
|
|
529
529
|
}
|
|
530
530
|
throw new TypeError(L(t, ...R, "Uint8Array"));
|
|
531
531
|
}
|
|
532
532
|
const Ke = async (e, t, r, n) => {
|
|
533
|
-
const a = await
|
|
533
|
+
const a = await Re(e, t, "verify");
|
|
534
534
|
pe(e, a);
|
|
535
|
-
const
|
|
535
|
+
const o = Ie(e, a.algorithm);
|
|
536
536
|
try {
|
|
537
|
-
return await
|
|
537
|
+
return await O.subtle.verify(o, a, r, n);
|
|
538
538
|
} catch {
|
|
539
539
|
return !1;
|
|
540
540
|
}
|
|
541
541
|
};
|
|
542
542
|
async function Oe(e, t, r) {
|
|
543
|
-
if (!
|
|
543
|
+
if (!K(e))
|
|
544
544
|
throw new u("Flattened JWS must be an object");
|
|
545
545
|
if (e.protected === void 0 && e.header === void 0)
|
|
546
546
|
throw new u('Flattened JWS must have either of the "protected" or "header" members');
|
|
@@ -550,13 +550,13 @@ async function Oe(e, t, r) {
|
|
|
550
550
|
throw new u("JWS Payload missing");
|
|
551
551
|
if (typeof e.signature != "string")
|
|
552
552
|
throw new u("JWS Signature missing or incorrect type");
|
|
553
|
-
if (e.header !== void 0 && !
|
|
553
|
+
if (e.header !== void 0 && !K(e.header))
|
|
554
554
|
throw new u("JWS Unprotected Header incorrect type");
|
|
555
555
|
let n = {};
|
|
556
556
|
if (e.protected)
|
|
557
557
|
try {
|
|
558
|
-
const
|
|
559
|
-
n = JSON.parse(C.decode(
|
|
558
|
+
const x = b(e.protected);
|
|
559
|
+
n = JSON.parse(C.decode(x));
|
|
560
560
|
} catch {
|
|
561
561
|
throw new u("JWS Protected Header is invalid");
|
|
562
562
|
}
|
|
@@ -565,17 +565,17 @@ async function Oe(e, t, r) {
|
|
|
565
565
|
const a = {
|
|
566
566
|
...n,
|
|
567
567
|
...e.header
|
|
568
|
-
},
|
|
569
|
-
let
|
|
570
|
-
if (
|
|
568
|
+
}, o = _e(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
|
|
569
|
+
let i = !0;
|
|
570
|
+
if (o.has("b64") && (i = n.b64, typeof i != "boolean"))
|
|
571
571
|
throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
572
572
|
const { alg: c } = a;
|
|
573
573
|
if (typeof c != "string" || !c)
|
|
574
574
|
throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
575
|
-
const s = r &&
|
|
575
|
+
const s = r && Pe("algorithms", r.algorithms);
|
|
576
576
|
if (s && !s.has(c))
|
|
577
577
|
throw new ce('"alg" (Algorithm) Header Parameter value not allowed');
|
|
578
|
-
if (
|
|
578
|
+
if (i) {
|
|
579
579
|
if (typeof e.payload != "string")
|
|
580
580
|
throw new u("JWS Payload must be a string");
|
|
581
581
|
} else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
|
|
@@ -592,7 +592,7 @@ async function Oe(e, t, r) {
|
|
|
592
592
|
if (!await Ke(c, t, f, g))
|
|
593
593
|
throw new se();
|
|
594
594
|
let y;
|
|
595
|
-
if (
|
|
595
|
+
if (i)
|
|
596
596
|
try {
|
|
597
597
|
y = b(e.payload);
|
|
598
598
|
} catch {
|
|
@@ -605,13 +605,13 @@ async function Oe(e, t, r) {
|
|
|
605
605
|
async function xe(e, t, r) {
|
|
606
606
|
if (e instanceof Uint8Array && (e = C.decode(e)), typeof e != "string")
|
|
607
607
|
throw new u("Compact JWS must be a string or Uint8Array");
|
|
608
|
-
const { 0: n, 1: a, 2:
|
|
609
|
-
if (
|
|
608
|
+
const { 0: n, 1: a, 2: o, length: i } = e.split(".");
|
|
609
|
+
if (i !== 3)
|
|
610
610
|
throw new u("Invalid Compact JWS");
|
|
611
|
-
const c = await Oe({ payload: a, protected: n, signature:
|
|
611
|
+
const c = await Oe({ payload: a, protected: n, signature: o }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
|
|
612
612
|
return typeof t == "function" ? { ...s, key: c.key } : s;
|
|
613
613
|
}
|
|
614
|
-
const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60,
|
|
614
|
+
const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, N = te * 24, Je = N * 7, De = N * 365.25, He = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i, M = (e) => {
|
|
615
615
|
const t = He.exec(e);
|
|
616
616
|
if (!t || t[4] && t[1])
|
|
617
617
|
throw new TypeError("Invalid time period format");
|
|
@@ -642,7 +642,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
642
642
|
case "day":
|
|
643
643
|
case "days":
|
|
644
644
|
case "d":
|
|
645
|
-
a = Math.round(r *
|
|
645
|
+
a = Math.round(r * N);
|
|
646
646
|
break;
|
|
647
647
|
case "week":
|
|
648
648
|
case "weeks":
|
|
@@ -660,17 +660,17 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
660
660
|
n = JSON.parse(C.decode(t));
|
|
661
661
|
} catch {
|
|
662
662
|
}
|
|
663
|
-
if (!
|
|
663
|
+
if (!K(n))
|
|
664
664
|
throw new S("JWT Claims Set must be a top-level JSON object");
|
|
665
665
|
const { typ: a } = r;
|
|
666
666
|
if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
|
|
667
667
|
throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
|
|
668
|
-
const { requiredClaims:
|
|
669
|
-
p !== void 0 && g.push("iat"), s !== void 0 && g.push("aud"), c !== void 0 && g.push("sub"),
|
|
668
|
+
const { requiredClaims: o = [], issuer: i, subject: c, audience: s, maxTokenAge: p } = r, g = [...o];
|
|
669
|
+
p !== void 0 && g.push("iat"), s !== void 0 && g.push("aud"), c !== void 0 && g.push("sub"), i !== void 0 && g.push("iss");
|
|
670
670
|
for (const l of new Set(g.reverse()))
|
|
671
671
|
if (!(l in n))
|
|
672
672
|
throw new h(`missing required "${l}" claim`, n, l, "missing");
|
|
673
|
-
if (
|
|
673
|
+
if (i && !(Array.isArray(i) ? i : [i]).includes(n.iss))
|
|
674
674
|
throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
|
|
675
675
|
if (c && n.sub !== c)
|
|
676
676
|
throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
|
|
@@ -679,7 +679,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
679
679
|
let f;
|
|
680
680
|
switch (typeof r.clockTolerance) {
|
|
681
681
|
case "string":
|
|
682
|
-
f =
|
|
682
|
+
f = M(r.clockTolerance);
|
|
683
683
|
break;
|
|
684
684
|
case "number":
|
|
685
685
|
f = r.clockTolerance;
|
|
@@ -690,7 +690,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
690
690
|
default:
|
|
691
691
|
throw new TypeError("Invalid clockTolerance option type");
|
|
692
692
|
}
|
|
693
|
-
const { currentDate:
|
|
693
|
+
const { currentDate: U } = r, y = We(U || /* @__PURE__ */ new Date());
|
|
694
694
|
if ((n.iat !== void 0 || p) && typeof n.iat != "number")
|
|
695
695
|
throw new h('"iat" claim must be a number', n, "iat", "invalid");
|
|
696
696
|
if (n.nbf !== void 0) {
|
|
@@ -706,8 +706,8 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
706
706
|
throw new $('"exp" claim timestamp check failed', n, "exp", "check_failed");
|
|
707
707
|
}
|
|
708
708
|
if (p) {
|
|
709
|
-
const l = y - n.iat,
|
|
710
|
-
if (l - f >
|
|
709
|
+
const l = y - n.iat, x = typeof p == "number" ? p : M(p);
|
|
710
|
+
if (l - f > x)
|
|
711
711
|
throw new $('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
|
|
712
712
|
if (l < 0 - f)
|
|
713
713
|
throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
|
|
@@ -715,15 +715,15 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
715
715
|
return n;
|
|
716
716
|
};
|
|
717
717
|
async function $e(e, t, r) {
|
|
718
|
-
var
|
|
718
|
+
var i;
|
|
719
719
|
const n = await xe(e, t, r);
|
|
720
|
-
if ((
|
|
720
|
+
if ((i = n.protectedHeader.crit) != null && i.includes("b64") && n.protectedHeader.b64 === !1)
|
|
721
721
|
throw new S("JWTs MUST NOT use unencoded payload");
|
|
722
|
-
const
|
|
723
|
-
return typeof t == "function" ? { ...
|
|
722
|
+
const o = { payload: Ue(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
|
|
723
|
+
return typeof t == "function" ? { ...o, key: n.key } : o;
|
|
724
724
|
}
|
|
725
725
|
const Le = b;
|
|
726
|
-
function
|
|
726
|
+
function ke(e) {
|
|
727
727
|
if (typeof e != "string")
|
|
728
728
|
throw new S("JWTs must use Compact JWS serialization, JWT must be a string");
|
|
729
729
|
const { 1: t, length: r } = e.split(".");
|
|
@@ -745,50 +745,50 @@ function Me(e) {
|
|
|
745
745
|
} catch {
|
|
746
746
|
throw new S("Failed to parse the decoded payload as JSON");
|
|
747
747
|
}
|
|
748
|
-
if (!
|
|
748
|
+
if (!K(a))
|
|
749
749
|
throw new S("Invalid JWT Claims Set");
|
|
750
750
|
return a;
|
|
751
751
|
}
|
|
752
|
-
const
|
|
752
|
+
const Me = async (e) => {
|
|
753
753
|
try {
|
|
754
|
-
const t =
|
|
754
|
+
const t = I.ALG, n = await be(ae, t);
|
|
755
755
|
return await $e(e, n, {
|
|
756
|
-
issuer:
|
|
756
|
+
issuer: I.ISSUER
|
|
757
757
|
});
|
|
758
758
|
} catch {
|
|
759
759
|
return;
|
|
760
760
|
}
|
|
761
761
|
}, nt = (e) => {
|
|
762
762
|
try {
|
|
763
|
-
return
|
|
763
|
+
return ke(e);
|
|
764
764
|
} catch {
|
|
765
765
|
return;
|
|
766
766
|
}
|
|
767
767
|
};
|
|
768
768
|
var d = [];
|
|
769
|
-
for (var
|
|
770
|
-
d.push((
|
|
771
|
-
function
|
|
769
|
+
for (var H = 0; H < 256; ++H)
|
|
770
|
+
d.push((H + 256).toString(16).slice(1));
|
|
771
|
+
function Be(e, t = 0) {
|
|
772
772
|
return (d[e[t + 0]] + d[e[t + 1]] + d[e[t + 2]] + d[e[t + 3]] + "-" + d[e[t + 4]] + d[e[t + 5]] + "-" + d[e[t + 6]] + d[e[t + 7]] + "-" + d[e[t + 8]] + d[e[t + 9]] + "-" + d[e[t + 10]] + d[e[t + 11]] + d[e[t + 12]] + d[e[t + 13]] + d[e[t + 14]] + d[e[t + 15]]).toLowerCase();
|
|
773
773
|
}
|
|
774
|
-
var _,
|
|
775
|
-
function
|
|
774
|
+
var _, Fe = new Uint8Array(16);
|
|
775
|
+
function Ve() {
|
|
776
776
|
if (!_ && (_ = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !_))
|
|
777
777
|
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
778
|
-
return _(
|
|
778
|
+
return _(Fe);
|
|
779
779
|
}
|
|
780
|
-
var
|
|
780
|
+
var Ye = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
781
781
|
const F = {
|
|
782
|
-
randomUUID:
|
|
782
|
+
randomUUID: Ye
|
|
783
783
|
};
|
|
784
784
|
function V(e, t, r) {
|
|
785
785
|
if (F.randomUUID && !t && !e)
|
|
786
786
|
return F.randomUUID();
|
|
787
787
|
e = e || {};
|
|
788
|
-
var n = e.random || (e.rng ||
|
|
789
|
-
return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128,
|
|
788
|
+
var n = e.random || (e.rng || Ve)();
|
|
789
|
+
return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Be(n);
|
|
790
790
|
}
|
|
791
|
-
const Y = globalThis.crypto,
|
|
791
|
+
const Y = globalThis.crypto, Ge = (e) => `${V()}${V()}`.slice(0, e), qe = (e) => btoa(
|
|
792
792
|
[...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
|
|
793
793
|
);
|
|
794
794
|
async function re(e) {
|
|
@@ -803,7 +803,7 @@ async function at(e) {
|
|
|
803
803
|
const t = e || 43;
|
|
804
804
|
if (t < 43 || t > 128)
|
|
805
805
|
throw `Expected a length between 43 and 128. Received ${e}.`;
|
|
806
|
-
const r =
|
|
806
|
+
const r = Ge(t), n = await re(r);
|
|
807
807
|
return {
|
|
808
808
|
code_verifier: r,
|
|
809
809
|
code_challenge: n
|
|
@@ -812,39 +812,49 @@ async function at(e) {
|
|
|
812
812
|
async function ot(e, t) {
|
|
813
813
|
return t === await re(e);
|
|
814
814
|
}
|
|
815
|
-
const ze = /^Bearer (.+)$/i,
|
|
815
|
+
const ze = /^Bearer (.+)$/i, Xe = (e) => {
|
|
816
816
|
if (typeof (e == null ? void 0 : e.authorization) != "string")
|
|
817
817
|
return;
|
|
818
818
|
const t = e.authorization.match(ze);
|
|
819
819
|
if (t)
|
|
820
820
|
return t[1];
|
|
821
|
-
},
|
|
821
|
+
}, Qe = (e, t) => {
|
|
822
822
|
const r = e == null ? void 0 : e.cookie;
|
|
823
823
|
if (typeof r != "string")
|
|
824
824
|
return;
|
|
825
825
|
const n = new RegExp(`auth.${t}=(.+?)(?:;|$)`), a = r.match(n);
|
|
826
826
|
if (a)
|
|
827
827
|
return a[1];
|
|
828
|
-
},
|
|
828
|
+
}, Ze = (e) => {
|
|
829
829
|
const t = e == null ? void 0 : e[ne.ACCESS_TOKEN];
|
|
830
830
|
if (typeof t == "string")
|
|
831
831
|
return t;
|
|
832
832
|
}, it = ({ headers: e, body: t, clientId: r }) => {
|
|
833
|
-
const n =
|
|
834
|
-
return
|
|
833
|
+
const n = Xe(e), a = Qe(e, r);
|
|
834
|
+
return Ze(t) || a || n || "";
|
|
835
|
+
}, ct = async (e, t) => {
|
|
836
|
+
var a;
|
|
837
|
+
const r = await Me(e);
|
|
838
|
+
if (!r || !Array.isArray((a = r.payload) == null ? void 0 : a[I.SCOPES_KEY]))
|
|
839
|
+
return !1;
|
|
840
|
+
const n = r.payload[I.SCOPES_KEY];
|
|
841
|
+
return Array.isArray(t) ? t.every((o) => n.includes(o)) : Object.keys(t).some(
|
|
842
|
+
(o) => t[o].every((i) => n.includes(i))
|
|
843
|
+
);
|
|
835
844
|
};
|
|
836
845
|
export {
|
|
837
|
-
|
|
838
|
-
|
|
846
|
+
rt as API_TYPE,
|
|
847
|
+
je as AUTH_TYPES,
|
|
839
848
|
ne as BODY,
|
|
840
|
-
|
|
841
|
-
|
|
849
|
+
et as HEADERS,
|
|
850
|
+
I as JWT,
|
|
842
851
|
ae as JWT_PUBLIC_KEY,
|
|
843
|
-
|
|
852
|
+
tt as TOKEN_EXPIRATION,
|
|
844
853
|
nt as decodeToken,
|
|
845
854
|
re as generateCodeChallenge,
|
|
846
855
|
it as getToken,
|
|
856
|
+
ct as isGranted,
|
|
847
857
|
at as pkceChallengePair,
|
|
848
|
-
|
|
858
|
+
Me as verifyAndExtractToken,
|
|
849
859
|
ot as verifyChallenge
|
|
850
860
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/auth-common",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"jose": "5.6.3",
|
|
37
37
|
"uuid": "10.0.0"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "8df31ef170a75c2983d7ac821fe0371641139815"
|
|
40
40
|
}
|