@versini/auth-common 3.2.0 → 3.3.1
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 -2
- package/dist/index.js +87 -85
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ declare const JWT: {
|
|
|
24
24
|
EXPIRES_AT_KEY: string;
|
|
25
25
|
CREATED_AT_KEY: string;
|
|
26
26
|
SCOPES_KEY: string;
|
|
27
|
+
CLIENT_ID_KEY: string;
|
|
27
28
|
ISSUER: string;
|
|
28
29
|
};
|
|
29
30
|
declare const JWT_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7\nw5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5\ni1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle\naMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+\nl0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE\nsjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81\nawIDAQAB\n-----END PUBLIC KEY-----";
|
|
@@ -94,6 +95,43 @@ type GetToken = {
|
|
|
94
95
|
};
|
|
95
96
|
declare const getToken: ({ headers, body, clientId }: GetToken) => string;
|
|
96
97
|
|
|
97
|
-
|
|
98
|
+
type ScopesGrants = {
|
|
99
|
+
[key: string]: string[];
|
|
100
|
+
} | string[];
|
|
101
|
+
/**
|
|
102
|
+
* Checks if the given token grants the required scopes.
|
|
103
|
+
*
|
|
104
|
+
* This function verifies the provided token and extracts its payload.
|
|
105
|
+
* It then checks if the token contains the required scopes. The scopes can be provided
|
|
106
|
+
* either as an array of strings or as a map of string arrays. When the scopes are provided
|
|
107
|
+
* as a map, the function checks if the token contains at least one of the scopes in each
|
|
108
|
+
* of the map's values (OR operation).
|
|
109
|
+
*
|
|
110
|
+
*
|
|
111
|
+
* @async
|
|
112
|
+
* @function isGranted
|
|
113
|
+
* @param {string} token - The token to be verified and checked for scopes.
|
|
114
|
+
* @param {ScopesGrants} scopes - The required scopes. This can be an array of strings
|
|
115
|
+
* representing the scopes or a map where the keys are strings
|
|
116
|
+
* and the values are arrays of strings representing the scopes.
|
|
117
|
+
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating whether the
|
|
118
|
+
* token grants the required scopes.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* Example with an array of scopes (AND operation)
|
|
122
|
+
* const scopesArray = ["read", "write"];
|
|
123
|
+
* const res = isGranted(token, scopesArray);
|
|
124
|
+
* console.log(res); // true only if the token has both "read" and "write" scopes
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* Example with a map of scopes (OR operation)
|
|
128
|
+
* const scopesMap = {
|
|
129
|
+
* "admin": ["read", "write"],
|
|
130
|
+
* "user": ["read"]
|
|
131
|
+
* };
|
|
132
|
+
* const res = isGranted(token, scopesMap);
|
|
133
|
+
* console.log(res); // true if the token has either "read" and "write" scopes or "read" scope
|
|
134
|
+
*/
|
|
135
|
+
declare const isGranted: (token: string, scopes: ScopesGrants) => Promise<boolean>;
|
|
98
136
|
|
|
99
|
-
export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, isGranted, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
|
|
137
|
+
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,11 +1,11 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/auth-common v3.
|
|
2
|
+
@versini/auth-common v3.3.1
|
|
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: "
|
|
7
|
+
version: "3.3.1",
|
|
8
|
+
buildTime: "08/01/2024 10:23 AM EDT",
|
|
9
9
|
homepage: "https://github.com/aversini/auth-client",
|
|
10
10
|
license: "MIT"
|
|
11
11
|
});
|
|
@@ -22,7 +22,7 @@ const je = {
|
|
|
22
22
|
CLIENT_ID: "X-Auth-ClientId"
|
|
23
23
|
}, ne = {
|
|
24
24
|
ACCESS_TOKEN: "access_token"
|
|
25
|
-
},
|
|
25
|
+
}, P = {
|
|
26
26
|
ALG: "RS256",
|
|
27
27
|
USER_ID_KEY: "sub",
|
|
28
28
|
TOKEN_ID_KEY: "__raw",
|
|
@@ -32,6 +32,7 @@ const je = {
|
|
|
32
32
|
EXPIRES_AT_KEY: "exp",
|
|
33
33
|
CREATED_AT_KEY: "iat",
|
|
34
34
|
SCOPES_KEY: "scopes",
|
|
35
|
+
CLIENT_ID_KEY: "aud",
|
|
35
36
|
ISSUER: "gizmette.com"
|
|
36
37
|
}, ae = `-----BEGIN PUBLIC KEY-----
|
|
37
38
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7
|
|
@@ -51,7 +52,7 @@ awIDAQAB
|
|
|
51
52
|
LOGOUT: "logout"
|
|
52
53
|
}, O = crypto, G = (e) => e instanceof CryptoKey, T = new TextEncoder(), C = new TextDecoder();
|
|
53
54
|
function oe(...e) {
|
|
54
|
-
const t = e.reduce((a, { length:
|
|
55
|
+
const t = e.reduce((a, { length: o }) => a + o, 0), r = new Uint8Array(t);
|
|
55
56
|
let n = 0;
|
|
56
57
|
for (const a of e)
|
|
57
58
|
r.set(a, n), n += a.length;
|
|
@@ -242,10 +243,10 @@ const X = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
|
|
|
242
243
|
r = new Set(a);
|
|
243
244
|
continue;
|
|
244
245
|
}
|
|
245
|
-
for (const
|
|
246
|
-
if (r.has(
|
|
246
|
+
for (const o of a) {
|
|
247
|
+
if (r.has(o))
|
|
247
248
|
return !1;
|
|
248
|
-
r.add(
|
|
249
|
+
r.add(o);
|
|
249
250
|
}
|
|
250
251
|
}
|
|
251
252
|
return !0;
|
|
@@ -352,23 +353,23 @@ const Se = async (e) => {
|
|
|
352
353
|
], a = { ...e };
|
|
353
354
|
return delete a.alg, delete a.use, O.subtle.importKey("jwk", a, ...n);
|
|
354
355
|
}, Q = (e) => b(e);
|
|
355
|
-
let
|
|
356
|
+
let D, J;
|
|
356
357
|
const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j = async (e, t, r, n) => {
|
|
357
358
|
let a = e.get(t);
|
|
358
359
|
if (a != null && a[n])
|
|
359
360
|
return a[n];
|
|
360
|
-
const
|
|
361
|
-
return a ? a[n] =
|
|
361
|
+
const o = await Se({ ...r, alg: n });
|
|
362
|
+
return a ? a[n] = o : e.set(t, { [n]: o }), o;
|
|
362
363
|
}, ye = (e, t) => {
|
|
363
364
|
if (Z(e)) {
|
|
364
365
|
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) : (
|
|
366
|
+
return delete r.d, delete r.dp, delete r.dq, delete r.p, delete r.q, delete r.qi, r.k ? Q(r.k) : (J || (J = /* @__PURE__ */ new WeakMap()), j(J, e, r, t));
|
|
366
367
|
}
|
|
367
368
|
return e;
|
|
368
369
|
}, Ee = (e, t) => {
|
|
369
370
|
if (Z(e)) {
|
|
370
371
|
let r = e.export({ format: "jwk" });
|
|
371
|
-
return r.k ? Q(r.k) : (
|
|
372
|
+
return r.k ? Q(r.k) : (D || (D = /* @__PURE__ */ new WeakMap()), j(D, e, r, t));
|
|
372
373
|
}
|
|
373
374
|
return e;
|
|
374
375
|
}, we = { normalizePublicKey: ye, normalizePrivateKey: Ee }, E = (e, t, r = 0) => {
|
|
@@ -377,7 +378,7 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
377
378
|
if (n === -1)
|
|
378
379
|
return !1;
|
|
379
380
|
const a = e.subarray(n, n + t.length);
|
|
380
|
-
return a.length !== t.length ? !1 : a.every((
|
|
381
|
+
return a.length !== t.length ? !1 : a.every((o, i) => o === t[i]) || E(e, t, n + 1);
|
|
381
382
|
}, k = (e) => {
|
|
382
383
|
switch (!0) {
|
|
383
384
|
case E(e, [42, 134, 72, 206, 61, 3, 1, 7]):
|
|
@@ -398,74 +399,74 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
398
399
|
throw new w("Invalid or unsupported EC Key Curve or OKP Key Sub Type");
|
|
399
400
|
}
|
|
400
401
|
}, ge = async (e, t, r, n, a) => {
|
|
401
|
-
let
|
|
402
|
+
let o, i;
|
|
402
403
|
const c = new Uint8Array(atob(r.replace(e, "")).split("").map((s) => s.charCodeAt(0)));
|
|
403
404
|
switch (n) {
|
|
404
405
|
case "PS256":
|
|
405
406
|
case "PS384":
|
|
406
407
|
case "PS512":
|
|
407
|
-
|
|
408
|
+
o = { name: "RSA-PSS", hash: `SHA-${n.slice(-3)}` }, i = ["verify"];
|
|
408
409
|
break;
|
|
409
410
|
case "RS256":
|
|
410
411
|
case "RS384":
|
|
411
412
|
case "RS512":
|
|
412
|
-
|
|
413
|
+
o = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${n.slice(-3)}` }, i = ["verify"];
|
|
413
414
|
break;
|
|
414
415
|
case "RSA-OAEP":
|
|
415
416
|
case "RSA-OAEP-256":
|
|
416
417
|
case "RSA-OAEP-384":
|
|
417
418
|
case "RSA-OAEP-512":
|
|
418
|
-
|
|
419
|
+
o = {
|
|
419
420
|
name: "RSA-OAEP",
|
|
420
421
|
hash: `SHA-${parseInt(n.slice(-3), 10) || 1}`
|
|
421
|
-
},
|
|
422
|
+
}, i = ["encrypt", "wrapKey"];
|
|
422
423
|
break;
|
|
423
424
|
case "ES256":
|
|
424
|
-
|
|
425
|
+
o = { name: "ECDSA", namedCurve: "P-256" }, i = ["verify"];
|
|
425
426
|
break;
|
|
426
427
|
case "ES384":
|
|
427
|
-
|
|
428
|
+
o = { name: "ECDSA", namedCurve: "P-384" }, i = ["verify"];
|
|
428
429
|
break;
|
|
429
430
|
case "ES512":
|
|
430
|
-
|
|
431
|
+
o = { name: "ECDSA", namedCurve: "P-521" }, i = ["verify"];
|
|
431
432
|
break;
|
|
432
433
|
case "ECDH-ES":
|
|
433
434
|
case "ECDH-ES+A128KW":
|
|
434
435
|
case "ECDH-ES+A192KW":
|
|
435
436
|
case "ECDH-ES+A256KW": {
|
|
436
437
|
const s = k(c);
|
|
437
|
-
|
|
438
|
+
o = s.startsWith("P-") ? { name: "ECDH", namedCurve: s } : { name: s }, i = [];
|
|
438
439
|
break;
|
|
439
440
|
}
|
|
440
441
|
case "EdDSA":
|
|
441
|
-
|
|
442
|
+
o = { name: k(c) }, i = ["verify"];
|
|
442
443
|
break;
|
|
443
444
|
default:
|
|
444
445
|
throw new w('Invalid or unsupported "alg" (Algorithm) value');
|
|
445
446
|
}
|
|
446
|
-
return O.subtle.importKey(t, c,
|
|
447
|
+
return O.subtle.importKey(t, c, o, !1, i);
|
|
447
448
|
}, Ae = (e, t, r) => ge(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
|
|
448
449
|
async function be(e, t, r) {
|
|
449
450
|
if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
|
|
450
451
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
451
452
|
return Ae(e, t);
|
|
452
453
|
}
|
|
453
|
-
const
|
|
454
|
+
const I = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
|
|
454
455
|
if (!(t instanceof Uint8Array)) {
|
|
455
456
|
if (!X(t))
|
|
456
457
|
throw new TypeError(z(e, t, ...R, "Uint8Array"));
|
|
457
458
|
if (t.type !== "secret")
|
|
458
|
-
throw new TypeError(`${
|
|
459
|
+
throw new TypeError(`${I(t)} instances for symmetric algorithms must be of type "secret"`);
|
|
459
460
|
}
|
|
460
461
|
}, Te = (e, t, r) => {
|
|
461
462
|
if (!X(t))
|
|
462
463
|
throw new TypeError(z(e, t, ...R));
|
|
463
464
|
if (t.type === "secret")
|
|
464
|
-
throw new TypeError(`${
|
|
465
|
+
throw new TypeError(`${I(t)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
465
466
|
if (t.algorithm && r === "verify" && t.type === "private")
|
|
466
|
-
throw new TypeError(`${
|
|
467
|
+
throw new TypeError(`${I(t)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
467
468
|
if (t.algorithm && r === "encrypt" && t.type === "private")
|
|
468
|
-
throw new TypeError(`${
|
|
469
|
+
throw new TypeError(`${I(t)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
469
470
|
}, _e = (e, t, r) => {
|
|
470
471
|
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : Te(e, t, r);
|
|
471
472
|
};
|
|
@@ -474,27 +475,27 @@ function ve(e, t, r, n, a) {
|
|
|
474
475
|
throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
475
476
|
if (!n || n.crit === void 0)
|
|
476
477
|
return /* @__PURE__ */ new Set();
|
|
477
|
-
if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((
|
|
478
|
+
if (!Array.isArray(n.crit) || n.crit.length === 0 || n.crit.some((i) => typeof i != "string" || i.length === 0))
|
|
478
479
|
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 "${
|
|
480
|
+
let o;
|
|
481
|
+
r !== void 0 ? o = new Map([...Object.entries(r), ...t.entries()]) : o = t;
|
|
482
|
+
for (const i of n.crit) {
|
|
483
|
+
if (!o.has(i))
|
|
484
|
+
throw new w(`Extension Header Parameter "${i}" is not recognized`);
|
|
485
|
+
if (a[i] === void 0)
|
|
486
|
+
throw new e(`Extension Header Parameter "${i}" is missing`);
|
|
487
|
+
if (o.get(i) && n[i] === void 0)
|
|
488
|
+
throw new e(`Extension Header Parameter "${i}" MUST be integrity protected`);
|
|
488
489
|
}
|
|
489
490
|
return new Set(n.crit);
|
|
490
491
|
}
|
|
491
|
-
const
|
|
492
|
+
const Ie = (e, t) => {
|
|
492
493
|
if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
|
|
493
494
|
throw new TypeError(`"${e}" option must be an array of strings`);
|
|
494
495
|
if (t)
|
|
495
496
|
return new Set(t);
|
|
496
497
|
};
|
|
497
|
-
function
|
|
498
|
+
function Pe(e, t) {
|
|
498
499
|
const r = `SHA-${e.slice(-3)}`;
|
|
499
500
|
switch (e) {
|
|
500
501
|
case "HS256":
|
|
@@ -532,9 +533,9 @@ async function Re(e, t, r) {
|
|
|
532
533
|
const Ke = async (e, t, r, n) => {
|
|
533
534
|
const a = await Re(e, t, "verify");
|
|
534
535
|
pe(e, a);
|
|
535
|
-
const
|
|
536
|
+
const o = Pe(e, a.algorithm);
|
|
536
537
|
try {
|
|
537
|
-
return await O.subtle.verify(
|
|
538
|
+
return await O.subtle.verify(o, a, r, n);
|
|
538
539
|
} catch {
|
|
539
540
|
return !1;
|
|
540
541
|
}
|
|
@@ -565,17 +566,17 @@ async function Oe(e, t, r) {
|
|
|
565
566
|
const a = {
|
|
566
567
|
...n,
|
|
567
568
|
...e.header
|
|
568
|
-
},
|
|
569
|
-
let
|
|
570
|
-
if (
|
|
569
|
+
}, o = ve(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
|
|
570
|
+
let i = !0;
|
|
571
|
+
if (o.has("b64") && (i = n.b64, typeof i != "boolean"))
|
|
571
572
|
throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
572
573
|
const { alg: c } = a;
|
|
573
574
|
if (typeof c != "string" || !c)
|
|
574
575
|
throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
575
|
-
const s = r &&
|
|
576
|
+
const s = r && Ie("algorithms", r.algorithms);
|
|
576
577
|
if (s && !s.has(c))
|
|
577
578
|
throw new ce('"alg" (Algorithm) Header Parameter value not allowed');
|
|
578
|
-
if (
|
|
579
|
+
if (i) {
|
|
579
580
|
if (typeof e.payload != "string")
|
|
580
581
|
throw new u("JWS Payload must be a string");
|
|
581
582
|
} else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
|
|
@@ -592,7 +593,7 @@ async function Oe(e, t, r) {
|
|
|
592
593
|
if (!await Ke(c, t, f, g))
|
|
593
594
|
throw new se();
|
|
594
595
|
let y;
|
|
595
|
-
if (
|
|
596
|
+
if (i)
|
|
596
597
|
try {
|
|
597
598
|
y = b(e.payload);
|
|
598
599
|
} catch {
|
|
@@ -605,13 +606,13 @@ async function Oe(e, t, r) {
|
|
|
605
606
|
async function xe(e, t, r) {
|
|
606
607
|
if (e instanceof Uint8Array && (e = C.decode(e)), typeof e != "string")
|
|
607
608
|
throw new u("Compact JWS must be a string or Uint8Array");
|
|
608
|
-
const { 0: n, 1: a, 2:
|
|
609
|
-
if (
|
|
609
|
+
const { 0: n, 1: a, 2: o, length: i } = e.split(".");
|
|
610
|
+
if (i !== 3)
|
|
610
611
|
throw new u("Invalid Compact JWS");
|
|
611
|
-
const c = await Oe({ payload: a, protected: n, signature:
|
|
612
|
+
const c = await Oe({ payload: a, protected: n, signature: o }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
|
|
612
613
|
return typeof t == "function" ? { ...s, key: c.key } : s;
|
|
613
614
|
}
|
|
614
|
-
const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, N = te * 24,
|
|
615
|
+
const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, N = te * 24, De = N * 7, Je = 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
616
|
const t = He.exec(e);
|
|
616
617
|
if (!t || t[4] && t[1])
|
|
617
618
|
throw new TypeError("Invalid time period format");
|
|
@@ -647,10 +648,10 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, N = te *
|
|
|
647
648
|
case "week":
|
|
648
649
|
case "weeks":
|
|
649
650
|
case "w":
|
|
650
|
-
a = Math.round(r *
|
|
651
|
+
a = Math.round(r * De);
|
|
651
652
|
break;
|
|
652
653
|
default:
|
|
653
|
-
a = Math.round(r *
|
|
654
|
+
a = Math.round(r * Je);
|
|
654
655
|
break;
|
|
655
656
|
}
|
|
656
657
|
return t[1] === "-" || t[4] === "ago" ? -a : a;
|
|
@@ -665,12 +666,12 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, N = te *
|
|
|
665
666
|
const { typ: a } = r;
|
|
666
667
|
if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
|
|
667
668
|
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"),
|
|
669
|
+
const { requiredClaims: o = [], issuer: i, subject: c, audience: s, maxTokenAge: p } = r, g = [...o];
|
|
670
|
+
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
671
|
for (const l of new Set(g.reverse()))
|
|
671
672
|
if (!(l in n))
|
|
672
673
|
throw new h(`missing required "${l}" claim`, n, l, "missing");
|
|
673
|
-
if (
|
|
674
|
+
if (i && !(Array.isArray(i) ? i : [i]).includes(n.iss))
|
|
674
675
|
throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
|
|
675
676
|
if (c && n.sub !== c)
|
|
676
677
|
throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
|
|
@@ -715,12 +716,12 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, N = te *
|
|
|
715
716
|
return n;
|
|
716
717
|
};
|
|
717
718
|
async function $e(e, t, r) {
|
|
718
|
-
var
|
|
719
|
+
var i;
|
|
719
720
|
const n = await xe(e, t, r);
|
|
720
|
-
if ((
|
|
721
|
+
if ((i = n.protectedHeader.crit) != null && i.includes("b64") && n.protectedHeader.b64 === !1)
|
|
721
722
|
throw new S("JWTs MUST NOT use unencoded payload");
|
|
722
|
-
const
|
|
723
|
-
return typeof t == "function" ? { ...
|
|
723
|
+
const o = { payload: Ue(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
|
|
724
|
+
return typeof t == "function" ? { ...o, key: n.key } : o;
|
|
724
725
|
}
|
|
725
726
|
const Le = b;
|
|
726
727
|
function ke(e) {
|
|
@@ -751,9 +752,9 @@ function ke(e) {
|
|
|
751
752
|
}
|
|
752
753
|
const Me = async (e) => {
|
|
753
754
|
try {
|
|
754
|
-
const t =
|
|
755
|
+
const t = P.ALG, n = await be(ae, t);
|
|
755
756
|
return await $e(e, n, {
|
|
756
|
-
issuer:
|
|
757
|
+
issuer: P.ISSUER
|
|
757
758
|
});
|
|
758
759
|
} catch {
|
|
759
760
|
return;
|
|
@@ -771,32 +772,32 @@ for (var H = 0; H < 256; ++H)
|
|
|
771
772
|
function Be(e, t = 0) {
|
|
772
773
|
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
774
|
}
|
|
774
|
-
var v,
|
|
775
|
-
function
|
|
775
|
+
var v, Ye = new Uint8Array(16);
|
|
776
|
+
function Fe() {
|
|
776
777
|
if (!v && (v = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !v))
|
|
777
778
|
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
778
|
-
return v(
|
|
779
|
+
return v(Ye);
|
|
779
780
|
}
|
|
780
|
-
var
|
|
781
|
-
const
|
|
782
|
-
randomUUID:
|
|
781
|
+
var Ve = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
782
|
+
const Y = {
|
|
783
|
+
randomUUID: Ve
|
|
783
784
|
};
|
|
784
|
-
function
|
|
785
|
-
if (
|
|
786
|
-
return
|
|
785
|
+
function F(e, t, r) {
|
|
786
|
+
if (Y.randomUUID && !t && !e)
|
|
787
|
+
return Y.randomUUID();
|
|
787
788
|
e = e || {};
|
|
788
|
-
var n = e.random || (e.rng ||
|
|
789
|
+
var n = e.random || (e.rng || Fe)();
|
|
789
790
|
return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Be(n);
|
|
790
791
|
}
|
|
791
|
-
const
|
|
792
|
+
const V = globalThis.crypto, Ge = (e) => `${F()}${F()}`.slice(0, e), qe = (e) => btoa(
|
|
792
793
|
[...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
|
|
793
794
|
);
|
|
794
795
|
async function re(e) {
|
|
795
|
-
if (!
|
|
796
|
+
if (!V.subtle)
|
|
796
797
|
throw new Error(
|
|
797
798
|
"crypto.subtle is available only in secure contexts (HTTPS)."
|
|
798
799
|
);
|
|
799
|
-
const t = new TextEncoder().encode(e), r = await
|
|
800
|
+
const t = new TextEncoder().encode(e), r = await V.subtle.digest("SHA-256", t);
|
|
800
801
|
return qe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
801
802
|
}
|
|
802
803
|
async function at(e) {
|
|
@@ -833,20 +834,21 @@ const ze = /^Bearer (.+)$/i, Xe = (e) => {
|
|
|
833
834
|
const n = Xe(e), a = Qe(e, r);
|
|
834
835
|
return Ze(t) || a || n || "";
|
|
835
836
|
}, ct = async (e, t) => {
|
|
836
|
-
var
|
|
837
|
+
var a;
|
|
837
838
|
const r = await Me(e);
|
|
838
|
-
if (
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
839
|
+
if (!r || !Array.isArray((a = r.payload) == null ? void 0 : a[P.SCOPES_KEY]))
|
|
840
|
+
return !1;
|
|
841
|
+
const n = r.payload[P.SCOPES_KEY];
|
|
842
|
+
return Array.isArray(t) ? t.every((o) => n.includes(o)) : Object.keys(t).some(
|
|
843
|
+
(o) => t[o].every((i) => n.includes(i))
|
|
844
|
+
);
|
|
843
845
|
};
|
|
844
846
|
export {
|
|
845
847
|
rt as API_TYPE,
|
|
846
848
|
je as AUTH_TYPES,
|
|
847
849
|
ne as BODY,
|
|
848
850
|
et as HEADERS,
|
|
849
|
-
|
|
851
|
+
P as JWT,
|
|
850
852
|
ae as JWT_PUBLIC_KEY,
|
|
851
853
|
tt as TOKEN_EXPIRATION,
|
|
852
854
|
nt as decodeToken,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/auth-common",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
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": "01228321ca4f7b62244a5dc9b2b001053fd0bba2"
|
|
40
40
|
}
|