@versini/auth-common 3.3.1 → 4.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
@@ -34,9 +34,10 @@ declare const TOKEN_EXPIRATION: {
34
34
  REFRESH: string;
35
35
  };
36
36
  declare const API_TYPE: {
37
- AUTHENTICATE: string;
38
37
  CODE: string;
39
38
  LOGOUT: string;
39
+ LOGIN: string;
40
+ REFRESH: string;
40
41
  };
41
42
 
42
43
  declare const verifyAndExtractToken: (token: string) => Promise<jose.JWTVerifyResult<jose.JWTPayload> | undefined>;
@@ -134,4 +135,17 @@ type ScopesGrants = {
134
135
  */
135
136
  declare const isGranted: (token: string, scopes: ScopesGrants) => Promise<boolean>;
136
137
 
137
- export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, type ScopesGrants, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, isGranted, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
138
+ /**
139
+ * Get a Session Id from a request.
140
+ *
141
+ * @param headers An object containing the request headers, usually `req.headers`.
142
+ * @param clientId The client ID to use.
143
+ *
144
+ */
145
+ type GetSessionProps = {
146
+ clientId: string;
147
+ headers: HeadersLike;
148
+ };
149
+ declare const getSession: ({ headers, clientId }: GetSessionProps) => string;
150
+
151
+ export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, type ScopesGrants, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getSession, getToken, isGranted, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
package/dist/index.js CHANGED
@@ -1,28 +1,28 @@
1
1
  /*!
2
- @versini/auth-common v3.3.1
2
+ @versini/auth-common v4.0.0
3
3
  © 2024 gizmette.com
4
4
  */
5
5
  try {
6
6
  window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
7
- version: "3.3.1",
8
- buildTime: "08/01/2024 10:23 AM EDT",
7
+ version: "4.0.0",
8
+ buildTime: "08/19/2024 06:11 PM EDT",
9
9
  homepage: "https://github.com/aversini/auth-client",
10
10
  license: "MIT"
11
11
  });
12
12
  } catch {
13
13
  }
14
- const je = {
14
+ const et = {
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
- }, et = {
21
+ }, tt = {
22
22
  CLIENT_ID: "X-Auth-ClientId"
23
23
  }, ne = {
24
24
  ACCESS_TOKEN: "access_token"
25
- }, P = {
25
+ }, I = {
26
26
  ALG: "RS256",
27
27
  USER_ID_KEY: "sub",
28
28
  TOKEN_ID_KEY: "__raw",
@@ -42,15 +42,16 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
42
42
  l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
43
43
  sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
44
44
  awIDAQAB
45
- -----END PUBLIC KEY-----`, tt = {
45
+ -----END PUBLIC KEY-----`, rt = {
46
46
  ACCESS: "5m",
47
47
  ID: "90d",
48
48
  REFRESH: "90d"
49
- }, rt = {
50
- AUTHENTICATE: "authenticate",
49
+ }, nt = {
51
50
  CODE: "code",
52
- LOGOUT: "logout"
53
- }, O = crypto, G = (e) => e instanceof CryptoKey, T = new TextEncoder(), C = new TextDecoder();
51
+ LOGOUT: "logout",
52
+ LOGIN: "login",
53
+ REFRESH: "refresh"
54
+ }, O = crypto, G = (e) => e instanceof CryptoKey, _ = new TextEncoder(), C = new TextDecoder();
54
55
  function oe(...e) {
55
56
  const t = e.reduce((a, { length: o }) => a + o, 0), r = new Uint8Array(t);
56
57
  let n = 0;
@@ -140,7 +141,7 @@ class se extends A {
140
141
  function m(e, t = "algorithm.name") {
141
142
  return new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`);
142
143
  }
143
- function _(e, t) {
144
+ function v(e, t) {
144
145
  return e.name === t;
145
146
  }
146
147
  function W(e) {
@@ -173,7 +174,7 @@ function le(e, t, ...r) {
173
174
  case "HS256":
174
175
  case "HS384":
175
176
  case "HS512": {
176
- if (!_(e.algorithm, "HMAC"))
177
+ if (!v(e.algorithm, "HMAC"))
177
178
  throw m("HMAC");
178
179
  const n = parseInt(t.slice(2), 10);
179
180
  if (W(e.algorithm.hash) !== n)
@@ -183,7 +184,7 @@ function le(e, t, ...r) {
183
184
  case "RS256":
184
185
  case "RS384":
185
186
  case "RS512": {
186
- if (!_(e.algorithm, "RSASSA-PKCS1-v1_5"))
187
+ if (!v(e.algorithm, "RSASSA-PKCS1-v1_5"))
187
188
  throw m("RSASSA-PKCS1-v1_5");
188
189
  const n = parseInt(t.slice(2), 10);
189
190
  if (W(e.algorithm.hash) !== n)
@@ -193,7 +194,7 @@ function le(e, t, ...r) {
193
194
  case "PS256":
194
195
  case "PS384":
195
196
  case "PS512": {
196
- if (!_(e.algorithm, "RSA-PSS"))
197
+ if (!v(e.algorithm, "RSA-PSS"))
197
198
  throw m("RSA-PSS");
198
199
  const n = parseInt(t.slice(2), 10);
199
200
  if (W(e.algorithm.hash) !== n)
@@ -208,7 +209,7 @@ function le(e, t, ...r) {
208
209
  case "ES256":
209
210
  case "ES384":
210
211
  case "ES512": {
211
- if (!_(e.algorithm, "ECDSA"))
212
+ if (!v(e.algorithm, "ECDSA"))
212
213
  throw m("ECDSA");
213
214
  const n = de(t);
214
215
  if (e.algorithm.namedCurve !== n)
@@ -232,7 +233,7 @@ const L = (e, ...t) => q("Key must be ", e, ...t);
232
233
  function z(e, t, ...r) {
233
234
  return q(`Key for the ${e} algorithm must be `, t, ...r);
234
235
  }
235
- const X = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"], fe = (...e) => {
236
+ const X = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", P = ["CryptoKey"], fe = (...e) => {
236
237
  const t = e.filter(Boolean);
237
238
  if (t.length === 0 || t.length === 1)
238
239
  return !0;
@@ -451,26 +452,26 @@ async function be(e, t, r) {
451
452
  throw new TypeError('"spki" must be SPKI formatted string');
452
453
  return Ae(e, t);
453
454
  }
454
- const I = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
455
+ const R = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
455
456
  if (!(t instanceof Uint8Array)) {
456
457
  if (!X(t))
457
- throw new TypeError(z(e, t, ...R, "Uint8Array"));
458
+ throw new TypeError(z(e, t, ...P, "Uint8Array"));
458
459
  if (t.type !== "secret")
459
- throw new TypeError(`${I(t)} instances for symmetric algorithms must be of type "secret"`);
460
+ throw new TypeError(`${R(t)} instances for symmetric algorithms must be of type "secret"`);
460
461
  }
461
- }, Te = (e, t, r) => {
462
+ }, _e = (e, t, r) => {
462
463
  if (!X(t))
463
- throw new TypeError(z(e, t, ...R));
464
+ throw new TypeError(z(e, t, ...P));
464
465
  if (t.type === "secret")
465
- throw new TypeError(`${I(t)} instances for asymmetric algorithms must not be of type "secret"`);
466
+ throw new TypeError(`${R(t)} instances for asymmetric algorithms must not be of type "secret"`);
466
467
  if (t.algorithm && r === "verify" && t.type === "private")
467
- throw new TypeError(`${I(t)} instances for asymmetric algorithm verifying must be of type "public"`);
468
+ throw new TypeError(`${R(t)} instances for asymmetric algorithm verifying must be of type "public"`);
468
469
  if (t.algorithm && r === "encrypt" && t.type === "private")
469
- throw new TypeError(`${I(t)} instances for asymmetric algorithm encryption must be of type "public"`);
470
- }, _e = (e, t, r) => {
471
- e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : Te(e, t, r);
470
+ throw new TypeError(`${R(t)} instances for asymmetric algorithm encryption must be of type "public"`);
471
+ }, ve = (e, t, r) => {
472
+ e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : _e(e, t, r);
472
473
  };
473
- function ve(e, t, r, n, a) {
474
+ function Te(e, t, r, n, a) {
474
475
  if (a.crit !== void 0 && (n == null ? void 0 : n.crit) === void 0)
475
476
  throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
476
477
  if (!n || n.crit === void 0)
@@ -489,13 +490,13 @@ function ve(e, t, r, n, a) {
489
490
  }
490
491
  return new Set(n.crit);
491
492
  }
492
- const Ie = (e, t) => {
493
+ const Re = (e, t) => {
493
494
  if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
494
495
  throw new TypeError(`"${e}" option must be an array of strings`);
495
496
  if (t)
496
497
  return new Set(t);
497
498
  };
498
- function Pe(e, t) {
499
+ function Ie(e, t) {
499
500
  const r = `SHA-${e.slice(-3)}`;
500
501
  switch (e) {
501
502
  case "HS256":
@@ -520,20 +521,20 @@ function Pe(e, t) {
520
521
  throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
521
522
  }
522
523
  }
523
- async function Re(e, t, r) {
524
+ async function Pe(e, t, r) {
524
525
  if (t = await we.normalizePublicKey(t, e), G(t))
525
526
  return le(t, e, r), t;
526
527
  if (t instanceof Uint8Array) {
527
528
  if (!e.startsWith("HS"))
528
- throw new TypeError(L(t, ...R));
529
+ throw new TypeError(L(t, ...P));
529
530
  return O.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
530
531
  }
531
- throw new TypeError(L(t, ...R, "Uint8Array"));
532
+ throw new TypeError(L(t, ...P, "Uint8Array"));
532
533
  }
533
534
  const Ke = async (e, t, r, n) => {
534
- const a = await Re(e, t, "verify");
535
+ const a = await Pe(e, t, "verify");
535
536
  pe(e, a);
536
- const o = Pe(e, a.algorithm);
537
+ const o = Ie(e, a.algorithm);
537
538
  try {
538
539
  return await O.subtle.verify(o, a, r, n);
539
540
  } catch {
@@ -566,14 +567,14 @@ async function Oe(e, t, r) {
566
567
  const a = {
567
568
  ...n,
568
569
  ...e.header
569
- }, o = ve(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
570
+ }, o = Te(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
570
571
  let i = !0;
571
572
  if (o.has("b64") && (i = n.b64, typeof i != "boolean"))
572
573
  throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
573
574
  const { alg: c } = a;
574
575
  if (typeof c != "string" || !c)
575
576
  throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
576
- const s = r && Ie("algorithms", r.algorithms);
577
+ const s = r && Re("algorithms", r.algorithms);
577
578
  if (s && !s.has(c))
578
579
  throw new ce('"alg" (Algorithm) Header Parameter value not allowed');
579
580
  if (i) {
@@ -582,8 +583,8 @@ async function Oe(e, t, r) {
582
583
  } else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
583
584
  throw new u("JWS Payload must be a string or an Uint8Array instance");
584
585
  let p = !1;
585
- typeof t == "function" && (t = await t(n, e), p = !0), _e(c, t, "verify");
586
- const g = oe(T.encode(e.protected ?? ""), T.encode("."), typeof e.payload == "string" ? T.encode(e.payload) : e.payload);
586
+ typeof t == "function" && (t = await t(n, e), p = !0), ve(c, t, "verify");
587
+ const g = oe(_.encode(e.protected ?? ""), _.encode("."), typeof e.payload == "string" ? _.encode(e.payload) : e.payload);
587
588
  let f;
588
589
  try {
589
590
  f = b(e.signature);
@@ -599,7 +600,7 @@ async function Oe(e, t, r) {
599
600
  } catch {
600
601
  throw new u("Failed to base64url decode the payload");
601
602
  }
602
- else typeof e.payload == "string" ? y = T.encode(e.payload) : y = e.payload;
603
+ else typeof e.payload == "string" ? y = _.encode(e.payload) : y = e.payload;
603
604
  const l = { payload: y };
604
605
  return e.protected !== void 0 && (l.protectedHeader = n), e.header !== void 0 && (l.unprotectedHeader = e.header), p ? { ...l, key: t } : l;
605
606
  }
@@ -752,14 +753,14 @@ function ke(e) {
752
753
  }
753
754
  const Me = async (e) => {
754
755
  try {
755
- const t = P.ALG, n = await be(ae, t);
756
+ const t = I.ALG, n = await be(ae, t);
756
757
  return await $e(e, n, {
757
- issuer: P.ISSUER
758
+ issuer: I.ISSUER
758
759
  });
759
760
  } catch {
760
761
  return;
761
762
  }
762
- }, nt = (e) => {
763
+ }, at = (e) => {
763
764
  try {
764
765
  return ke(e);
765
766
  } catch {
@@ -772,24 +773,24 @@ for (var H = 0; H < 256; ++H)
772
773
  function Be(e, t = 0) {
773
774
  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();
774
775
  }
775
- var v, Ye = new Uint8Array(16);
776
- function Fe() {
777
- if (!v && (v = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !v))
776
+ var T, Fe = new Uint8Array(16);
777
+ function Ye() {
778
+ if (!T && (T = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !T))
778
779
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
779
- return v(Ye);
780
+ return T(Fe);
780
781
  }
781
782
  var Ve = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
782
- const Y = {
783
+ const F = {
783
784
  randomUUID: Ve
784
785
  };
785
- function F(e, t, r) {
786
- if (Y.randomUUID && !t && !e)
787
- return Y.randomUUID();
786
+ function Y(e, t, r) {
787
+ if (F.randomUUID && !t && !e)
788
+ return F.randomUUID();
788
789
  e = e || {};
789
- var n = e.random || (e.rng || Fe)();
790
+ var n = e.random || (e.rng || Ye)();
790
791
  return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Be(n);
791
792
  }
792
- const V = globalThis.crypto, Ge = (e) => `${F()}${F()}`.slice(0, e), qe = (e) => btoa(
793
+ const V = globalThis.crypto, Ge = (e) => `${Y()}${Y()}`.slice(0, e), qe = (e) => btoa(
793
794
  [...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
794
795
  );
795
796
  async function re(e) {
@@ -800,7 +801,7 @@ async function re(e) {
800
801
  const t = new TextEncoder().encode(e), r = await V.subtle.digest("SHA-256", t);
801
802
  return qe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
802
803
  }
803
- async function at(e) {
804
+ async function ot(e) {
804
805
  const t = e || 43;
805
806
  if (t < 43 || t > 128)
806
807
  throw `Expected a length between 43 and 128. Received ${e}.`;
@@ -810,7 +811,7 @@ async function at(e) {
810
811
  code_challenge: n
811
812
  };
812
813
  }
813
- async function ot(e, t) {
814
+ async function it(e, t) {
814
815
  return t === await re(e);
815
816
  }
816
817
  const ze = /^Bearer (.+)$/i, Xe = (e) => {
@@ -830,32 +831,40 @@ const ze = /^Bearer (.+)$/i, Xe = (e) => {
830
831
  const t = e == null ? void 0 : e[ne.ACCESS_TOKEN];
831
832
  if (typeof t == "string")
832
833
  return t;
833
- }, it = ({ headers: e, body: t, clientId: r }) => {
834
+ }, ct = ({ headers: e, body: t, clientId: r }) => {
834
835
  const n = Xe(e), a = Qe(e, r);
835
836
  return Ze(t) || a || n || "";
836
- }, ct = async (e, t) => {
837
+ }, st = async (e, t) => {
837
838
  var a;
838
839
  const r = await Me(e);
839
- if (!r || !Array.isArray((a = r.payload) == null ? void 0 : a[P.SCOPES_KEY]))
840
+ if (!r || !Array.isArray((a = r.payload) == null ? void 0 : a[I.SCOPES_KEY]))
840
841
  return !1;
841
- const n = r.payload[P.SCOPES_KEY];
842
+ const n = r.payload[I.SCOPES_KEY];
842
843
  return Array.isArray(t) ? t.every((o) => n.includes(o)) : Object.keys(t).some(
843
844
  (o) => t[o].every((i) => n.includes(i))
844
845
  );
845
- };
846
+ }, je = (e, t) => {
847
+ const r = e == null ? void 0 : e.cookie;
848
+ if (typeof r != "string")
849
+ return;
850
+ const n = new RegExp(`auth.${t}.session=(.+?)(?:;|$)`), a = r.match(n);
851
+ if (a)
852
+ return a[1];
853
+ }, dt = ({ headers: e, clientId: t }) => je(e, t) || "";
846
854
  export {
847
- rt as API_TYPE,
848
- je as AUTH_TYPES,
855
+ nt as API_TYPE,
856
+ et as AUTH_TYPES,
849
857
  ne as BODY,
850
- et as HEADERS,
851
- P as JWT,
858
+ tt as HEADERS,
859
+ I as JWT,
852
860
  ae as JWT_PUBLIC_KEY,
853
- tt as TOKEN_EXPIRATION,
854
- nt as decodeToken,
861
+ rt as TOKEN_EXPIRATION,
862
+ at as decodeToken,
855
863
  re as generateCodeChallenge,
856
- it as getToken,
857
- ct as isGranted,
858
- at as pkceChallengePair,
864
+ dt as getSession,
865
+ ct as getToken,
866
+ st as isGranted,
867
+ ot as pkceChallengePair,
859
868
  Me as verifyAndExtractToken,
860
- ot as verifyChallenge
869
+ it as verifyChallenge
861
870
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/auth-common",
3
- "version": "3.3.1",
3
+ "version": "4.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -33,8 +33,8 @@
33
33
  "test:coverage": "vitest run --coverage"
34
34
  },
35
35
  "dependencies": {
36
- "jose": "5.6.3",
36
+ "jose": "5.7.0",
37
37
  "uuid": "10.0.0"
38
38
  },
39
- "gitHead": "01228321ca4f7b62244a5dc9b2b001053fd0bba2"
39
+ "gitHead": "810b2920c2281e78b52ffbe1db7e52f4fefa8cbe"
40
40
  }