@versini/auth-common 4.4.0 → 4.5.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
@@ -27,6 +27,7 @@ declare const JWT: {
27
27
  EXPIRES_AT_KEY: string;
28
28
  CREATED_AT_KEY: string;
29
29
  SCOPES_KEY: string;
30
+ SCOPE_KEY: string;
30
31
  CLIENT_ID_KEY: string;
31
32
  ISSUER: string;
32
33
  };
@@ -106,10 +107,11 @@ type ScopesGrants = {
106
107
  * Checks if the given encoded access token grants the required scopes.
107
108
  *
108
109
  * This function verifies the provided token and extracts its payload.
109
- * It then checks if the token contains the required scopes. The scopes can be provided
110
- * either as an array of strings or as a map of string arrays. When the scopes are provided
111
- * as a map, the function checks if the token contains at least one of the scopes in each
112
- * of the map's values (OR operation).
110
+ * It then checks if the token contains the required scopes. The function supports
111
+ * scopes in two formats: as an array of strings (JWT.SCOPES_KEY) or as a space-separated
112
+ * string (JWT.SCOPE_KEY). The scopes can be provided either as an array of strings or
113
+ * as a map of string arrays. When the scopes are provided as a map, the function checks
114
+ * if the token contains at least one of the scopes in each of the map's values (OR operation).
113
115
  *
114
116
  *
115
117
  * @async
@@ -141,10 +143,11 @@ declare const isGranted: (token: string, scopes: ScopesGrants) => Promise<boolea
141
143
  * Checks if the given non-encoded id token grants the required scopes.
142
144
  *
143
145
  * This function does not verify the token, it simply extracts its payload.
144
- * It then checks if the token contains the required scopes. The scopes can be provided
145
- * either as an array of strings or as a map of string arrays. When the scopes are provided
146
- * as a map, the function checks if the token contains at least one of the scopes in each
147
- * of the map's values (OR operation).
146
+ * It then checks if the token contains the required scopes. The function supports
147
+ * scopes in two formats: as an array of strings (JWT.SCOPES_KEY) or as a space-separated
148
+ * string (JWT.SCOPE_KEY). The scopes can be provided either as an array of strings or
149
+ * as a map of string arrays. When the scopes are provided as a map, the function checks
150
+ * if the token contains at least one of the scopes in each of the map's values (OR operation).
148
151
  *
149
152
  *
150
153
  * @function isGrantedSync
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  var ne = Object.defineProperty;
2
2
  var ae = (e, t, r) => t in e ? ne(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
- var u = (e, t, r) => ae(e, typeof t != "symbol" ? t + "" : t, r);
3
+ var d = (e, t, r) => ae(e, typeof t != "symbol" ? t + "" : t, r);
4
4
  /*!
5
- @versini/auth-common v4.4.0
5
+ @versini/auth-common v4.5.0
6
6
  © 2025 gizmette.com
7
7
  */
8
8
  try {
9
9
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
10
- version: "4.4.0",
11
- buildTime: "06/02/2025 05:45 PM EDT",
10
+ version: "4.5.0",
11
+ buildTime: "06/03/2025 09:53 AM EDT",
12
12
  homepage: "https://github.com/aversini/auth-client",
13
13
  license: "MIT"
14
14
  });
@@ -27,7 +27,7 @@ const nt = {
27
27
  AUTH_TYPE: "X-Auth-Type"
28
28
  }, ie = {
29
29
  ACCESS_TOKEN: "access_token"
30
- }, K = {
30
+ }, m = {
31
31
  ALG: "RS256",
32
32
  USER_ID_KEY: "sub",
33
33
  USERNAME_KEY: "username",
@@ -38,6 +38,7 @@ const nt = {
38
38
  EXPIRES_AT_KEY: "exp",
39
39
  CREATED_AT_KEY: "iat",
40
40
  SCOPES_KEY: "scopes",
41
+ SCOPE_KEY: "scope",
41
42
  CLIENT_ID_KEY: "aud",
42
43
  ISSUER: "gizmette.com"
43
44
  }, oe = `-----BEGIN PUBLIC KEY-----
@@ -73,7 +74,7 @@ function ce(e) {
73
74
  r[n] = t.charCodeAt(n);
74
75
  return r;
75
76
  }
76
- function P(e) {
77
+ function _(e) {
77
78
  if (Uint8Array.fromBase64)
78
79
  return Uint8Array.fromBase64(typeof e == "string" ? e : T.decode(e), {
79
80
  alphabet: "base64url"
@@ -86,73 +87,73 @@ function P(e) {
86
87
  throw new TypeError("The input to be decoded is not correctly encoded.");
87
88
  }
88
89
  }
89
- class w extends Error {
90
+ class A extends Error {
90
91
  constructor(r, n) {
91
92
  var a;
92
93
  super(r, n);
93
- u(this, "code", "ERR_JOSE_GENERIC");
94
+ d(this, "code", "ERR_JOSE_GENERIC");
94
95
  this.name = this.constructor.name, (a = Error.captureStackTrace) == null || a.call(Error, this, this.constructor);
95
96
  }
96
97
  }
97
- u(w, "code", "ERR_JOSE_GENERIC");
98
- class h extends w {
98
+ d(A, "code", "ERR_JOSE_GENERIC");
99
+ class h extends A {
99
100
  constructor(r, n, a = "unspecified", i = "unspecified") {
100
101
  super(r, { cause: { claim: a, reason: i, payload: n } });
101
- u(this, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
102
- u(this, "claim");
103
- u(this, "reason");
104
- u(this, "payload");
102
+ d(this, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
103
+ d(this, "claim");
104
+ d(this, "reason");
105
+ d(this, "payload");
105
106
  this.claim = a, this.reason = i, this.payload = n;
106
107
  }
107
108
  }
108
- u(h, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
109
- class J extends w {
109
+ d(h, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
110
+ class J extends A {
110
111
  constructor(r, n, a = "unspecified", i = "unspecified") {
111
112
  super(r, { cause: { claim: a, reason: i, payload: n } });
112
- u(this, "code", "ERR_JWT_EXPIRED");
113
- u(this, "claim");
114
- u(this, "reason");
115
- u(this, "payload");
113
+ d(this, "code", "ERR_JWT_EXPIRED");
114
+ d(this, "claim");
115
+ d(this, "reason");
116
+ d(this, "payload");
116
117
  this.claim = a, this.reason = i, this.payload = n;
117
118
  }
118
119
  }
119
- u(J, "code", "ERR_JWT_EXPIRED");
120
- class F extends w {
120
+ d(J, "code", "ERR_JWT_EXPIRED");
121
+ class F extends A {
121
122
  constructor() {
122
123
  super(...arguments);
123
- u(this, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
124
+ d(this, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
124
125
  }
125
126
  }
126
- u(F, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
127
- class E extends w {
127
+ d(F, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
128
+ class w extends A {
128
129
  constructor() {
129
130
  super(...arguments);
130
- u(this, "code", "ERR_JOSE_NOT_SUPPORTED");
131
+ d(this, "code", "ERR_JOSE_NOT_SUPPORTED");
131
132
  }
132
133
  }
133
- u(E, "code", "ERR_JOSE_NOT_SUPPORTED");
134
- class d extends w {
134
+ d(w, "code", "ERR_JOSE_NOT_SUPPORTED");
135
+ class u extends A {
135
136
  constructor() {
136
137
  super(...arguments);
137
- u(this, "code", "ERR_JWS_INVALID");
138
+ d(this, "code", "ERR_JWS_INVALID");
138
139
  }
139
140
  }
140
- u(d, "code", "ERR_JWS_INVALID");
141
- class y extends w {
141
+ d(u, "code", "ERR_JWS_INVALID");
142
+ class y extends A {
142
143
  constructor() {
143
144
  super(...arguments);
144
- u(this, "code", "ERR_JWT_INVALID");
145
+ d(this, "code", "ERR_JWT_INVALID");
145
146
  }
146
147
  }
147
- u(y, "code", "ERR_JWT_INVALID");
148
- class V extends w {
148
+ d(y, "code", "ERR_JWT_INVALID");
149
+ class V extends A {
149
150
  constructor(r = "signature verification failed", n) {
150
151
  super(r, n);
151
- u(this, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
152
+ d(this, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
152
153
  }
153
154
  }
154
- u(V, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
155
- function S(e, t = "algorithm.name") {
155
+ d(V, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
156
+ function E(e, t = "algorithm.name") {
156
157
  return new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`);
157
158
  }
158
159
  function v(e, t) {
@@ -161,7 +162,7 @@ function v(e, t) {
161
162
  function D(e) {
162
163
  return parseInt(e.name.slice(4), 10);
163
164
  }
164
- function ue(e) {
165
+ function de(e) {
165
166
  switch (e) {
166
167
  case "ES256":
167
168
  return "P-256";
@@ -173,7 +174,7 @@ function ue(e) {
173
174
  throw new Error("unreachable");
174
175
  }
175
176
  }
176
- function de(e, t) {
177
+ function ue(e, t) {
177
178
  if (!e.usages.includes(t))
178
179
  throw new TypeError(`CryptoKey does not support this operation, its usages must include ${t}.`);
179
180
  }
@@ -183,52 +184,52 @@ function fe(e, t, r) {
183
184
  case "HS384":
184
185
  case "HS512": {
185
186
  if (!v(e.algorithm, "HMAC"))
186
- throw S("HMAC");
187
+ throw E("HMAC");
187
188
  const n = parseInt(t.slice(2), 10);
188
189
  if (D(e.algorithm.hash) !== n)
189
- throw S(`SHA-${n}`, "algorithm.hash");
190
+ throw E(`SHA-${n}`, "algorithm.hash");
190
191
  break;
191
192
  }
192
193
  case "RS256":
193
194
  case "RS384":
194
195
  case "RS512": {
195
196
  if (!v(e.algorithm, "RSASSA-PKCS1-v1_5"))
196
- throw S("RSASSA-PKCS1-v1_5");
197
+ throw E("RSASSA-PKCS1-v1_5");
197
198
  const n = parseInt(t.slice(2), 10);
198
199
  if (D(e.algorithm.hash) !== n)
199
- throw S(`SHA-${n}`, "algorithm.hash");
200
+ throw E(`SHA-${n}`, "algorithm.hash");
200
201
  break;
201
202
  }
202
203
  case "PS256":
203
204
  case "PS384":
204
205
  case "PS512": {
205
206
  if (!v(e.algorithm, "RSA-PSS"))
206
- throw S("RSA-PSS");
207
+ throw E("RSA-PSS");
207
208
  const n = parseInt(t.slice(2), 10);
208
209
  if (D(e.algorithm.hash) !== n)
209
- throw S(`SHA-${n}`, "algorithm.hash");
210
+ throw E(`SHA-${n}`, "algorithm.hash");
210
211
  break;
211
212
  }
212
213
  case "Ed25519":
213
214
  case "EdDSA": {
214
215
  if (!v(e.algorithm, "Ed25519"))
215
- throw S("Ed25519");
216
+ throw E("Ed25519");
216
217
  break;
217
218
  }
218
219
  case "ES256":
219
220
  case "ES384":
220
221
  case "ES512": {
221
222
  if (!v(e.algorithm, "ECDSA"))
222
- throw S("ECDSA");
223
- const n = ue(t);
223
+ throw E("ECDSA");
224
+ const n = de(t);
224
225
  if (e.algorithm.namedCurve !== n)
225
- throw S(n, "algorithm.namedCurve");
226
+ throw E(n, "algorithm.namedCurve");
226
227
  break;
227
228
  }
228
229
  default:
229
230
  throw new TypeError("CryptoKey does not support this operation");
230
231
  }
231
- de(e, r);
232
+ ue(e, r);
232
233
  }
233
234
  function G(e, t, ...r) {
234
235
  var n;
@@ -270,7 +271,7 @@ const Q = (e) => z(e) || X(e), he = (...e) => {
270
271
  function pe(e) {
271
272
  return typeof e == "object" && e !== null;
272
273
  }
273
- const _ = (e) => {
274
+ const P = (e) => {
274
275
  if (!pe(e) || Object.prototype.toString.call(e) !== "[object Object]")
275
276
  return !1;
276
277
  if (Object.getPrototypeOf(e) === null)
@@ -348,7 +349,7 @@ const _ = (e) => {
348
349
  i = { name: "Ed25519" }, o = ["verify"];
349
350
  break;
350
351
  default:
351
- throw new E('Invalid or unsupported "alg" (Algorithm) value');
352
+ throw new w('Invalid or unsupported "alg" (Algorithm) value');
352
353
  }
353
354
  return crypto.subtle.importKey(t, c, i, !0, o);
354
355
  }, Ee = (e, t, r) => Se(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
@@ -377,7 +378,7 @@ function we(e) {
377
378
  }, r = e.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
378
379
  break;
379
380
  default:
380
- throw new E('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
381
+ throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
381
382
  }
382
383
  break;
383
384
  }
@@ -399,7 +400,7 @@ function we(e) {
399
400
  t = { name: "ECDH", namedCurve: e.crv }, r = e.d ? ["deriveBits"] : [];
400
401
  break;
401
402
  default:
402
- throw new E('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
403
+ throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
403
404
  }
404
405
  break;
405
406
  }
@@ -416,12 +417,12 @@ function we(e) {
416
417
  t = { name: e.crv }, r = e.d ? ["deriveBits"] : [];
417
418
  break;
418
419
  default:
419
- throw new E('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
420
+ throw new w('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
420
421
  }
421
422
  break;
422
423
  }
423
424
  default:
424
- throw new E('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
425
+ throw new w('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
425
426
  }
426
427
  return { algorithm: t, keyUsages: r };
427
428
  }
@@ -447,7 +448,7 @@ const ge = (e, t, r, n, a) => {
447
448
  r !== void 0 ? i = new Map([...Object.entries(r), ...t.entries()]) : i = t;
448
449
  for (const o of n.crit) {
449
450
  if (!i.has(o))
450
- throw new E(`Extension Header Parameter "${o}" is not recognized`);
451
+ throw new w(`Extension Header Parameter "${o}" is not recognized`);
451
452
  if (a[o] === void 0)
452
453
  throw new e(`Extension Header Parameter "${o}" is missing`);
453
454
  if (i.get(o) && n[o] === void 0)
@@ -461,7 +462,7 @@ const ge = (e, t, r, n, a) => {
461
462
  return new Set(t);
462
463
  };
463
464
  function N(e) {
464
- return _(e) && typeof e.kty == "string";
465
+ return P(e) && typeof e.kty == "string";
465
466
  }
466
467
  function Ke(e) {
467
468
  return e.kty !== "oct" && typeof e.d == "string";
@@ -472,18 +473,18 @@ function Te(e) {
472
473
  function ve(e) {
473
474
  return e.kty === "oct" && typeof e.k == "string";
474
475
  }
475
- let C;
476
+ let K;
476
477
  const $ = async (e, t, r, n = !1) => {
477
- C || (C = /* @__PURE__ */ new WeakMap());
478
- let a = C.get(e);
478
+ K || (K = /* @__PURE__ */ new WeakMap());
479
+ let a = K.get(e);
479
480
  if (a != null && a[r])
480
481
  return a[r];
481
482
  const i = await Ae({ ...t, alg: r });
482
- return n && Object.freeze(e), a ? a[r] = i : C.set(e, { [r]: i }), i;
483
- }, Pe = (e, t) => {
483
+ return n && Object.freeze(e), a ? a[r] = i : K.set(e, { [r]: i }), i;
484
+ }, _e = (e, t) => {
484
485
  var o;
485
- C || (C = /* @__PURE__ */ new WeakMap());
486
- let r = C.get(e);
486
+ K || (K = /* @__PURE__ */ new WeakMap());
487
+ let r = K.get(e);
487
488
  if (r != null && r[t])
488
489
  return r[t];
489
490
  const n = e.type === "public", a = !!n;
@@ -565,8 +566,8 @@ const $ = async (e, t, r, n = !1) => {
565
566
  }
566
567
  if (!i)
567
568
  throw new TypeError("given KeyObject instance cannot be used for this algorithm");
568
- return r ? r[t] = i : C.set(e, { [t]: i }), i;
569
- }, _e = async (e, t) => {
569
+ return r ? r[t] = i : K.set(e, { [t]: i }), i;
570
+ }, Pe = async (e, t) => {
570
571
  if (e instanceof Uint8Array || z(e))
571
572
  return e;
572
573
  if (X(e)) {
@@ -574,7 +575,7 @@ const $ = async (e, t, r, n = !1) => {
574
575
  return e.export();
575
576
  if ("toCryptoKey" in e && typeof e.toCryptoKey == "function")
576
577
  try {
577
- return Pe(e, t);
578
+ return _e(e, t);
578
579
  } catch (n) {
579
580
  if (n instanceof TypeError)
580
581
  throw n;
@@ -583,9 +584,9 @@ const $ = async (e, t, r, n = !1) => {
583
584
  return $(e, r, t);
584
585
  }
585
586
  if (N(e))
586
- return e.k ? P(e.k) : $(e, e, t, !0);
587
+ return e.k ? _(e.k) : $(e, e, t, !0);
587
588
  throw new Error("unreachable");
588
- }, g = (e) => e == null ? void 0 : e[Symbol.toStringTag], x = (e, t, r) => {
589
+ }, C = (e) => e == null ? void 0 : e[Symbol.toStringTag], x = (e, t, r) => {
589
590
  var n, a;
590
591
  if (t.use !== void 0) {
591
592
  let i;
@@ -639,7 +640,7 @@ const $ = async (e, t, r, n = !1) => {
639
640
  if (!Q(t))
640
641
  throw new TypeError(q(e, t, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
641
642
  if (t.type !== "secret")
642
- throw new TypeError(`${g(t)} instances for symmetric algorithms must be of type "secret"`);
643
+ throw new TypeError(`${C(t)} instances for symmetric algorithms must be of type "secret"`);
643
644
  }
644
645
  }, Ie = (e, t, r) => {
645
646
  if (N(t))
@@ -658,20 +659,20 @@ const $ = async (e, t, r, n = !1) => {
658
659
  if (!Q(t))
659
660
  throw new TypeError(q(e, t, "CryptoKey", "KeyObject", "JSON Web Key"));
660
661
  if (t.type === "secret")
661
- throw new TypeError(`${g(t)} instances for asymmetric algorithms must not be of type "secret"`);
662
+ throw new TypeError(`${C(t)} instances for asymmetric algorithms must not be of type "secret"`);
662
663
  if (t.type === "public")
663
664
  switch (r) {
664
665
  case "sign":
665
- throw new TypeError(`${g(t)} instances for asymmetric algorithm signing must be of type "private"`);
666
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm signing must be of type "private"`);
666
667
  case "decrypt":
667
- throw new TypeError(`${g(t)} instances for asymmetric algorithm decryption must be of type "private"`);
668
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm decryption must be of type "private"`);
668
669
  }
669
670
  if (t.type === "private")
670
671
  switch (r) {
671
672
  case "verify":
672
- throw new TypeError(`${g(t)} instances for asymmetric algorithm verifying must be of type "public"`);
673
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm verifying must be of type "public"`);
673
674
  case "encrypt":
674
- throw new TypeError(`${g(t)} instances for asymmetric algorithm encryption must be of type "public"`);
675
+ throw new TypeError(`${C(t)} instances for asymmetric algorithm encryption must be of type "public"`);
675
676
  }
676
677
  }, Oe = (e, t, r) => {
677
678
  e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A(?:128|192|256)(?:GCM)?(?:KW)?$/.test(e) || /^A(?:128|192|256)CBC-HS(?:256|384|512)$/.test(e) ? Re(e, t, r) : Ie(e, t, r);
@@ -698,7 +699,7 @@ const $ = async (e, t, r, n = !1) => {
698
699
  case "EdDSA":
699
700
  return { name: "Ed25519" };
700
701
  default:
701
- throw new E(`alg ${e} is not supported either by JOSE or your javascript runtime`);
702
+ throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
702
703
  }
703
704
  }, De = async (e, t, r) => {
704
705
  if (t instanceof Uint8Array) {
@@ -718,75 +719,75 @@ const $ = async (e, t, r, n = !1) => {
718
719
  }
719
720
  };
720
721
  async function Je(e, t, r) {
721
- if (!_(e))
722
- throw new d("Flattened JWS must be an object");
722
+ if (!P(e))
723
+ throw new u("Flattened JWS must be an object");
723
724
  if (e.protected === void 0 && e.header === void 0)
724
- throw new d('Flattened JWS must have either of the "protected" or "header" members');
725
+ throw new u('Flattened JWS must have either of the "protected" or "header" members');
725
726
  if (e.protected !== void 0 && typeof e.protected != "string")
726
- throw new d("JWS Protected Header incorrect type");
727
+ throw new u("JWS Protected Header incorrect type");
727
728
  if (e.payload === void 0)
728
- throw new d("JWS Payload missing");
729
+ throw new u("JWS Payload missing");
729
730
  if (typeof e.signature != "string")
730
- throw new d("JWS Signature missing or incorrect type");
731
- if (e.header !== void 0 && !_(e.header))
732
- throw new d("JWS Unprotected Header incorrect type");
731
+ throw new u("JWS Signature missing or incorrect type");
732
+ if (e.header !== void 0 && !P(e.header))
733
+ throw new u("JWS Unprotected Header incorrect type");
733
734
  let n = {};
734
735
  if (e.protected)
735
736
  try {
736
- const re = P(e.protected);
737
+ const re = _(e.protected);
737
738
  n = JSON.parse(T.decode(re));
738
739
  } catch {
739
- throw new d("JWS Protected Header is invalid");
740
+ throw new u("JWS Protected Header is invalid");
740
741
  }
741
742
  if (!he(n, e.header))
742
- throw new d("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
743
+ throw new u("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
743
744
  const a = {
744
745
  ...n,
745
746
  ...e.header
746
- }, i = ge(d, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
747
+ }, i = ge(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
747
748
  let o = !0;
748
749
  if (i.has("b64") && (o = n.b64, typeof o != "boolean"))
749
- throw new d('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
750
+ throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
750
751
  const { alg: c } = a;
751
752
  if (typeof c != "string" || !c)
752
- throw new d('JWS "alg" (Algorithm) Header Parameter missing or invalid');
753
+ throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
753
754
  const s = r && Ce("algorithms", r.algorithms);
754
755
  if (s && !s.has(c))
755
756
  throw new F('"alg" (Algorithm) Header Parameter value not allowed');
756
757
  if (o) {
757
758
  if (typeof e.payload != "string")
758
- throw new d("JWS Payload must be a string");
759
+ throw new u("JWS Payload must be a string");
759
760
  } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
760
- throw new d("JWS Payload must be a string or an Uint8Array instance");
761
- let m = !1;
762
- typeof t == "function" && (t = await t(n, e), m = !0), Oe(c, t, "verify");
763
- const A = se(O.encode(e.protected ?? ""), O.encode("."), typeof e.payload == "string" ? O.encode(e.payload) : e.payload);
761
+ throw new u("JWS Payload must be a string or an Uint8Array instance");
762
+ let S = !1;
763
+ typeof t == "function" && (t = await t(n, e), S = !0), Oe(c, t, "verify");
764
+ const b = se(O.encode(e.protected ?? ""), O.encode("."), typeof e.payload == "string" ? O.encode(e.payload) : e.payload);
764
765
  let p;
765
766
  try {
766
- p = P(e.signature);
767
+ p = _(e.signature);
767
768
  } catch {
768
- throw new d("Failed to base64url decode the signature");
769
+ throw new u("Failed to base64url decode the signature");
769
770
  }
770
- const R = await _e(t, c);
771
- if (!await He(c, R, p, A))
771
+ const R = await Pe(t, c);
772
+ if (!await He(c, R, p, b))
772
773
  throw new V();
773
774
  let l;
774
775
  if (o)
775
776
  try {
776
- l = P(e.payload);
777
+ l = _(e.payload);
777
778
  } catch {
778
- throw new d("Failed to base64url decode the payload");
779
+ throw new u("Failed to base64url decode the payload");
779
780
  }
780
781
  else typeof e.payload == "string" ? l = O.encode(e.payload) : l = e.payload;
781
- const b = { payload: l };
782
- return e.protected !== void 0 && (b.protectedHeader = n), e.header !== void 0 && (b.unprotectedHeader = e.header), m ? { ...b, key: R } : b;
782
+ const g = { payload: l };
783
+ return e.protected !== void 0 && (g.protectedHeader = n), e.header !== void 0 && (g.unprotectedHeader = e.header), S ? { ...g, key: R } : g;
783
784
  }
784
785
  async function xe(e, t, r) {
785
786
  if (e instanceof Uint8Array && (e = T.decode(e)), typeof e != "string")
786
- throw new d("Compact JWS must be a string or Uint8Array");
787
+ throw new u("Compact JWS must be a string or Uint8Array");
787
788
  const { 0: n, 1: a, 2: i, length: o } = e.split(".");
788
789
  if (o !== 3)
789
- throw new d("Invalid Compact JWS");
790
+ throw new u("Invalid Compact JWS");
790
791
  const c = await Je({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
791
792
  return typeof t == "function" ? { ...s, key: c.key } : s;
792
793
  }
@@ -840,14 +841,14 @@ function Me(e, t, r = {}) {
840
841
  n = JSON.parse(T.decode(t));
841
842
  } catch {
842
843
  }
843
- if (!_(n))
844
+ if (!P(n))
844
845
  throw new y("JWT Claims Set must be a top-level JSON object");
845
846
  const { typ: a } = r;
846
847
  if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
847
848
  throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
848
- const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: m } = r, A = [...i];
849
- m !== void 0 && A.push("iat"), s !== void 0 && A.push("aud"), c !== void 0 && A.push("sub"), o !== void 0 && A.push("iss");
850
- for (const l of new Set(A.reverse()))
849
+ const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: S } = r, b = [...i];
850
+ S !== void 0 && b.push("iat"), s !== void 0 && b.push("aud"), c !== void 0 && b.push("sub"), o !== void 0 && b.push("iss");
851
+ for (const l of new Set(b.reverse()))
851
852
  if (!(l in n))
852
853
  throw new h(`missing required "${l}" claim`, n, l, "missing");
853
854
  if (o && !(Array.isArray(o) ? o : [o]).includes(n.iss))
@@ -871,7 +872,7 @@ function Me(e, t, r = {}) {
871
872
  throw new TypeError("Invalid clockTolerance option type");
872
873
  }
873
874
  const { currentDate: R } = r, I = Ne(R || /* @__PURE__ */ new Date());
874
- if ((n.iat !== void 0 || m) && typeof n.iat != "number")
875
+ if ((n.iat !== void 0 || S) && typeof n.iat != "number")
875
876
  throw new h('"iat" claim must be a number', n, "iat", "invalid");
876
877
  if (n.nbf !== void 0) {
877
878
  if (typeof n.nbf != "number")
@@ -885,16 +886,16 @@ function Me(e, t, r = {}) {
885
886
  if (n.exp <= I - p)
886
887
  throw new J('"exp" claim timestamp check failed', n, "exp", "check_failed");
887
888
  }
888
- if (m) {
889
- const l = I - n.iat, b = typeof m == "number" ? m : L(m);
890
- if (l - p > b)
889
+ if (S) {
890
+ const l = I - n.iat, g = typeof S == "number" ? S : L(S);
891
+ if (l - p > g)
891
892
  throw new J('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
892
893
  if (l < 0 - p)
893
894
  throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
894
895
  }
895
896
  return n;
896
897
  }
897
- async function ke(e, t, r) {
898
+ async function Ye(e, t, r) {
898
899
  var o;
899
900
  const n = await xe(e, t, r);
900
901
  if ((o = n.protectedHeader.crit) != null && o.includes("b64") && n.protectedHeader.b64 === !1)
@@ -914,7 +915,7 @@ function ee(e) {
914
915
  throw new y("JWTs must contain a payload");
915
916
  let n;
916
917
  try {
917
- n = P(t);
918
+ n = _(t);
918
919
  } catch {
919
920
  throw new y("Failed to base64url decode the payload");
920
921
  }
@@ -924,15 +925,15 @@ function ee(e) {
924
925
  } catch {
925
926
  throw new y("Failed to parse the decoded payload as JSON");
926
927
  }
927
- if (!_(a))
928
+ if (!P(a))
928
929
  throw new y("Invalid JWT Claims Set");
929
930
  return a;
930
931
  }
931
- const Ye = async (e) => {
932
+ const ke = async (e) => {
932
933
  try {
933
- const t = K.ALG, n = await be(oe, t);
934
- return await ke(e, n, {
935
- issuer: K.ISSUER
934
+ const t = m.ALG, n = await be(oe, t);
935
+ return await Ye(e, n, {
936
+ issuer: m.ISSUER
936
937
  });
937
938
  } catch {
938
939
  return;
@@ -960,7 +961,7 @@ function Ge() {
960
961
  return H(Ve);
961
962
  }
962
963
  const qe = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), M = { randomUUID: qe };
963
- function k(e, t, r) {
964
+ function Y(e, t, r) {
964
965
  var a;
965
966
  if (M.randomUUID && !e)
966
967
  return M.randomUUID();
@@ -970,15 +971,15 @@ function k(e, t, r) {
970
971
  throw new Error("Random bytes length must be >= 16");
971
972
  return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Fe(n);
972
973
  }
973
- const Y = globalThis.crypto, ze = (e) => `${k()}${k()}`.slice(0, e), Xe = (e) => btoa(
974
+ const k = globalThis.crypto, ze = (e) => `${Y()}${Y()}`.slice(0, e), Xe = (e) => btoa(
974
975
  [...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
975
976
  );
976
977
  async function te(e) {
977
- if (!Y.subtle)
978
+ if (!k.subtle)
978
979
  throw new Error(
979
980
  "crypto.subtle is available only in secure contexts (HTTPS)."
980
981
  );
981
- const t = new TextEncoder().encode(e), r = await Y.subtle.digest("SHA-256", t);
982
+ const t = new TextEncoder().encode(e), r = await k.subtle.digest("SHA-256", t);
982
983
  return Xe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
983
984
  }
984
985
  async function ct(e) {
@@ -991,7 +992,7 @@ async function ct(e) {
991
992
  code_challenge: n
992
993
  };
993
994
  }
994
- async function ut(e, t) {
995
+ async function dt(e, t) {
995
996
  return t === await te(e);
996
997
  }
997
998
  const Qe = /^Bearer (.+)$/i, Ze = (e) => {
@@ -1011,24 +1012,35 @@ const Qe = /^Bearer (.+)$/i, Ze = (e) => {
1011
1012
  const t = e == null ? void 0 : e[ie.ACCESS_TOKEN];
1012
1013
  if (typeof t == "string")
1013
1014
  return t;
1014
- }, dt = ({ headers: e, body: t, clientId: r }) => {
1015
+ }, ut = ({ headers: e, body: t, clientId: r }) => {
1015
1016
  const n = Ze(e), a = je(e, r);
1016
1017
  return et(t) || a || n || "";
1017
1018
  }, ft = async (e, t) => {
1018
- var a;
1019
- const r = await Ye(e);
1020
- if (!r || !Array.isArray((a = r.payload) == null ? void 0 : a[K.SCOPES_KEY]))
1019
+ const r = await ke(e);
1020
+ if (!r || !r.payload)
1021
+ return !1;
1022
+ let n = [];
1023
+ if (Array.isArray(r.payload[m.SCOPES_KEY]))
1024
+ n = r.payload[m.SCOPES_KEY];
1025
+ else if (typeof r.payload[m.SCOPE_KEY] == "string")
1026
+ n = r.payload[m.SCOPE_KEY].split(" ").filter((i) => i.trim() !== "");
1027
+ else
1021
1028
  return !1;
1022
- const n = r.payload[K.SCOPES_KEY];
1023
- return Array.isArray(t) ? t.every((i) => n.includes(i)) : Object.keys(t).some(
1024
- (i) => t[i].every((o) => n.includes(o))
1029
+ return Array.isArray(t) ? t.every((a) => n.includes(a)) : Object.keys(t).some(
1030
+ (a) => t[a].every((i) => n.includes(i))
1025
1031
  );
1026
1032
  }, lt = (e, t) => {
1027
1033
  try {
1028
1034
  const r = ee(e);
1029
- if (!r || !Array.isArray(r[K.SCOPES_KEY]))
1035
+ if (!r)
1036
+ return !1;
1037
+ let n = [];
1038
+ if (Array.isArray(r[m.SCOPES_KEY]))
1039
+ n = r[m.SCOPES_KEY];
1040
+ else if (typeof r[m.SCOPE_KEY] == "string")
1041
+ n = r[m.SCOPE_KEY].split(" ").filter((i) => i.trim() !== "");
1042
+ else
1030
1043
  return !1;
1031
- const n = r[K.SCOPES_KEY];
1032
1044
  return Array.isArray(t) ? t.every((a) => n.includes(a)) : Object.keys(t).some(
1033
1045
  (a) => t[a].every((i) => n.includes(i))
1034
1046
  );
@@ -1048,16 +1060,16 @@ export {
1048
1060
  nt as AUTH_TYPES,
1049
1061
  ie as BODY,
1050
1062
  at as HEADERS,
1051
- K as JWT,
1063
+ m as JWT,
1052
1064
  oe as JWT_PUBLIC_KEY,
1053
1065
  it as TOKEN_EXPIRATION,
1054
1066
  st as decodeToken,
1055
1067
  te as generateCodeChallenge,
1056
1068
  ht as getSession,
1057
- dt as getToken,
1069
+ ut as getToken,
1058
1070
  ft as isGranted,
1059
1071
  lt as isGrantedSync,
1060
1072
  ct as pkceChallengePair,
1061
- Ye as verifyAndExtractToken,
1062
- ut as verifyChallenge
1073
+ ke as verifyAndExtractToken,
1074
+ dt as verifyChallenge
1063
1075
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-common",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -36,5 +36,5 @@
36
36
  "jose": "6.0.10",
37
37
  "uuid": "11.1.0"
38
38
  },
39
- "gitHead": "3109c57783f179b3d0f2fee8cecf16cc8ba333e1"
39
+ "gitHead": "fb77e97e486bd0fa158543e93d35b3a9be64d451"
40
40
  }