@versini/auth-common 2.10.1 → 2.12.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
@@ -6,6 +6,7 @@ declare const AUTH_TYPES: {
6
6
  ID_AND_ACCESS_TOKEN: string;
7
7
  CODE: string;
8
8
  REFRESH_TOKEN: string;
9
+ PASSKEY: string;
9
10
  };
10
11
  declare const HEADERS: {
11
12
  CLIENT_ID: string;
@@ -59,4 +60,18 @@ declare function pkceChallengePair(length?: number): Promise<{
59
60
  */
60
61
  declare function verifyChallenge(code_verifier: string, expectedChallenge: string): Promise<boolean>;
61
62
 
62
- export { API_TYPE, AUTH_TYPES, HEADERS, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
63
+ type HeadersLike = Record<string, unknown> & {
64
+ authorization?: string;
65
+ "content-type"?: string;
66
+ cookie?: string;
67
+ };
68
+ /**
69
+ * Get a Bearer Token from a request.
70
+ *
71
+ * @param headers An object containing the request headers, usually `req.headers`.
72
+ * @param clientId The client ID to use.
73
+ *
74
+ */
75
+ declare const getToken: (headers: HeadersLike, clientId: string) => string;
76
+
77
+ export { API_TYPE, AUTH_TYPES, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
package/dist/index.js CHANGED
@@ -1,23 +1,24 @@
1
1
  /*!
2
- @versini/auth-common v2.10.1
2
+ @versini/auth-common v2.12.0
3
3
  © 2024 gizmette.com
4
4
  */
5
5
  try {
6
6
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
7
- version: "2.10.1",
8
- buildTime: "06/29/2024 01:31 PM EDT",
7
+ version: "2.12.0",
8
+ buildTime: "07/15/2024 09:28 AM EDT",
9
9
  homepage: "https://github.com/aversini/auth-client",
10
10
  license: "MIT"
11
11
  });
12
12
  } catch {
13
13
  }
14
- const Ge = {
14
+ const Qe = {
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
- REFRESH_TOKEN: "refresh_token"
20
- }, Ye = {
19
+ REFRESH_TOKEN: "refresh_token",
20
+ PASSKEY: "passkey"
21
+ }, Xe = {
21
22
  CLIENT_ID: "X-Auth-ClientId"
22
23
  }, N = {
23
24
  ALG: "RS256",
@@ -34,15 +35,15 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
34
35
  l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
35
36
  sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
36
37
  awIDAQAB
37
- -----END PUBLIC KEY-----`, ze = {
38
+ -----END PUBLIC KEY-----`, Ze = {
38
39
  ACCESS: "5m",
39
40
  ID: "90d",
40
41
  REFRESH: "90d"
41
- }, Qe = {
42
+ }, je = {
42
43
  AUTHENTICATE: "authenticate",
43
44
  CODE: "code",
44
45
  LOGOUT: "logout"
45
- }, x = crypto, G = (e) => e instanceof CryptoKey, v = new TextEncoder(), C = new TextDecoder();
46
+ }, x = crypto, z = (e) => e instanceof CryptoKey, v = new TextEncoder(), C = new TextDecoder();
46
47
  function ae(...e) {
47
48
  const t = e.reduce((a, { length: i }) => a + i, 0), r = new Uint8Array(t);
48
49
  let n = 0;
@@ -73,7 +74,7 @@ class A extends Error {
73
74
  super(t), this.code = "ERR_JOSE_GENERIC", this.name = this.constructor.name, (r = Error.captureStackTrace) == null || r.call(Error, this, this.constructor);
74
75
  }
75
76
  }
76
- class f extends A {
77
+ class h extends A {
77
78
  static get code() {
78
79
  return "ERR_JWT_CLAIM_VALIDATION_FAILED";
79
80
  }
@@ -156,8 +157,7 @@ function de(e, t) {
156
157
  if (t.length > 2) {
157
158
  const n = t.pop();
158
159
  r += `one of ${t.join(", ")}, or ${n}.`;
159
- } else
160
- t.length === 2 ? r += `one of ${t[0]} or ${t[1]}.` : r += `${t[0]}.`;
160
+ } else t.length === 2 ? r += `one of ${t[0]} or ${t[1]}.` : r += `${t[0]}.`;
161
161
  throw new TypeError(r);
162
162
  }
163
163
  }
@@ -213,20 +213,19 @@ function ue(e, t, ...r) {
213
213
  }
214
214
  de(e, r);
215
215
  }
216
- function Y(e, t, ...r) {
216
+ function G(e, t, ...r) {
217
217
  var n;
218
218
  if (r.length > 2) {
219
219
  const a = r.pop();
220
220
  e += `one of type ${r.join(", ")}, or ${a}.`;
221
- } else
222
- r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
221
+ } else r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
223
222
  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;
224
223
  }
225
- const L = (e, ...t) => Y("Key must be ", e, ...t);
226
- function z(e, t, ...r) {
227
- return Y(`Key for the ${e} algorithm must be `, t, ...r);
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);
228
227
  }
229
- const Q = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", _ = ["CryptoKey"], le = (...e) => {
228
+ const Q = (e) => z(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", _ = ["CryptoKey"], le = (...e) => {
230
229
  const t = e.filter(Boolean);
231
230
  if (t.length === 0 || t.length === 1)
232
231
  return !0;
@@ -245,11 +244,11 @@ const Q = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
245
244
  }
246
245
  return !0;
247
246
  };
248
- function he(e) {
247
+ function fe(e) {
249
248
  return typeof e == "object" && e !== null;
250
249
  }
251
250
  function P(e) {
252
- if (!he(e) || Object.prototype.toString.call(e) !== "[object Object]")
251
+ if (!fe(e) || Object.prototype.toString.call(e) !== "[object Object]")
253
252
  return !1;
254
253
  if (Object.getPrototypeOf(e) === null)
255
254
  return !0;
@@ -258,7 +257,7 @@ function P(e) {
258
257
  t = Object.getPrototypeOf(t);
259
258
  return Object.getPrototypeOf(e) === t;
260
259
  }
261
- const fe = (e, t) => {
260
+ const he = (e, t) => {
262
261
  if (e.startsWith("RS") || e.startsWith("PS")) {
263
262
  const { modulusLength: r } = t.algorithm;
264
263
  if (typeof r != "number" || r < 2048)
@@ -448,13 +447,13 @@ async function Ae(e, t, r) {
448
447
  const R = (e) => e == null ? void 0 : e[Symbol.toStringTag], be = (e, t) => {
449
448
  if (!(t instanceof Uint8Array)) {
450
449
  if (!Q(t))
451
- throw new TypeError(z(e, t, ..._, "Uint8Array"));
450
+ throw new TypeError(Y(e, t, ..._, "Uint8Array"));
452
451
  if (t.type !== "secret")
453
452
  throw new TypeError(`${R(t)} instances for symmetric algorithms must be of type "secret"`);
454
453
  }
455
454
  }, Ce = (e, t, r) => {
456
455
  if (!Q(t))
457
- throw new TypeError(z(e, t, ..._));
456
+ throw new TypeError(Y(e, t, ..._));
458
457
  if (t.type === "secret")
459
458
  throw new TypeError(`${R(t)} instances for asymmetric algorithms must not be of type "secret"`);
460
459
  if (t.algorithm && r === "verify" && t.type === "private")
@@ -515,7 +514,7 @@ function Re(e, t) {
515
514
  }
516
515
  }
517
516
  async function _e(e, t, r) {
518
- if (t = await Ee.normalizePublicKey(t, e), G(t))
517
+ if (t = await Ee.normalizePublicKey(t, e), z(t))
519
518
  return ue(t, e, r), t;
520
519
  if (t instanceof Uint8Array) {
521
520
  if (!e.startsWith("HS"))
@@ -526,7 +525,7 @@ async function _e(e, t, r) {
526
525
  }
527
526
  const Pe = async (e, t, r, n) => {
528
527
  const a = await _e(e, t, "verify");
529
- fe(e, a);
528
+ he(e, a);
530
529
  const i = Re(e, a.algorithm);
531
530
  try {
532
531
  return await x.subtle.verify(i, a, r, n);
@@ -578,13 +577,13 @@ async function xe(e, t, r) {
578
577
  let p = !1;
579
578
  typeof t == "function" && (t = await t(n, e), p = !0), ve(c, t, "verify");
580
579
  const g = ae(v.encode(e.protected ?? ""), v.encode("."), typeof e.payload == "string" ? v.encode(e.payload) : e.payload);
581
- let h;
580
+ let f;
582
581
  try {
583
- h = b(e.signature);
582
+ f = b(e.signature);
584
583
  } catch {
585
584
  throw new u("Failed to base64url decode the signature");
586
585
  }
587
- if (!await Pe(c, t, h, g))
586
+ if (!await Pe(c, t, f, g))
588
587
  throw new ce();
589
588
  let y;
590
589
  if (o)
@@ -593,8 +592,7 @@ async function xe(e, t, r) {
593
592
  } catch {
594
593
  throw new u("Failed to base64url decode the payload");
595
594
  }
596
- else
597
- typeof e.payload == "string" ? y = v.encode(e.payload) : y = e.payload;
595
+ else typeof e.payload == "string" ? y = v.encode(e.payload) : y = e.payload;
598
596
  const l = { payload: y };
599
597
  return e.protected !== void 0 && (l.protectedHeader = n), e.header !== void 0 && (l.unprotectedHeader = e.header), p ? { ...l, key: t } : l;
600
598
  }
@@ -607,7 +605,7 @@ async function Ke(e, t, r) {
607
605
  const c = await xe({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
608
606
  return typeof t == "function" ? { ...s, key: c.key } : s;
609
607
  }
610
- 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, V = (e) => {
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) => {
611
609
  const t = De.exec(e);
612
610
  if (!t || t[4] && t[1])
613
611
  throw new TypeError("Invalid time period format");
@@ -650,7 +648,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
650
648
  break;
651
649
  }
652
650
  return t[1] === "-" || t[4] === "ago" ? -a : a;
653
- }, k = (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 = {}) => {
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
652
  let n;
655
653
  try {
656
654
  n = JSON.parse(C.decode(t));
@@ -659,54 +657,54 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
659
657
  if (!P(n))
660
658
  throw new S("JWT Claims Set must be a top-level JSON object");
661
659
  const { typ: a } = r;
662
- if (a && (typeof e.typ != "string" || k(e.typ) !== k(a)))
663
- throw new f('unexpected "typ" JWT header value', n, "typ", "check_failed");
660
+ if (a && (typeof e.typ != "string" || V(e.typ) !== V(a)))
661
+ throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
664
662
  const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: p } = r, g = [...i];
665
663
  p !== void 0 && g.push("iat"), s !== void 0 && g.push("aud"), c !== void 0 && g.push("sub"), o !== void 0 && g.push("iss");
666
664
  for (const l of new Set(g.reverse()))
667
665
  if (!(l in n))
668
- throw new f(`missing required "${l}" claim`, n, l, "missing");
666
+ throw new h(`missing required "${l}" claim`, n, l, "missing");
669
667
  if (o && !(Array.isArray(o) ? o : [o]).includes(n.iss))
670
- throw new f('unexpected "iss" claim value', n, "iss", "check_failed");
668
+ throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
671
669
  if (c && n.sub !== c)
672
- throw new f('unexpected "sub" claim value', n, "sub", "check_failed");
670
+ throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
673
671
  if (s && !He(n.aud, typeof s == "string" ? [s] : s))
674
- throw new f('unexpected "aud" claim value', n, "aud", "check_failed");
675
- let h;
672
+ throw new h('unexpected "aud" claim value', n, "aud", "check_failed");
673
+ let f;
676
674
  switch (typeof r.clockTolerance) {
677
675
  case "string":
678
- h = V(r.clockTolerance);
676
+ f = k(r.clockTolerance);
679
677
  break;
680
678
  case "number":
681
- h = r.clockTolerance;
679
+ f = r.clockTolerance;
682
680
  break;
683
681
  case "undefined":
684
- h = 0;
682
+ f = 0;
685
683
  break;
686
684
  default:
687
685
  throw new TypeError("Invalid clockTolerance option type");
688
686
  }
689
687
  const { currentDate: U } = r, y = Oe(U || /* @__PURE__ */ new Date());
690
688
  if ((n.iat !== void 0 || p) && typeof n.iat != "number")
691
- throw new f('"iat" claim must be a number', n, "iat", "invalid");
689
+ throw new h('"iat" claim must be a number', n, "iat", "invalid");
692
690
  if (n.nbf !== void 0) {
693
691
  if (typeof n.nbf != "number")
694
- throw new f('"nbf" claim must be a number', n, "nbf", "invalid");
695
- if (n.nbf > y + h)
696
- throw new f('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
692
+ throw new h('"nbf" claim must be a number', n, "nbf", "invalid");
693
+ if (n.nbf > y + f)
694
+ throw new h('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
697
695
  }
698
696
  if (n.exp !== void 0) {
699
697
  if (typeof n.exp != "number")
700
- throw new f('"exp" claim must be a number', n, "exp", "invalid");
701
- if (n.exp <= y - h)
698
+ throw new h('"exp" claim must be a number', n, "exp", "invalid");
699
+ if (n.exp <= y - f)
702
700
  throw new $('"exp" claim timestamp check failed', n, "exp", "check_failed");
703
701
  }
704
702
  if (p) {
705
- const l = y - n.iat, K = typeof p == "number" ? p : V(p);
706
- if (l - h > K)
703
+ const l = y - n.iat, K = typeof p == "number" ? p : k(p);
704
+ if (l - f > K)
707
705
  throw new $('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
708
- if (l < 0 - h)
709
- throw new f('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
706
+ if (l < 0 - f)
707
+ throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
710
708
  }
711
709
  return n;
712
710
  };
@@ -745,7 +743,7 @@ function Le(e) {
745
743
  throw new S("Invalid JWT Claims Set");
746
744
  return a;
747
745
  }
748
- const Xe = async (e) => {
746
+ const et = async (e) => {
749
747
  try {
750
748
  const t = N.ALG, n = await Ae(ne, t);
751
749
  return await Ne(e, n, {
@@ -754,7 +752,7 @@ const Xe = async (e) => {
754
752
  } catch {
755
753
  return;
756
754
  }
757
- }, Ze = (e) => {
755
+ }, tt = (e) => {
758
756
  try {
759
757
  return Le(e);
760
758
  } catch {
@@ -767,11 +765,11 @@ for (var D = 0; D < 256; ++D)
767
765
  function Me(e, t = 0) {
768
766
  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();
769
767
  }
770
- var I, Ve = new Uint8Array(16);
771
- function ke() {
768
+ var I, ke = new Uint8Array(16);
769
+ function Ve() {
772
770
  if (!I && (I = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !I))
773
771
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
774
- return I(Ve);
772
+ return I(ke);
775
773
  }
776
774
  var Be = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
777
775
  const B = {
@@ -781,7 +779,7 @@ function F(e, t, r) {
781
779
  if (B.randomUUID && !t && !e)
782
780
  return B.randomUUID();
783
781
  e = e || {};
784
- var n = e.random || (e.rng || ke)();
782
+ var n = e.random || (e.rng || Ve)();
785
783
  return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Me(n);
786
784
  }
787
785
  const q = globalThis.crypto, Fe = (e) => `${F()}${F()}`.slice(0, e), qe = (e) => btoa(
@@ -795,7 +793,7 @@ async function re(e) {
795
793
  const t = new TextEncoder().encode(e), r = await q.subtle.digest("SHA-256", t);
796
794
  return qe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
797
795
  }
798
- async function je(e) {
796
+ async function rt(e) {
799
797
  const t = e || 43;
800
798
  if (t < 43 || t > 128)
801
799
  throw `Expected a length between 43 and 128. Received ${e}.`;
@@ -805,19 +803,37 @@ async function je(e) {
805
803
  code_challenge: n
806
804
  };
807
805
  }
808
- async function et(e, t) {
806
+ async function nt(e, t) {
809
807
  return t === await re(e);
810
808
  }
809
+ const ze = /^Bearer (.+)$/i, Ge = (e) => {
810
+ if (typeof e.authorization != "string")
811
+ return;
812
+ const t = e.authorization.match(ze);
813
+ if (t)
814
+ return t[1];
815
+ }, Ye = (e, t) => {
816
+ const r = e.cookie, n = new RegExp(`auth.${t}=(.+?)(?:;|$)`);
817
+ if (typeof r != "string")
818
+ return;
819
+ const a = r.match(n);
820
+ if (a)
821
+ return a[1];
822
+ }, at = (e, t) => {
823
+ const r = Ge(e), n = Ye(e, t);
824
+ return !n && !r ? "" : n || r;
825
+ };
811
826
  export {
812
- Qe as API_TYPE,
813
- Ge as AUTH_TYPES,
814
- Ye as HEADERS,
827
+ je as API_TYPE,
828
+ Qe as AUTH_TYPES,
829
+ Xe as HEADERS,
815
830
  N as JWT,
816
831
  ne as JWT_PUBLIC_KEY,
817
- ze as TOKEN_EXPIRATION,
818
- Ze as decodeToken,
832
+ Ze as TOKEN_EXPIRATION,
833
+ tt as decodeToken,
819
834
  re as generateCodeChallenge,
820
- je as pkceChallengePair,
821
- Xe as verifyAndExtractToken,
822
- et as verifyChallenge
835
+ at as getToken,
836
+ rt as pkceChallengePair,
837
+ et as verifyAndExtractToken,
838
+ nt as verifyChallenge
823
839
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-common",
3
- "version": "2.10.1",
3
+ "version": "2.12.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -29,11 +29,12 @@
29
29
  "lint": "biome lint src",
30
30
  "start": "static-server dist --port 5173",
31
31
  "test:watch": "vitest",
32
- "test": "vitest run"
32
+ "test": "vitest run",
33
+ "test:coverage": "vitest run --coverage"
33
34
  },
34
35
  "dependencies": {
35
- "jose": "5.6.2",
36
+ "jose": "5.6.3",
36
37
  "uuid": "10.0.0"
37
38
  },
38
- "gitHead": "b1190439a142ac9c34d7744273ed3cebbccabc15"
39
+ "gitHead": "ae50d0965f6c30ea5bb8ace592c3cd99e736803e"
39
40
  }