@authhero/multi-tenancy 14.20.1 → 14.20.3

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