@versini/auth-common 3.0.1 → 3.2.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 CHANGED
@@ -23,6 +23,7 @@ declare const JWT: {
23
23
  AUTH_TYPE_KEY: string;
24
24
  EXPIRES_AT_KEY: string;
25
25
  CREATED_AT_KEY: string;
26
+ SCOPES_KEY: string;
26
27
  ISSUER: string;
27
28
  };
28
29
  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-----";
@@ -93,4 +94,6 @@ type GetToken = {
93
94
  };
94
95
  declare const getToken: ({ headers, body, clientId }: GetToken) => string;
95
96
 
96
- export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
97
+ declare const isGranted: (token: string, scopes: string[]) => Promise<boolean>;
98
+
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 };
package/dist/index.js CHANGED
@@ -1,28 +1,28 @@
1
1
  /*!
2
- @versini/auth-common v3.0.1
2
+ @versini/auth-common v3.2.0
3
3
  © 2024 gizmette.com
4
4
  */
5
5
  try {
6
6
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
7
- version: "3.0.1",
8
- buildTime: "07/20/2024 09:31 AM EDT",
7
+ version: "3.2.0",
8
+ buildTime: "07/21/2024 03:41 PM EDT",
9
9
  homepage: "https://github.com/aversini/auth-client",
10
10
  license: "MIT"
11
11
  });
12
12
  } catch {
13
13
  }
14
- const Ze = {
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
- }, je = {
21
+ }, et = {
22
22
  CLIENT_ID: "X-Auth-ClientId"
23
23
  }, ne = {
24
24
  ACCESS_TOKEN: "access_token"
25
- }, U = {
25
+ }, I = {
26
26
  ALG: "RS256",
27
27
  USER_ID_KEY: "sub",
28
28
  TOKEN_ID_KEY: "__raw",
@@ -31,6 +31,7 @@ const Ze = {
31
31
  AUTH_TYPE_KEY: "auth_type",
32
32
  EXPIRES_AT_KEY: "exp",
33
33
  CREATED_AT_KEY: "iat",
34
+ SCOPES_KEY: "scopes",
34
35
  ISSUER: "gizmette.com"
35
36
  }, ae = `-----BEGIN PUBLIC KEY-----
36
37
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7
@@ -40,15 +41,15 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
40
41
  l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
41
42
  sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
42
43
  awIDAQAB
43
- -----END PUBLIC KEY-----`, et = {
44
+ -----END PUBLIC KEY-----`, tt = {
44
45
  ACCESS: "5m",
45
46
  ID: "90d",
46
47
  REFRESH: "90d"
47
- }, tt = {
48
+ }, rt = {
48
49
  AUTHENTICATE: "authenticate",
49
50
  CODE: "code",
50
51
  LOGOUT: "logout"
51
- }, K = crypto, q = (e) => e instanceof CryptoKey, T = new TextEncoder(), C = new TextDecoder();
52
+ }, O = crypto, G = (e) => e instanceof CryptoKey, T = new TextEncoder(), C = new TextDecoder();
52
53
  function oe(...e) {
53
54
  const t = e.reduce((a, { length: i }) => a + i, 0), r = new Uint8Array(t);
54
55
  let n = 0;
@@ -138,10 +139,10 @@ class se extends A {
138
139
  function m(e, t = "algorithm.name") {
139
140
  return new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`);
140
141
  }
141
- function v(e, t) {
142
+ function _(e, t) {
142
143
  return e.name === t;
143
144
  }
144
- function x(e) {
145
+ function W(e) {
145
146
  return parseInt(e.name.slice(4), 10);
146
147
  }
147
148
  function de(e) {
@@ -171,30 +172,30 @@ function le(e, t, ...r) {
171
172
  case "HS256":
172
173
  case "HS384":
173
174
  case "HS512": {
174
- if (!v(e.algorithm, "HMAC"))
175
+ if (!_(e.algorithm, "HMAC"))
175
176
  throw m("HMAC");
176
177
  const n = parseInt(t.slice(2), 10);
177
- if (x(e.algorithm.hash) !== n)
178
+ if (W(e.algorithm.hash) !== n)
178
179
  throw m(`SHA-${n}`, "algorithm.hash");
179
180
  break;
180
181
  }
181
182
  case "RS256":
182
183
  case "RS384":
183
184
  case "RS512": {
184
- if (!v(e.algorithm, "RSASSA-PKCS1-v1_5"))
185
+ if (!_(e.algorithm, "RSASSA-PKCS1-v1_5"))
185
186
  throw m("RSASSA-PKCS1-v1_5");
186
187
  const n = parseInt(t.slice(2), 10);
187
- if (x(e.algorithm.hash) !== n)
188
+ if (W(e.algorithm.hash) !== n)
188
189
  throw m(`SHA-${n}`, "algorithm.hash");
189
190
  break;
190
191
  }
191
192
  case "PS256":
192
193
  case "PS384":
193
194
  case "PS512": {
194
- if (!v(e.algorithm, "RSA-PSS"))
195
+ if (!_(e.algorithm, "RSA-PSS"))
195
196
  throw m("RSA-PSS");
196
197
  const n = parseInt(t.slice(2), 10);
197
- if (x(e.algorithm.hash) !== n)
198
+ if (W(e.algorithm.hash) !== n)
198
199
  throw m(`SHA-${n}`, "algorithm.hash");
199
200
  break;
200
201
  }
@@ -206,7 +207,7 @@ function le(e, t, ...r) {
206
207
  case "ES256":
207
208
  case "ES384":
208
209
  case "ES512": {
209
- if (!v(e.algorithm, "ECDSA"))
210
+ if (!_(e.algorithm, "ECDSA"))
210
211
  throw m("ECDSA");
211
212
  const n = de(t);
212
213
  if (e.algorithm.namedCurve !== n)
@@ -218,7 +219,7 @@ function le(e, t, ...r) {
218
219
  }
219
220
  ue(e, r);
220
221
  }
221
- function z(e, t, ...r) {
222
+ function q(e, t, ...r) {
222
223
  var n;
223
224
  if (r.length > 2) {
224
225
  const a = r.pop();
@@ -226,11 +227,11 @@ function z(e, t, ...r) {
226
227
  } else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
227
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;
228
229
  }
229
- const L = (e, ...t) => z("Key must be ", e, ...t);
230
- function G(e, t, ...r) {
231
- return z(`Key for the ${e} algorithm must be `, t, ...r);
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);
232
233
  }
233
- const X = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"], fe = (...e) => {
234
+ const X = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"], fe = (...e) => {
234
235
  const t = e.filter(Boolean);
235
236
  if (t.length === 0 || t.length === 1)
236
237
  return !0;
@@ -252,7 +253,7 @@ const X = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
252
253
  function he(e) {
253
254
  return typeof e == "object" && e !== null;
254
255
  }
255
- function P(e) {
256
+ function K(e) {
256
257
  if (!he(e) || Object.prototype.toString.call(e) !== "[object Object]")
257
258
  return !1;
258
259
  if (Object.getPrototypeOf(e) === null)
@@ -349,9 +350,9 @@ const Se = async (e) => {
349
350
  e.ext ?? !1,
350
351
  e.key_ops ?? r
351
352
  ], a = { ...e };
352
- return delete a.alg, delete a.use, K.subtle.importKey("jwk", a, ...n);
353
+ return delete a.alg, delete a.use, O.subtle.importKey("jwk", a, ...n);
353
354
  }, Q = (e) => b(e);
354
- let W, J;
355
+ let J, D;
355
356
  const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j = async (e, t, r, n) => {
356
357
  let a = e.get(t);
357
358
  if (a != null && a[n])
@@ -361,13 +362,13 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
361
362
  }, ye = (e, t) => {
362
363
  if (Z(e)) {
363
364
  let r = e.export({ format: "jwk" });
364
- 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));
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));
365
366
  }
366
367
  return e;
367
368
  }, Ee = (e, t) => {
368
369
  if (Z(e)) {
369
370
  let r = e.export({ format: "jwk" });
370
- return r.k ? Q(r.k) : (W || (W = /* @__PURE__ */ new WeakMap()), j(W, e, r, t));
371
+ return r.k ? Q(r.k) : (J || (J = /* @__PURE__ */ new WeakMap()), j(J, e, r, t));
371
372
  }
372
373
  return e;
373
374
  }, 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
  return !1;
378
379
  const a = e.subarray(n, n + t.length);
379
380
  return a.length !== t.length ? !1 : a.every((i, o) => i === t[o]) || E(e, t, n + 1);
380
- }, M = (e) => {
381
+ }, k = (e) => {
381
382
  switch (!0) {
382
383
  case E(e, [42, 134, 72, 206, 61, 3, 1, 7]):
383
384
  return "P-256";
@@ -432,43 +433,43 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
432
433
  case "ECDH-ES+A128KW":
433
434
  case "ECDH-ES+A192KW":
434
435
  case "ECDH-ES+A256KW": {
435
- const s = M(c);
436
+ const s = k(c);
436
437
  i = s.startsWith("P-") ? { name: "ECDH", namedCurve: s } : { name: s }, o = [];
437
438
  break;
438
439
  }
439
440
  case "EdDSA":
440
- i = { name: M(c) }, o = ["verify"];
441
+ i = { name: k(c) }, o = ["verify"];
441
442
  break;
442
443
  default:
443
444
  throw new w('Invalid or unsupported "alg" (Algorithm) value');
444
445
  }
445
- return K.subtle.importKey(t, c, i, !1, o);
446
+ return O.subtle.importKey(t, c, i, !1, o);
446
447
  }, Ae = (e, t, r) => ge(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
447
448
  async function be(e, t, r) {
448
449
  if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
449
450
  throw new TypeError('"spki" must be SPKI formatted string');
450
451
  return Ae(e, t);
451
452
  }
452
- const I = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
453
+ const P = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
453
454
  if (!(t instanceof Uint8Array)) {
454
455
  if (!X(t))
455
- throw new TypeError(G(e, t, ...R, "Uint8Array"));
456
+ throw new TypeError(z(e, t, ...R, "Uint8Array"));
456
457
  if (t.type !== "secret")
457
- throw new TypeError(`${I(t)} instances for symmetric algorithms must be of type "secret"`);
458
+ throw new TypeError(`${P(t)} instances for symmetric algorithms must be of type "secret"`);
458
459
  }
459
460
  }, Te = (e, t, r) => {
460
461
  if (!X(t))
461
- throw new TypeError(G(e, t, ...R));
462
+ throw new TypeError(z(e, t, ...R));
462
463
  if (t.type === "secret")
463
- throw new TypeError(`${I(t)} instances for asymmetric algorithms must not be of type "secret"`);
464
+ throw new TypeError(`${P(t)} instances for asymmetric algorithms must not be of type "secret"`);
464
465
  if (t.algorithm && r === "verify" && t.type === "private")
465
- throw new TypeError(`${I(t)} instances for asymmetric algorithm verifying must be of type "public"`);
466
+ throw new TypeError(`${P(t)} instances for asymmetric algorithm verifying must be of type "public"`);
466
467
  if (t.algorithm && r === "encrypt" && t.type === "private")
467
- throw new TypeError(`${I(t)} instances for asymmetric algorithm encryption must be of type "public"`);
468
- }, ve = (e, t, r) => {
468
+ throw new TypeError(`${P(t)} instances for asymmetric algorithm encryption must be of type "public"`);
469
+ }, _e = (e, t, r) => {
469
470
  e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : Te(e, t, r);
470
471
  };
471
- function _e(e, t, r, n, a) {
472
+ function ve(e, t, r, n, a) {
472
473
  if (a.crit !== void 0 && (n == null ? void 0 : n.crit) === void 0)
473
474
  throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
474
475
  if (!n || n.crit === void 0)
@@ -487,13 +488,13 @@ function _e(e, t, r, n, a) {
487
488
  }
488
489
  return new Set(n.crit);
489
490
  }
490
- const Ie = (e, t) => {
491
+ const Pe = (e, t) => {
491
492
  if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
492
493
  throw new TypeError(`"${e}" option must be an array of strings`);
493
494
  if (t)
494
495
  return new Set(t);
495
496
  };
496
- function Re(e, t) {
497
+ function Ie(e, t) {
497
498
  const r = `SHA-${e.slice(-3)}`;
498
499
  switch (e) {
499
500
  case "HS256":
@@ -518,28 +519,28 @@ function Re(e, t) {
518
519
  throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
519
520
  }
520
521
  }
521
- async function Pe(e, t, r) {
522
- if (t = await we.normalizePublicKey(t, e), q(t))
522
+ async function Re(e, t, r) {
523
+ if (t = await we.normalizePublicKey(t, e), G(t))
523
524
  return le(t, e, r), t;
524
525
  if (t instanceof Uint8Array) {
525
526
  if (!e.startsWith("HS"))
526
527
  throw new TypeError(L(t, ...R));
527
- return K.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
528
+ return O.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
528
529
  }
529
530
  throw new TypeError(L(t, ...R, "Uint8Array"));
530
531
  }
531
532
  const Ke = async (e, t, r, n) => {
532
- const a = await Pe(e, t, "verify");
533
+ const a = await Re(e, t, "verify");
533
534
  pe(e, a);
534
- const i = Re(e, a.algorithm);
535
+ const i = Ie(e, a.algorithm);
535
536
  try {
536
- return await K.subtle.verify(i, a, r, n);
537
+ return await O.subtle.verify(i, a, r, n);
537
538
  } catch {
538
539
  return !1;
539
540
  }
540
541
  };
541
542
  async function Oe(e, t, r) {
542
- if (!P(e))
543
+ if (!K(e))
543
544
  throw new u("Flattened JWS must be an object");
544
545
  if (e.protected === void 0 && e.header === void 0)
545
546
  throw new u('Flattened JWS must have either of the "protected" or "header" members');
@@ -549,13 +550,13 @@ async function Oe(e, t, r) {
549
550
  throw new u("JWS Payload missing");
550
551
  if (typeof e.signature != "string")
551
552
  throw new u("JWS Signature missing or incorrect type");
552
- if (e.header !== void 0 && !P(e.header))
553
+ if (e.header !== void 0 && !K(e.header))
553
554
  throw new u("JWS Unprotected Header incorrect type");
554
555
  let n = {};
555
556
  if (e.protected)
556
557
  try {
557
- const O = b(e.protected);
558
- n = JSON.parse(C.decode(O));
558
+ const x = b(e.protected);
559
+ n = JSON.parse(C.decode(x));
559
560
  } catch {
560
561
  throw new u("JWS Protected Header is invalid");
561
562
  }
@@ -564,14 +565,14 @@ async function Oe(e, t, r) {
564
565
  const a = {
565
566
  ...n,
566
567
  ...e.header
567
- }, i = _e(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
568
+ }, i = ve(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
568
569
  let o = !0;
569
570
  if (i.has("b64") && (o = n.b64, typeof o != "boolean"))
570
571
  throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
571
572
  const { alg: c } = a;
572
573
  if (typeof c != "string" || !c)
573
574
  throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
574
- const s = r && Ie("algorithms", r.algorithms);
575
+ const s = r && Pe("algorithms", r.algorithms);
575
576
  if (s && !s.has(c))
576
577
  throw new ce('"alg" (Algorithm) Header Parameter value not allowed');
577
578
  if (o) {
@@ -580,7 +581,7 @@ async function Oe(e, t, r) {
580
581
  } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
581
582
  throw new u("JWS Payload must be a string or an Uint8Array instance");
582
583
  let p = !1;
583
- typeof t == "function" && (t = await t(n, e), p = !0), ve(c, t, "verify");
584
+ typeof t == "function" && (t = await t(n, e), p = !0), _e(c, t, "verify");
584
585
  const g = oe(T.encode(e.protected ?? ""), T.encode("."), typeof e.payload == "string" ? T.encode(e.payload) : e.payload);
585
586
  let f;
586
587
  try {
@@ -610,7 +611,7 @@ async function xe(e, t, r) {
610
611
  const c = await Oe({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
611
612
  return typeof t == "function" ? { ...s, key: c.key } : s;
612
613
  }
613
- const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te * 24, Je = H * 7, De = H * 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, k = (e) => {
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) => {
614
615
  const t = He.exec(e);
615
616
  if (!t || t[4] && t[1])
616
617
  throw new TypeError("Invalid time period format");
@@ -641,7 +642,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
641
642
  case "day":
642
643
  case "days":
643
644
  case "d":
644
- a = Math.round(r * H);
645
+ a = Math.round(r * N);
645
646
  break;
646
647
  case "week":
647
648
  case "weeks":
@@ -659,7 +660,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
659
660
  n = JSON.parse(C.decode(t));
660
661
  } catch {
661
662
  }
662
- if (!P(n))
663
+ if (!K(n))
663
664
  throw new S("JWT Claims Set must be a top-level JSON object");
664
665
  const { typ: a } = r;
665
666
  if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
@@ -678,7 +679,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
678
679
  let f;
679
680
  switch (typeof r.clockTolerance) {
680
681
  case "string":
681
- f = k(r.clockTolerance);
682
+ f = M(r.clockTolerance);
682
683
  break;
683
684
  case "number":
684
685
  f = r.clockTolerance;
@@ -689,7 +690,7 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
689
690
  default:
690
691
  throw new TypeError("Invalid clockTolerance option type");
691
692
  }
692
- const { currentDate: N } = r, y = We(N || /* @__PURE__ */ new Date());
693
+ const { currentDate: U } = r, y = We(U || /* @__PURE__ */ new Date());
693
694
  if ((n.iat !== void 0 || p) && typeof n.iat != "number")
694
695
  throw new h('"iat" claim must be a number', n, "iat", "invalid");
695
696
  if (n.nbf !== void 0) {
@@ -705,8 +706,8 @@ const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
705
706
  throw new $('"exp" claim timestamp check failed', n, "exp", "check_failed");
706
707
  }
707
708
  if (p) {
708
- const l = y - n.iat, O = typeof p == "number" ? p : k(p);
709
- if (l - f > O)
709
+ const l = y - n.iat, x = typeof p == "number" ? p : M(p);
710
+ if (l - f > x)
710
711
  throw new $('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
711
712
  if (l < 0 - f)
712
713
  throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
@@ -722,7 +723,7 @@ async function $e(e, t, r) {
722
723
  return typeof t == "function" ? { ...i, key: n.key } : i;
723
724
  }
724
725
  const Le = b;
725
- function Me(e) {
726
+ function ke(e) {
726
727
  if (typeof e != "string")
727
728
  throw new S("JWTs must use Compact JWS serialization, JWT must be a string");
728
729
  const { 1: t, length: r } = e.split(".");
@@ -744,50 +745,50 @@ function Me(e) {
744
745
  } catch {
745
746
  throw new S("Failed to parse the decoded payload as JSON");
746
747
  }
747
- if (!P(a))
748
+ if (!K(a))
748
749
  throw new S("Invalid JWT Claims Set");
749
750
  return a;
750
751
  }
751
- const rt = async (e) => {
752
+ const Me = async (e) => {
752
753
  try {
753
- const t = U.ALG, n = await be(ae, t);
754
+ const t = I.ALG, n = await be(ae, t);
754
755
  return await $e(e, n, {
755
- issuer: U.ISSUER
756
+ issuer: I.ISSUER
756
757
  });
757
758
  } catch {
758
759
  return;
759
760
  }
760
761
  }, nt = (e) => {
761
762
  try {
762
- return Me(e);
763
+ return ke(e);
763
764
  } catch {
764
765
  return;
765
766
  }
766
767
  };
767
768
  var d = [];
768
- for (var D = 0; D < 256; ++D)
769
- d.push((D + 256).toString(16).slice(1));
770
- function ke(e, t = 0) {
769
+ for (var H = 0; H < 256; ++H)
770
+ d.push((H + 256).toString(16).slice(1));
771
+ function Be(e, t = 0) {
771
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();
772
773
  }
773
- var _, Be = new Uint8Array(16);
774
- function Fe() {
775
- if (!_ && (_ = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !_))
774
+ var v, Fe = new Uint8Array(16);
775
+ function Ve() {
776
+ if (!v && (v = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !v))
776
777
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
777
- return _(Be);
778
+ return v(Fe);
778
779
  }
779
- var Ve = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
780
+ var Ye = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
780
781
  const F = {
781
- randomUUID: Ve
782
+ randomUUID: Ye
782
783
  };
783
784
  function V(e, t, r) {
784
785
  if (F.randomUUID && !t && !e)
785
786
  return F.randomUUID();
786
787
  e = e || {};
787
- var n = e.random || (e.rng || Fe)();
788
- return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, ke(n);
788
+ var n = e.random || (e.rng || Ve)();
789
+ return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Be(n);
789
790
  }
790
- const Y = globalThis.crypto, Ye = (e) => `${V()}${V()}`.slice(0, e), qe = (e) => btoa(
791
+ const Y = globalThis.crypto, Ge = (e) => `${V()}${V()}`.slice(0, e), qe = (e) => btoa(
791
792
  [...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
792
793
  );
793
794
  async function re(e) {
@@ -802,7 +803,7 @@ async function at(e) {
802
803
  const t = e || 43;
803
804
  if (t < 43 || t > 128)
804
805
  throw `Expected a length between 43 and 128. Received ${e}.`;
805
- const r = Ye(t), n = await re(r);
806
+ const r = Ge(t), n = await re(r);
806
807
  return {
807
808
  code_verifier: r,
808
809
  code_challenge: n
@@ -811,39 +812,48 @@ async function at(e) {
811
812
  async function ot(e, t) {
812
813
  return t === await re(e);
813
814
  }
814
- const ze = /^Bearer (.+)$/i, Ge = (e) => {
815
+ const ze = /^Bearer (.+)$/i, Xe = (e) => {
815
816
  if (typeof (e == null ? void 0 : e.authorization) != "string")
816
817
  return;
817
818
  const t = e.authorization.match(ze);
818
819
  if (t)
819
820
  return t[1];
820
- }, Xe = (e, t) => {
821
+ }, Qe = (e, t) => {
821
822
  const r = e == null ? void 0 : e.cookie;
822
823
  if (typeof r != "string")
823
824
  return;
824
825
  const n = new RegExp(`auth.${t}=(.+?)(?:;|$)`), a = r.match(n);
825
826
  if (a)
826
827
  return a[1];
827
- }, Qe = (e) => {
828
+ }, Ze = (e) => {
828
829
  const t = e == null ? void 0 : e[ne.ACCESS_TOKEN];
829
830
  if (typeof t == "string")
830
831
  return t;
831
832
  }, it = ({ headers: e, body: t, clientId: r }) => {
832
- const n = Ge(e), a = Xe(e, r);
833
- return Qe(t) || a || n || "";
833
+ const n = Xe(e), a = Qe(e, r);
834
+ return Ze(t) || a || n || "";
835
+ }, ct = async (e, t) => {
836
+ var n, a;
837
+ const r = await Me(e);
838
+ if ((a = r && ((n = r == null ? void 0 : r.payload) == null ? void 0 : n[I.SCOPES_KEY])) != null && a.length) {
839
+ const i = r.payload[I.SCOPES_KEY];
840
+ return t.every((o) => i.includes(o));
841
+ }
842
+ return !1;
834
843
  };
835
844
  export {
836
- tt as API_TYPE,
837
- Ze as AUTH_TYPES,
845
+ rt as API_TYPE,
846
+ je as AUTH_TYPES,
838
847
  ne as BODY,
839
- je as HEADERS,
840
- U as JWT,
848
+ et as HEADERS,
849
+ I as JWT,
841
850
  ae as JWT_PUBLIC_KEY,
842
- et as TOKEN_EXPIRATION,
851
+ tt as TOKEN_EXPIRATION,
843
852
  nt as decodeToken,
844
853
  re as generateCodeChallenge,
845
854
  it as getToken,
855
+ ct as isGranted,
846
856
  at as pkceChallengePair,
847
- rt as verifyAndExtractToken,
857
+ Me as verifyAndExtractToken,
848
858
  ot as verifyChallenge
849
859
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-common",
3
- "version": "3.0.1",
3
+ "version": "3.2.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": "b21bfead4526c0deff0a015887b5dee5e398a02d"
39
+ "gitHead": "57d681a2201591a0623cbbef9dc2edc65f2e3cdc"
40
40
  }