@versini/auth-common 2.12.0 → 3.0.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
@@ -11,12 +11,16 @@ declare const AUTH_TYPES: {
11
11
  declare const HEADERS: {
12
12
  CLIENT_ID: string;
13
13
  };
14
+ declare const BODY: {
15
+ ACCESS_TOKEN: string;
16
+ };
14
17
  declare const JWT: {
15
18
  ALG: string;
16
19
  USER_ID_KEY: string;
17
20
  TOKEN_ID_KEY: string;
18
21
  NONCE_KEY: string;
19
22
  USERNAME_KEY: string;
23
+ AUTH_TYPE_KEY: string;
20
24
  ISSUER: string;
21
25
  };
22
26
  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-----";
@@ -60,6 +64,9 @@ declare function pkceChallengePair(length?: number): Promise<{
60
64
  */
61
65
  declare function verifyChallenge(code_verifier: string, expectedChallenge: string): Promise<boolean>;
62
66
 
67
+ type BodyLike = Record<string, unknown> & {
68
+ access_token?: string;
69
+ };
63
70
  type HeadersLike = Record<string, unknown> & {
64
71
  authorization?: string;
65
72
  "content-type"?: string;
@@ -67,11 +74,21 @@ type HeadersLike = Record<string, unknown> & {
67
74
  };
68
75
  /**
69
76
  * Get a Bearer Token from a request.
77
+ * It checks the following sources in order:
78
+ * 1. The `access_token` body parameter.
79
+ * 2. The `auth.${clientId}` cookie.
80
+ * 3. The `Authorization` header.
70
81
  *
71
82
  * @param headers An object containing the request headers, usually `req.headers`.
83
+ * @param body An object containing the request body, usually `req.body`.
72
84
  * @param clientId The client ID to use.
73
85
  *
74
86
  */
75
- declare const getToken: (headers: HeadersLike, clientId: string) => string;
87
+ type GetToken = {
88
+ clientId: string;
89
+ headers: HeadersLike;
90
+ body?: BodyLike;
91
+ };
92
+ declare const getToken: ({ headers, body, clientId }: GetToken) => string;
76
93
 
77
- export { API_TYPE, AUTH_TYPES, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
94
+ export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
package/dist/index.js CHANGED
@@ -1,33 +1,36 @@
1
1
  /*!
2
- @versini/auth-common v2.12.0
2
+ @versini/auth-common v3.0.0
3
3
  © 2024 gizmette.com
4
4
  */
5
5
  try {
6
6
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
7
- version: "2.12.0",
8
- buildTime: "07/15/2024 09:28 AM EDT",
7
+ version: "3.0.0",
8
+ buildTime: "07/19/2024 09:13 AM EDT",
9
9
  homepage: "https://github.com/aversini/auth-client",
10
10
  license: "MIT"
11
11
  });
12
12
  } catch {
13
13
  }
14
- const Qe = {
14
+ const Ze = {
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
- }, Xe = {
21
+ }, je = {
22
22
  CLIENT_ID: "X-Auth-ClientId"
23
- }, N = {
23
+ }, ne = {
24
+ ACCESS_TOKEN: "access_token"
25
+ }, U = {
24
26
  ALG: "RS256",
25
27
  USER_ID_KEY: "sub",
26
28
  TOKEN_ID_KEY: "__raw",
27
29
  NONCE_KEY: "_nonce",
28
30
  USERNAME_KEY: "username",
31
+ AUTH_TYPE_KEY: "auth_type",
29
32
  ISSUER: "gizmette.com"
30
- }, ne = `-----BEGIN PUBLIC KEY-----
33
+ }, ae = `-----BEGIN PUBLIC KEY-----
31
34
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7
32
35
  w5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5
33
36
  i1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle
@@ -35,23 +38,23 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
35
38
  l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
36
39
  sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
37
40
  awIDAQAB
38
- -----END PUBLIC KEY-----`, Ze = {
41
+ -----END PUBLIC KEY-----`, et = {
39
42
  ACCESS: "5m",
40
43
  ID: "90d",
41
44
  REFRESH: "90d"
42
- }, je = {
45
+ }, tt = {
43
46
  AUTHENTICATE: "authenticate",
44
47
  CODE: "code",
45
48
  LOGOUT: "logout"
46
- }, x = crypto, z = (e) => e instanceof CryptoKey, v = new TextEncoder(), C = new TextDecoder();
47
- function ae(...e) {
49
+ }, K = crypto, q = (e) => e instanceof CryptoKey, v = new TextEncoder(), C = new TextDecoder();
50
+ function oe(...e) {
48
51
  const t = e.reduce((a, { length: i }) => a + i, 0), r = new Uint8Array(t);
49
52
  let n = 0;
50
53
  for (const a of e)
51
54
  r.set(a, n), n += a.length;
52
55
  return r;
53
56
  }
54
- const oe = (e) => {
57
+ const ie = (e) => {
55
58
  const t = atob(e), r = new Uint8Array(t.length);
56
59
  for (let n = 0; n < t.length; n++)
57
60
  r[n] = t.charCodeAt(n);
@@ -60,7 +63,7 @@ const oe = (e) => {
60
63
  let t = e;
61
64
  t instanceof Uint8Array && (t = C.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
62
65
  try {
63
- return oe(t);
66
+ return ie(t);
64
67
  } catch {
65
68
  throw new TypeError("The input to be decoded is not correctly encoded.");
66
69
  }
@@ -90,7 +93,7 @@ class $ extends A {
90
93
  super(t), this.code = "ERR_JWT_EXPIRED", this.claim = n, this.reason = a, this.payload = r;
91
94
  }
92
95
  }
93
- class ie extends A {
96
+ class ce extends A {
94
97
  constructor() {
95
98
  super(...arguments), this.code = "ERR_JOSE_ALG_NOT_ALLOWED";
96
99
  }
@@ -122,7 +125,7 @@ class S extends A {
122
125
  return "ERR_JWT_INVALID";
123
126
  }
124
127
  }
125
- class ce extends A {
128
+ class se extends A {
126
129
  constructor() {
127
130
  super(...arguments), this.code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED", this.message = "signature verification failed";
128
131
  }
@@ -136,10 +139,10 @@ function m(e, t = "algorithm.name") {
136
139
  function T(e, t) {
137
140
  return e.name === t;
138
141
  }
139
- function O(e) {
142
+ function x(e) {
140
143
  return parseInt(e.name.slice(4), 10);
141
144
  }
142
- function se(e) {
145
+ function de(e) {
143
146
  switch (e) {
144
147
  case "ES256":
145
148
  return "P-256";
@@ -151,7 +154,7 @@ function se(e) {
151
154
  throw new Error("unreachable");
152
155
  }
153
156
  }
154
- function de(e, t) {
157
+ function ue(e, t) {
155
158
  if (t.length && !t.some((r) => e.usages.includes(r))) {
156
159
  let r = "CryptoKey does not support this operation, its usages must include ";
157
160
  if (t.length > 2) {
@@ -161,7 +164,7 @@ function de(e, t) {
161
164
  throw new TypeError(r);
162
165
  }
163
166
  }
164
- function ue(e, t, ...r) {
167
+ function le(e, t, ...r) {
165
168
  switch (t) {
166
169
  case "HS256":
167
170
  case "HS384":
@@ -169,7 +172,7 @@ function ue(e, t, ...r) {
169
172
  if (!T(e.algorithm, "HMAC"))
170
173
  throw m("HMAC");
171
174
  const n = parseInt(t.slice(2), 10);
172
- if (O(e.algorithm.hash) !== n)
175
+ if (x(e.algorithm.hash) !== n)
173
176
  throw m(`SHA-${n}`, "algorithm.hash");
174
177
  break;
175
178
  }
@@ -179,7 +182,7 @@ function ue(e, t, ...r) {
179
182
  if (!T(e.algorithm, "RSASSA-PKCS1-v1_5"))
180
183
  throw m("RSASSA-PKCS1-v1_5");
181
184
  const n = parseInt(t.slice(2), 10);
182
- if (O(e.algorithm.hash) !== n)
185
+ if (x(e.algorithm.hash) !== n)
183
186
  throw m(`SHA-${n}`, "algorithm.hash");
184
187
  break;
185
188
  }
@@ -189,7 +192,7 @@ function ue(e, t, ...r) {
189
192
  if (!T(e.algorithm, "RSA-PSS"))
190
193
  throw m("RSA-PSS");
191
194
  const n = parseInt(t.slice(2), 10);
192
- if (O(e.algorithm.hash) !== n)
195
+ if (x(e.algorithm.hash) !== n)
193
196
  throw m(`SHA-${n}`, "algorithm.hash");
194
197
  break;
195
198
  }
@@ -203,7 +206,7 @@ function ue(e, t, ...r) {
203
206
  case "ES512": {
204
207
  if (!T(e.algorithm, "ECDSA"))
205
208
  throw m("ECDSA");
206
- const n = se(t);
209
+ const n = de(t);
207
210
  if (e.algorithm.namedCurve !== n)
208
211
  throw m(n, "algorithm.namedCurve");
209
212
  break;
@@ -211,9 +214,9 @@ function ue(e, t, ...r) {
211
214
  default:
212
215
  throw new TypeError("CryptoKey does not support this operation");
213
216
  }
214
- de(e, r);
217
+ ue(e, r);
215
218
  }
216
- function G(e, t, ...r) {
219
+ function z(e, t, ...r) {
217
220
  var n;
218
221
  if (r.length > 2) {
219
222
  const a = r.pop();
@@ -221,11 +224,11 @@ function G(e, t, ...r) {
221
224
  } else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
222
225
  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;
223
226
  }
224
- const L = (e, ...t) => G("Key must be ", e, ...t);
225
- function Y(e, t, ...r) {
226
- return G(`Key for the ${e} algorithm must be `, t, ...r);
227
+ const L = (e, ...t) => z("Key must be ", e, ...t);
228
+ function G(e, t, ...r) {
229
+ return z(`Key for the ${e} algorithm must be `, t, ...r);
227
230
  }
228
- const Q = (e) => z(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", _ = ["CryptoKey"], le = (...e) => {
231
+ const Q = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"], fe = (...e) => {
229
232
  const t = e.filter(Boolean);
230
233
  if (t.length === 0 || t.length === 1)
231
234
  return !0;
@@ -244,11 +247,11 @@ const Q = (e) => z(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
244
247
  }
245
248
  return !0;
246
249
  };
247
- function fe(e) {
250
+ function he(e) {
248
251
  return typeof e == "object" && e !== null;
249
252
  }
250
253
  function P(e) {
251
- if (!fe(e) || Object.prototype.toString.call(e) !== "[object Object]")
254
+ if (!he(e) || Object.prototype.toString.call(e) !== "[object Object]")
252
255
  return !1;
253
256
  if (Object.getPrototypeOf(e) === null)
254
257
  return !0;
@@ -257,14 +260,14 @@ function P(e) {
257
260
  t = Object.getPrototypeOf(t);
258
261
  return Object.getPrototypeOf(e) === t;
259
262
  }
260
- const he = (e, t) => {
263
+ const pe = (e, t) => {
261
264
  if (e.startsWith("RS") || e.startsWith("PS")) {
262
265
  const { modulusLength: r } = t.algorithm;
263
266
  if (typeof r != "number" || r < 2048)
264
267
  throw new TypeError(`${e} requires key modulusLength to be 2048 bits or larger`);
265
268
  }
266
269
  };
267
- function pe(e) {
270
+ function me(e) {
268
271
  let t, r;
269
272
  switch (e.kty) {
270
273
  case "RSA": {
@@ -336,36 +339,36 @@ function pe(e) {
336
339
  }
337
340
  return { algorithm: t, keyUsages: r };
338
341
  }
339
- const me = async (e) => {
342
+ const Se = async (e) => {
340
343
  if (!e.alg)
341
344
  throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
342
- const { algorithm: t, keyUsages: r } = pe(e), n = [
345
+ const { algorithm: t, keyUsages: r } = me(e), n = [
343
346
  t,
344
347
  e.ext ?? !1,
345
348
  e.key_ops ?? r
346
349
  ], a = { ...e };
347
- return delete a.alg, delete a.use, x.subtle.importKey("jwk", a, ...n);
350
+ return delete a.alg, delete a.use, K.subtle.importKey("jwk", a, ...n);
348
351
  }, X = (e) => b(e);
349
352
  let W, J;
350
353
  const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j = async (e, t, r, n) => {
351
354
  let a = e.get(t);
352
355
  if (a != null && a[n])
353
356
  return a[n];
354
- const i = await me({ ...r, alg: n });
357
+ const i = await Se({ ...r, alg: n });
355
358
  return a ? a[n] = i : e.set(t, { [n]: i }), i;
356
- }, Se = (e, t) => {
359
+ }, ye = (e, t) => {
357
360
  if (Z(e)) {
358
361
  let r = e.export({ format: "jwk" });
359
362
  return delete r.d, delete r.dp, delete r.dq, delete r.p, delete r.q, delete r.qi, r.k ? X(r.k) : (J || (J = /* @__PURE__ */ new WeakMap()), j(J, e, r, t));
360
363
  }
361
364
  return e;
362
- }, ye = (e, t) => {
365
+ }, Ee = (e, t) => {
363
366
  if (Z(e)) {
364
367
  let r = e.export({ format: "jwk" });
365
368
  return r.k ? X(r.k) : (W || (W = /* @__PURE__ */ new WeakMap()), j(W, e, r, t));
366
369
  }
367
370
  return e;
368
- }, Ee = { normalizePublicKey: Se, normalizePrivateKey: ye }, E = (e, t, r = 0) => {
371
+ }, we = { normalizePublicKey: ye, normalizePrivateKey: Ee }, E = (e, t, r = 0) => {
369
372
  r === 0 && (t.unshift(t.length), t.unshift(6));
370
373
  const n = e.indexOf(t[0], r);
371
374
  if (n === -1)
@@ -391,7 +394,7 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
391
394
  default:
392
395
  throw new w("Invalid or unsupported EC Key Curve or OKP Key Sub Type");
393
396
  }
394
- }, we = async (e, t, r, n, a) => {
397
+ }, ge = async (e, t, r, n, a) => {
395
398
  let i, o;
396
399
  const c = new Uint8Array(atob(r.replace(e, "")).split("").map((s) => s.charCodeAt(0)));
397
400
  switch (n) {
@@ -437,33 +440,33 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
437
440
  default:
438
441
  throw new w('Invalid or unsupported "alg" (Algorithm) value');
439
442
  }
440
- return x.subtle.importKey(t, c, i, !1, o);
441
- }, ge = (e, t, r) => we(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
442
- async function Ae(e, t, r) {
443
+ return K.subtle.importKey(t, c, i, !1, o);
444
+ }, Ae = (e, t, r) => ge(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
445
+ async function be(e, t, r) {
443
446
  if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
444
447
  throw new TypeError('"spki" must be SPKI formatted string');
445
- return ge(e, t);
448
+ return Ae(e, t);
446
449
  }
447
- const R = (e) => e == null ? void 0 : e[Symbol.toStringTag], be = (e, t) => {
450
+ const I = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
448
451
  if (!(t instanceof Uint8Array)) {
449
452
  if (!Q(t))
450
- throw new TypeError(Y(e, t, ..._, "Uint8Array"));
453
+ throw new TypeError(G(e, t, ...R, "Uint8Array"));
451
454
  if (t.type !== "secret")
452
- throw new TypeError(`${R(t)} instances for symmetric algorithms must be of type "secret"`);
455
+ throw new TypeError(`${I(t)} instances for symmetric algorithms must be of type "secret"`);
453
456
  }
454
- }, Ce = (e, t, r) => {
457
+ }, ve = (e, t, r) => {
455
458
  if (!Q(t))
456
- throw new TypeError(Y(e, t, ..._));
459
+ throw new TypeError(G(e, t, ...R));
457
460
  if (t.type === "secret")
458
- throw new TypeError(`${R(t)} instances for asymmetric algorithms must not be of type "secret"`);
461
+ throw new TypeError(`${I(t)} instances for asymmetric algorithms must not be of type "secret"`);
459
462
  if (t.algorithm && r === "verify" && t.type === "private")
460
- throw new TypeError(`${R(t)} instances for asymmetric algorithm verifying must be of type "public"`);
463
+ throw new TypeError(`${I(t)} instances for asymmetric algorithm verifying must be of type "public"`);
461
464
  if (t.algorithm && r === "encrypt" && t.type === "private")
462
- throw new TypeError(`${R(t)} instances for asymmetric algorithm encryption must be of type "public"`);
463
- }, ve = (e, t, r) => {
464
- e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? be(e, t) : Ce(e, t, r);
465
+ throw new TypeError(`${I(t)} instances for asymmetric algorithm encryption must be of type "public"`);
466
+ }, Te = (e, t, r) => {
467
+ e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : ve(e, t, r);
465
468
  };
466
- function Te(e, t, r, n, a) {
469
+ function _e(e, t, r, n, a) {
467
470
  if (a.crit !== void 0 && (n == null ? void 0 : n.crit) === void 0)
468
471
  throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
469
472
  if (!n || n.crit === void 0)
@@ -513,27 +516,27 @@ function Re(e, t) {
513
516
  throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
514
517
  }
515
518
  }
516
- async function _e(e, t, r) {
517
- if (t = await Ee.normalizePublicKey(t, e), z(t))
518
- return ue(t, e, r), t;
519
+ async function Pe(e, t, r) {
520
+ if (t = await we.normalizePublicKey(t, e), q(t))
521
+ return le(t, e, r), t;
519
522
  if (t instanceof Uint8Array) {
520
523
  if (!e.startsWith("HS"))
521
- throw new TypeError(L(t, ..._));
522
- return x.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
524
+ throw new TypeError(L(t, ...R));
525
+ return K.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
523
526
  }
524
- throw new TypeError(L(t, ..._, "Uint8Array"));
527
+ throw new TypeError(L(t, ...R, "Uint8Array"));
525
528
  }
526
- const Pe = async (e, t, r, n) => {
527
- const a = await _e(e, t, "verify");
528
- he(e, a);
529
+ const Ke = async (e, t, r, n) => {
530
+ const a = await Pe(e, t, "verify");
531
+ pe(e, a);
529
532
  const i = Re(e, a.algorithm);
530
533
  try {
531
- return await x.subtle.verify(i, a, r, n);
534
+ return await K.subtle.verify(i, a, r, n);
532
535
  } catch {
533
536
  return !1;
534
537
  }
535
538
  };
536
- async function xe(e, t, r) {
539
+ async function Oe(e, t, r) {
537
540
  if (!P(e))
538
541
  throw new u("Flattened JWS must be an object");
539
542
  if (e.protected === void 0 && e.header === void 0)
@@ -549,17 +552,17 @@ async function xe(e, t, r) {
549
552
  let n = {};
550
553
  if (e.protected)
551
554
  try {
552
- const K = b(e.protected);
553
- n = JSON.parse(C.decode(K));
555
+ const O = b(e.protected);
556
+ n = JSON.parse(C.decode(O));
554
557
  } catch {
555
558
  throw new u("JWS Protected Header is invalid");
556
559
  }
557
- if (!le(n, e.header))
560
+ if (!fe(n, e.header))
558
561
  throw new u("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
559
562
  const a = {
560
563
  ...n,
561
564
  ...e.header
562
- }, i = Te(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
565
+ }, i = _e(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
563
566
  let o = !0;
564
567
  if (i.has("b64") && (o = n.b64, typeof o != "boolean"))
565
568
  throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
@@ -568,23 +571,23 @@ async function xe(e, t, r) {
568
571
  throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
569
572
  const s = r && Ie("algorithms", r.algorithms);
570
573
  if (s && !s.has(c))
571
- throw new ie('"alg" (Algorithm) Header Parameter value not allowed');
574
+ throw new ce('"alg" (Algorithm) Header Parameter value not allowed');
572
575
  if (o) {
573
576
  if (typeof e.payload != "string")
574
577
  throw new u("JWS Payload must be a string");
575
578
  } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
576
579
  throw new u("JWS Payload must be a string or an Uint8Array instance");
577
580
  let p = !1;
578
- typeof t == "function" && (t = await t(n, e), p = !0), ve(c, t, "verify");
579
- const g = ae(v.encode(e.protected ?? ""), v.encode("."), typeof e.payload == "string" ? v.encode(e.payload) : e.payload);
581
+ typeof t == "function" && (t = await t(n, e), p = !0), Te(c, t, "verify");
582
+ const g = oe(v.encode(e.protected ?? ""), v.encode("."), typeof e.payload == "string" ? v.encode(e.payload) : e.payload);
580
583
  let f;
581
584
  try {
582
585
  f = b(e.signature);
583
586
  } catch {
584
587
  throw new u("Failed to base64url decode the signature");
585
588
  }
586
- if (!await Pe(c, t, f, g))
587
- throw new ce();
589
+ if (!await Ke(c, t, f, g))
590
+ throw new se();
588
591
  let y;
589
592
  if (o)
590
593
  try {
@@ -596,17 +599,17 @@ async function xe(e, t, r) {
596
599
  const l = { payload: y };
597
600
  return e.protected !== void 0 && (l.protectedHeader = n), e.header !== void 0 && (l.unprotectedHeader = e.header), p ? { ...l, key: t } : l;
598
601
  }
599
- async function Ke(e, t, r) {
602
+ async function xe(e, t, r) {
600
603
  if (e instanceof Uint8Array && (e = C.decode(e)), typeof e != "string")
601
604
  throw new u("Compact JWS must be a string or Uint8Array");
602
605
  const { 0: n, 1: a, 2: i, length: o } = e.split(".");
603
606
  if (o !== 3)
604
607
  throw new u("Invalid Compact JWS");
605
- const c = await xe({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
608
+ const c = await Oe({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
606
609
  return typeof t == "function" ? { ...s, key: c.key } : s;
607
610
  }
608
- const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te * 24, We = H * 7, Je = H * 365.25, De = /^(\+|\-)? ?(\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) => {
609
- const t = De.exec(e);
611
+ 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) => {
612
+ const t = He.exec(e);
610
613
  if (!t || t[4] && t[1])
611
614
  throw new TypeError("Invalid time period format");
612
615
  const r = parseFloat(t[2]), n = t[3].toLowerCase();
@@ -641,14 +644,14 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
641
644
  case "week":
642
645
  case "weeks":
643
646
  case "w":
644
- a = Math.round(r * We);
647
+ a = Math.round(r * Je);
645
648
  break;
646
649
  default:
647
- a = Math.round(r * Je);
650
+ a = Math.round(r * De);
648
651
  break;
649
652
  }
650
653
  return t[1] === "-" || t[4] === "ago" ? -a : a;
651
- }, V = (e) => e.toLowerCase().replace(/^application\//, ""), He = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1, Ue = (e, t, r = {}) => {
654
+ }, B = (e) => e.toLowerCase().replace(/^application\//, ""), Ne = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1, Ue = (e, t, r = {}) => {
652
655
  let n;
653
656
  try {
654
657
  n = JSON.parse(C.decode(t));
@@ -657,7 +660,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
657
660
  if (!P(n))
658
661
  throw new S("JWT Claims Set must be a top-level JSON object");
659
662
  const { typ: a } = r;
660
- if (a && (typeof e.typ != "string" || V(e.typ) !== V(a)))
663
+ if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
661
664
  throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
662
665
  const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: p } = r, g = [...i];
663
666
  p !== void 0 && g.push("iat"), s !== void 0 && g.push("aud"), c !== void 0 && g.push("sub"), o !== void 0 && g.push("iss");
@@ -668,7 +671,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
668
671
  throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
669
672
  if (c && n.sub !== c)
670
673
  throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
671
- if (s && !He(n.aud, typeof s == "string" ? [s] : s))
674
+ if (s && !Ne(n.aud, typeof s == "string" ? [s] : s))
672
675
  throw new h('unexpected "aud" claim value', n, "aud", "check_failed");
673
676
  let f;
674
677
  switch (typeof r.clockTolerance) {
@@ -684,7 +687,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
684
687
  default:
685
688
  throw new TypeError("Invalid clockTolerance option type");
686
689
  }
687
- const { currentDate: U } = r, y = Oe(U || /* @__PURE__ */ new Date());
690
+ const { currentDate: N } = r, y = We(N || /* @__PURE__ */ new Date());
688
691
  if ((n.iat !== void 0 || p) && typeof n.iat != "number")
689
692
  throw new h('"iat" claim must be a number', n, "iat", "invalid");
690
693
  if (n.nbf !== void 0) {
@@ -700,24 +703,24 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
700
703
  throw new $('"exp" claim timestamp check failed', n, "exp", "check_failed");
701
704
  }
702
705
  if (p) {
703
- const l = y - n.iat, K = typeof p == "number" ? p : k(p);
704
- if (l - f > K)
706
+ const l = y - n.iat, O = typeof p == "number" ? p : k(p);
707
+ if (l - f > O)
705
708
  throw new $('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
706
709
  if (l < 0 - f)
707
710
  throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
708
711
  }
709
712
  return n;
710
713
  };
711
- async function Ne(e, t, r) {
714
+ async function $e(e, t, r) {
712
715
  var o;
713
- const n = await Ke(e, t, r);
716
+ const n = await xe(e, t, r);
714
717
  if ((o = n.protectedHeader.crit) != null && o.includes("b64") && n.protectedHeader.b64 === !1)
715
718
  throw new S("JWTs MUST NOT use unencoded payload");
716
719
  const i = { payload: Ue(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
717
720
  return typeof t == "function" ? { ...i, key: n.key } : i;
718
721
  }
719
- const $e = b;
720
- function Le(e) {
722
+ const Le = b;
723
+ function Me(e) {
721
724
  if (typeof e != "string")
722
725
  throw new S("JWTs must use Compact JWS serialization, JWT must be a string");
723
726
  const { 1: t, length: r } = e.split(".");
@@ -729,7 +732,7 @@ function Le(e) {
729
732
  throw new S("JWTs must contain a payload");
730
733
  let n;
731
734
  try {
732
- n = $e(t);
735
+ n = Le(t);
733
736
  } catch {
734
737
  throw new S("Failed to base64url decode the payload");
735
738
  }
@@ -743,18 +746,18 @@ function Le(e) {
743
746
  throw new S("Invalid JWT Claims Set");
744
747
  return a;
745
748
  }
746
- const et = async (e) => {
749
+ const rt = async (e) => {
747
750
  try {
748
- const t = N.ALG, n = await Ae(ne, t);
749
- return await Ne(e, n, {
750
- issuer: N.ISSUER
751
+ const t = U.ALG, n = await be(ae, t);
752
+ return await $e(e, n, {
753
+ issuer: U.ISSUER
751
754
  });
752
755
  } catch {
753
756
  return;
754
757
  }
755
- }, tt = (e) => {
758
+ }, nt = (e) => {
756
759
  try {
757
- return Le(e);
760
+ return Me(e);
758
761
  } catch {
759
762
  return;
760
763
  }
@@ -762,48 +765,48 @@ const et = async (e) => {
762
765
  var d = [];
763
766
  for (var D = 0; D < 256; ++D)
764
767
  d.push((D + 256).toString(16).slice(1));
765
- function Me(e, t = 0) {
768
+ function ke(e, t = 0) {
766
769
  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();
767
770
  }
768
- var I, ke = new Uint8Array(16);
769
- function Ve() {
770
- if (!I && (I = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !I))
771
+ var _, Be = new Uint8Array(16);
772
+ function Fe() {
773
+ if (!_ && (_ = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !_))
771
774
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
772
- return I(ke);
775
+ return _(Be);
773
776
  }
774
- var Be = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
775
- const B = {
776
- randomUUID: Be
777
+ var Ve = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
778
+ const F = {
779
+ randomUUID: Ve
777
780
  };
778
- function F(e, t, r) {
779
- if (B.randomUUID && !t && !e)
780
- return B.randomUUID();
781
+ function V(e, t, r) {
782
+ if (F.randomUUID && !t && !e)
783
+ return F.randomUUID();
781
784
  e = e || {};
782
- var n = e.random || (e.rng || Ve)();
783
- return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Me(n);
785
+ var n = e.random || (e.rng || Fe)();
786
+ return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, ke(n);
784
787
  }
785
- const q = globalThis.crypto, Fe = (e) => `${F()}${F()}`.slice(0, e), qe = (e) => btoa(
788
+ const Y = globalThis.crypto, Ye = (e) => `${V()}${V()}`.slice(0, e), qe = (e) => btoa(
786
789
  [...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
787
790
  );
788
791
  async function re(e) {
789
- if (!q.subtle)
792
+ if (!Y.subtle)
790
793
  throw new Error(
791
794
  "crypto.subtle is available only in secure contexts (HTTPS)."
792
795
  );
793
- const t = new TextEncoder().encode(e), r = await q.subtle.digest("SHA-256", t);
796
+ const t = new TextEncoder().encode(e), r = await Y.subtle.digest("SHA-256", t);
794
797
  return qe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
795
798
  }
796
- async function rt(e) {
799
+ async function at(e) {
797
800
  const t = e || 43;
798
801
  if (t < 43 || t > 128)
799
802
  throw `Expected a length between 43 and 128. Received ${e}.`;
800
- const r = Fe(t), n = await re(r);
803
+ const r = Ye(t), n = await re(r);
801
804
  return {
802
805
  code_verifier: r,
803
806
  code_challenge: n
804
807
  };
805
808
  }
806
- async function nt(e, t) {
809
+ async function ot(e, t) {
807
810
  return t === await re(e);
808
811
  }
809
812
  const ze = /^Bearer (.+)$/i, Ge = (e) => {
@@ -812,28 +815,33 @@ const ze = /^Bearer (.+)$/i, Ge = (e) => {
812
815
  const t = e.authorization.match(ze);
813
816
  if (t)
814
817
  return t[1];
815
- }, Ye = (e, t) => {
818
+ }, Qe = (e, t) => {
816
819
  const r = e.cookie, n = new RegExp(`auth.${t}=(.+?)(?:;|$)`);
817
820
  if (typeof r != "string")
818
821
  return;
819
822
  const a = r.match(n);
820
823
  if (a)
821
824
  return a[1];
822
- }, at = (e, t) => {
823
- const r = Ge(e), n = Ye(e, t);
824
- return !n && !r ? "" : n || r;
825
+ }, Xe = (e) => {
826
+ const t = e == null ? void 0 : e[ne.ACCESS_TOKEN];
827
+ if (typeof t == "string")
828
+ return t;
829
+ }, it = ({ headers: e, body: t, clientId: r }) => {
830
+ const n = Ge(e), a = Qe(e, r);
831
+ return Xe(t) || a || n || "";
825
832
  };
826
833
  export {
827
- je as API_TYPE,
828
- Qe as AUTH_TYPES,
829
- Xe as HEADERS,
830
- N as JWT,
831
- ne as JWT_PUBLIC_KEY,
832
- Ze as TOKEN_EXPIRATION,
833
- tt as decodeToken,
834
+ tt as API_TYPE,
835
+ Ze as AUTH_TYPES,
836
+ ne as BODY,
837
+ je as HEADERS,
838
+ U as JWT,
839
+ ae as JWT_PUBLIC_KEY,
840
+ et as TOKEN_EXPIRATION,
841
+ nt as decodeToken,
834
842
  re as generateCodeChallenge,
835
- at as getToken,
836
- rt as pkceChallengePair,
837
- et as verifyAndExtractToken,
838
- nt as verifyChallenge
843
+ it as getToken,
844
+ at as pkceChallengePair,
845
+ rt as verifyAndExtractToken,
846
+ ot as verifyChallenge
839
847
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-common",
3
- "version": "2.12.0",
3
+ "version": "3.0.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": "ae50d0965f6c30ea5bb8ace592c3cd99e736803e"
39
+ "gitHead": "ce92ce0065a9faf54d5c498ff63a195b249043af"
40
40
  }