@authhero/multi-tenancy 14.9.0 → 14.11.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.
@@ -1,30 +1,30 @@
1
- var X = Object.defineProperty;
2
- var Y = (e, t, n) => t in e ? X(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var R = (e, t, n) => Y(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { Hono as Z } from "hono";
5
- import { MANAGEMENT_API_SCOPES as x, MANAGEMENT_API_AUDIENCE as V, fetchAll as F, auth0QuerySchema as ee, tenantSchema as H, tenantInsertSchema as te, connectionSchema as ne, connectionOptionsSchema as re, init as ae } from "authhero";
6
- import { OpenAPIHono as se, createRoute as D, z as S } from "@hono/zod-openapi";
7
- function oe(e) {
1
+ var Y = Object.defineProperty;
2
+ var Z = (e, t, n) => t in e ? Y(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var M = (e, t, n) => Z(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { Hono as x } from "hono";
5
+ import { MANAGEMENT_API_SCOPES as ee, MANAGEMENT_API_AUDIENCE as Q, fetchAll as z, auth0QuerySchema as te, tenantSchema as N, tenantInsertSchema as ne, connectionSchema as re, connectionOptionsSchema as se, init as ae } from "authhero";
6
+ import { OpenAPIHono as oe, createRoute as O, z as S } from "@hono/zod-openapi";
7
+ function ie(e) {
8
8
  const { controlPlaneTenantId: t, requireOrganizationMatch: n = !0 } = e;
9
9
  return {
10
10
  async onTenantAccessValidation(r, s) {
11
11
  if (s === t)
12
12
  return !0;
13
13
  if (n) {
14
- const o = r.var.org_name, i = r.var.organization_id, a = o || i;
15
- return a ? a.toLowerCase() === s.toLowerCase() : !1;
14
+ const a = r.var.org_name, o = r.var.organization_id, i = a || o;
15
+ return i ? i.toLowerCase() === s.toLowerCase() : !1;
16
16
  }
17
17
  return !0;
18
18
  }
19
19
  };
20
20
  }
21
- function ie(e, t, n, r) {
21
+ function ce(e, t, n, r) {
22
22
  if (t === n)
23
23
  return !0;
24
24
  const s = r || e;
25
25
  return s ? s.toLowerCase() === t.toLowerCase() : !1;
26
26
  }
27
- function ce(e) {
27
+ function le(e) {
28
28
  return {
29
29
  async resolveDataAdapters(t) {
30
30
  try {
@@ -39,57 +39,57 @@ function ce(e) {
39
39
  }
40
40
  };
41
41
  }
42
- function le(e) {
42
+ function ue(e) {
43
43
  return `urn:authhero:tenant:${e.toLowerCase()}`;
44
44
  }
45
- function ue(e) {
45
+ function de(e) {
46
46
  return {
47
47
  async beforeCreate(t, n) {
48
48
  return !n.audience && n.id ? {
49
49
  ...n,
50
- audience: le(n.id)
50
+ audience: ue(n.id)
51
51
  } : n;
52
52
  },
53
53
  async afterCreate(t, n) {
54
54
  const { accessControl: r, databaseIsolation: s } = e;
55
- r && t.ctx && await de(t, n, r), s != null && s.onProvision && await s.onProvision(n.id);
55
+ r && t.ctx && await fe(t, n, r), s != null && s.onProvision && await s.onProvision(n.id);
56
56
  },
57
57
  async beforeDelete(t, n) {
58
58
  const { accessControl: r, databaseIsolation: s } = e;
59
59
  if (r)
60
60
  try {
61
- const i = (await t.adapters.organizations.list(
61
+ const o = (await t.adapters.organizations.list(
62
62
  r.controlPlaneTenantId
63
- )).organizations.find((a) => a.name === n);
64
- i && await t.adapters.organizations.remove(
63
+ )).organizations.find((i) => i.name === n);
64
+ o && await t.adapters.organizations.remove(
65
65
  r.controlPlaneTenantId,
66
- i.id
66
+ o.id
67
67
  );
68
- } catch (o) {
68
+ } catch (a) {
69
69
  console.warn(
70
70
  `Failed to remove organization for tenant ${n}:`,
71
- o
71
+ a
72
72
  );
73
73
  }
74
74
  if (s != null && s.onDeprovision)
75
75
  try {
76
76
  await s.onDeprovision(n);
77
- } catch (o) {
77
+ } catch (a) {
78
78
  console.warn(
79
79
  `Failed to deprovision database for tenant ${n}:`,
80
- o
80
+ a
81
81
  );
82
82
  }
83
83
  }
84
84
  };
85
85
  }
86
- async function de(e, t, n) {
86
+ async function fe(e, t, n) {
87
87
  const {
88
88
  controlPlaneTenantId: r,
89
89
  defaultPermissions: s,
90
- defaultRoles: o,
91
- issuer: i,
92
- adminRoleName: a = "Tenant Admin",
90
+ defaultRoles: a,
91
+ issuer: o,
92
+ adminRoleName: i = "Tenant Admin",
93
93
  adminRoleDescription: u = "Full access to all tenant management operations",
94
94
  addCreatorToOrganization: c = !0
95
95
  } = n, l = await e.adapters.organizations.create(
@@ -100,14 +100,14 @@ async function de(e, t, n) {
100
100
  }
101
101
  );
102
102
  let g;
103
- if (i && (g = await me(
103
+ if (o && (g = await ge(
104
104
  e,
105
105
  r,
106
- a,
106
+ i,
107
107
  u
108
108
  )), c && e.ctx) {
109
109
  const d = e.ctx.var.user;
110
- if (d != null && d.sub && !await fe(
110
+ if (d != null && d.sub && !await me(
111
111
  e,
112
112
  r,
113
113
  d.sub
@@ -130,13 +130,13 @@ async function de(e, t, n) {
130
130
  );
131
131
  }
132
132
  }
133
- o && o.length > 0 && console.log(
134
- `Would assign roles ${o.join(", ")} to organization ${l.id}`
133
+ a && a.length > 0 && console.log(
134
+ `Would assign roles ${a.join(", ")} to organization ${l.id}`
135
135
  ), s && s.length > 0 && console.log(
136
136
  `Would grant permissions ${s.join(", ")} to organization ${l.id}`
137
137
  );
138
138
  }
139
- async function fe(e, t, n) {
139
+ async function me(e, t, n) {
140
140
  const r = await e.adapters.userRoles.list(
141
141
  t,
142
142
  n,
@@ -150,46 +150,46 @@ async function fe(e, t, n) {
150
150
  s.id,
151
151
  { per_page: 1e3 }
152
152
  )).some(
153
- (a) => a.permission_name === "admin:organizations"
153
+ (i) => i.permission_name === "admin:organizations"
154
154
  ))
155
155
  return !0;
156
156
  return !1;
157
157
  }
158
- async function me(e, t, n, r) {
159
- const o = (await e.adapters.roles.list(t, {})).roles.find((c) => c.name === n);
160
- if (o)
161
- return o.id;
162
- const i = await e.adapters.roles.create(t, {
158
+ async function ge(e, t, n, r) {
159
+ const a = (await e.adapters.roles.list(t, {})).roles.find((c) => c.name === n);
160
+ if (a)
161
+ return a.id;
162
+ const o = await e.adapters.roles.create(t, {
163
163
  name: n,
164
164
  description: r
165
- }), a = V, u = x.map((c) => ({
166
- role_id: i.id,
167
- resource_server_identifier: a,
165
+ }), i = Q, u = ee.map((c) => ({
166
+ role_id: o.id,
167
+ resource_server_identifier: i,
168
168
  permission_name: c.value
169
169
  }));
170
170
  return await e.adapters.rolePermissions.assign(
171
171
  t,
172
- i.id,
172
+ o.id,
173
173
  u
174
- ), i.id;
174
+ ), o.id;
175
175
  }
176
- function B(e, t, n = () => !0) {
177
- const { controlPlaneTenantId: r, getChildTenantIds: s, getAdapters: o } = e, i = /* @__PURE__ */ new Map();
178
- async function a(l, g, d) {
176
+ function H(e, t, n = () => !0) {
177
+ const { controlPlaneTenantId: r, getChildTenantIds: s, getAdapters: a } = e, o = /* @__PURE__ */ new Map();
178
+ async function i(l, g, d) {
179
179
  return (await t(l).list(g, {
180
180
  q: `name:${d}`,
181
181
  per_page: 1
182
182
  }))[0] ?? null;
183
183
  }
184
184
  async function u(l) {
185
- const g = await s(), d = t(await o(r));
185
+ const g = await s(), d = t(await a(r));
186
186
  await Promise.all(
187
187
  g.map(async (f) => {
188
188
  try {
189
- const m = await o(f), w = t(m), p = {
189
+ const m = await a(f), w = t(m), p = {
190
190
  ...d.transform(l),
191
191
  is_system: !0
192
- }, y = await a(m, f, l.name), _ = y ? w.getId(y) : void 0;
192
+ }, y = await i(m, f, l.name), _ = y ? w.getId(y) : void 0;
193
193
  if (y && _) {
194
194
  const T = w.preserveOnUpdate ? w.preserveOnUpdate(y, p) : p;
195
195
  await w.update(f, _, T);
@@ -209,7 +209,7 @@ function B(e, t, n = () => !0) {
209
209
  await Promise.all(
210
210
  g.map(async (d) => {
211
211
  try {
212
- const f = await o(d), m = t(f), w = await a(f, d, l), h = w ? m.getId(w) : void 0;
212
+ const f = await a(d), m = t(f), w = await i(f, d, l), h = w ? m.getId(w) : void 0;
213
213
  w && h && await m.remove(d, h);
214
214
  } catch (f) {
215
215
  console.error(
@@ -230,22 +230,22 @@ function B(e, t, n = () => !0) {
230
230
  beforeDelete: async (l, g) => {
231
231
  if (l.tenantId !== r) return;
232
232
  const f = await t(l.adapters).get(l.tenantId, g);
233
- f && n(f) && i.set(g, f);
233
+ f && n(f) && o.set(g, f);
234
234
  },
235
235
  afterDelete: async (l, g) => {
236
236
  if (l.tenantId !== r) return;
237
- const d = i.get(g);
238
- d && (i.delete(g), await c(d.name));
237
+ const d = o.get(g);
238
+ d && (o.delete(g), await c(d.name));
239
239
  }
240
240
  };
241
241
  }
242
- function G(e, t, n = () => !0) {
243
- const { controlPlaneTenantId: r, getControlPlaneAdapters: s, getAdapters: o } = e;
242
+ function B(e, t, n = () => !0) {
243
+ const { controlPlaneTenantId: r, getControlPlaneAdapters: s, getAdapters: a } = e;
244
244
  return {
245
- async afterCreate(i, a) {
246
- if (a.id !== r)
245
+ async afterCreate(o, i) {
246
+ if (i.id !== r)
247
247
  try {
248
- const u = await s(), c = await o(a.id), l = t(u), g = t(c), d = await F(
248
+ const u = await s(), c = await a(i.id), l = t(u), g = t(c), d = await z(
249
249
  (f) => l.listPaginated(r, f),
250
250
  l.listKey,
251
251
  { cursorField: "id", pageSize: 100 }
@@ -254,13 +254,13 @@ function G(e, t, n = () => !0) {
254
254
  d.filter((f) => n(f)).map(async (f) => {
255
255
  try {
256
256
  const m = l.transform(f);
257
- await g.create(a.id, {
257
+ await g.create(i.id, {
258
258
  ...m,
259
259
  is_system: !0
260
260
  });
261
261
  } catch (m) {
262
262
  console.error(
263
- `Failed to sync entity to new tenant "${a.id}":`,
263
+ `Failed to sync entity to new tenant "${i.id}":`,
264
264
  m
265
265
  );
266
266
  }
@@ -268,14 +268,14 @@ function G(e, t, n = () => !0) {
268
268
  );
269
269
  } catch (u) {
270
270
  console.error(
271
- `Failed to sync entities to new tenant "${a.id}":`,
271
+ `Failed to sync entities to new tenant "${i.id}":`,
272
272
  u
273
273
  );
274
274
  }
275
275
  }
276
276
  };
277
277
  }
278
- const L = (e) => ({
278
+ const G = (e) => ({
279
279
  list: async (t, n) => (await e.resourceServers.list(t, n)).resource_servers,
280
280
  listPaginated: (t, n) => e.resourceServers.list(t, n),
281
281
  get: (t, n) => e.resourceServers.get(t, n),
@@ -308,34 +308,34 @@ const L = (e) => ({
308
308
  description: t.description
309
309
  })
310
310
  });
311
- function W(e) {
311
+ function L(e) {
312
312
  var t;
313
313
  return ((t = e.metadata) == null ? void 0 : t.sync) !== !1;
314
314
  }
315
- function ge(e) {
316
- const { sync: t = {}, filters: n = {} } = e, r = t.resourceServers ?? !0, s = t.roles ?? !0, o = (m) => W(m) ? n.resourceServers ? n.resourceServers(m) : !0 : !1, i = (m) => W(m) ? n.roles ? n.roles(m) : !0 : !1, a = r ? B(
315
+ function we(e) {
316
+ const { sync: t = {}, filters: n = {} } = e, r = t.resourceServers ?? !0, s = t.roles ?? !0, a = (m) => L(m) ? n.resourceServers ? n.resourceServers(m) : !0 : !1, o = (m) => L(m) ? n.roles ? n.roles(m) : !0 : !1, i = r ? H(
317
317
  e,
318
- L,
319
- o
320
- ) : void 0, u = s ? B(
318
+ G,
319
+ a
320
+ ) : void 0, u = s ? H(
321
321
  e,
322
322
  U,
323
- i
324
- ) : void 0, c = r ? G(
325
- e,
326
- L,
327
323
  o
328
- ) : void 0, l = s ? G(
324
+ ) : void 0, c = r ? B(
325
+ e,
326
+ G,
327
+ a
328
+ ) : void 0, l = s ? B(
329
329
  e,
330
330
  U,
331
- i
331
+ o
332
332
  ) : void 0, g = s ? {
333
333
  async afterCreate(m, w) {
334
334
  var h;
335
335
  if (w.id !== e.controlPlaneTenantId) {
336
336
  await ((h = l == null ? void 0 : l.afterCreate) == null ? void 0 : h.call(l, m, w));
337
337
  try {
338
- const p = await e.getControlPlaneAdapters(), y = await e.getAdapters(w.id), _ = await F(
338
+ const p = await e.getControlPlaneAdapters(), y = await e.getAdapters(w.id), _ = await z(
339
339
  (C) => p.roles.list(
340
340
  e.controlPlaneTenantId,
341
341
  C
@@ -403,7 +403,7 @@ function ge(e) {
403
403
  }
404
404
  return {
405
405
  entityHooks: {
406
- resourceServers: a,
406
+ resourceServers: i,
407
407
  roles: u
408
408
  },
409
409
  tenantHooks: {
@@ -437,8 +437,8 @@ var b = class extends Error {
437
437
  */
438
438
  constructor(t = 500, n) {
439
439
  super(n == null ? void 0 : n.message, { cause: n == null ? void 0 : n.cause });
440
- R(this, "res");
441
- R(this, "status");
440
+ M(this, "res");
441
+ M(this, "status");
442
442
  this.res = n == null ? void 0 : n.res, this.status = t;
443
443
  }
444
444
  /**
@@ -455,15 +455,15 @@ var b = class extends Error {
455
455
  });
456
456
  }
457
457
  };
458
- function j(e, t) {
459
- const n = new se();
458
+ function q(e, t) {
459
+ const n = new oe();
460
460
  return n.openapi(
461
- D({
461
+ O({
462
462
  tags: ["tenants"],
463
463
  method: "get",
464
464
  path: "/",
465
465
  request: {
466
- query: ee
466
+ query: te
467
467
  },
468
468
  security: [
469
469
  {
@@ -475,7 +475,7 @@ function j(e, t) {
475
475
  content: {
476
476
  "application/json": {
477
477
  schema: S.object({
478
- tenants: S.array(H),
478
+ tenants: S.array(N),
479
479
  start: S.number().optional(),
480
480
  limit: S.number().optional(),
481
481
  length: S.number().optional()
@@ -488,74 +488,74 @@ function j(e, t) {
488
488
  }),
489
489
  async (r) => {
490
490
  var m, w, h, p, y, _;
491
- const s = r.req.valid("query"), { page: o, per_page: i, include_totals: a, q: u } = s, c = r.var.user, l = (c == null ? void 0 : c.permissions) || [];
491
+ const s = r.req.valid("query"), { page: a, per_page: o, include_totals: i, q: u } = s, c = r.var.user, l = (c == null ? void 0 : c.permissions) || [];
492
492
  if (l.includes("auth:read") || l.includes("admin:organizations")) {
493
493
  const T = await r.env.data.tenants.list({
494
- page: o,
495
- per_page: i,
496
- include_totals: a,
494
+ page: a,
495
+ per_page: o,
496
+ include_totals: i,
497
497
  q: u
498
498
  });
499
- return a ? r.json({
499
+ return i ? r.json({
500
500
  tenants: T.tenants,
501
501
  start: ((m = T.totals) == null ? void 0 : m.start) ?? 0,
502
- limit: ((w = T.totals) == null ? void 0 : w.limit) ?? i,
502
+ limit: ((w = T.totals) == null ? void 0 : w.limit) ?? o,
503
503
  length: T.tenants.length
504
504
  }) : r.json({ tenants: T.tenants });
505
505
  }
506
506
  const d = ((h = e.accessControl) == null ? void 0 : h.controlPlaneTenantId) ?? ((p = r.env.data.multiTenancyConfig) == null ? void 0 : p.controlPlaneTenantId);
507
507
  if (d && (c != null && c.sub)) {
508
- const C = (await F(
509
- (z) => r.env.data.userOrganizations.listUserOrganizations(
508
+ const C = (await z(
509
+ ($) => r.env.data.userOrganizations.listUserOrganizations(
510
510
  d,
511
511
  c.sub,
512
- z
512
+ $
513
513
  ),
514
514
  "organizations"
515
- )).map((z) => z.name);
515
+ )).map(($) => $.name);
516
516
  if (C.length === 0)
517
- return a ? r.json({
517
+ return i ? r.json({
518
518
  tenants: [],
519
519
  start: 0,
520
- limit: i ?? 50,
520
+ limit: o ?? 50,
521
521
  length: 0
522
522
  }) : r.json({ tenants: [] });
523
- const v = C.length, P = o ?? 0, A = i ?? 50, $ = P * A, I = C.slice($, $ + A);
523
+ const v = C.length, P = a ?? 0, A = o ?? 50, R = P * A, I = C.slice(R, R + A);
524
524
  if (I.length === 0)
525
- return a ? r.json({
525
+ return i ? r.json({
526
526
  tenants: [],
527
- start: $,
527
+ start: R,
528
528
  limit: A,
529
529
  length: v
530
530
  }) : r.json({ tenants: [] });
531
- const E = I.map((z) => `id:${z}`).join(" OR "), J = u ? `(${E}) AND (${u})` : E, N = await r.env.data.tenants.list({
532
- q: J,
531
+ const k = I.map(($) => `id:${$}`).join(" OR "), X = u ? `(${k}) AND (${u})` : k, E = await r.env.data.tenants.list({
532
+ q: X,
533
533
  per_page: A,
534
534
  include_totals: !1
535
535
  // We calculate totals from accessibleTenantIds
536
536
  });
537
- return a ? r.json({
538
- tenants: N.tenants,
539
- start: $,
537
+ return i ? r.json({
538
+ tenants: E.tenants,
539
+ start: R,
540
540
  limit: A,
541
541
  length: v
542
- }) : r.json({ tenants: N.tenants });
542
+ }) : r.json({ tenants: E.tenants });
543
543
  }
544
544
  const f = await r.env.data.tenants.list({
545
- page: o,
546
- per_page: i,
547
- include_totals: a,
545
+ page: a,
546
+ per_page: o,
547
+ include_totals: i,
548
548
  q: u
549
549
  });
550
- return a ? r.json({
550
+ return i ? r.json({
551
551
  tenants: f.tenants,
552
552
  start: ((y = f.totals) == null ? void 0 : y.start) ?? 0,
553
- limit: ((_ = f.totals) == null ? void 0 : _.limit) ?? i,
553
+ limit: ((_ = f.totals) == null ? void 0 : _.limit) ?? o,
554
554
  length: f.tenants.length
555
555
  }) : r.json({ tenants: f.tenants });
556
556
  }
557
557
  ), n.openapi(
558
- D({
558
+ O({
559
559
  tags: ["tenants"],
560
560
  method: "post",
561
561
  path: "/",
@@ -563,7 +563,7 @@ function j(e, t) {
563
563
  body: {
564
564
  content: {
565
565
  "application/json": {
566
- schema: te
566
+ schema: ne
567
567
  }
568
568
  }
569
569
  }
@@ -577,7 +577,7 @@ function j(e, t) {
577
577
  201: {
578
578
  content: {
579
579
  "application/json": {
580
- schema: H
580
+ schema: N
581
581
  }
582
582
  },
583
583
  description: "Tenant created"
@@ -597,17 +597,17 @@ function j(e, t) {
597
597
  throw new b(401, {
598
598
  message: "Authentication required to create tenants"
599
599
  });
600
- let o = r.req.valid("json");
601
- const i = {
600
+ let a = r.req.valid("json");
601
+ const o = {
602
602
  adapters: r.env.data,
603
603
  ctx: r
604
604
  };
605
- (u = t.tenants) != null && u.beforeCreate && (o = await t.tenants.beforeCreate(i, o));
606
- const a = await r.env.data.tenants.create(o);
607
- return (c = t.tenants) != null && c.afterCreate && await t.tenants.afterCreate(i, a), r.json(a, 201);
605
+ (u = t.tenants) != null && u.beforeCreate && (a = await t.tenants.beforeCreate(o, a));
606
+ const i = await r.env.data.tenants.create(a);
607
+ return (c = t.tenants) != null && c.afterCreate && await t.tenants.afterCreate(o, i), r.json(i, 201);
608
608
  }
609
609
  ), n.openapi(
610
- D({
610
+ O({
611
611
  tags: ["tenants"],
612
612
  method: "delete",
613
613
  path: "/{id}",
@@ -635,20 +635,20 @@ function j(e, t) {
635
635
  }),
636
636
  async (r) => {
637
637
  var u, c, l, g;
638
- const { id: s } = r.req.valid("param"), o = ((u = e.accessControl) == null ? void 0 : u.controlPlaneTenantId) ?? ((c = r.env.data.multiTenancyConfig) == null ? void 0 : c.controlPlaneTenantId);
639
- if (o) {
638
+ const { id: s } = r.req.valid("param"), a = ((u = e.accessControl) == null ? void 0 : u.controlPlaneTenantId) ?? ((c = r.env.data.multiTenancyConfig) == null ? void 0 : c.controlPlaneTenantId);
639
+ if (a) {
640
640
  const d = r.var.user;
641
641
  if (!(d != null && d.sub))
642
642
  throw new b(401, {
643
643
  message: "Authentication required"
644
644
  });
645
- if (s === o)
645
+ if (s === a)
646
646
  throw new b(403, {
647
647
  message: "Cannot delete the control plane"
648
648
  });
649
- if (!(await F(
649
+ if (!(await z(
650
650
  (w) => r.env.data.userOrganizations.listUserOrganizations(
651
- o,
651
+ a,
652
652
  d.sub,
653
653
  w
654
654
  ),
@@ -662,15 +662,15 @@ function j(e, t) {
662
662
  throw new b(404, {
663
663
  message: "Tenant not found"
664
664
  });
665
- const a = {
665
+ const i = {
666
666
  adapters: r.env.data,
667
667
  ctx: r
668
668
  };
669
- return (l = t.tenants) != null && l.beforeDelete && await t.tenants.beforeDelete(a, s), await r.env.data.tenants.remove(s), (g = t.tenants) != null && g.afterDelete && await t.tenants.afterDelete(a, s), r.body(null, 204);
669
+ return (l = t.tenants) != null && l.beforeDelete && await t.tenants.beforeDelete(i, s), await r.env.data.tenants.remove(s), (g = t.tenants) != null && g.afterDelete && await t.tenants.afterDelete(i, s), r.body(null, 204);
670
670
  }
671
671
  ), n;
672
672
  }
673
- function we(e) {
673
+ function pe(e) {
674
674
  const t = [
675
675
  {
676
676
  pattern: /\/api\/v2\/resource-servers\/([^/]+)$/,
@@ -686,7 +686,7 @@ function we(e) {
686
686
  }
687
687
  return null;
688
688
  }
689
- async function pe(e, t, n) {
689
+ async function ye(e, t, n) {
690
690
  try {
691
691
  switch (n.type) {
692
692
  case "resource_server": {
@@ -708,88 +708,93 @@ async function pe(e, t, n) {
708
708
  return !1;
709
709
  }
710
710
  }
711
- function ye(e) {
711
+ function he(e) {
712
712
  return {
713
713
  resource_server: "resource server",
714
714
  role: "role",
715
715
  connection: "connection"
716
716
  }[e];
717
717
  }
718
- function he() {
718
+ function ve() {
719
719
  return async (e, t) => {
720
720
  if (!["PATCH", "PUT", "DELETE"].includes(e.req.method))
721
721
  return t();
722
- const n = we(e.req.path);
722
+ const n = pe(e.req.path);
723
723
  if (!n)
724
724
  return t();
725
725
  const r = e.var.tenant_id || e.req.header("x-tenant-id") || e.req.header("tenant-id");
726
726
  if (!r)
727
727
  return t();
728
- if (await pe(e.env.data, r, n))
728
+ if (await ye(e.env.data, r, n))
729
729
  throw new b(403, {
730
- message: `This ${ye(n.type)} is a system resource and cannot be modified. Make changes in the control plane instead.`
730
+ message: `This ${he(n.type)} is a system resource and cannot be modified. Make changes in the control plane instead.`
731
731
  });
732
732
  return t();
733
733
  };
734
734
  }
735
- const ve = [
736
- "client_secret",
737
- "app_secret",
738
- "twilio_token"
739
- ];
740
- function _e(e) {
741
- if (!e) return e;
742
- const t = { ...e };
743
- for (const n of ve)
744
- delete t[n];
745
- return t;
746
- }
747
- function M(e, t, n) {
748
- const r = t.find(
749
- (i) => i.strategy === e.strategy
735
+ function D(e, t) {
736
+ const n = t.find(
737
+ (s) => s.strategy === e.strategy
750
738
  );
751
- if (!(r != null && r.options))
739
+ if (!(n != null && n.options))
752
740
  return e;
753
- const s = ne.passthrough().parse({
754
- ...r,
741
+ const r = re.passthrough().parse({
742
+ ...n,
755
743
  ...e
756
- }), o = n ? _e(r.options) : r.options;
757
- return s.options = re.passthrough().parse({
758
- ...o || {},
744
+ });
745
+ return r.options = se.passthrough().parse({
746
+ ...n.options || {},
759
747
  ...e.options
760
- }), s;
748
+ }), r;
761
749
  }
762
- function O(e, t) {
750
+ function F(e, t) {
763
751
  const n = [
764
752
  ...t || [],
765
753
  ...e || []
766
754
  ];
767
755
  return [...new Set(n)];
768
756
  }
757
+ function _e(e, t) {
758
+ if (!(t != null && t.length))
759
+ return e || [];
760
+ if (!(e != null && e.length))
761
+ return t;
762
+ const n = /* @__PURE__ */ new Map();
763
+ for (const r of t)
764
+ n.set(r.value, r);
765
+ for (const r of e)
766
+ n.set(r.value, r);
767
+ return Array.from(n.values());
768
+ }
769
+ function W(e, t) {
770
+ return t ? {
771
+ ...e,
772
+ scopes: _e(
773
+ e.scopes,
774
+ t.scopes
775
+ )
776
+ } : e;
777
+ }
769
778
  function K(e, t) {
770
779
  return t ? {
771
780
  ...e,
772
- callbacks: O(e.callbacks, t.callbacks),
773
- web_origins: O(
781
+ callbacks: F(e.callbacks, t.callbacks),
782
+ web_origins: F(
774
783
  e.web_origins,
775
784
  t.web_origins
776
785
  ),
777
- allowed_logout_urls: O(
786
+ allowed_logout_urls: F(
778
787
  e.allowed_logout_urls,
779
788
  t.allowed_logout_urls
780
789
  ),
781
- allowed_origins: O(
790
+ allowed_origins: F(
782
791
  e.allowed_origins,
783
792
  t.allowed_origins
784
793
  )
785
794
  } : e;
786
795
  }
787
796
  function Te(e, t) {
788
- const {
789
- controlPlaneTenantId: n,
790
- controlPlaneClientId: r,
791
- excludeSensitiveFields: s = !1
792
- } = t;
797
+ const { controlPlaneTenantId: n, controlPlaneClientId: r } = t;
793
798
  return {
794
799
  ...e,
795
800
  // Store config for use by tenants route access control
@@ -799,91 +804,126 @@ function Te(e, t) {
799
804
  },
800
805
  connections: {
801
806
  ...e.connections,
802
- get: async (o, i) => {
803
- const a = await e.connections.get(
804
- o,
805
- i
807
+ get: async (s, a) => {
808
+ const o = await e.connections.get(
809
+ s,
810
+ a
806
811
  );
807
- if (!a || !n || o === n)
808
- return a;
809
- const u = await e.connections.list(n);
810
- return M(
811
- a,
812
- u.connections || [],
813
- s
812
+ if (!o || !n || s === n)
813
+ return o;
814
+ const i = await e.connections.list(n);
815
+ return D(
816
+ o,
817
+ i.connections || []
814
818
  );
815
819
  },
816
- list: async (o, i) => {
817
- const a = await e.connections.list(o, i);
818
- if (!n || o === n)
819
- return a;
820
- const u = await e.connections.list(n), c = a.connections.map(
821
- (l) => M(
822
- l,
823
- u.connections || [],
824
- s
820
+ list: async (s, a) => {
821
+ const o = await e.connections.list(s, a);
822
+ if (!n || s === n)
823
+ return o;
824
+ const i = await e.connections.list(n), u = o.connections.map(
825
+ (c) => D(
826
+ c,
827
+ i.connections || []
825
828
  )
826
829
  );
827
830
  return {
828
- ...a,
829
- connections: c
831
+ ...o,
832
+ connections: u
830
833
  };
831
834
  }
832
835
  },
833
836
  clientConnections: {
834
837
  ...e.clientConnections,
835
- listByClient: async (o, i) => {
836
- let a = await e.clientConnections.listByClient(
837
- o,
838
- i
838
+ listByClient: async (s, a) => {
839
+ let o = await e.clientConnections.listByClient(
840
+ s,
841
+ a
839
842
  );
840
- if (a.length === 0 && (a = (await e.connections.list(o)).connections || []), !n || o === n)
841
- return a;
842
- const u = await e.connections.list(n);
843
- return a.map(
844
- (c) => M(
845
- c,
846
- u.connections || [],
847
- s
843
+ if (o.length === 0 && (o = (await e.connections.list(s)).connections || []), !n || s === n)
844
+ return o;
845
+ const i = await e.connections.list(n);
846
+ return o.map(
847
+ (u) => D(
848
+ u,
849
+ i.connections || []
848
850
  )
849
851
  );
850
852
  }
851
853
  },
852
854
  clients: {
853
855
  ...e.clients,
854
- get: async (o, i) => {
855
- const a = await e.clients.get(o, i);
856
- if (!a)
856
+ get: async (s, a) => {
857
+ const o = await e.clients.get(s, a);
858
+ if (!o)
857
859
  return null;
858
- if (!n || !r || o === n && i === r)
859
- return a;
860
- const u = await e.clients.get(
860
+ if (!n || !r || s === n && a === r)
861
+ return o;
862
+ const i = await e.clients.get(
861
863
  n,
862
864
  r
863
865
  );
864
- return K(a, u);
866
+ return K(o, i);
865
867
  },
866
- getByClientId: async (o) => {
867
- const i = await e.clients.getByClientId(o);
868
- if (!i)
868
+ getByClientId: async (s) => {
869
+ const a = await e.clients.getByClientId(s);
870
+ if (!a)
869
871
  return null;
870
- if (!n || !r || i.tenant_id === n && i.client_id === r)
871
- return i;
872
- const a = await e.clients.get(
872
+ if (!n || !r || a.tenant_id === n && a.client_id === r)
873
+ return a;
874
+ const o = await e.clients.get(
873
875
  n,
874
876
  r
875
877
  );
876
878
  return {
877
- ...K(i, a),
878
- tenant_id: i.tenant_id
879
+ ...K(a, o),
880
+ tenant_id: a.tenant_id
879
881
  };
880
882
  }
881
883
  },
882
884
  emailProviders: {
883
885
  ...e.emailProviders,
884
- get: async (o) => {
885
- const i = await e.emailProviders.get(o);
886
- return i || (!n || o === n ? null : e.emailProviders.get(n));
886
+ get: async (s) => {
887
+ const a = await e.emailProviders.get(s);
888
+ return a || (!n || s === n ? null : e.emailProviders.get(n));
889
+ }
890
+ },
891
+ resourceServers: {
892
+ ...e.resourceServers,
893
+ get: async (s, a) => {
894
+ const o = await e.resourceServers.get(
895
+ s,
896
+ a
897
+ );
898
+ if (!o || !n || s === n)
899
+ return o;
900
+ const u = (await e.resourceServers.list(
901
+ n,
902
+ { q: `identifier:${o.identifier}`, per_page: 1 }
903
+ )).resource_servers[0] ?? null;
904
+ return W(
905
+ o,
906
+ u
907
+ );
908
+ },
909
+ list: async (s, a) => {
910
+ const o = await e.resourceServers.list(s, a);
911
+ if (!n || s === n)
912
+ return o;
913
+ const i = await e.resourceServers.list(
914
+ n
915
+ ), u = new Map(
916
+ i.resource_servers.map((l) => [l.identifier, l])
917
+ ), c = o.resource_servers.map(
918
+ (l) => W(
919
+ l,
920
+ u.get(l.identifier) ?? null
921
+ )
922
+ );
923
+ return {
924
+ ...o,
925
+ resource_servers: c
926
+ };
887
927
  }
888
928
  }
889
929
  // Note: Additional adapters can be extended here for runtime fallback:
@@ -891,7 +931,7 @@ function Te(e, t) {
891
931
  // - branding: Fall back to control plane branding/themes
892
932
  };
893
933
  }
894
- function q(e, t) {
934
+ function V(e, t) {
895
935
  return Te(e, t);
896
936
  }
897
937
  function Ce(e) {
@@ -904,21 +944,21 @@ function Pe(e) {
904
944
  return async (t, n) => {
905
945
  if (!e.accessControl)
906
946
  return n();
907
- const { controlPlaneTenantId: r } = e.accessControl, s = t.var.org_name, o = t.var.organization_id, i = s || o;
908
- let a = t.var.tenant_id;
909
- const u = t.var.user, l = (u != null && u.aud ? Array.isArray(u.aud) ? u.aud : [u.aud] : []).includes(V);
910
- if (!a && i && l && (t.set("tenant_id", i), a = i), !a)
947
+ const { controlPlaneTenantId: r } = e.accessControl, s = t.var.org_name, a = t.var.organization_id, o = s || a;
948
+ let i = t.var.tenant_id;
949
+ const u = t.var.user, l = (u != null && u.aud ? Array.isArray(u.aud) ? u.aud : [u.aud] : []).includes(Q);
950
+ if (!i && o && l && (t.set("tenant_id", o), i = o), !i)
911
951
  throw new b(400, {
912
952
  message: "Tenant ID not found in request"
913
953
  });
914
- if (!ie(
915
- o,
954
+ if (!ce(
916
955
  a,
956
+ i,
917
957
  r,
918
958
  s
919
959
  ))
920
960
  throw new b(403, {
921
- message: `Access denied to tenant ${a}`
961
+ message: `Access denied to tenant ${i}`
922
962
  });
923
963
  return n();
924
964
  };
@@ -930,30 +970,30 @@ function Ae(e) {
930
970
  const {
931
971
  baseDomain: r,
932
972
  reservedSubdomains: s = [],
933
- resolveSubdomain: o
934
- } = e.subdomainRouting, i = t.req.header("host") || "";
935
- let a = null;
936
- if (i.endsWith(r)) {
937
- const c = i.slice(0, -(r.length + 1));
938
- c && !c.includes(".") && (a = c);
973
+ resolveSubdomain: a
974
+ } = e.subdomainRouting, o = t.req.header("host") || "";
975
+ let i = null;
976
+ if (o.endsWith(r)) {
977
+ const c = o.slice(0, -(r.length + 1));
978
+ c && !c.includes(".") && (i = c);
939
979
  }
940
- if (a && s.includes(a) && (a = null), !a)
980
+ if (i && s.includes(i) && (i = null), !i)
941
981
  return e.accessControl && t.set("tenant_id", e.accessControl.controlPlaneTenantId), n();
942
982
  let u = null;
943
- if (o)
944
- u = await o(a);
983
+ if (a)
984
+ u = await a(i);
945
985
  else if (e.subdomainRouting.useOrganizations !== !1 && e.accessControl)
946
986
  try {
947
987
  const c = await t.env.data.organizations.get(
948
988
  e.accessControl.controlPlaneTenantId,
949
- a
989
+ i
950
990
  );
951
991
  c && (u = c.id);
952
992
  } catch {
953
993
  }
954
994
  if (!u)
955
995
  throw new b(404, {
956
- message: `Tenant not found for subdomain: ${a}`
996
+ message: `Tenant not found for subdomain: ${i}`
957
997
  });
958
998
  return t.set("tenant_id", u), n();
959
999
  };
@@ -981,14 +1021,14 @@ function be(e) {
981
1021
  return n();
982
1022
  };
983
1023
  }
984
- function Q(e) {
1024
+ function J(e) {
985
1025
  const t = Ae(e), n = Pe(e), r = be(e);
986
- return async (s, o) => (await t(s, async () => {
1026
+ return async (s, a) => (await t(s, async () => {
987
1027
  }), await n(s, async () => {
988
1028
  }), await r(s, async () => {
989
- }), o());
1029
+ }), a());
990
1030
  }
991
- function Oe(e) {
1031
+ function Fe(e) {
992
1032
  const {
993
1033
  dataAdapter: t,
994
1034
  controlPlane: n,
@@ -996,9 +1036,9 @@ function Oe(e) {
996
1036
  tenantId: r = "control_plane",
997
1037
  clientId: s
998
1038
  } = {},
999
- sync: o = { resourceServers: !0, roles: !0 },
1000
- defaultPermissions: i = ["tenant:admin"],
1001
- requireOrganizationMatch: a = !1,
1039
+ sync: a = { resourceServers: !0, roles: !0 },
1040
+ defaultPermissions: o = ["tenant:admin"],
1041
+ requireOrganizationMatch: i = !1,
1002
1042
  managementApiExtensions: u = [],
1003
1043
  entityHooks: c,
1004
1044
  getChildTenantIds: l,
@@ -1006,20 +1046,22 @@ function Oe(e) {
1006
1046
  ...d
1007
1047
  } = e;
1008
1048
  let f = t, m = t;
1009
- n && (f = q(t, {
1049
+ n && (f = V(t, {
1010
1050
  controlPlaneTenantId: r,
1011
1051
  controlPlaneClientId: s
1012
- }), m = q(t, {
1013
- controlPlaneTenantId: r,
1014
- controlPlaneClientId: s,
1015
- excludeSensitiveFields: !0
1016
- }));
1017
- const w = o !== !1, h = w ? {
1018
- resourceServers: o.resourceServers ?? !0,
1019
- roles: o.roles ?? !0
1052
+ }), m = {
1053
+ ...t,
1054
+ multiTenancyConfig: {
1055
+ controlPlaneTenantId: r,
1056
+ controlPlaneClientId: s
1057
+ }
1058
+ });
1059
+ const w = a !== !1, h = w ? {
1060
+ resourceServers: a.resourceServers ?? !0,
1061
+ roles: a.roles ?? !0
1020
1062
  } : { resourceServers: !1, roles: !1 }, _ = {
1021
1063
  controlPlaneTenantId: r,
1022
- getChildTenantIds: l ?? (async () => (await F(
1064
+ getChildTenantIds: l ?? (async () => (await z(
1023
1065
  (I) => f.tenants.list(I),
1024
1066
  "tenants",
1025
1067
  { cursorField: "id", pageSize: 100 }
@@ -1027,7 +1069,7 @@ function Oe(e) {
1027
1069
  getAdapters: g ?? (async () => f),
1028
1070
  getControlPlaneAdapters: async () => f,
1029
1071
  sync: h
1030
- }, { entityHooks: T, tenantHooks: C } = ge(_), v = {
1072
+ }, { entityHooks: T, tenantHooks: C } = we(_), v = {
1031
1073
  resourceServers: [
1032
1074
  T.resourceServers,
1033
1075
  ...(c == null ? void 0 : c.resourceServers) ?? []
@@ -1036,12 +1078,12 @@ function Oe(e) {
1036
1078
  connections: (c == null ? void 0 : c.connections) ?? [],
1037
1079
  tenants: (c == null ? void 0 : c.tenants) ?? [],
1038
1080
  rolePermissions: (c == null ? void 0 : c.rolePermissions) ?? []
1039
- }, P = j(
1081
+ }, P = q(
1040
1082
  {
1041
1083
  accessControl: {
1042
1084
  controlPlaneTenantId: r,
1043
- requireOrganizationMatch: a,
1044
- defaultPermissions: i
1085
+ requireOrganizationMatch: i,
1086
+ defaultPermissions: o
1045
1087
  }
1046
1088
  },
1047
1089
  { tenants: C }
@@ -1055,21 +1097,21 @@ function Oe(e) {
1055
1097
  { path: "/tenants", router: P }
1056
1098
  ]
1057
1099
  });
1058
- return A.use("/api/v2/*", Ce(r)), w && A.use("/api/v2/*", he()), { app: A, controlPlaneTenantId: r };
1100
+ return A.use("/api/v2/*", Ce(r)), w && A.use("/api/v2/*", ve()), { app: A, controlPlaneTenantId: r };
1059
1101
  }
1060
- function Re(e) {
1061
- const t = k(e);
1102
+ function Me(e) {
1103
+ const t = j(e);
1062
1104
  return {
1063
1105
  name: "multi-tenancy",
1064
1106
  // Apply multi-tenancy middleware for subdomain routing, database resolution, etc.
1065
- middleware: Q(e),
1107
+ middleware: J(e),
1066
1108
  // Provide lifecycle hooks
1067
1109
  hooks: t,
1068
1110
  // Mount tenant management routes
1069
1111
  routes: [
1070
1112
  {
1071
1113
  path: "/management",
1072
- handler: j(e, t)
1114
+ handler: q(e, t)
1073
1115
  }
1074
1116
  ],
1075
1117
  // Called when plugin is registered
@@ -1082,8 +1124,8 @@ function Re(e) {
1082
1124
  }
1083
1125
  };
1084
1126
  }
1085
- function k(e) {
1086
- const t = e.accessControl ? oe(e.accessControl) : {}, n = e.databaseIsolation ? ce(e.databaseIsolation) : {}, r = ue(e);
1127
+ function j(e) {
1128
+ const t = e.accessControl ? ie(e.accessControl) : {}, n = e.databaseIsolation ? le(e.databaseIsolation) : {}, r = de(e);
1087
1129
  return {
1088
1130
  ...t,
1089
1131
  ...n,
@@ -1091,13 +1133,13 @@ function k(e) {
1091
1133
  };
1092
1134
  }
1093
1135
  function Ie(e) {
1094
- const t = new Z(), n = k(e);
1095
- return t.route("/tenants", j(e, n)), t;
1136
+ const t = new x(), n = j(e);
1137
+ return t.route("/tenants", q(e, n)), t;
1096
1138
  }
1097
- function De(e) {
1139
+ function Oe(e) {
1098
1140
  return {
1099
- hooks: k(e),
1100
- middleware: Q(e),
1141
+ hooks: j(e),
1142
+ middleware: J(e),
1101
1143
  app: Ie(e),
1102
1144
  config: e,
1103
1145
  /**
@@ -1110,7 +1152,7 @@ function De(e) {
1110
1152
  */
1111
1153
  wrapAdapters: (t, n) => {
1112
1154
  var r;
1113
- return q(t, {
1155
+ return V(t, {
1114
1156
  controlPlaneTenantId: (r = e.accessControl) == null ? void 0 : r.controlPlaneTenantId,
1115
1157
  controlPlaneClientId: n == null ? void 0 : n.controlPlaneClientId
1116
1158
  });
@@ -1118,23 +1160,23 @@ function De(e) {
1118
1160
  };
1119
1161
  }
1120
1162
  export {
1121
- oe as createAccessControlHooks,
1163
+ ie as createAccessControlHooks,
1122
1164
  Pe as createAccessControlMiddleware,
1123
1165
  Ce as createControlPlaneTenantMiddleware,
1124
- ce as createDatabaseHooks,
1166
+ le as createDatabaseHooks,
1125
1167
  be as createDatabaseMiddleware,
1126
1168
  Ie as createMultiTenancy,
1127
- k as createMultiTenancyHooks,
1128
- Q as createMultiTenancyMiddleware,
1129
- Re as createMultiTenancyPlugin,
1130
- he as createProtectSyncedMiddleware,
1131
- ue as createProvisioningHooks,
1169
+ j as createMultiTenancyHooks,
1170
+ J as createMultiTenancyMiddleware,
1171
+ Me as createMultiTenancyPlugin,
1172
+ ve as createProtectSyncedMiddleware,
1173
+ de as createProvisioningHooks,
1132
1174
  Te as createRuntimeFallbackAdapter,
1133
1175
  Ae as createSubdomainMiddleware,
1134
- ge as createSyncHooks,
1135
- j as createTenantsOpenAPIRouter,
1136
- Oe as initMultiTenant,
1137
- De as setupMultiTenancy,
1138
- ie as validateTenantAccess,
1139
- q as withRuntimeFallback
1176
+ we as createSyncHooks,
1177
+ q as createTenantsOpenAPIRouter,
1178
+ Fe as initMultiTenant,
1179
+ Oe as setupMultiTenancy,
1180
+ ce as validateTenantAccess,
1181
+ V as withRuntimeFallback
1140
1182
  };