@authhero/multi-tenancy 14.20.0 → 14.20.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.
@@ -1,30 +1,30 @@
1
- var ee = Object.defineProperty;
2
- var te = (e, t, n) => t in e ? ee(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var E = (e, t, n) => te(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { Hono as ne } from "hono";
5
- import { MANAGEMENT_API_SCOPES as re, MANAGEMENT_API_AUDIENCE as J, fetchAll as D, auth0QuerySchema as ae, tenantSchema as q, tenantInsertSchema as U, deepMergePatch as se, connectionSchema as oe, connectionOptionsSchema as ie, init as ce } from "authhero";
6
- import { OpenAPIHono as le, createRoute as M, z as I } from "@hono/zod-openapi";
7
- function ue(e) {
1
+ var ne = Object.defineProperty;
2
+ var re = (e, t, n) => t in e ? ne(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var E = (e, t, n) => re(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { Hono as ae } from "hono";
5
+ import { MANAGEMENT_API_SCOPES as se, MANAGEMENT_API_AUDIENCE as Y, fetchAll as D, auth0QuerySchema as oe, tenantSchema as O, tenantInsertSchema as G, deepMergePatch as ie, connectionSchema as ce, connectionOptionsSchema as le, init as ue } from "authhero";
6
+ import { OpenAPIHono as de, createRoute as k, z as A } from "@hono/zod-openapi";
7
+ function fe(e) {
8
8
  const { controlPlaneTenantId: t, requireOrganizationMatch: n = !0 } = e;
9
9
  return {
10
10
  async onTenantAccessValidation(r, a) {
11
11
  if (a === t)
12
12
  return !0;
13
13
  if (n) {
14
- const s = r.var.org_name, c = r.var.organization_id, i = s || c;
15
- return i ? i.toLowerCase() === a.toLowerCase() : !1;
14
+ const s = r.var.org_name, i = r.var.organization_id, c = s || i;
15
+ return c ? c.toLowerCase() === a.toLowerCase() : !1;
16
16
  }
17
17
  return !0;
18
18
  }
19
19
  };
20
20
  }
21
- function de(e, t, n, r) {
21
+ function me(e, t, n, r) {
22
22
  if (t === n)
23
23
  return !0;
24
24
  const a = r || e;
25
25
  return a ? a.toLowerCase() === t.toLowerCase() : !1;
26
26
  }
27
- function fe(e) {
27
+ function ge(e) {
28
28
  return {
29
29
  async resolveDataAdapters(t) {
30
30
  try {
@@ -39,31 +39,31 @@ function fe(e) {
39
39
  }
40
40
  };
41
41
  }
42
- function me(e) {
42
+ function we(e) {
43
43
  return `urn:authhero:tenant:${e.toLowerCase()}`;
44
44
  }
45
- function X(e) {
45
+ function Z(e) {
46
46
  return {
47
47
  async beforeCreate(t, n) {
48
48
  return !n.audience && n.id ? {
49
49
  ...n,
50
- audience: me(n.id)
50
+ audience: we(n.id)
51
51
  } : n;
52
52
  },
53
53
  async afterCreate(t, n) {
54
54
  const { accessControl: r, databaseIsolation: a } = e;
55
- r && t.ctx && await ge(t, n, r), a != null && a.onProvision && await a.onProvision(n.id);
55
+ r && t.ctx && await he(t, n, r), a != null && a.onProvision && await a.onProvision(n.id);
56
56
  },
57
57
  async beforeDelete(t, n) {
58
58
  const { accessControl: r, databaseIsolation: a } = e;
59
59
  if (r)
60
60
  try {
61
- const c = (await t.adapters.organizations.list(
61
+ const i = (await t.adapters.organizations.list(
62
62
  r.controlPlaneTenantId
63
- )).organizations.find((i) => i.name === n);
64
- c && await t.adapters.organizations.remove(
63
+ )).organizations.find((c) => c.name === n);
64
+ i && await t.adapters.organizations.remove(
65
65
  r.controlPlaneTenantId,
66
- c.id
66
+ i.id
67
67
  );
68
68
  } catch (s) {
69
69
  console.warn(
@@ -83,13 +83,13 @@ function X(e) {
83
83
  }
84
84
  };
85
85
  }
86
- async function ge(e, t, n) {
86
+ async function he(e, t, n) {
87
87
  const {
88
88
  controlPlaneTenantId: r,
89
89
  defaultPermissions: a,
90
90
  defaultRoles: s,
91
- issuer: c,
92
- adminRoleName: i = "Tenant Admin",
91
+ issuer: i,
92
+ adminRoleName: c = "Tenant Admin",
93
93
  adminRoleDescription: u = "Full access to all tenant management operations",
94
94
  addCreatorToOrganization: o = !0
95
95
  } = n, l = await e.adapters.organizations.create(
@@ -100,14 +100,14 @@ async function ge(e, t, n) {
100
100
  }
101
101
  );
102
102
  let g;
103
- if (c && (g = await pe(
103
+ if (i && (g = await ye(
104
104
  e,
105
105
  r,
106
- i,
106
+ c,
107
107
  u
108
108
  )), o && e.ctx) {
109
109
  const d = e.ctx.var.user;
110
- if (d != null && d.sub && !await we(
110
+ if (d != null && d.sub && !await pe(
111
111
  e,
112
112
  r,
113
113
  d.sub
@@ -123,10 +123,10 @@ async function ge(e, t, n) {
123
123
  l.id
124
124
  // organizationId
125
125
  );
126
- } catch (m) {
126
+ } catch (f) {
127
127
  console.warn(
128
128
  `Failed to add creator ${d.sub} to organization ${l.id}:`,
129
- m
129
+ f
130
130
  );
131
131
  }
132
132
  }
@@ -136,7 +136,7 @@ async function ge(e, t, n) {
136
136
  `Would grant permissions ${a.join(", ")} to organization ${l.id}`
137
137
  );
138
138
  }
139
- async function we(e, t, n) {
139
+ async function pe(e, t, n) {
140
140
  const r = await e.adapters.userRoles.list(
141
141
  t,
142
142
  n,
@@ -150,32 +150,32 @@ async function we(e, t, n) {
150
150
  a.id,
151
151
  { per_page: 1e3 }
152
152
  )).some(
153
- (i) => i.permission_name === "admin:organizations"
153
+ (c) => c.permission_name === "admin:organizations"
154
154
  ))
155
155
  return !0;
156
156
  return !1;
157
157
  }
158
- async function pe(e, t, n, r) {
158
+ async function ye(e, t, n, r) {
159
159
  const s = (await e.adapters.roles.list(t, {})).roles.find((o) => o.name === n);
160
160
  if (s)
161
161
  return s.id;
162
- const c = await e.adapters.roles.create(t, {
162
+ const i = await e.adapters.roles.create(t, {
163
163
  name: n,
164
164
  description: r
165
- }), i = J, u = re.map((o) => ({
166
- role_id: c.id,
167
- resource_server_identifier: i,
165
+ }), c = Y, u = se.map((o) => ({
166
+ role_id: i.id,
167
+ resource_server_identifier: c,
168
168
  permission_name: o.value
169
169
  }));
170
170
  return await e.adapters.rolePermissions.assign(
171
171
  t,
172
- c.id,
172
+ i.id,
173
173
  u
174
- ), c.id;
174
+ ), i.id;
175
175
  }
176
- function G(e, t, n = () => !0) {
177
- const { controlPlaneTenantId: r, getChildTenantIds: a, getAdapters: s } = e, c = /* @__PURE__ */ new Map();
178
- async function i(l, g, d) {
176
+ function H(e, t, n = () => !0) {
177
+ const { controlPlaneTenantId: r, getChildTenantIds: a, getAdapters: s } = e, i = /* @__PURE__ */ new Map();
178
+ async function c(l, g, d) {
179
179
  return (await t(l).list(g, {
180
180
  q: `name:${d}`,
181
181
  per_page: 1
@@ -184,21 +184,21 @@ function G(e, t, n = () => !0) {
184
184
  async function u(l) {
185
185
  const g = await a(), d = t(await s(r));
186
186
  await Promise.all(
187
- g.map(async (f) => {
187
+ g.map(async (m) => {
188
188
  try {
189
- const m = await s(f), w = t(m), y = {
189
+ const f = await s(m), w = t(f), y = {
190
190
  ...d.transform(l),
191
191
  is_system: !0
192
- }, _ = await i(m, f, l.name), b = _ ? w.getId(_) : void 0;
192
+ }, _ = await c(f, m, l.name), b = _ ? w.getId(_) : void 0;
193
193
  if (_ && b) {
194
- const P = w.preserveOnUpdate ? w.preserveOnUpdate(_, y) : y;
195
- await w.update(f, b, P);
194
+ const I = w.preserveOnUpdate ? w.preserveOnUpdate(_, y) : y;
195
+ await w.update(m, b, I);
196
196
  } else
197
- await w.create(f, y);
198
- } catch (m) {
197
+ await w.create(m, y);
198
+ } catch (f) {
199
199
  console.error(
200
- `Failed to sync ${d.listKey} "${l.name}" to tenant "${f}":`,
201
- m
200
+ `Failed to sync ${d.listKey} "${l.name}" to tenant "${m}":`,
201
+ f
202
202
  );
203
203
  }
204
204
  })
@@ -209,12 +209,12 @@ function G(e, t, n = () => !0) {
209
209
  await Promise.all(
210
210
  g.map(async (d) => {
211
211
  try {
212
- const f = await s(d), m = t(f), w = await i(f, d, l), C = w ? m.getId(w) : void 0;
213
- w && C && await m.remove(d, C);
214
- } catch (f) {
212
+ const m = await s(d), f = t(m), w = await c(m, d, l), T = w ? f.getId(w) : void 0;
213
+ w && T && await f.remove(d, T);
214
+ } catch (m) {
215
215
  console.error(
216
216
  `Failed to delete entity "${l}" from tenant "${d}":`,
217
- f
217
+ m
218
218
  );
219
219
  }
220
220
  })
@@ -229,53 +229,53 @@ function G(e, t, n = () => !0) {
229
229
  },
230
230
  beforeDelete: async (l, g) => {
231
231
  if (l.tenantId !== r) return;
232
- const f = await t(l.adapters).get(l.tenantId, g);
233
- f && n(f) && c.set(g, f);
232
+ const m = await t(l.adapters).get(l.tenantId, g);
233
+ m && n(m) && i.set(g, m);
234
234
  },
235
235
  afterDelete: async (l, g) => {
236
236
  if (l.tenantId !== r) return;
237
- const d = c.get(g);
238
- d && (c.delete(g), await o(d.name));
237
+ const d = i.get(g);
238
+ d && (i.delete(g), await o(d.name));
239
239
  }
240
240
  };
241
241
  }
242
- function L(e, t, n = () => !0) {
242
+ function W(e, t, n = () => !0) {
243
243
  const { controlPlaneTenantId: r, getControlPlaneAdapters: a, getAdapters: s } = e;
244
244
  return {
245
- async afterCreate(c, i) {
246
- if (i.id !== r)
245
+ async afterCreate(i, c) {
246
+ if (c.id !== r)
247
247
  try {
248
- const u = await a(), o = await s(i.id), l = t(u), g = t(o), d = await D(
249
- (f) => l.listPaginated(r, f),
248
+ const u = await a(), o = await s(c.id), l = t(u), g = t(o), d = await D(
249
+ (m) => l.listPaginated(r, m),
250
250
  l.listKey,
251
251
  { cursorField: "id", pageSize: 100 }
252
252
  );
253
253
  await Promise.all(
254
- d.filter((f) => n(f)).map(async (f) => {
254
+ d.filter((m) => n(m)).map(async (m) => {
255
255
  try {
256
- const m = l.transform(f);
257
- await g.create(i.id, {
258
- ...m,
256
+ const f = l.transform(m);
257
+ await g.create(c.id, {
258
+ ...f,
259
259
  is_system: !0
260
260
  });
261
- } catch (m) {
261
+ } catch (f) {
262
262
  console.error(
263
- `Failed to sync entity to new tenant "${i.id}":`,
264
- m
263
+ `Failed to sync entity to new tenant "${c.id}":`,
264
+ f
265
265
  );
266
266
  }
267
267
  })
268
268
  );
269
269
  } catch (u) {
270
270
  console.error(
271
- `Failed to sync entities to new tenant "${i.id}":`,
271
+ `Failed to sync entities to new tenant "${c.id}":`,
272
272
  u
273
273
  );
274
274
  }
275
275
  }
276
276
  };
277
277
  }
278
- const W = (e) => ({
278
+ const L = (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),
@@ -293,7 +293,7 @@ const W = (e) => ({
293
293
  token_lifetime: t.token_lifetime,
294
294
  token_lifetime_for_web: t.token_lifetime_for_web
295
295
  })
296
- }), H = (e) => ({
296
+ }), K = (e) => ({
297
297
  list: async (t, n) => (await e.roles.list(t, n)).roles,
298
298
  listPaginated: (t, n) => e.roles.list(t, n),
299
299
  get: (t, n) => e.roles.get(t, n),
@@ -308,77 +308,77 @@ const W = (e) => ({
308
308
  description: t.description
309
309
  })
310
310
  });
311
- function K(e) {
311
+ function Q(e) {
312
312
  var t;
313
313
  return ((t = e.metadata) == null ? void 0 : t.sync) !== !1;
314
314
  }
315
- function he(e) {
316
- const { sync: t = {}, filters: n = {} } = e, r = t.resourceServers ?? !0, a = t.roles ?? !0, s = (m) => K(m) ? n.resourceServers ? n.resourceServers(m) : !0 : !1, c = (m) => K(m) ? n.roles ? n.roles(m) : !0 : !1, i = r ? G(
315
+ function ve(e) {
316
+ const { sync: t = {}, filters: n = {} } = e, r = t.resourceServers ?? !0, a = t.roles ?? !0, s = (f) => Q(f) ? n.resourceServers ? n.resourceServers(f) : !0 : !1, i = (f) => Q(f) ? n.roles ? n.roles(f) : !0 : !1, c = r ? H(
317
317
  e,
318
- W,
318
+ L,
319
319
  s
320
- ) : void 0, u = a ? G(e, H, c) : void 0, o = r ? L(
320
+ ) : void 0, u = a ? H(e, K, i) : void 0, o = r ? W(
321
321
  e,
322
- W,
322
+ L,
323
323
  s
324
- ) : void 0, l = a ? L(
324
+ ) : void 0, l = a ? W(
325
325
  e,
326
- H,
327
- c
326
+ K,
327
+ i
328
328
  ) : void 0, g = a ? {
329
- async afterCreate(m, w) {
330
- var C;
329
+ async afterCreate(f, w) {
330
+ var T;
331
331
  if (w.id !== e.controlPlaneTenantId) {
332
- await ((C = l == null ? void 0 : l.afterCreate) == null ? void 0 : C.call(l, m, w));
332
+ await ((T = l == null ? void 0 : l.afterCreate) == null ? void 0 : T.call(l, f, w));
333
333
  try {
334
334
  const y = await e.getControlPlaneAdapters(), _ = await e.getAdapters(w.id), b = await D(
335
- (p) => y.roles.list(
335
+ (h) => y.roles.list(
336
336
  e.controlPlaneTenantId,
337
- p
337
+ h
338
338
  ),
339
339
  "roles",
340
340
  { cursorField: "id", pageSize: 100 }
341
- ), P = /* @__PURE__ */ new Map();
342
- for (const p of b.filter(
343
- (T) => {
344
- var h;
345
- return ((h = n.roles) == null ? void 0 : h.call(n, T)) ?? !0;
341
+ ), I = /* @__PURE__ */ new Map();
342
+ for (const h of b.filter(
343
+ (C) => {
344
+ var p;
345
+ return ((p = n.roles) == null ? void 0 : p.call(n, C)) ?? !0;
346
346
  }
347
347
  )) {
348
- const T = await d(
348
+ const C = await d(
349
349
  _,
350
350
  w.id,
351
- p.name
351
+ h.name
352
352
  );
353
- T && P.set(p.name, T.id);
353
+ C && I.set(h.name, C.id);
354
354
  }
355
- for (const p of b.filter(
356
- (T) => {
357
- var h;
358
- return ((h = n.roles) == null ? void 0 : h.call(n, T)) ?? !0;
355
+ for (const h of b.filter(
356
+ (C) => {
357
+ var p;
358
+ return ((p = n.roles) == null ? void 0 : p.call(n, C)) ?? !0;
359
359
  }
360
360
  )) {
361
- const T = P.get(p.name);
362
- if (T)
361
+ const C = I.get(h.name);
362
+ if (C)
363
363
  try {
364
- const h = await y.rolePermissions.list(
364
+ const p = await y.rolePermissions.list(
365
365
  e.controlPlaneTenantId,
366
- p.id,
366
+ h.id,
367
367
  {}
368
368
  );
369
- h.length > 0 && await _.rolePermissions.assign(
369
+ p.length > 0 && await _.rolePermissions.assign(
370
370
  w.id,
371
- T,
372
- h.map((z) => ({
373
- role_id: T,
374
- resource_server_identifier: z.resource_server_identifier,
375
- permission_name: z.permission_name
371
+ C,
372
+ p.map((F) => ({
373
+ role_id: C,
374
+ resource_server_identifier: F.resource_server_identifier,
375
+ permission_name: F.permission_name
376
376
  }))
377
377
  );
378
- } catch (h) {
378
+ } catch (p) {
379
379
  console.error(
380
- `Failed to sync permissions for role "${p.name}" to tenant "${w.id}":`,
381
- h
380
+ `Failed to sync permissions for role "${h.name}" to tenant "${w.id}":`,
381
+ p
382
382
  );
383
383
  }
384
384
  }
@@ -391,27 +391,27 @@ function he(e) {
391
391
  }
392
392
  }
393
393
  } : void 0;
394
- async function d(m, w, C) {
395
- return (await m.roles.list(w, {
396
- q: `name:${C}`,
394
+ async function d(f, w, T) {
395
+ return (await f.roles.list(w, {
396
+ q: `name:${T}`,
397
397
  per_page: 1
398
398
  })).roles[0] ?? null;
399
399
  }
400
400
  return {
401
401
  entityHooks: {
402
- resourceServers: i,
402
+ resourceServers: c,
403
403
  roles: u
404
404
  },
405
405
  tenantHooks: {
406
- async afterCreate(m, w) {
407
- const C = [
406
+ async afterCreate(f, w) {
407
+ const T = [
408
408
  o == null ? void 0 : o.afterCreate,
409
409
  (g == null ? void 0 : g.afterCreate) ?? (l == null ? void 0 : l.afterCreate)
410
410
  ], y = [];
411
- for (const _ of C)
411
+ for (const _ of T)
412
412
  if (_)
413
413
  try {
414
- await _(m, w);
414
+ await _(f, w);
415
415
  } catch (b) {
416
416
  y.push(b instanceof Error ? b : new Error(String(b)));
417
417
  }
@@ -425,7 +425,7 @@ function he(e) {
425
425
  }
426
426
  };
427
427
  }
428
- var A = class extends Error {
428
+ var P = class extends Error {
429
429
  /**
430
430
  * Creates an instance of `HTTPException`.
431
431
  * @param status - HTTP status code for the exception. Defaults to 500.
@@ -452,29 +452,29 @@ var A = class extends Error {
452
452
  }
453
453
  };
454
454
  function N(e, t) {
455
- const n = new le();
455
+ const n = new de();
456
456
  return n.openapi(
457
- M({
457
+ k({
458
458
  tags: ["tenants"],
459
459
  method: "get",
460
460
  path: "/",
461
461
  request: {
462
- query: ae
462
+ query: oe
463
463
  },
464
464
  security: [
465
465
  {
466
- Bearer: []
466
+ Bearer: ["read:tenants", "auth:read"]
467
467
  }
468
468
  ],
469
469
  responses: {
470
470
  200: {
471
471
  content: {
472
472
  "application/json": {
473
- schema: I.object({
474
- tenants: I.array(q),
475
- start: I.number().optional(),
476
- limit: I.number().optional(),
477
- length: I.number().optional()
473
+ schema: A.object({
474
+ tenants: A.array(O),
475
+ start: A.number().optional(),
476
+ limit: A.number().optional(),
477
+ length: A.number().optional()
478
478
  })
479
479
  }
480
480
  },
@@ -483,75 +483,75 @@ function N(e, t) {
483
483
  }
484
484
  }),
485
485
  async (r) => {
486
- var m, w, C, y, _, b;
487
- const a = r.req.valid("query"), { page: s, per_page: c, include_totals: i, q: u } = a, o = r.var.user, l = (o == null ? void 0 : o.permissions) || [];
488
- if (l.includes("auth:read") || l.includes("admin:organizations")) {
489
- const P = await r.env.data.tenants.list({
486
+ var w, T, y, _, b, I;
487
+ const a = r.req.valid("query"), { page: s, per_page: i, include_totals: c, q: u } = a, o = r.var.user, l = (o == null ? void 0 : o.permissions) || [];
488
+ if (!!!((o == null ? void 0 : o.org_id) ?? r.var.organization_id) && (l.includes("auth:read") || l.includes("admin:organizations"))) {
489
+ const h = await r.env.data.tenants.list({
490
490
  page: s,
491
- per_page: c,
492
- include_totals: i,
491
+ per_page: i,
492
+ include_totals: c,
493
493
  q: u
494
494
  });
495
- return i ? r.json({
496
- tenants: P.tenants,
497
- start: ((m = P.totals) == null ? void 0 : m.start) ?? 0,
498
- limit: ((w = P.totals) == null ? void 0 : w.limit) ?? c,
499
- length: P.tenants.length
500
- }) : r.json({ tenants: P.tenants });
495
+ return c ? r.json({
496
+ tenants: h.tenants,
497
+ start: ((w = h.totals) == null ? void 0 : w.start) ?? 0,
498
+ limit: ((T = h.totals) == null ? void 0 : T.limit) ?? i,
499
+ length: h.tenants.length
500
+ }) : r.json({ tenants: h.tenants });
501
501
  }
502
- const d = ((C = e.accessControl) == null ? void 0 : C.controlPlaneTenantId) ?? ((y = r.env.data.multiTenancyConfig) == null ? void 0 : y.controlPlaneTenantId);
503
- if (d && (o != null && o.sub)) {
504
- const p = (await D(
505
- (R) => r.env.data.userOrganizations.listUserOrganizations(
506
- d,
502
+ const m = ((y = e.accessControl) == null ? void 0 : y.controlPlaneTenantId) ?? ((_ = r.env.data.multiTenancyConfig) == null ? void 0 : _.controlPlaneTenantId);
503
+ if (m && (o != null && o.sub)) {
504
+ const C = (await D(
505
+ (M) => r.env.data.userOrganizations.listUserOrganizations(
506
+ m,
507
507
  o.sub,
508
- R
508
+ M
509
509
  ),
510
510
  "organizations"
511
- )).map((R) => R.name);
512
- if (p.length === 0)
513
- return i ? r.json({
511
+ )).map((M) => M.name);
512
+ if (C.length === 0)
513
+ return c ? r.json({
514
514
  tenants: [],
515
515
  start: 0,
516
- limit: c ?? 50,
516
+ limit: i ?? 50,
517
517
  length: 0
518
518
  }) : r.json({ tenants: [] });
519
- const T = p.length, h = s ?? 0, z = c ?? 50, F = h * z, j = p.slice(F, F + z);
520
- if (j.length === 0)
521
- return i ? r.json({
519
+ const p = C.length, F = s ?? 0, $ = i ?? 50, R = F * $, S = C.slice(R, R + $);
520
+ if (S.length === 0)
521
+ return c ? r.json({
522
522
  tenants: [],
523
- start: F,
524
- limit: z,
525
- length: T
523
+ start: R,
524
+ limit: $,
525
+ length: p
526
526
  }) : r.json({ tenants: [] });
527
- const S = j.map((R) => `id:${R}`).join(" OR "), v = u ? `(${S}) AND (${u})` : S, $ = await r.env.data.tenants.list({
528
- q: v,
529
- per_page: z,
527
+ const v = S.map((M) => `id:${M}`).join(" OR "), j = u ? `(${v}) AND (${u})` : v, z = await r.env.data.tenants.list({
528
+ q: j,
529
+ per_page: $,
530
530
  include_totals: !1
531
531
  // We calculate totals from accessibleTenantIds
532
532
  });
533
- return i ? r.json({
534
- tenants: $.tenants,
535
- start: F,
536
- limit: z,
537
- length: T
538
- }) : r.json({ tenants: $.tenants });
533
+ return c ? r.json({
534
+ tenants: z.tenants,
535
+ start: R,
536
+ limit: $,
537
+ length: p
538
+ }) : r.json({ tenants: z.tenants });
539
539
  }
540
540
  const f = await r.env.data.tenants.list({
541
541
  page: s,
542
- per_page: c,
543
- include_totals: i,
542
+ per_page: i,
543
+ include_totals: c,
544
544
  q: u
545
545
  });
546
- return i ? r.json({
546
+ return c ? r.json({
547
547
  tenants: f.tenants,
548
- start: ((_ = f.totals) == null ? void 0 : _.start) ?? 0,
549
- limit: ((b = f.totals) == null ? void 0 : b.limit) ?? c,
548
+ start: ((b = f.totals) == null ? void 0 : b.start) ?? 0,
549
+ limit: ((I = f.totals) == null ? void 0 : I.limit) ?? i,
550
550
  length: f.tenants.length
551
551
  }) : r.json({ tenants: f.tenants });
552
552
  }
553
553
  ), n.openapi(
554
- M({
554
+ k({
555
555
  tags: ["tenants"],
556
556
  method: "post",
557
557
  path: "/",
@@ -559,7 +559,7 @@ function N(e, t) {
559
559
  body: {
560
560
  content: {
561
561
  "application/json": {
562
- schema: U
562
+ schema: G
563
563
  }
564
564
  }
565
565
  }
@@ -573,7 +573,7 @@ function N(e, t) {
573
573
  201: {
574
574
  content: {
575
575
  "application/json": {
576
- schema: q
576
+ schema: O
577
577
  }
578
578
  },
579
579
  description: "Tenant created"
@@ -590,26 +590,26 @@ function N(e, t) {
590
590
  var u, o;
591
591
  const a = r.var.user;
592
592
  if (!(a != null && a.sub))
593
- throw new A(401, {
593
+ throw new P(401, {
594
594
  message: "Authentication required to create tenants"
595
595
  });
596
596
  let s = r.req.valid("json");
597
- const c = {
597
+ const i = {
598
598
  adapters: r.env.data,
599
599
  ctx: r
600
600
  };
601
- (u = t.tenants) != null && u.beforeCreate && (s = await t.tenants.beforeCreate(c, s));
602
- const i = await r.env.data.tenants.create(s);
603
- return (o = t.tenants) != null && o.afterCreate && await t.tenants.afterCreate(c, i), r.json(i, 201);
601
+ (u = t.tenants) != null && u.beforeCreate && (s = await t.tenants.beforeCreate(i, s));
602
+ const c = await r.env.data.tenants.create(s);
603
+ return (o = t.tenants) != null && o.afterCreate && await t.tenants.afterCreate(i, c), r.json(c, 201);
604
604
  }
605
605
  ), n.openapi(
606
- M({
606
+ k({
607
607
  tags: ["tenants"],
608
608
  method: "delete",
609
609
  path: "/{id}",
610
610
  request: {
611
- params: I.object({
612
- id: I.string()
611
+ params: A.object({
612
+ id: A.string()
613
613
  })
614
614
  },
615
615
  security: [
@@ -635,11 +635,11 @@ function N(e, t) {
635
635
  if (s) {
636
636
  const d = r.var.user;
637
637
  if (!(d != null && d.sub))
638
- throw new A(401, {
638
+ throw new P(401, {
639
639
  message: "Authentication required"
640
640
  });
641
641
  if (a === s)
642
- throw new A(403, {
642
+ throw new P(403, {
643
643
  message: "Cannot delete the control plane"
644
644
  });
645
645
  if (!(await D(
@@ -650,28 +650,28 @@ function N(e, t) {
650
650
  ),
651
651
  "organizations"
652
652
  )).some((w) => w.name === a))
653
- throw new A(403, {
653
+ throw new P(403, {
654
654
  message: "Access denied to this tenant"
655
655
  });
656
656
  }
657
657
  if (!await r.env.data.tenants.get(a))
658
- throw new A(404, {
658
+ throw new P(404, {
659
659
  message: "Tenant not found"
660
660
  });
661
- const i = {
661
+ const c = {
662
662
  adapters: r.env.data,
663
663
  ctx: r
664
664
  };
665
- return (l = t.tenants) != null && l.beforeDelete && await t.tenants.beforeDelete(i, a), await r.env.data.tenants.remove(a), (g = t.tenants) != null && g.afterDelete && await t.tenants.afterDelete(i, a), r.body(null, 204);
665
+ return (l = t.tenants) != null && l.beforeDelete && await t.tenants.beforeDelete(c, a), await r.env.data.tenants.remove(a), (g = t.tenants) != null && g.afterDelete && await t.tenants.afterDelete(c, a), r.body(null, 204);
666
666
  }
667
667
  ), n.openapi(
668
- M({
668
+ k({
669
669
  tags: ["tenants", "settings"],
670
670
  method: "get",
671
671
  path: "/settings",
672
672
  request: {
673
- headers: I.object({
674
- "tenant-id": I.string().optional()
673
+ headers: A.object({
674
+ "tenant-id": A.string().optional()
675
675
  })
676
676
  },
677
677
  security: [
@@ -683,7 +683,7 @@ function N(e, t) {
683
683
  200: {
684
684
  content: {
685
685
  "application/json": {
686
- schema: q
686
+ schema: O
687
687
  }
688
688
  },
689
689
  description: "Current tenant settings"
@@ -693,24 +693,24 @@ function N(e, t) {
693
693
  async (r) => {
694
694
  const a = await r.env.data.tenants.get(r.var.tenant_id);
695
695
  if (!a)
696
- throw new A(404, {
696
+ throw new P(404, {
697
697
  message: "Tenant not found"
698
698
  });
699
699
  return r.json(a);
700
700
  }
701
701
  ), n.openapi(
702
- M({
702
+ k({
703
703
  tags: ["tenants", "settings"],
704
704
  method: "patch",
705
705
  path: "/settings",
706
706
  request: {
707
- headers: I.object({
708
- "tenant-id": I.string().optional()
707
+ headers: A.object({
708
+ "tenant-id": A.string().optional()
709
709
  }),
710
710
  body: {
711
711
  content: {
712
712
  "application/json": {
713
- schema: I.object(U.shape).partial()
713
+ schema: A.object(G.shape).partial()
714
714
  }
715
715
  }
716
716
  }
@@ -724,7 +724,7 @@ function N(e, t) {
724
724
  200: {
725
725
  content: {
726
726
  "application/json": {
727
- schema: q
727
+ schema: O
728
728
  }
729
729
  },
730
730
  description: "Updated tenant settings"
@@ -732,23 +732,23 @@ function N(e, t) {
732
732
  }
733
733
  }),
734
734
  async (r) => {
735
- const a = r.req.valid("json"), { id: s, ...c } = a, i = await r.env.data.tenants.get(r.var.tenant_id);
736
- if (!i)
737
- throw new A(404, {
735
+ const a = r.req.valid("json"), { id: s, ...i } = a, c = await r.env.data.tenants.get(r.var.tenant_id);
736
+ if (!c)
737
+ throw new P(404, {
738
738
  message: "Tenant not found"
739
739
  });
740
- const u = se(i, c);
740
+ const u = ie(c, i);
741
741
  await r.env.data.tenants.update(r.var.tenant_id, u);
742
742
  const o = await r.env.data.tenants.get(r.var.tenant_id);
743
743
  if (!o)
744
- throw new A(500, {
744
+ throw new P(500, {
745
745
  message: "Failed to retrieve updated tenant"
746
746
  });
747
747
  return r.json(o);
748
748
  }
749
749
  ), n;
750
750
  }
751
- function ye(e) {
751
+ function _e(e) {
752
752
  const t = [
753
753
  {
754
754
  pattern: /\/api\/v2\/resource-servers\/([^/]+)$/,
@@ -764,7 +764,7 @@ function ye(e) {
764
764
  }
765
765
  return null;
766
766
  }
767
- async function ve(e, t, n) {
767
+ async function Ce(e, t, n) {
768
768
  try {
769
769
  switch (n.type) {
770
770
  case "resource_server": {
@@ -786,50 +786,50 @@ async function ve(e, t, n) {
786
786
  return !1;
787
787
  }
788
788
  }
789
- function _e(e) {
789
+ function Te(e) {
790
790
  return {
791
791
  resource_server: "resource server",
792
792
  role: "role",
793
793
  connection: "connection"
794
794
  }[e];
795
795
  }
796
- function Ce() {
796
+ function be() {
797
797
  return async (e, t) => {
798
798
  if (!["PATCH", "PUT", "DELETE"].includes(e.req.method))
799
799
  return t();
800
- const n = ye(e.req.path);
800
+ const n = _e(e.req.path);
801
801
  if (!n)
802
802
  return t();
803
803
  const r = e.var.tenant_id || e.req.header("x-tenant-id") || e.req.header("tenant-id");
804
804
  if (!r)
805
805
  return t();
806
- if (await ve(e.env.data, r, n))
807
- throw new A(403, {
808
- message: `This ${_e(n.type)} is a system resource and cannot be modified. Make changes in the control plane instead.`
806
+ if (await Ce(e.env.data, r, n))
807
+ throw new P(403, {
808
+ message: `This ${Te(n.type)} is a system resource and cannot be modified. Make changes in the control plane instead.`
809
809
  });
810
810
  return t();
811
811
  };
812
812
  }
813
- function k(e, t) {
813
+ function B(e, t) {
814
814
  const n = t.find(
815
815
  (a) => a.strategy === e.strategy
816
816
  );
817
817
  if (!(n != null && n.options))
818
818
  return e;
819
- const r = oe.passthrough().parse({
819
+ const r = ce.passthrough().parse({
820
820
  ...n,
821
821
  ...e
822
822
  });
823
- return r.options = ie.passthrough().parse({
823
+ return r.options = le.passthrough().parse({
824
824
  ...n.options || {},
825
825
  ...e.options
826
826
  }), r;
827
827
  }
828
- function O(e, t) {
828
+ function q(e, t) {
829
829
  const n = [...t || [], ...e || []];
830
830
  return [...new Set(n)];
831
831
  }
832
- function Te(e, t) {
832
+ function Pe(e, t) {
833
833
  if (!(t != null && t.length))
834
834
  return e || [];
835
835
  if (!(e != null && e.length))
@@ -841,34 +841,34 @@ function Te(e, t) {
841
841
  n.set(r.value, r);
842
842
  return Array.from(n.values());
843
843
  }
844
- function Q(e, t) {
844
+ function V(e, t) {
845
845
  return t ? {
846
846
  ...e,
847
- scopes: Te(
847
+ scopes: Pe(
848
848
  e.scopes,
849
849
  t.scopes
850
850
  )
851
851
  } : e;
852
852
  }
853
- function V(e, t) {
853
+ function J(e, t) {
854
854
  return t ? {
855
855
  ...e,
856
- callbacks: O(e.callbacks, t.callbacks),
857
- web_origins: O(
856
+ callbacks: q(e.callbacks, t.callbacks),
857
+ web_origins: q(
858
858
  e.web_origins,
859
859
  t.web_origins
860
860
  ),
861
- allowed_logout_urls: O(
861
+ allowed_logout_urls: q(
862
862
  e.allowed_logout_urls,
863
863
  t.allowed_logout_urls
864
864
  ),
865
- allowed_origins: O(
865
+ allowed_origins: q(
866
866
  e.allowed_origins,
867
867
  t.allowed_origins
868
868
  )
869
869
  } : e;
870
870
  }
871
- function Y(e, t) {
871
+ function x(e, t) {
872
872
  return {
873
873
  ...e.resourceServers,
874
874
  get: async (n, r) => {
@@ -882,7 +882,7 @@ function Y(e, t) {
882
882
  t,
883
883
  r
884
884
  );
885
- return Q(
885
+ return V(
886
886
  a,
887
887
  s
888
888
  );
@@ -891,22 +891,22 @@ function Y(e, t) {
891
891
  const a = await e.resourceServers.list(n, r);
892
892
  if (!t || n === t)
893
893
  return a;
894
- const s = t, c = a.resource_servers.filter(
894
+ const s = t, i = a.resource_servers.filter(
895
895
  (o) => !!(o.is_system && o.id)
896
896
  ).map((o) => o.id);
897
- if (c.length === 0)
897
+ if (i.length === 0)
898
898
  return a;
899
- const i = /* @__PURE__ */ new Map();
899
+ const c = /* @__PURE__ */ new Map();
900
900
  await Promise.all(
901
- c.map(async (o) => {
901
+ i.map(async (o) => {
902
902
  const l = await e.resourceServers.get(s, o);
903
- l && i.set(o, l);
903
+ l && c.set(o, l);
904
904
  })
905
905
  );
906
906
  const u = a.resource_servers.map(
907
- (o) => o.is_system && o.id ? Q(
907
+ (o) => o.is_system && o.id ? V(
908
908
  o,
909
- i.get(o.id) ?? null
909
+ c.get(o.id) ?? null
910
910
  ) : o
911
911
  );
912
912
  return {
@@ -916,16 +916,16 @@ function Y(e, t) {
916
916
  }
917
917
  };
918
918
  }
919
- function be(e, t) {
919
+ function Se(e, t) {
920
920
  return {
921
921
  ...e,
922
- resourceServers: Y(
922
+ resourceServers: x(
923
923
  e,
924
924
  t.controlPlaneTenantId
925
925
  )
926
926
  };
927
927
  }
928
- function Pe(e, t) {
928
+ function Ae(e, t) {
929
929
  const { controlPlaneTenantId: n, controlPlaneClientId: r } = t;
930
930
  return {
931
931
  ...e,
@@ -937,30 +937,30 @@ function Pe(e, t) {
937
937
  connections: {
938
938
  ...e.connections,
939
939
  get: async (a, s) => {
940
- const c = await e.connections.get(
940
+ const i = await e.connections.get(
941
941
  a,
942
942
  s
943
943
  );
944
- if (!c || !n || a === n)
945
- return c;
946
- const i = await e.connections.list(n);
947
- return k(
948
- c,
949
- i.connections || []
944
+ if (!i || !n || a === n)
945
+ return i;
946
+ const c = await e.connections.list(n);
947
+ return B(
948
+ i,
949
+ c.connections || []
950
950
  );
951
951
  },
952
952
  list: async (a, s) => {
953
- const c = await e.connections.list(a, s);
953
+ const i = await e.connections.list(a, s);
954
954
  if (!n || a === n)
955
- return c;
956
- const i = await e.connections.list(n), u = c.connections.map(
957
- (o) => k(
955
+ return i;
956
+ const c = await e.connections.list(n), u = i.connections.map(
957
+ (o) => B(
958
958
  o,
959
- i.connections || []
959
+ c.connections || []
960
960
  )
961
961
  );
962
962
  return {
963
- ...c,
963
+ ...i,
964
964
  connections: u
965
965
  };
966
966
  }
@@ -968,17 +968,17 @@ function Pe(e, t) {
968
968
  clientConnections: {
969
969
  ...e.clientConnections,
970
970
  listByClient: async (a, s) => {
971
- let c = await e.clientConnections.listByClient(
971
+ let i = await e.clientConnections.listByClient(
972
972
  a,
973
973
  s
974
974
  );
975
- if (c.length === 0 && (c = (await e.connections.list(a)).connections || []), !n || a === n)
976
- return c;
977
- const i = await e.connections.list(n);
978
- return c.map(
979
- (u) => k(
975
+ if (i.length === 0 && (i = (await e.connections.list(a)).connections || []), !n || a === n)
976
+ return i;
977
+ const c = await e.connections.list(n);
978
+ return i.map(
979
+ (u) => B(
980
980
  u,
981
- i.connections || []
981
+ c.connections || []
982
982
  )
983
983
  );
984
984
  }
@@ -986,16 +986,16 @@ function Pe(e, t) {
986
986
  clients: {
987
987
  ...e.clients,
988
988
  get: async (a, s) => {
989
- const c = await e.clients.get(a, s);
990
- if (!c)
989
+ const i = await e.clients.get(a, s);
990
+ if (!i)
991
991
  return null;
992
992
  if (!n || !r || a === n && s === r)
993
- return c;
994
- const i = await e.clients.get(
993
+ return i;
994
+ const c = await e.clients.get(
995
995
  n,
996
996
  r
997
997
  );
998
- return V(c, i);
998
+ return J(i, c);
999
999
  },
1000
1000
  getByClientId: async (a) => {
1001
1001
  const s = await e.clients.getByClientId(a);
@@ -1003,12 +1003,12 @@ function Pe(e, t) {
1003
1003
  return null;
1004
1004
  if (!n || !r || s.tenant_id === n && s.client_id === r)
1005
1005
  return s;
1006
- const c = await e.clients.get(
1006
+ const i = await e.clients.get(
1007
1007
  n,
1008
1008
  r
1009
1009
  );
1010
1010
  return {
1011
- ...V(s, c),
1011
+ ...J(s, i),
1012
1012
  tenant_id: s.tenant_id
1013
1013
  };
1014
1014
  }
@@ -1020,48 +1020,88 @@ function Pe(e, t) {
1020
1020
  return s || (!n || a === n ? null : e.emailProviders.get(n));
1021
1021
  }
1022
1022
  },
1023
- resourceServers: Y(
1023
+ resourceServers: x(
1024
1024
  e,
1025
1025
  n
1026
- )
1026
+ ),
1027
+ hooks: Ie(e, n)
1027
1028
  // Note: Additional adapters can be extended here for runtime fallback:
1028
1029
  // - promptSettings: Fall back to control plane prompts
1029
1030
  // - branding: Fall back to control plane branding/themes
1030
1031
  };
1031
1032
  }
1032
- function Z(e, t) {
1033
- return Pe(e, t);
1033
+ function X(e) {
1034
+ if (!e || typeof e != "object") return !1;
1035
+ const t = e.metadata;
1036
+ return !t || typeof t != "object" ? !1 : t.inheritable === !0;
1037
+ }
1038
+ function Ie(e, t) {
1039
+ return {
1040
+ ...e.hooks,
1041
+ list: async (n, r) => {
1042
+ const a = await e.hooks.list(n, r);
1043
+ if (!t || n === t)
1044
+ return a;
1045
+ const i = ((await e.hooks.list(
1046
+ t,
1047
+ r
1048
+ )).hooks || []).filter(
1049
+ X
1050
+ );
1051
+ if (i.length === 0)
1052
+ return a;
1053
+ const c = new Set((a.hooks || []).map((o) => o.hook_id)), u = i.filter((o) => !c.has(o.hook_id));
1054
+ return {
1055
+ ...a,
1056
+ hooks: [...a.hooks || [], ...u],
1057
+ length: typeof a.length == "number" ? a.length + u.length : a.length
1058
+ };
1059
+ },
1060
+ get: async (n, r) => {
1061
+ const a = await e.hooks.get(n, r);
1062
+ if (a || !t || n === t)
1063
+ return a;
1064
+ const s = await e.hooks.get(
1065
+ t,
1066
+ r
1067
+ );
1068
+ return s && X(s) ? s : null;
1069
+ }
1070
+ };
1034
1071
  }
1035
- function Ae(e) {
1072
+ function ee(e, t) {
1073
+ return Ae(e, t);
1074
+ }
1075
+ function Re(e) {
1036
1076
  return async (t, n) => {
1037
1077
  const r = t.var.user;
1038
1078
  return (r == null ? void 0 : r.tenant_id) === e && r.org_name && t.set("tenant_id", r.org_name), n();
1039
1079
  };
1040
1080
  }
1041
- function Se(e) {
1081
+ function ze(e) {
1042
1082
  return async (t, n) => {
1043
1083
  if (!e.accessControl)
1044
1084
  return n();
1045
- const { controlPlaneTenantId: r } = e.accessControl, a = t.var.org_name, s = t.var.organization_id, c = a || s;
1046
- let i = t.var.tenant_id;
1047
- const u = t.var.user, l = (u != null && u.aud ? Array.isArray(u.aud) ? u.aud : [u.aud] : []).includes(J);
1048
- if (!i && c && l && (t.set("tenant_id", c), i = c), !i)
1049
- throw new A(400, {
1085
+ const { controlPlaneTenantId: r } = e.accessControl, a = t.var.org_name, s = t.var.organization_id, i = a || s;
1086
+ let c = t.var.tenant_id;
1087
+ const u = t.var.user, l = (u != null && u.aud ? Array.isArray(u.aud) ? u.aud : [u.aud] : []).includes(Y);
1088
+ if (!c && i && l && (t.set("tenant_id", i), c = i), !c)
1089
+ throw new P(400, {
1050
1090
  message: "Tenant ID not found in request"
1051
1091
  });
1052
- if (!de(
1092
+ if (!me(
1053
1093
  s,
1054
- i,
1094
+ c,
1055
1095
  r,
1056
1096
  a
1057
1097
  ))
1058
- throw new A(403, {
1059
- message: `Access denied to tenant ${i}`
1098
+ throw new P(403, {
1099
+ message: `Access denied to tenant ${c}`
1060
1100
  });
1061
1101
  return n();
1062
1102
  };
1063
1103
  }
1064
- function Ie(e) {
1104
+ function $e(e) {
1065
1105
  return async (t, n) => {
1066
1106
  if (!e.subdomainRouting)
1067
1107
  return n();
@@ -1069,40 +1109,40 @@ function Ie(e) {
1069
1109
  baseDomain: r,
1070
1110
  reservedSubdomains: a = [],
1071
1111
  resolveSubdomain: s
1072
- } = e.subdomainRouting, c = t.req.header("x-forwarded-host") || t.req.header("host") || "";
1073
- let i = null;
1074
- if (c.endsWith(r)) {
1075
- const o = c.slice(0, -(r.length + 1));
1076
- o && !o.includes(".") && (i = o);
1112
+ } = e.subdomainRouting, i = t.req.header("x-forwarded-host") || t.req.header("host") || "";
1113
+ let c = null;
1114
+ if (i.endsWith(r)) {
1115
+ const o = i.slice(0, -(r.length + 1));
1116
+ o && !o.includes(".") && (c = o);
1077
1117
  }
1078
- if (i && a.includes(i) && (i = null), !i)
1118
+ if (c && a.includes(c) && (c = null), !c)
1079
1119
  return e.accessControl && t.set("tenant_id", e.accessControl.controlPlaneTenantId), n();
1080
1120
  let u = null;
1081
1121
  if (s)
1082
- u = await s(i);
1122
+ u = await s(c);
1083
1123
  else if (e.subdomainRouting.useOrganizations !== !1 && e.accessControl)
1084
1124
  try {
1085
1125
  const o = await t.env.data.organizations.get(
1086
1126
  e.accessControl.controlPlaneTenantId,
1087
- i
1127
+ c
1088
1128
  );
1089
1129
  o && (u = o.id);
1090
1130
  } catch {
1091
1131
  }
1092
1132
  if (!u)
1093
- throw new A(404, {
1094
- message: `Tenant not found for subdomain: ${i}`
1133
+ throw new P(404, {
1134
+ message: `Tenant not found for subdomain: ${c}`
1095
1135
  });
1096
1136
  return t.set("tenant_id", u), n();
1097
1137
  };
1098
1138
  }
1099
- function Re(e) {
1139
+ function je(e) {
1100
1140
  return async (t, n) => {
1101
1141
  if (!e.databaseIsolation)
1102
1142
  return n();
1103
1143
  const r = t.var.tenant_id;
1104
1144
  if (!r)
1105
- throw new A(400, {
1145
+ throw new P(400, {
1106
1146
  message: "Tenant ID not found in request"
1107
1147
  });
1108
1148
  try {
@@ -1112,21 +1152,21 @@ function Re(e) {
1112
1152
  throw console.error(
1113
1153
  `Failed to resolve database for tenant ${r}:`,
1114
1154
  a
1115
- ), new A(500, {
1155
+ ), new P(500, {
1116
1156
  message: "Failed to resolve tenant database"
1117
1157
  });
1118
1158
  }
1119
1159
  return n();
1120
1160
  };
1121
1161
  }
1122
- function x(e) {
1123
- const t = Ie(e), n = Se(e), r = Re(e);
1162
+ function te(e) {
1163
+ const t = $e(e), n = ze(e), r = je(e);
1124
1164
  return async (a, s) => (await t(a, async () => {
1125
1165
  }), await n(a, async () => {
1126
1166
  }), await r(a, async () => {
1127
1167
  }), s());
1128
1168
  }
1129
- function De(e) {
1169
+ function qe(e) {
1130
1170
  const {
1131
1171
  dataAdapter: t,
1132
1172
  controlPlane: n,
@@ -1135,20 +1175,20 @@ function De(e) {
1135
1175
  clientId: a
1136
1176
  } = {},
1137
1177
  sync: s = { resourceServers: !0, roles: !0 },
1138
- defaultPermissions: c = ["tenant:admin"],
1139
- requireOrganizationMatch: i = !1,
1178
+ defaultPermissions: i = ["tenant:admin"],
1179
+ requireOrganizationMatch: c = !1,
1140
1180
  managementApiExtensions: u = [],
1141
1181
  entityHooks: o,
1142
1182
  getChildTenantIds: l,
1143
1183
  getAdapters: g,
1144
1184
  ...d
1145
1185
  } = e;
1146
- let f = t, m = t;
1147
- n && (f = Z(t, {
1186
+ let m = t, f = t;
1187
+ n && (m = ee(t, {
1148
1188
  controlPlaneTenantId: r,
1149
1189
  controlPlaneClientId: a
1150
- }), m = {
1151
- ...be(t, {
1190
+ }), f = {
1191
+ ...Se(t, {
1152
1192
  controlPlaneTenantId: r
1153
1193
  }),
1154
1194
  multiTenancyConfig: {
@@ -1156,76 +1196,76 @@ function De(e) {
1156
1196
  controlPlaneClientId: a
1157
1197
  }
1158
1198
  });
1159
- const w = s !== !1, C = w ? {
1199
+ const w = s !== !1, T = w ? {
1160
1200
  resourceServers: s.resourceServers ?? !0,
1161
1201
  roles: s.roles ?? !0
1162
1202
  } : { resourceServers: !1, roles: !1 }, b = {
1163
1203
  controlPlaneTenantId: r,
1164
1204
  getChildTenantIds: l ?? (async () => (await D(
1165
- (v) => f.tenants.list(v),
1205
+ (v) => m.tenants.list(v),
1166
1206
  "tenants",
1167
1207
  { cursorField: "id", pageSize: 100 }
1168
1208
  )).filter((v) => v.id !== r).map((v) => v.id)),
1169
- getAdapters: g ?? (async () => f),
1170
- getControlPlaneAdapters: async () => f,
1171
- sync: C
1172
- }, { entityHooks: P, tenantHooks: p } = he(b), T = {
1209
+ getAdapters: g ?? (async () => m),
1210
+ getControlPlaneAdapters: async () => m,
1211
+ sync: T
1212
+ }, { entityHooks: I, tenantHooks: h } = ve(b), C = {
1173
1213
  resourceServers: [
1174
- P.resourceServers,
1214
+ I.resourceServers,
1175
1215
  ...(o == null ? void 0 : o.resourceServers) ?? []
1176
1216
  ],
1177
- roles: [P.roles, ...(o == null ? void 0 : o.roles) ?? []],
1217
+ roles: [I.roles, ...(o == null ? void 0 : o.roles) ?? []],
1178
1218
  connections: (o == null ? void 0 : o.connections) ?? [],
1179
1219
  tenants: (o == null ? void 0 : o.tenants) ?? [],
1180
1220
  rolePermissions: (o == null ? void 0 : o.rolePermissions) ?? []
1181
- }, h = X({
1221
+ }, p = Z({
1182
1222
  accessControl: {
1183
1223
  controlPlaneTenantId: r,
1184
- requireOrganizationMatch: i,
1185
- defaultPermissions: c
1224
+ requireOrganizationMatch: c,
1225
+ defaultPermissions: i
1186
1226
  }
1187
- }), F = N(
1227
+ }), $ = N(
1188
1228
  {
1189
1229
  accessControl: {
1190
1230
  controlPlaneTenantId: r,
1191
- requireOrganizationMatch: i,
1192
- defaultPermissions: c
1231
+ requireOrganizationMatch: c,
1232
+ defaultPermissions: i
1193
1233
  }
1194
1234
  },
1195
1235
  { tenants: {
1196
1236
  async beforeCreate(S, v) {
1197
- return h.beforeCreate && (v = await h.beforeCreate(S, v)), p.beforeCreate && (v = await p.beforeCreate(S, v)), v;
1237
+ return p.beforeCreate && (v = await p.beforeCreate(S, v)), h.beforeCreate && (v = await h.beforeCreate(S, v)), v;
1198
1238
  },
1199
1239
  async afterCreate(S, v) {
1200
- var $, R;
1201
- await (($ = h.afterCreate) == null ? void 0 : $.call(h, S, v)), await ((R = p.afterCreate) == null ? void 0 : R.call(p, S, v));
1240
+ var j, z;
1241
+ await ((j = p.afterCreate) == null ? void 0 : j.call(p, S, v)), await ((z = h.afterCreate) == null ? void 0 : z.call(h, S, v));
1202
1242
  },
1203
1243
  async beforeDelete(S, v) {
1204
- var $, R;
1205
- await (($ = h.beforeDelete) == null ? void 0 : $.call(h, S, v)), await ((R = p.beforeDelete) == null ? void 0 : R.call(p, S, v));
1244
+ var j, z;
1245
+ await ((j = p.beforeDelete) == null ? void 0 : j.call(p, S, v)), await ((z = h.beforeDelete) == null ? void 0 : z.call(h, S, v));
1206
1246
  }
1207
1247
  } }
1208
- ), { app: j } = ce({
1209
- dataAdapter: f,
1210
- managementDataAdapter: m,
1248
+ ), { app: R } = ue({
1249
+ dataAdapter: m,
1250
+ managementDataAdapter: f,
1211
1251
  ...d,
1212
- entityHooks: T,
1252
+ entityHooks: C,
1213
1253
  managementApiExtensions: [
1214
1254
  ...u,
1215
- { path: "/tenants", router: F }
1255
+ { path: "/tenants", router: $ }
1216
1256
  ]
1217
1257
  });
1218
- return j.use(
1258
+ return R.use(
1219
1259
  "/api/v2/*",
1220
- Ae(r)
1221
- ), w && j.use("/api/v2/*", Ce()), { app: j, controlPlaneTenantId: r };
1260
+ Re(r)
1261
+ ), w && R.use("/api/v2/*", be()), { app: R, controlPlaneTenantId: r };
1222
1262
  }
1223
- function qe(e) {
1224
- const t = B(e);
1263
+ function Ee(e) {
1264
+ const t = U(e);
1225
1265
  return {
1226
1266
  name: "multi-tenancy",
1227
1267
  // Apply multi-tenancy middleware for subdomain routing, database resolution, etc.
1228
- middleware: x(e),
1268
+ middleware: te(e),
1229
1269
  // Provide lifecycle hooks
1230
1270
  hooks: t,
1231
1271
  // Mount tenant management routes
@@ -1245,23 +1285,23 @@ function qe(e) {
1245
1285
  }
1246
1286
  };
1247
1287
  }
1248
- function B(e) {
1249
- const t = e.accessControl ? ue(e.accessControl) : {}, n = e.databaseIsolation ? fe(e.databaseIsolation) : {}, r = X(e);
1288
+ function U(e) {
1289
+ const t = e.accessControl ? fe(e.accessControl) : {}, n = e.databaseIsolation ? ge(e.databaseIsolation) : {}, r = Z(e);
1250
1290
  return {
1251
1291
  ...t,
1252
1292
  ...n,
1253
1293
  tenants: r
1254
1294
  };
1255
1295
  }
1256
- function ze(e) {
1257
- const t = new ne(), n = B(e);
1296
+ function Fe(e) {
1297
+ const t = new ae(), n = U(e);
1258
1298
  return t.route("/tenants", N(e, n)), t;
1259
1299
  }
1260
- function Oe(e) {
1300
+ function Be(e) {
1261
1301
  return {
1262
- hooks: B(e),
1263
- middleware: x(e),
1264
- app: ze(e),
1302
+ hooks: U(e),
1303
+ middleware: te(e),
1304
+ app: Fe(e),
1265
1305
  config: e,
1266
1306
  /**
1267
1307
  * Wraps data adapters with runtime fallback from the control plane.
@@ -1273,7 +1313,7 @@ function Oe(e) {
1273
1313
  */
1274
1314
  wrapAdapters: (t, n) => {
1275
1315
  var r;
1276
- return Z(t, {
1316
+ return ee(t, {
1277
1317
  controlPlaneTenantId: (r = e.accessControl) == null ? void 0 : r.controlPlaneTenantId,
1278
1318
  controlPlaneClientId: n == null ? void 0 : n.controlPlaneClientId
1279
1319
  });
@@ -1281,24 +1321,24 @@ function Oe(e) {
1281
1321
  };
1282
1322
  }
1283
1323
  export {
1284
- ue as createAccessControlHooks,
1285
- Se as createAccessControlMiddleware,
1286
- Ae as createControlPlaneTenantMiddleware,
1287
- fe as createDatabaseHooks,
1288
- Re as createDatabaseMiddleware,
1289
- ze as createMultiTenancy,
1290
- B as createMultiTenancyHooks,
1291
- x as createMultiTenancyMiddleware,
1292
- qe as createMultiTenancyPlugin,
1293
- Ce as createProtectSyncedMiddleware,
1294
- X as createProvisioningHooks,
1295
- Pe as createRuntimeFallbackAdapter,
1296
- Ie as createSubdomainMiddleware,
1297
- he as createSyncHooks,
1324
+ fe as createAccessControlHooks,
1325
+ ze as createAccessControlMiddleware,
1326
+ Re as createControlPlaneTenantMiddleware,
1327
+ ge as createDatabaseHooks,
1328
+ je as createDatabaseMiddleware,
1329
+ Fe as createMultiTenancy,
1330
+ U as createMultiTenancyHooks,
1331
+ te as createMultiTenancyMiddleware,
1332
+ Ee as createMultiTenancyPlugin,
1333
+ be as createProtectSyncedMiddleware,
1334
+ Z as createProvisioningHooks,
1335
+ Ae as createRuntimeFallbackAdapter,
1336
+ $e as createSubdomainMiddleware,
1337
+ ve as createSyncHooks,
1298
1338
  N as createTenantsOpenAPIRouter,
1299
- De as initMultiTenant,
1300
- Oe as setupMultiTenancy,
1301
- de as validateTenantAccess,
1302
- Z as withRuntimeFallback,
1303
- be as withSystemResourceServerInheritance
1339
+ qe as initMultiTenant,
1340
+ Be as setupMultiTenancy,
1341
+ me as validateTenantAccess,
1342
+ ee as withRuntimeFallback,
1343
+ Se as withSystemResourceServerInheritance
1304
1344
  };