cauthy-auth 0.24.0 → 0.24.2

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.mjs CHANGED
@@ -57,10 +57,29 @@ async function unseal(sealed, key) {
57
57
  // src/system.ts
58
58
  import { exportJWK, importJWK, importPKCS8, SignJWT } from "jose";
59
59
 
60
+ // src/url.ts
61
+ function withoutTrailingSlashes(s) {
62
+ let end = s.length;
63
+ while (end > 0 && s.charCodeAt(end - 1) === 47) end--;
64
+ return s.slice(0, end);
65
+ }
66
+
60
67
  // src/theme.ts
61
68
  var MAX_RADIUS = 40;
69
+ function withoutComments(css) {
70
+ let out2 = "";
71
+ let i = 0;
72
+ for (; ; ) {
73
+ const open = css.indexOf("/*", i);
74
+ if (open < 0) return out2 + css.slice(i);
75
+ out2 += css.slice(i, open);
76
+ const close = css.indexOf("*/", open + 2);
77
+ if (close < 0) return out2;
78
+ i = close + 2;
79
+ }
80
+ }
62
81
  function declarations(css) {
63
- const text = css.replace(/\/\*[\s\S]*?\*\//g, "");
82
+ const text = withoutComments(css);
64
83
  const out2 = [];
65
84
  const within = [];
66
85
  let chunk = "";
@@ -155,7 +174,11 @@ function tokenValue(value) {
155
174
  if (/^\(|[ ,(]\(/.test(v)) return null;
156
175
  return v;
157
176
  }
158
- var THEME_SCOPE = /^(?:\s*(?::root|html|body|\[data-(?:theme|mode|color-scheme)(?:=["']?[\w-]+["']?)?\])\s*,?)+$/i;
177
+ var THEME_SELECTOR = /^(?::root|html|body|\[data-(?:theme|mode|color-scheme)(?:=["']?[\w-]+["']?)?\])$/i;
178
+ var isThemeScope = (selectors) => {
179
+ const parts = selectors.split(/[\s,]+/).filter(Boolean);
180
+ return parts.length > 0 && parts.every((one) => THEME_SELECTOR.test(one));
181
+ };
159
182
  function tokensFromStackmyth(css, options = {}) {
160
183
  const mode = options.mode ?? "light";
161
184
  const all = (Array.isArray(css) ? css : [css]).flatMap(declarations);
@@ -163,7 +186,7 @@ function tokensFromStackmyth(css, options = {}) {
163
186
  const vars = /* @__PURE__ */ new Map();
164
187
  for (const d of all) {
165
188
  if (!applies(d, { mode, theme: named })) continue;
166
- if (!d.within.every((w) => w.startsWith("@") || THEME_SCOPE.test(w))) continue;
189
+ if (!d.within.every((w) => w.startsWith("@") || isThemeScope(w))) continue;
167
190
  vars.set(d.name, d.value);
168
191
  }
169
192
  const inline = (value, depth = 0) => {
@@ -385,7 +408,7 @@ function fromWire(wire) {
385
408
  }
386
409
  async function pushTheme(o) {
387
410
  const env4 = (n) => typeof process !== "undefined" ? process.env?.[n] : void 0;
388
- const issuer = (o.issuer ?? env4("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
411
+ const issuer = withoutTrailingSlashes(o.issuer ?? env4("CAUTHY_ISSUER") ?? "");
389
412
  const clientId = o.clientId ?? env4("CAUTHY_CLIENT_ID") ?? "";
390
413
  const tenant = o.tenant ?? env4("CAUTHY_TENANT") ?? "";
391
414
  const token = o.token ?? env4("CAUTHY_TOKEN") ?? "";
@@ -453,7 +476,7 @@ async function loadKey(k) {
453
476
  return jwk ? importJWK(jwk, "RS256") : importPKCS8(k.privateKey, "RS256", { extractable: true });
454
477
  }
455
478
  function createSystem(given = {}) {
456
- const issuer = (given.issuer ?? env("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
479
+ const issuer = withoutTrailingSlashes(given.issuer ?? env("CAUTHY_ISSUER") ?? "");
457
480
  const clientId = given.clientId ?? env("CAUTHY_CLIENT_ID") ?? "";
458
481
  const clientSecret = given.clientSecret ?? env("CAUTHY_CLIENT_SECRET") ?? "";
459
482
  const envKey = env("CAUTHY_SYSTEM_KEY");
@@ -642,7 +665,7 @@ function keysOf(issuer) {
642
665
  var hex = (s) => typeof s === "string" && s !== "" ? s : void 0;
643
666
  async function verifyWebhook(request, options = {}) {
644
667
  const env4 = typeof process !== "undefined" ? process.env?.CAUTHY_ISSUER : void 0;
645
- const issuer = (options.issuer ?? env4 ?? "").replace(/\/+$/, "");
668
+ const issuer = withoutTrailingSlashes(options.issuer ?? env4 ?? "");
646
669
  if (issuer === "") throw new CauthyWebhookError("issuer (CAUTHY_ISSUER) is not set");
647
670
  const signature = request.headers.get("x-cauthy-signature-jwt");
648
671
  if (!signature) throw new CauthyWebhookError("no signature");
@@ -735,7 +758,7 @@ function redirect(location, cookies = []) {
735
758
  return new Response(null, { status: 302, headers: h });
736
759
  }
737
760
  function createCauthy(given = {}) {
738
- const issuer = (given.issuer ?? env2("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
761
+ const issuer = withoutTrailingSlashes(given.issuer ?? env2("CAUTHY_ISSUER") ?? "");
739
762
  const clientId = given.clientId ?? env2("CAUTHY_CLIENT_ID") ?? "";
740
763
  const clientSecret = given.clientSecret ?? env2("CAUTHY_CLIENT_SECRET");
741
764
  const secret = given.secret ?? env2("CAUTHY_SECRET") ?? "";
@@ -759,7 +782,7 @@ function createCauthy(given = {}) {
759
782
  verifyEmail: given.pages?.verifyEmail ? localPath(given.pages.verifyEmail, "/") : void 0
760
783
  };
761
784
  const ownSystem = createSystem({ issuer, clientId, clientSecret, key: given.systemKey, fetch: given.fetch });
762
- const originOf = (request) => (given.baseURL ?? new URL(request.url).origin).replace(/\/+$/, "");
785
+ const originOf = (request) => withoutTrailingSlashes(given.baseURL ?? new URL(request.url).origin);
763
786
  const attrs = (request, maxAge) => ({ secure: originOf(request).startsWith("https://"), maxAge });
764
787
  async function token(form) {
765
788
  const body2 = new URLSearchParams({ client_id: clientId, ...form });
@@ -895,6 +918,25 @@ function createCauthy(given = {}) {
895
918
  orgCache.set(session.accessToken, { at: Date.now(), value });
896
919
  return value;
897
920
  }
921
+ const forgetting = (fn) => async (...args) => {
922
+ const out2 = await fn(...args);
923
+ orgCache.clear();
924
+ return out2;
925
+ };
926
+ const systemThatRefreshes = {
927
+ ...ownSystem,
928
+ organizations: {
929
+ ...ownSystem.organizations,
930
+ update: forgetting(ownSystem.organizations.update),
931
+ remove: forgetting(ownSystem.organizations.remove)
932
+ },
933
+ members: {
934
+ ...ownSystem.members,
935
+ add: forgetting(ownSystem.members.add),
936
+ setRole: forgetting(ownSystem.members.setRole),
937
+ remove: forgetting(ownSystem.members.remove)
938
+ }
939
+ };
898
940
  async function toPublic(session) {
899
941
  let orgs = [];
900
942
  try {
@@ -1758,7 +1800,7 @@ function createCauthy(given = {}) {
1758
1800
  async function handler(request) {
1759
1801
  const url = new URL(request.url);
1760
1802
  if (url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) return new Response("Not found", { status: 404 });
1761
- const route = url.pathname.slice(basePath.length).replace(/\/+$/, "") || "/";
1803
+ const route = withoutTrailingSlashes(url.pathname.slice(basePath.length)) || "/";
1762
1804
  const get = request.method === "GET" || request.method === "HEAD";
1763
1805
  const mine = await ownRoute(request, url, route);
1764
1806
  if (mine !== null) return mine;
@@ -1805,7 +1847,7 @@ function createCauthy(given = {}) {
1805
1847
  return {
1806
1848
  handler,
1807
1849
  webhook,
1808
- system: ownSystem,
1850
+ system: systemThatRefreshes,
1809
1851
  getSession,
1810
1852
  status,
1811
1853
  resolve: resolve2,
@@ -1905,7 +1947,7 @@ export type Permission = PermissionOf<typeof policy>
1905
1947
  }
1906
1948
  async function pullPolicy(o = {}) {
1907
1949
  const env4 = (n) => typeof process !== "undefined" ? process.env?.[n] : void 0;
1908
- const issuer = (o.issuer ?? env4("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
1950
+ const issuer = withoutTrailingSlashes(o.issuer ?? env4("CAUTHY_ISSUER") ?? "");
1909
1951
  const system = o.system ?? env4("CAUTHY_SYSTEM") ?? "";
1910
1952
  const tenant = o.tenant ?? env4("CAUTHY_TENANT") ?? "";
1911
1953
  const token = o.token ?? env4("CAUTHY_TOKEN") ?? "";
@@ -1921,7 +1963,7 @@ async function pullPolicy(o = {}) {
1921
1963
  }
1922
1964
  async function pushPolicy(o) {
1923
1965
  const env4 = (n) => typeof process !== "undefined" ? process.env?.[n] : void 0;
1924
- const issuer = (o.issuer ?? env4("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
1966
+ const issuer = withoutTrailingSlashes(o.issuer ?? env4("CAUTHY_ISSUER") ?? "");
1925
1967
  const system = o.system ?? env4("CAUTHY_SYSTEM") ?? "";
1926
1968
  const tenant = o.tenant ?? env4("CAUTHY_TENANT") ?? "";
1927
1969
  const token = o.token ?? env4("CAUTHY_TOKEN") ?? "";
@@ -1948,7 +1990,7 @@ function sessionSecret() {
1948
1990
  }
1949
1991
  async function connectSystem(o) {
1950
1992
  const env4 = (n) => typeof process !== "undefined" ? process.env?.[n] : void 0;
1951
- const issuer = (o.issuer ?? env4("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
1993
+ const issuer = withoutTrailingSlashes(o.issuer ?? env4("CAUTHY_ISSUER") ?? "");
1952
1994
  const tenant = o.tenant ?? env4("CAUTHY_TENANT") ?? "";
1953
1995
  const token = o.token ?? env4("CAUTHY_TOKEN") ?? "";
1954
1996
  for (const [name, v] of Object.entries({ CAUTHY_ISSUER: issuer, CAUTHY_TENANT: tenant, CAUTHY_TOKEN: token })) {
@@ -2011,7 +2053,7 @@ function bearer(from) {
2011
2053
  return /^Bearer\s+/i.test(header) ? header.replace(/^Bearer\s+/i, "").trim() : "";
2012
2054
  }
2013
2055
  async function verifyToken(from, options = {}) {
2014
- const issuer = (options.issuer ?? env3("CAUTHY_ISSUER") ?? "").replace(/\/+$/, "");
2056
+ const issuer = withoutTrailingSlashes(options.issuer ?? env3("CAUTHY_ISSUER") ?? "");
2015
2057
  if (issuer === "") throw new CauthyTokenError("issuer (CAUTHY_ISSUER) is not set");
2016
2058
  const token = bearer(from);
2017
2059
  if (token === "") throw new CauthyTokenError("no token");