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