@authhero/multi-tenancy 14.3.0 → 14.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/multi-tenancy.cjs +1 -1
- package/dist/multi-tenancy.mjs +535 -507
- package/dist/types/hooks/sync.d.ts +0 -25
- package/dist/types/hooks/sync.d.ts.map +1 -1
- package/dist/types/index.d.ts +18 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/init.d.ts +35 -7
- package/dist/types/init.d.ts.map +1 -1
- package/dist/types/middleware/settings-inheritance.d.ts.map +1 -1
- package/dist/types/routes/tenants.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/multi-tenancy.mjs
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { Hono as
|
|
5
|
-
import { MANAGEMENT_API_SCOPES as
|
|
6
|
-
import { OpenAPIHono as
|
|
7
|
-
function
|
|
1
|
+
var X = Object.defineProperty;
|
|
2
|
+
var Y = (t, e, n) => e in t ? X(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
|
|
3
|
+
var R = (t, e, n) => Y(t, typeof e != "symbol" ? e + "" : e, n);
|
|
4
|
+
import { Hono as Z } from "hono";
|
|
5
|
+
import { MANAGEMENT_API_SCOPES as x, MANAGEMENT_API_AUDIENCE as W, fetchAll as z, auth0QuerySchema as ee, tenantSchema as H, tenantInsertSchema as te, connectionSchema as F, connectionOptionsSchema as O, init as ne } from "authhero";
|
|
6
|
+
import { OpenAPIHono as re, createRoute as M, z as S } from "@hono/zod-openapi";
|
|
7
|
+
function se(t) {
|
|
8
8
|
const { controlPlaneTenantId: e, requireOrganizationMatch: n = !0 } = t;
|
|
9
9
|
return {
|
|
10
|
-
async onTenantAccessValidation(
|
|
11
|
-
if (
|
|
10
|
+
async onTenantAccessValidation(r, s) {
|
|
11
|
+
if (s === e)
|
|
12
12
|
return !0;
|
|
13
13
|
if (n) {
|
|
14
|
-
const
|
|
15
|
-
return
|
|
14
|
+
const c = r.var.org_name, o = r.var.organization_id, a = c || o;
|
|
15
|
+
return a ? a.toLowerCase() === s.toLowerCase() : !1;
|
|
16
16
|
}
|
|
17
17
|
return !0;
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function ae(t, e, n, r) {
|
|
22
22
|
if (e === n)
|
|
23
23
|
return !0;
|
|
24
|
-
const
|
|
25
|
-
return
|
|
24
|
+
const s = r || t;
|
|
25
|
+
return s ? s.toLowerCase() === e.toLowerCase() : !1;
|
|
26
26
|
}
|
|
27
|
-
function
|
|
27
|
+
function oe(t) {
|
|
28
28
|
return {
|
|
29
29
|
async resolveDataAdapters(e) {
|
|
30
30
|
try {
|
|
@@ -39,248 +39,248 @@ function ne(t) {
|
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
-
function
|
|
42
|
+
function ie(t) {
|
|
43
43
|
return `urn:authhero:tenant:${t.toLowerCase()}`;
|
|
44
44
|
}
|
|
45
|
-
function
|
|
45
|
+
function ce(t) {
|
|
46
46
|
return {
|
|
47
47
|
async beforeCreate(e, n) {
|
|
48
48
|
return !n.audience && n.id ? {
|
|
49
49
|
...n,
|
|
50
|
-
audience:
|
|
50
|
+
audience: ie(n.id)
|
|
51
51
|
} : n;
|
|
52
52
|
},
|
|
53
53
|
async afterCreate(e, n) {
|
|
54
|
-
const { accessControl:
|
|
55
|
-
|
|
54
|
+
const { accessControl: r, databaseIsolation: s } = t;
|
|
55
|
+
r && e.ctx && await le(e, n, r), s != null && s.onProvision && await s.onProvision(n.id);
|
|
56
56
|
},
|
|
57
57
|
async beforeDelete(e, n) {
|
|
58
|
-
const { accessControl:
|
|
59
|
-
if (
|
|
58
|
+
const { accessControl: r, databaseIsolation: s } = t;
|
|
59
|
+
if (r)
|
|
60
60
|
try {
|
|
61
61
|
const o = (await e.adapters.organizations.list(
|
|
62
|
-
|
|
63
|
-
)).organizations.find((
|
|
62
|
+
r.controlPlaneTenantId
|
|
63
|
+
)).organizations.find((a) => a.name === n);
|
|
64
64
|
o && await e.adapters.organizations.remove(
|
|
65
|
-
|
|
65
|
+
r.controlPlaneTenantId,
|
|
66
66
|
o.id
|
|
67
67
|
);
|
|
68
|
-
} catch (
|
|
68
|
+
} catch (c) {
|
|
69
69
|
console.warn(
|
|
70
70
|
`Failed to remove organization for tenant ${n}:`,
|
|
71
|
-
|
|
71
|
+
c
|
|
72
72
|
);
|
|
73
73
|
}
|
|
74
|
-
if (
|
|
74
|
+
if (s != null && s.onDeprovision)
|
|
75
75
|
try {
|
|
76
|
-
await
|
|
77
|
-
} catch (
|
|
76
|
+
await s.onDeprovision(n);
|
|
77
|
+
} catch (c) {
|
|
78
78
|
console.warn(
|
|
79
79
|
`Failed to deprovision database for tenant ${n}:`,
|
|
80
|
-
|
|
80
|
+
c
|
|
81
81
|
);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
|
-
async function
|
|
86
|
+
async function le(t, e, n) {
|
|
87
87
|
const {
|
|
88
|
-
controlPlaneTenantId:
|
|
89
|
-
defaultPermissions:
|
|
90
|
-
defaultRoles:
|
|
88
|
+
controlPlaneTenantId: r,
|
|
89
|
+
defaultPermissions: s,
|
|
90
|
+
defaultRoles: c,
|
|
91
91
|
issuer: o,
|
|
92
|
-
adminRoleName:
|
|
93
|
-
adminRoleDescription:
|
|
94
|
-
addCreatorToOrganization:
|
|
95
|
-
} = n,
|
|
96
|
-
|
|
92
|
+
adminRoleName: a = "Tenant Admin",
|
|
93
|
+
adminRoleDescription: d = "Full access to all tenant management operations",
|
|
94
|
+
addCreatorToOrganization: u = !0
|
|
95
|
+
} = n, i = await t.adapters.organizations.create(
|
|
96
|
+
r,
|
|
97
97
|
{
|
|
98
98
|
name: e.id,
|
|
99
99
|
display_name: e.friendly_name || e.id
|
|
100
100
|
}
|
|
101
101
|
);
|
|
102
102
|
let f;
|
|
103
|
-
if (o && (f = await
|
|
103
|
+
if (o && (f = await ue(
|
|
104
104
|
t,
|
|
105
|
-
s,
|
|
106
105
|
r,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
106
|
+
a,
|
|
107
|
+
d
|
|
108
|
+
)), u && t.ctx) {
|
|
109
|
+
const l = t.ctx.var.user;
|
|
110
|
+
if (l != null && l.sub && !await de(
|
|
111
111
|
t,
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
r,
|
|
113
|
+
l.sub
|
|
114
114
|
))
|
|
115
115
|
try {
|
|
116
|
-
await t.adapters.userOrganizations.create(
|
|
117
|
-
user_id:
|
|
118
|
-
organization_id:
|
|
116
|
+
await t.adapters.userOrganizations.create(r, {
|
|
117
|
+
user_id: l.sub,
|
|
118
|
+
organization_id: i.id
|
|
119
119
|
}), f && await t.adapters.userRoles.create(
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
r,
|
|
121
|
+
l.sub,
|
|
122
122
|
f,
|
|
123
|
-
|
|
123
|
+
i.id
|
|
124
124
|
// organizationId
|
|
125
125
|
);
|
|
126
|
-
} catch (
|
|
126
|
+
} catch (g) {
|
|
127
127
|
console.warn(
|
|
128
|
-
`Failed to add creator ${
|
|
129
|
-
|
|
128
|
+
`Failed to add creator ${l.sub} to organization ${i.id}:`,
|
|
129
|
+
g
|
|
130
130
|
);
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
|
-
|
|
134
|
-
`Would assign roles ${
|
|
135
|
-
),
|
|
136
|
-
`Would grant permissions ${
|
|
133
|
+
c && c.length > 0 && console.log(
|
|
134
|
+
`Would assign roles ${c.join(", ")} to organization ${i.id}`
|
|
135
|
+
), s && s.length > 0 && console.log(
|
|
136
|
+
`Would grant permissions ${s.join(", ")} to organization ${i.id}`
|
|
137
137
|
);
|
|
138
138
|
}
|
|
139
|
-
async function
|
|
140
|
-
const
|
|
139
|
+
async function de(t, e, n) {
|
|
140
|
+
const r = await t.adapters.userRoles.list(
|
|
141
141
|
e,
|
|
142
142
|
n,
|
|
143
143
|
void 0,
|
|
144
144
|
""
|
|
145
145
|
// Empty string for global roles
|
|
146
146
|
);
|
|
147
|
-
for (const
|
|
147
|
+
for (const s of r)
|
|
148
148
|
if ((await t.adapters.rolePermissions.list(
|
|
149
149
|
e,
|
|
150
|
-
|
|
150
|
+
s.id,
|
|
151
151
|
{ per_page: 1e3 }
|
|
152
152
|
)).some(
|
|
153
|
-
(
|
|
153
|
+
(a) => a.permission_name === "admin:organizations"
|
|
154
154
|
))
|
|
155
155
|
return !0;
|
|
156
156
|
return !1;
|
|
157
157
|
}
|
|
158
|
-
async function
|
|
159
|
-
const
|
|
160
|
-
if (
|
|
161
|
-
return
|
|
158
|
+
async function ue(t, e, n, r) {
|
|
159
|
+
const c = (await t.adapters.roles.list(e, {})).roles.find((u) => u.name === n);
|
|
160
|
+
if (c)
|
|
161
|
+
return c.id;
|
|
162
162
|
const o = await t.adapters.roles.create(e, {
|
|
163
163
|
name: n,
|
|
164
|
-
description:
|
|
165
|
-
}),
|
|
164
|
+
description: r
|
|
165
|
+
}), a = W, d = x.map((u) => ({
|
|
166
166
|
role_id: o.id,
|
|
167
|
-
resource_server_identifier:
|
|
168
|
-
permission_name:
|
|
167
|
+
resource_server_identifier: a,
|
|
168
|
+
permission_name: u.value
|
|
169
169
|
}));
|
|
170
170
|
return await t.adapters.rolePermissions.assign(
|
|
171
171
|
e,
|
|
172
172
|
o.id,
|
|
173
|
-
|
|
173
|
+
d
|
|
174
174
|
), o.id;
|
|
175
175
|
}
|
|
176
|
-
function
|
|
177
|
-
const { controlPlaneTenantId:
|
|
178
|
-
async function
|
|
179
|
-
return (await e(
|
|
180
|
-
q: `name:${
|
|
176
|
+
function G(t, e, n = () => !0) {
|
|
177
|
+
const { controlPlaneTenantId: r, getChildTenantIds: s, getAdapters: c } = t, o = /* @__PURE__ */ new Map();
|
|
178
|
+
async function a(i, f, l) {
|
|
179
|
+
return (await e(i).list(f, {
|
|
180
|
+
q: `name:${l}`,
|
|
181
181
|
per_page: 1
|
|
182
182
|
}))[0] ?? null;
|
|
183
183
|
}
|
|
184
|
-
async function
|
|
185
|
-
const f = await
|
|
184
|
+
async function d(i) {
|
|
185
|
+
const f = await s(), l = e(await c(r));
|
|
186
186
|
await Promise.all(
|
|
187
|
-
f.map(async (
|
|
187
|
+
f.map(async (m) => {
|
|
188
188
|
try {
|
|
189
|
-
const
|
|
190
|
-
...
|
|
189
|
+
const g = await c(m), p = e(g), w = {
|
|
190
|
+
...l.transform(i),
|
|
191
191
|
is_system: !0
|
|
192
|
-
},
|
|
193
|
-
if (
|
|
194
|
-
const
|
|
195
|
-
await
|
|
192
|
+
}, y = await a(g, m, i.name), T = y ? p.getId(y) : void 0;
|
|
193
|
+
if (y && T) {
|
|
194
|
+
const b = p.preserveOnUpdate ? p.preserveOnUpdate(y, w) : w;
|
|
195
|
+
await p.update(m, T, b);
|
|
196
196
|
} else
|
|
197
|
-
await
|
|
198
|
-
} catch (
|
|
197
|
+
await p.create(m, w);
|
|
198
|
+
} catch (g) {
|
|
199
199
|
console.error(
|
|
200
|
-
`Failed to sync ${
|
|
201
|
-
|
|
200
|
+
`Failed to sync ${l.listKey} "${i.name}" to tenant "${m}":`,
|
|
201
|
+
g
|
|
202
202
|
);
|
|
203
203
|
}
|
|
204
204
|
})
|
|
205
205
|
);
|
|
206
206
|
}
|
|
207
|
-
async function
|
|
208
|
-
const f = await
|
|
207
|
+
async function u(i) {
|
|
208
|
+
const f = await s();
|
|
209
209
|
await Promise.all(
|
|
210
|
-
f.map(async (
|
|
210
|
+
f.map(async (l) => {
|
|
211
211
|
try {
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
} catch (
|
|
212
|
+
const m = await c(l), g = e(m), p = await a(m, l, i), h = p ? g.getId(p) : void 0;
|
|
213
|
+
p && h && await g.remove(l, h);
|
|
214
|
+
} catch (m) {
|
|
215
215
|
console.error(
|
|
216
|
-
`Failed to delete entity "${
|
|
217
|
-
|
|
216
|
+
`Failed to delete entity "${i}" from tenant "${l}":`,
|
|
217
|
+
m
|
|
218
218
|
);
|
|
219
219
|
}
|
|
220
220
|
})
|
|
221
221
|
);
|
|
222
222
|
}
|
|
223
223
|
return {
|
|
224
|
-
afterCreate: async (
|
|
225
|
-
|
|
224
|
+
afterCreate: async (i, f) => {
|
|
225
|
+
i.tenantId === r && n(f) && await d(f);
|
|
226
226
|
},
|
|
227
|
-
afterUpdate: async (
|
|
228
|
-
|
|
227
|
+
afterUpdate: async (i, f, l) => {
|
|
228
|
+
i.tenantId === r && n(l) && await d(l);
|
|
229
229
|
},
|
|
230
|
-
beforeDelete: async (
|
|
231
|
-
if (
|
|
232
|
-
const
|
|
233
|
-
|
|
230
|
+
beforeDelete: async (i, f) => {
|
|
231
|
+
if (i.tenantId !== r) return;
|
|
232
|
+
const m = await e(i.adapters).get(i.tenantId, f);
|
|
233
|
+
m && n(m) && o.set(f, m);
|
|
234
234
|
},
|
|
235
|
-
afterDelete: async (
|
|
236
|
-
if (
|
|
237
|
-
const
|
|
238
|
-
|
|
235
|
+
afterDelete: async (i, f) => {
|
|
236
|
+
if (i.tenantId !== r) return;
|
|
237
|
+
const l = o.get(f);
|
|
238
|
+
l && (o.delete(f), await u(l.name));
|
|
239
239
|
}
|
|
240
240
|
};
|
|
241
241
|
}
|
|
242
|
-
function
|
|
243
|
-
const { controlPlaneTenantId:
|
|
242
|
+
function L(t, e, n = () => !0) {
|
|
243
|
+
const { controlPlaneTenantId: r, getControlPlaneAdapters: s, getAdapters: c } = t;
|
|
244
244
|
return {
|
|
245
|
-
async afterCreate(o,
|
|
246
|
-
if (
|
|
245
|
+
async afterCreate(o, a) {
|
|
246
|
+
if (a.id !== r)
|
|
247
247
|
try {
|
|
248
|
-
const
|
|
249
|
-
(
|
|
250
|
-
|
|
248
|
+
const d = await s(), u = await c(a.id), i = e(d), f = e(u), l = await z(
|
|
249
|
+
(m) => i.listPaginated(r, m),
|
|
250
|
+
i.listKey,
|
|
251
251
|
{ cursorField: "id", pageSize: 100 }
|
|
252
252
|
);
|
|
253
253
|
await Promise.all(
|
|
254
|
-
|
|
254
|
+
l.filter((m) => n(m)).map(async (m) => {
|
|
255
255
|
try {
|
|
256
|
-
const
|
|
257
|
-
await f.create(
|
|
258
|
-
...
|
|
256
|
+
const g = i.transform(m);
|
|
257
|
+
await f.create(a.id, {
|
|
258
|
+
...g,
|
|
259
259
|
is_system: !0
|
|
260
260
|
});
|
|
261
|
-
} catch (
|
|
261
|
+
} catch (g) {
|
|
262
262
|
console.error(
|
|
263
|
-
`Failed to sync entity to new tenant "${
|
|
264
|
-
|
|
263
|
+
`Failed to sync entity to new tenant "${a.id}":`,
|
|
264
|
+
g
|
|
265
265
|
);
|
|
266
266
|
}
|
|
267
267
|
})
|
|
268
268
|
);
|
|
269
|
-
} catch (
|
|
269
|
+
} catch (d) {
|
|
270
270
|
console.error(
|
|
271
|
-
`Failed to sync entities to new tenant "${
|
|
272
|
-
|
|
271
|
+
`Failed to sync entities to new tenant "${a.id}":`,
|
|
272
|
+
d
|
|
273
273
|
);
|
|
274
274
|
}
|
|
275
275
|
}
|
|
276
276
|
};
|
|
277
277
|
}
|
|
278
|
-
const
|
|
278
|
+
const U = (t) => ({
|
|
279
279
|
list: async (e, n) => (await t.resourceServers.list(e, n)).resource_servers,
|
|
280
280
|
listPaginated: (e, n) => t.resourceServers.list(e, n),
|
|
281
281
|
get: (e, n) => t.resourceServers.get(e, n),
|
|
282
282
|
create: (e, n) => t.resourceServers.create(e, n),
|
|
283
|
-
update: (e, n,
|
|
283
|
+
update: (e, n, r) => t.resourceServers.update(e, n, r),
|
|
284
284
|
remove: (e, n) => t.resourceServers.remove(e, n),
|
|
285
285
|
listKey: "resource_servers",
|
|
286
286
|
getId: (e) => e.id,
|
|
@@ -293,12 +293,12 @@ const H = (t) => ({
|
|
|
293
293
|
token_lifetime: e.token_lifetime,
|
|
294
294
|
token_lifetime_for_web: e.token_lifetime_for_web
|
|
295
295
|
})
|
|
296
|
-
}),
|
|
296
|
+
}), B = (t) => ({
|
|
297
297
|
list: async (e, n) => (await t.roles.list(e, n)).roles,
|
|
298
298
|
listPaginated: (e, n) => t.roles.list(e, n),
|
|
299
299
|
get: (e, n) => t.roles.get(e, n),
|
|
300
300
|
create: (e, n) => t.roles.create(e, n),
|
|
301
|
-
update: (e, n,
|
|
301
|
+
update: (e, n, r) => t.roles.update(e, n, r),
|
|
302
302
|
remove: (e, n) => t.roles.remove(e, n),
|
|
303
303
|
listKey: "roles",
|
|
304
304
|
getId: (e) => e.id,
|
|
@@ -308,124 +308,128 @@ const H = (t) => ({
|
|
|
308
308
|
description: e.description
|
|
309
309
|
})
|
|
310
310
|
});
|
|
311
|
-
function
|
|
312
|
-
|
|
311
|
+
function K(t) {
|
|
312
|
+
var e;
|
|
313
|
+
return ((e = t.metadata) == null ? void 0 : e.sync) !== !1;
|
|
314
|
+
}
|
|
315
|
+
function fe(t) {
|
|
316
|
+
const { sync: e = {}, filters: n = {} } = t, r = e.resourceServers ?? !0, s = e.roles ?? !0, c = (g) => K(g) ? n.resourceServers ? n.resourceServers(g) : !0 : !1, o = (g) => K(g) ? n.roles ? n.roles(g) : !0 : !1, a = r ? G(
|
|
313
317
|
t,
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
) : void 0,
|
|
318
|
+
U,
|
|
319
|
+
c
|
|
320
|
+
) : void 0, d = s ? G(
|
|
317
321
|
t,
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
) : void 0,
|
|
322
|
+
B,
|
|
323
|
+
o
|
|
324
|
+
) : void 0, u = r ? L(
|
|
321
325
|
t,
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
) : void 0,
|
|
326
|
+
U,
|
|
327
|
+
c
|
|
328
|
+
) : void 0, i = s ? L(
|
|
325
329
|
t,
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
) : void 0,
|
|
329
|
-
async afterCreate(
|
|
330
|
-
var
|
|
331
|
-
if (
|
|
332
|
-
await ((
|
|
330
|
+
B,
|
|
331
|
+
o
|
|
332
|
+
) : void 0, f = s ? {
|
|
333
|
+
async afterCreate(g, p) {
|
|
334
|
+
var h;
|
|
335
|
+
if (p.id !== t.controlPlaneTenantId) {
|
|
336
|
+
await ((h = i == null ? void 0 : i.afterCreate) == null ? void 0 : h.call(i, g, p));
|
|
333
337
|
try {
|
|
334
|
-
const
|
|
335
|
-
(
|
|
338
|
+
const w = await t.getControlPlaneAdapters(), y = await t.getAdapters(p.id), T = await z(
|
|
339
|
+
(A) => w.roles.list(
|
|
336
340
|
t.controlPlaneTenantId,
|
|
337
|
-
|
|
341
|
+
A
|
|
338
342
|
),
|
|
339
343
|
"roles",
|
|
340
344
|
{ cursorField: "id", pageSize: 100 }
|
|
341
|
-
),
|
|
342
|
-
for (const
|
|
343
|
-
(
|
|
344
|
-
var
|
|
345
|
-
return ((
|
|
345
|
+
), b = /* @__PURE__ */ new Map();
|
|
346
|
+
for (const A of T.filter(
|
|
347
|
+
(_) => {
|
|
348
|
+
var v;
|
|
349
|
+
return ((v = n.roles) == null ? void 0 : v.call(n, _)) ?? !0;
|
|
346
350
|
}
|
|
347
351
|
)) {
|
|
348
|
-
const
|
|
352
|
+
const _ = await l(
|
|
349
353
|
y,
|
|
350
|
-
|
|
351
|
-
|
|
354
|
+
p.id,
|
|
355
|
+
A.name
|
|
352
356
|
);
|
|
353
|
-
|
|
357
|
+
_ && b.set(A.name, _.id);
|
|
354
358
|
}
|
|
355
|
-
for (const
|
|
356
|
-
(
|
|
357
|
-
var
|
|
358
|
-
return ((
|
|
359
|
+
for (const A of T.filter(
|
|
360
|
+
(_) => {
|
|
361
|
+
var v;
|
|
362
|
+
return ((v = n.roles) == null ? void 0 : v.call(n, _)) ?? !0;
|
|
359
363
|
}
|
|
360
364
|
)) {
|
|
361
|
-
const
|
|
362
|
-
if (
|
|
365
|
+
const _ = b.get(A.name);
|
|
366
|
+
if (_)
|
|
363
367
|
try {
|
|
364
|
-
const
|
|
368
|
+
const v = await w.rolePermissions.list(
|
|
365
369
|
t.controlPlaneTenantId,
|
|
366
|
-
|
|
370
|
+
A.id,
|
|
367
371
|
{}
|
|
368
372
|
);
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
role_id:
|
|
374
|
-
resource_server_identifier:
|
|
375
|
-
permission_name:
|
|
373
|
+
v.length > 0 && await y.rolePermissions.assign(
|
|
374
|
+
p.id,
|
|
375
|
+
_,
|
|
376
|
+
v.map((P) => ({
|
|
377
|
+
role_id: _,
|
|
378
|
+
resource_server_identifier: P.resource_server_identifier,
|
|
379
|
+
permission_name: P.permission_name
|
|
376
380
|
}))
|
|
377
381
|
);
|
|
378
|
-
} catch (
|
|
382
|
+
} catch (v) {
|
|
379
383
|
console.error(
|
|
380
|
-
`Failed to sync permissions for role "${
|
|
381
|
-
|
|
384
|
+
`Failed to sync permissions for role "${A.name}" to tenant "${p.id}":`,
|
|
385
|
+
v
|
|
382
386
|
);
|
|
383
387
|
}
|
|
384
388
|
}
|
|
385
|
-
} catch (
|
|
389
|
+
} catch (w) {
|
|
386
390
|
console.error(
|
|
387
|
-
`Failed to sync role permissions to tenant "${
|
|
388
|
-
|
|
391
|
+
`Failed to sync role permissions to tenant "${p.id}":`,
|
|
392
|
+
w
|
|
389
393
|
);
|
|
390
394
|
}
|
|
391
395
|
}
|
|
392
396
|
}
|
|
393
397
|
} : void 0;
|
|
394
|
-
async function l(
|
|
395
|
-
return (await
|
|
396
|
-
q: `name:${
|
|
398
|
+
async function l(g, p, h) {
|
|
399
|
+
return (await g.roles.list(p, {
|
|
400
|
+
q: `name:${h}`,
|
|
397
401
|
per_page: 1
|
|
398
402
|
})).roles[0] ?? null;
|
|
399
403
|
}
|
|
400
404
|
return {
|
|
401
405
|
entityHooks: {
|
|
402
|
-
resourceServers:
|
|
403
|
-
roles:
|
|
406
|
+
resourceServers: a,
|
|
407
|
+
roles: d
|
|
404
408
|
},
|
|
405
409
|
tenantHooks: {
|
|
406
|
-
async afterCreate(
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
(
|
|
410
|
-
],
|
|
411
|
-
for (const y of
|
|
410
|
+
async afterCreate(g, p) {
|
|
411
|
+
const h = [
|
|
412
|
+
u == null ? void 0 : u.afterCreate,
|
|
413
|
+
(f == null ? void 0 : f.afterCreate) ?? (i == null ? void 0 : i.afterCreate)
|
|
414
|
+
], w = [];
|
|
415
|
+
for (const y of h)
|
|
412
416
|
if (y)
|
|
413
417
|
try {
|
|
414
|
-
await y(
|
|
415
|
-
} catch (
|
|
416
|
-
|
|
418
|
+
await y(g, p);
|
|
419
|
+
} catch (T) {
|
|
420
|
+
w.push(T instanceof Error ? T : new Error(String(T)));
|
|
417
421
|
}
|
|
418
|
-
if (
|
|
419
|
-
if (
|
|
422
|
+
if (w.length === 1) throw w[0];
|
|
423
|
+
if (w.length > 1)
|
|
420
424
|
throw new AggregateError(
|
|
421
|
-
|
|
422
|
-
|
|
425
|
+
w,
|
|
426
|
+
w.map((y) => y.message).join("; ")
|
|
423
427
|
);
|
|
424
428
|
}
|
|
425
429
|
}
|
|
426
430
|
};
|
|
427
431
|
}
|
|
428
|
-
var
|
|
432
|
+
var C = class extends Error {
|
|
429
433
|
/**
|
|
430
434
|
* Creates an instance of `HTTPException`.
|
|
431
435
|
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
@@ -433,8 +437,8 @@ var b = class extends Error {
|
|
|
433
437
|
*/
|
|
434
438
|
constructor(e = 500, n) {
|
|
435
439
|
super(n == null ? void 0 : n.message, { cause: n == null ? void 0 : n.cause });
|
|
436
|
-
|
|
437
|
-
|
|
440
|
+
R(this, "res");
|
|
441
|
+
R(this, "status");
|
|
438
442
|
this.res = n == null ? void 0 : n.res, this.status = e;
|
|
439
443
|
}
|
|
440
444
|
/**
|
|
@@ -451,15 +455,15 @@ var b = class extends Error {
|
|
|
451
455
|
});
|
|
452
456
|
}
|
|
453
457
|
};
|
|
454
|
-
function
|
|
455
|
-
const n = new
|
|
458
|
+
function D(t, e) {
|
|
459
|
+
const n = new re();
|
|
456
460
|
return n.openapi(
|
|
457
|
-
|
|
461
|
+
M({
|
|
458
462
|
tags: ["tenants"],
|
|
459
463
|
method: "get",
|
|
460
464
|
path: "/",
|
|
461
465
|
request: {
|
|
462
|
-
query:
|
|
466
|
+
query: ee
|
|
463
467
|
},
|
|
464
468
|
security: [
|
|
465
469
|
{
|
|
@@ -470,11 +474,11 @@ function M(t, e) {
|
|
|
470
474
|
200: {
|
|
471
475
|
content: {
|
|
472
476
|
"application/json": {
|
|
473
|
-
schema:
|
|
474
|
-
tenants:
|
|
475
|
-
start:
|
|
476
|
-
limit:
|
|
477
|
-
length:
|
|
477
|
+
schema: S.object({
|
|
478
|
+
tenants: S.array(H),
|
|
479
|
+
start: S.number().optional(),
|
|
480
|
+
limit: S.number().optional(),
|
|
481
|
+
length: S.number().optional()
|
|
478
482
|
})
|
|
479
483
|
}
|
|
480
484
|
},
|
|
@@ -482,75 +486,76 @@ function M(t, e) {
|
|
|
482
486
|
}
|
|
483
487
|
}
|
|
484
488
|
}),
|
|
485
|
-
async (
|
|
486
|
-
var
|
|
487
|
-
const
|
|
488
|
-
if (
|
|
489
|
-
const
|
|
490
|
-
page:
|
|
489
|
+
async (r) => {
|
|
490
|
+
var g, p, h, w, y, T;
|
|
491
|
+
const s = r.req.valid("query"), { page: c, per_page: o, include_totals: a, q: d } = s, u = r.var.user, i = (u == null ? void 0 : u.permissions) || [];
|
|
492
|
+
if (i.includes("auth:read") || i.includes("admin:organizations")) {
|
|
493
|
+
const b = await r.env.data.tenants.list({
|
|
494
|
+
page: c,
|
|
491
495
|
per_page: o,
|
|
492
|
-
include_totals:
|
|
493
|
-
q:
|
|
496
|
+
include_totals: a,
|
|
497
|
+
q: d
|
|
494
498
|
});
|
|
495
|
-
return
|
|
496
|
-
tenants:
|
|
497
|
-
start: ((
|
|
498
|
-
limit: ((p =
|
|
499
|
-
length:
|
|
500
|
-
}) :
|
|
499
|
+
return a ? r.json({
|
|
500
|
+
tenants: b.tenants,
|
|
501
|
+
start: ((g = b.totals) == null ? void 0 : g.start) ?? 0,
|
|
502
|
+
limit: ((p = b.totals) == null ? void 0 : p.limit) ?? o,
|
|
503
|
+
length: b.tenants.length
|
|
504
|
+
}) : r.json({ tenants: b.tenants });
|
|
501
505
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
506
|
+
const l = ((h = t.accessControl) == null ? void 0 : h.controlPlaneTenantId) ?? ((w = r.env.data.multiTenancyConfig) == null ? void 0 : w.controlPlaneTenantId);
|
|
507
|
+
if (l && (u != null && u.sub)) {
|
|
508
|
+
const A = (await z(
|
|
509
|
+
($) => r.env.data.userOrganizations.listUserOrganizations(
|
|
510
|
+
l,
|
|
511
|
+
u.sub,
|
|
512
|
+
$
|
|
508
513
|
),
|
|
509
514
|
"organizations"
|
|
510
|
-
)).map((
|
|
511
|
-
if (
|
|
512
|
-
return
|
|
515
|
+
)).map(($) => $.name);
|
|
516
|
+
if (A.length === 0)
|
|
517
|
+
return a ? r.json({
|
|
513
518
|
tenants: [],
|
|
514
519
|
start: 0,
|
|
515
520
|
limit: o ?? 50,
|
|
516
521
|
length: 0
|
|
517
|
-
}) :
|
|
518
|
-
const
|
|
519
|
-
if (
|
|
520
|
-
return
|
|
522
|
+
}) : r.json({ tenants: [] });
|
|
523
|
+
const _ = A.length, v = c ?? 0, P = o ?? 50, I = v * P, k = A.slice(I, I + P);
|
|
524
|
+
if (k.length === 0)
|
|
525
|
+
return a ? r.json({
|
|
521
526
|
tenants: [],
|
|
522
|
-
start:
|
|
523
|
-
limit:
|
|
524
|
-
length:
|
|
525
|
-
}) :
|
|
526
|
-
const
|
|
527
|
-
q:
|
|
528
|
-
per_page:
|
|
527
|
+
start: I,
|
|
528
|
+
limit: P,
|
|
529
|
+
length: _
|
|
530
|
+
}) : r.json({ tenants: [] });
|
|
531
|
+
const E = k.map(($) => `id:${$}`).join(" OR "), J = d ? `(${E}) AND (${d})` : E, N = await r.env.data.tenants.list({
|
|
532
|
+
q: J,
|
|
533
|
+
per_page: P,
|
|
529
534
|
include_totals: !1
|
|
530
535
|
// We calculate totals from accessibleTenantIds
|
|
531
536
|
});
|
|
532
|
-
return
|
|
533
|
-
tenants:
|
|
534
|
-
start:
|
|
535
|
-
limit:
|
|
536
|
-
length:
|
|
537
|
-
}) :
|
|
537
|
+
return a ? r.json({
|
|
538
|
+
tenants: N.tenants,
|
|
539
|
+
start: I,
|
|
540
|
+
limit: P,
|
|
541
|
+
length: _
|
|
542
|
+
}) : r.json({ tenants: N.tenants });
|
|
538
543
|
}
|
|
539
|
-
const
|
|
540
|
-
page:
|
|
544
|
+
const m = await r.env.data.tenants.list({
|
|
545
|
+
page: c,
|
|
541
546
|
per_page: o,
|
|
542
|
-
include_totals:
|
|
543
|
-
q:
|
|
547
|
+
include_totals: a,
|
|
548
|
+
q: d
|
|
544
549
|
});
|
|
545
|
-
return
|
|
546
|
-
tenants:
|
|
547
|
-
start: ((
|
|
548
|
-
limit: ((
|
|
549
|
-
length:
|
|
550
|
-
}) :
|
|
550
|
+
return a ? r.json({
|
|
551
|
+
tenants: m.tenants,
|
|
552
|
+
start: ((y = m.totals) == null ? void 0 : y.start) ?? 0,
|
|
553
|
+
limit: ((T = m.totals) == null ? void 0 : T.limit) ?? o,
|
|
554
|
+
length: m.tenants.length
|
|
555
|
+
}) : r.json({ tenants: m.tenants });
|
|
551
556
|
}
|
|
552
557
|
), n.openapi(
|
|
553
|
-
|
|
558
|
+
M({
|
|
554
559
|
tags: ["tenants"],
|
|
555
560
|
method: "post",
|
|
556
561
|
path: "/",
|
|
@@ -558,7 +563,7 @@ function M(t, e) {
|
|
|
558
563
|
body: {
|
|
559
564
|
content: {
|
|
560
565
|
"application/json": {
|
|
561
|
-
schema:
|
|
566
|
+
schema: te
|
|
562
567
|
}
|
|
563
568
|
}
|
|
564
569
|
}
|
|
@@ -572,7 +577,7 @@ function M(t, e) {
|
|
|
572
577
|
201: {
|
|
573
578
|
content: {
|
|
574
579
|
"application/json": {
|
|
575
|
-
schema:
|
|
580
|
+
schema: H
|
|
576
581
|
}
|
|
577
582
|
},
|
|
578
583
|
description: "Tenant created"
|
|
@@ -585,30 +590,30 @@ function M(t, e) {
|
|
|
585
590
|
}
|
|
586
591
|
}
|
|
587
592
|
}),
|
|
588
|
-
async (
|
|
589
|
-
var
|
|
590
|
-
const
|
|
591
|
-
if (!(
|
|
592
|
-
throw new
|
|
593
|
+
async (r) => {
|
|
594
|
+
var d, u;
|
|
595
|
+
const s = r.var.user;
|
|
596
|
+
if (!(s != null && s.sub))
|
|
597
|
+
throw new C(401, {
|
|
593
598
|
message: "Authentication required to create tenants"
|
|
594
599
|
});
|
|
595
|
-
let
|
|
600
|
+
let c = r.req.valid("json");
|
|
596
601
|
const o = {
|
|
597
|
-
adapters:
|
|
598
|
-
ctx:
|
|
602
|
+
adapters: r.env.data,
|
|
603
|
+
ctx: r
|
|
599
604
|
};
|
|
600
|
-
(
|
|
601
|
-
const
|
|
602
|
-
return (
|
|
605
|
+
(d = e.tenants) != null && d.beforeCreate && (c = await e.tenants.beforeCreate(o, c));
|
|
606
|
+
const a = await r.env.data.tenants.create(c);
|
|
607
|
+
return (u = e.tenants) != null && u.afterCreate && await e.tenants.afterCreate(o, a), r.json(a, 201);
|
|
603
608
|
}
|
|
604
609
|
), n.openapi(
|
|
605
|
-
|
|
610
|
+
M({
|
|
606
611
|
tags: ["tenants"],
|
|
607
612
|
method: "delete",
|
|
608
613
|
path: "/{id}",
|
|
609
614
|
request: {
|
|
610
|
-
params:
|
|
611
|
-
id:
|
|
615
|
+
params: S.object({
|
|
616
|
+
id: S.string()
|
|
612
617
|
})
|
|
613
618
|
},
|
|
614
619
|
security: [
|
|
@@ -628,44 +633,44 @@ function M(t, e) {
|
|
|
628
633
|
}
|
|
629
634
|
}
|
|
630
635
|
}),
|
|
631
|
-
async (
|
|
632
|
-
var
|
|
633
|
-
const { id:
|
|
634
|
-
if (
|
|
635
|
-
const
|
|
636
|
-
if (!(
|
|
637
|
-
throw new
|
|
636
|
+
async (r) => {
|
|
637
|
+
var d, u, i, f;
|
|
638
|
+
const { id: s } = r.req.valid("param"), c = ((d = t.accessControl) == null ? void 0 : d.controlPlaneTenantId) ?? ((u = r.env.data.multiTenancyConfig) == null ? void 0 : u.controlPlaneTenantId);
|
|
639
|
+
if (c) {
|
|
640
|
+
const l = r.var.user;
|
|
641
|
+
if (!(l != null && l.sub))
|
|
642
|
+
throw new C(401, {
|
|
638
643
|
message: "Authentication required"
|
|
639
644
|
});
|
|
640
|
-
if (
|
|
641
|
-
throw new
|
|
645
|
+
if (s === c)
|
|
646
|
+
throw new C(403, {
|
|
642
647
|
message: "Cannot delete the control plane"
|
|
643
648
|
});
|
|
644
|
-
if (!(await
|
|
645
|
-
(
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
+
if (!(await z(
|
|
650
|
+
(p) => r.env.data.userOrganizations.listUserOrganizations(
|
|
651
|
+
c,
|
|
652
|
+
l.sub,
|
|
653
|
+
p
|
|
649
654
|
),
|
|
650
655
|
"organizations"
|
|
651
|
-
)).some((
|
|
652
|
-
throw new
|
|
656
|
+
)).some((p) => p.name === s))
|
|
657
|
+
throw new C(403, {
|
|
653
658
|
message: "Access denied to this tenant"
|
|
654
659
|
});
|
|
655
660
|
}
|
|
656
|
-
if (!await
|
|
657
|
-
throw new
|
|
661
|
+
if (!await r.env.data.tenants.get(s))
|
|
662
|
+
throw new C(404, {
|
|
658
663
|
message: "Tenant not found"
|
|
659
664
|
});
|
|
660
|
-
const
|
|
661
|
-
adapters:
|
|
662
|
-
ctx:
|
|
665
|
+
const a = {
|
|
666
|
+
adapters: r.env.data,
|
|
667
|
+
ctx: r
|
|
663
668
|
};
|
|
664
|
-
return (
|
|
669
|
+
return (i = e.tenants) != null && i.beforeDelete && await e.tenants.beforeDelete(a, s), await r.env.data.tenants.remove(s), (f = e.tenants) != null && f.afterDelete && await e.tenants.afterDelete(a, s), r.body(null, 204);
|
|
665
670
|
}
|
|
666
671
|
), n;
|
|
667
672
|
}
|
|
668
|
-
function
|
|
673
|
+
function me(t) {
|
|
669
674
|
const e = [
|
|
670
675
|
{
|
|
671
676
|
pattern: /\/api\/v2\/resource-servers\/([^/]+)$/,
|
|
@@ -674,27 +679,27 @@ function le(t) {
|
|
|
674
679
|
{ pattern: /\/api\/v2\/roles\/([^/]+)$/, type: "role" },
|
|
675
680
|
{ pattern: /\/api\/v2\/connections\/([^/]+)$/, type: "connection" }
|
|
676
681
|
];
|
|
677
|
-
for (const { pattern: n, type:
|
|
678
|
-
const
|
|
679
|
-
if (
|
|
680
|
-
return { type:
|
|
682
|
+
for (const { pattern: n, type: r } of e) {
|
|
683
|
+
const s = t.match(n);
|
|
684
|
+
if (s && s[1])
|
|
685
|
+
return { type: r, id: s[1] };
|
|
681
686
|
}
|
|
682
687
|
return null;
|
|
683
688
|
}
|
|
684
|
-
async function
|
|
689
|
+
async function ge(t, e, n) {
|
|
685
690
|
try {
|
|
686
691
|
switch (n.type) {
|
|
687
692
|
case "resource_server": {
|
|
688
|
-
const
|
|
689
|
-
return (
|
|
693
|
+
const r = await t.resourceServers.get(e, n.id);
|
|
694
|
+
return (r == null ? void 0 : r.is_system) === !0;
|
|
690
695
|
}
|
|
691
696
|
case "role": {
|
|
692
|
-
const
|
|
693
|
-
return (
|
|
697
|
+
const r = await t.roles.get(e, n.id);
|
|
698
|
+
return (r == null ? void 0 : r.is_system) === !0;
|
|
694
699
|
}
|
|
695
700
|
case "connection": {
|
|
696
|
-
const
|
|
697
|
-
return (
|
|
701
|
+
const r = await t.connections.get(e, n.id);
|
|
702
|
+
return (r == null ? void 0 : r.is_system) === !0;
|
|
698
703
|
}
|
|
699
704
|
default:
|
|
700
705
|
return !1;
|
|
@@ -703,128 +708,133 @@ async function de(t, e, n) {
|
|
|
703
708
|
return !1;
|
|
704
709
|
}
|
|
705
710
|
}
|
|
706
|
-
function
|
|
711
|
+
function pe(t) {
|
|
707
712
|
return {
|
|
708
713
|
resource_server: "resource server",
|
|
709
714
|
role: "role",
|
|
710
715
|
connection: "connection"
|
|
711
716
|
}[t];
|
|
712
717
|
}
|
|
713
|
-
function
|
|
718
|
+
function we() {
|
|
714
719
|
return async (t, e) => {
|
|
715
720
|
if (!["PATCH", "PUT", "DELETE"].includes(t.req.method))
|
|
716
721
|
return e();
|
|
717
|
-
const n =
|
|
722
|
+
const n = me(t.req.path);
|
|
718
723
|
if (!n)
|
|
719
724
|
return e();
|
|
720
|
-
const
|
|
721
|
-
if (!
|
|
725
|
+
const r = t.var.tenant_id || t.req.header("x-tenant-id") || t.req.header("tenant-id");
|
|
726
|
+
if (!r)
|
|
722
727
|
return e();
|
|
723
|
-
if (await
|
|
724
|
-
throw new
|
|
725
|
-
message: `This ${
|
|
728
|
+
if (await ge(t.env.data, r, n))
|
|
729
|
+
throw new C(403, {
|
|
730
|
+
message: `This ${pe(n.type)} is a system resource and cannot be modified. Make changes in the control plane instead.`
|
|
726
731
|
});
|
|
727
732
|
return e();
|
|
728
733
|
};
|
|
729
734
|
}
|
|
730
|
-
function
|
|
731
|
-
const { controlPlaneTenantId: n, controlPlaneClientId:
|
|
735
|
+
function Q(t, e) {
|
|
736
|
+
const { controlPlaneTenantId: n, controlPlaneClientId: r } = e;
|
|
732
737
|
return {
|
|
733
738
|
...t,
|
|
739
|
+
// Store config for use by tenants route access control
|
|
740
|
+
multiTenancyConfig: {
|
|
741
|
+
controlPlaneTenantId: n,
|
|
742
|
+
controlPlaneClientId: r
|
|
743
|
+
},
|
|
734
744
|
legacyClients: {
|
|
735
745
|
...t.legacyClients,
|
|
736
|
-
get: async (
|
|
746
|
+
get: async (s) => {
|
|
737
747
|
var f;
|
|
738
|
-
const
|
|
739
|
-
if (!
|
|
748
|
+
const c = await t.legacyClients.get(s);
|
|
749
|
+
if (!c)
|
|
740
750
|
return null;
|
|
741
|
-
const o =
|
|
742
|
-
|
|
743
|
-
),
|
|
744
|
-
var
|
|
745
|
-
const
|
|
746
|
-
(
|
|
751
|
+
const o = r ? await t.legacyClients.get(r) : void 0, a = await t.connections.list(
|
|
752
|
+
c.tenant.id
|
|
753
|
+
), d = n ? await t.connections.list(n) : { connections: [] }, u = a.connections.map((l) => {
|
|
754
|
+
var p;
|
|
755
|
+
const m = (p = d.connections) == null ? void 0 : p.find(
|
|
756
|
+
(h) => h.strategy === l.strategy
|
|
747
757
|
);
|
|
748
|
-
if (!(
|
|
749
|
-
return
|
|
750
|
-
const
|
|
751
|
-
...
|
|
752
|
-
...
|
|
758
|
+
if (!(m != null && m.options))
|
|
759
|
+
return l;
|
|
760
|
+
const g = F.parse({
|
|
761
|
+
...m || {},
|
|
762
|
+
...l
|
|
753
763
|
});
|
|
754
|
-
return
|
|
755
|
-
...
|
|
756
|
-
...
|
|
757
|
-
}),
|
|
758
|
-
}).filter((
|
|
764
|
+
return g.options = O.parse({
|
|
765
|
+
...m.options || {},
|
|
766
|
+
...l.options
|
|
767
|
+
}), g;
|
|
768
|
+
}).filter((l) => l), i = {
|
|
759
769
|
...(o == null ? void 0 : o.tenant) || {},
|
|
760
|
-
...
|
|
770
|
+
...c.tenant
|
|
761
771
|
};
|
|
762
|
-
return !
|
|
763
|
-
...
|
|
772
|
+
return !c.tenant.audience && ((f = o == null ? void 0 : o.tenant) != null && f.audience) && (i.audience = o.tenant.audience), {
|
|
773
|
+
...c,
|
|
764
774
|
web_origins: [
|
|
765
775
|
...(o == null ? void 0 : o.web_origins) || [],
|
|
766
|
-
...
|
|
776
|
+
...c.web_origins || []
|
|
767
777
|
],
|
|
768
778
|
allowed_logout_urls: [
|
|
769
779
|
...(o == null ? void 0 : o.allowed_logout_urls) || [],
|
|
770
|
-
...
|
|
780
|
+
...c.allowed_logout_urls || []
|
|
771
781
|
],
|
|
772
782
|
callbacks: [
|
|
773
783
|
...(o == null ? void 0 : o.callbacks) || [],
|
|
774
|
-
...
|
|
784
|
+
...c.callbacks || []
|
|
775
785
|
],
|
|
776
|
-
connections:
|
|
777
|
-
tenant:
|
|
786
|
+
connections: u,
|
|
787
|
+
tenant: i
|
|
778
788
|
};
|
|
779
789
|
}
|
|
780
790
|
},
|
|
781
791
|
connections: {
|
|
782
792
|
...t.connections,
|
|
783
|
-
get: async (
|
|
784
|
-
var
|
|
793
|
+
get: async (s, c) => {
|
|
794
|
+
var i;
|
|
785
795
|
const o = await t.connections.get(
|
|
786
|
-
|
|
787
|
-
|
|
796
|
+
s,
|
|
797
|
+
c
|
|
788
798
|
);
|
|
789
|
-
if (!o || !n ||
|
|
799
|
+
if (!o || !n || s === n)
|
|
790
800
|
return o;
|
|
791
|
-
const
|
|
801
|
+
const d = (i = (await t.connections.list(n)).connections) == null ? void 0 : i.find(
|
|
792
802
|
(f) => f.strategy === o.strategy
|
|
793
803
|
);
|
|
794
|
-
if (!(
|
|
804
|
+
if (!(d != null && d.options))
|
|
795
805
|
return o;
|
|
796
|
-
const
|
|
797
|
-
...
|
|
806
|
+
const u = F.parse({
|
|
807
|
+
...d,
|
|
798
808
|
...o
|
|
799
809
|
});
|
|
800
|
-
return
|
|
801
|
-
...
|
|
810
|
+
return u.options = O.parse({
|
|
811
|
+
...d.options || {},
|
|
802
812
|
...o.options
|
|
803
|
-
}),
|
|
813
|
+
}), u;
|
|
804
814
|
},
|
|
805
|
-
list: async (
|
|
806
|
-
const o = await t.connections.list(
|
|
807
|
-
if (!n ||
|
|
815
|
+
list: async (s, c) => {
|
|
816
|
+
const o = await t.connections.list(s, c);
|
|
817
|
+
if (!n || s === n)
|
|
808
818
|
return o;
|
|
809
|
-
const
|
|
810
|
-
var
|
|
811
|
-
const
|
|
812
|
-
(
|
|
819
|
+
const a = await t.connections.list(n), d = o.connections.map((u) => {
|
|
820
|
+
var l;
|
|
821
|
+
const i = (l = a.connections) == null ? void 0 : l.find(
|
|
822
|
+
(m) => m.strategy === u.strategy
|
|
813
823
|
);
|
|
814
|
-
if (!(
|
|
815
|
-
return
|
|
816
|
-
const f =
|
|
817
|
-
...
|
|
818
|
-
...
|
|
824
|
+
if (!(i != null && i.options))
|
|
825
|
+
return u;
|
|
826
|
+
const f = F.parse({
|
|
827
|
+
...i,
|
|
828
|
+
...u
|
|
819
829
|
});
|
|
820
|
-
return f.options =
|
|
821
|
-
...
|
|
822
|
-
...
|
|
830
|
+
return f.options = O.parse({
|
|
831
|
+
...i.options || {},
|
|
832
|
+
...u.options
|
|
823
833
|
}), f;
|
|
824
834
|
});
|
|
825
835
|
return {
|
|
826
836
|
...o,
|
|
827
|
-
connections:
|
|
837
|
+
connections: d
|
|
828
838
|
};
|
|
829
839
|
}
|
|
830
840
|
}
|
|
@@ -837,171 +847,174 @@ function U(t, e) {
|
|
|
837
847
|
// They remain part of ...baseAdapters and can be properly wrapped by caching.
|
|
838
848
|
};
|
|
839
849
|
}
|
|
840
|
-
function
|
|
841
|
-
return
|
|
850
|
+
function q(t, e) {
|
|
851
|
+
return Q(t, e);
|
|
842
852
|
}
|
|
843
|
-
const
|
|
844
|
-
function
|
|
853
|
+
const Pe = Q, Se = q;
|
|
854
|
+
function ye(t) {
|
|
845
855
|
return async (e, n) => {
|
|
846
|
-
const
|
|
847
|
-
return (
|
|
856
|
+
const r = e.var.user;
|
|
857
|
+
return (r == null ? void 0 : r.tenant_id) === t && r.org_name && e.set("tenant_id", r.org_name), n();
|
|
848
858
|
};
|
|
849
859
|
}
|
|
850
|
-
function
|
|
860
|
+
function he(t) {
|
|
851
861
|
return async (e, n) => {
|
|
852
862
|
if (!t.accessControl)
|
|
853
863
|
return n();
|
|
854
|
-
const { controlPlaneTenantId:
|
|
855
|
-
let
|
|
856
|
-
const
|
|
857
|
-
if (!
|
|
858
|
-
throw new
|
|
864
|
+
const { controlPlaneTenantId: r } = t.accessControl, s = e.var.org_name, c = e.var.organization_id, o = s || c;
|
|
865
|
+
let a = e.var.tenant_id;
|
|
866
|
+
const d = e.var.user, i = (d != null && d.aud ? Array.isArray(d.aud) ? d.aud : [d.aud] : []).includes(W);
|
|
867
|
+
if (!a && o && i && (e.set("tenant_id", o), a = o), !a)
|
|
868
|
+
throw new C(400, {
|
|
859
869
|
message: "Tenant ID not found in request"
|
|
860
870
|
});
|
|
861
|
-
if (!
|
|
862
|
-
|
|
871
|
+
if (!ae(
|
|
872
|
+
c,
|
|
873
|
+
a,
|
|
863
874
|
r,
|
|
864
|
-
s
|
|
865
|
-
a
|
|
875
|
+
s
|
|
866
876
|
))
|
|
867
|
-
throw new
|
|
868
|
-
message: `Access denied to tenant ${
|
|
877
|
+
throw new C(403, {
|
|
878
|
+
message: `Access denied to tenant ${a}`
|
|
869
879
|
});
|
|
870
880
|
return n();
|
|
871
881
|
};
|
|
872
882
|
}
|
|
873
|
-
function
|
|
883
|
+
function ve(t) {
|
|
874
884
|
return async (e, n) => {
|
|
875
885
|
if (!t.subdomainRouting)
|
|
876
886
|
return n();
|
|
877
887
|
const {
|
|
878
|
-
baseDomain:
|
|
879
|
-
reservedSubdomains:
|
|
880
|
-
resolveSubdomain:
|
|
888
|
+
baseDomain: r,
|
|
889
|
+
reservedSubdomains: s = [],
|
|
890
|
+
resolveSubdomain: c
|
|
881
891
|
} = t.subdomainRouting, o = e.req.header("host") || "";
|
|
882
|
-
let
|
|
883
|
-
if (o.endsWith(
|
|
884
|
-
const
|
|
885
|
-
|
|
892
|
+
let a = null;
|
|
893
|
+
if (o.endsWith(r)) {
|
|
894
|
+
const u = o.slice(0, -(r.length + 1));
|
|
895
|
+
u && !u.includes(".") && (a = u);
|
|
886
896
|
}
|
|
887
|
-
if (
|
|
897
|
+
if (a && s.includes(a) && (a = null), !a)
|
|
888
898
|
return t.accessControl && e.set("tenant_id", t.accessControl.controlPlaneTenantId), n();
|
|
889
|
-
let
|
|
890
|
-
if (
|
|
891
|
-
|
|
899
|
+
let d = null;
|
|
900
|
+
if (c)
|
|
901
|
+
d = await c(a);
|
|
892
902
|
else if (t.subdomainRouting.useOrganizations !== !1 && t.accessControl)
|
|
893
903
|
try {
|
|
894
|
-
const
|
|
904
|
+
const u = await e.env.data.organizations.get(
|
|
895
905
|
t.accessControl.controlPlaneTenantId,
|
|
896
|
-
|
|
906
|
+
a
|
|
897
907
|
);
|
|
898
|
-
|
|
908
|
+
u && (d = u.id);
|
|
899
909
|
} catch {
|
|
900
910
|
}
|
|
901
|
-
if (!
|
|
902
|
-
throw new
|
|
903
|
-
message: `Tenant not found for subdomain: ${
|
|
911
|
+
if (!d)
|
|
912
|
+
throw new C(404, {
|
|
913
|
+
message: `Tenant not found for subdomain: ${a}`
|
|
904
914
|
});
|
|
905
|
-
return e.set("tenant_id",
|
|
915
|
+
return e.set("tenant_id", d), n();
|
|
906
916
|
};
|
|
907
917
|
}
|
|
908
|
-
function
|
|
918
|
+
function Te(t) {
|
|
909
919
|
return async (e, n) => {
|
|
910
920
|
if (!t.databaseIsolation)
|
|
911
921
|
return n();
|
|
912
|
-
const
|
|
913
|
-
if (!
|
|
914
|
-
throw new
|
|
922
|
+
const r = e.var.tenant_id;
|
|
923
|
+
if (!r)
|
|
924
|
+
throw new C(400, {
|
|
915
925
|
message: "Tenant ID not found in request"
|
|
916
926
|
});
|
|
917
927
|
try {
|
|
918
|
-
const
|
|
919
|
-
e.env.data =
|
|
920
|
-
} catch (
|
|
928
|
+
const s = await t.databaseIsolation.getAdapters(r);
|
|
929
|
+
e.env.data = s;
|
|
930
|
+
} catch (s) {
|
|
921
931
|
throw console.error(
|
|
922
|
-
`Failed to resolve database for tenant ${
|
|
923
|
-
|
|
924
|
-
), new
|
|
932
|
+
`Failed to resolve database for tenant ${r}:`,
|
|
933
|
+
s
|
|
934
|
+
), new C(500, {
|
|
925
935
|
message: "Failed to resolve tenant database"
|
|
926
936
|
});
|
|
927
937
|
}
|
|
928
938
|
return n();
|
|
929
939
|
};
|
|
930
940
|
}
|
|
931
|
-
function
|
|
932
|
-
const e =
|
|
933
|
-
return async (
|
|
934
|
-
}), await n(
|
|
935
|
-
}), await s
|
|
936
|
-
}),
|
|
941
|
+
function V(t) {
|
|
942
|
+
const e = ve(t), n = he(t), r = Te(t);
|
|
943
|
+
return async (s, c) => (await e(s, async () => {
|
|
944
|
+
}), await n(s, async () => {
|
|
945
|
+
}), await r(s, async () => {
|
|
946
|
+
}), c());
|
|
937
947
|
}
|
|
938
|
-
function
|
|
948
|
+
function $e(t) {
|
|
939
949
|
const {
|
|
940
950
|
dataAdapter: e,
|
|
941
|
-
|
|
942
|
-
sync:
|
|
943
|
-
defaultPermissions:
|
|
944
|
-
requireOrganizationMatch:
|
|
951
|
+
controlPlane: n,
|
|
952
|
+
sync: r = { resourceServers: !0, roles: !0 },
|
|
953
|
+
defaultPermissions: s = ["tenant:admin"],
|
|
954
|
+
requireOrganizationMatch: c = !1,
|
|
945
955
|
managementApiExtensions: o = [],
|
|
946
|
-
entityHooks:
|
|
947
|
-
getChildTenantIds:
|
|
948
|
-
getAdapters:
|
|
949
|
-
...
|
|
950
|
-
} = t, f =
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
} :
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
956
|
+
entityHooks: a,
|
|
957
|
+
getChildTenantIds: d,
|
|
958
|
+
getAdapters: u,
|
|
959
|
+
...i
|
|
960
|
+
} = t, f = (n == null ? void 0 : n.tenantId) ?? "control_plane", l = n == null ? void 0 : n.clientId, m = n ? q(e, {
|
|
961
|
+
controlPlaneTenantId: f,
|
|
962
|
+
controlPlaneClientId: l
|
|
963
|
+
}) : e, g = r !== !1, p = g ? {
|
|
964
|
+
resourceServers: r.resourceServers ?? !0,
|
|
965
|
+
roles: r.roles ?? !0
|
|
966
|
+
} : { resourceServers: !1, roles: !1 }, y = {
|
|
967
|
+
controlPlaneTenantId: f,
|
|
968
|
+
getChildTenantIds: d ?? (async () => (await z(
|
|
969
|
+
(I) => m.tenants.list(I),
|
|
957
970
|
"tenants",
|
|
958
971
|
{ cursorField: "id", pageSize: 100 }
|
|
959
|
-
)).filter((
|
|
960
|
-
getAdapters:
|
|
961
|
-
getControlPlaneAdapters: async () =>
|
|
962
|
-
sync:
|
|
963
|
-
}, { entityHooks:
|
|
972
|
+
)).filter((I) => I.id !== f).map((I) => I.id)),
|
|
973
|
+
getAdapters: u ?? (async () => m),
|
|
974
|
+
getControlPlaneAdapters: async () => m,
|
|
975
|
+
sync: p
|
|
976
|
+
}, { entityHooks: T, tenantHooks: b } = fe(y), A = {
|
|
964
977
|
resourceServers: [
|
|
965
|
-
|
|
966
|
-
...(
|
|
978
|
+
T.resourceServers,
|
|
979
|
+
...(a == null ? void 0 : a.resourceServers) ?? []
|
|
967
980
|
],
|
|
968
|
-
roles: [
|
|
969
|
-
connections: (
|
|
970
|
-
tenants: (
|
|
971
|
-
rolePermissions: (
|
|
972
|
-
},
|
|
981
|
+
roles: [T.roles, ...(a == null ? void 0 : a.roles) ?? []],
|
|
982
|
+
connections: (a == null ? void 0 : a.connections) ?? [],
|
|
983
|
+
tenants: (a == null ? void 0 : a.tenants) ?? [],
|
|
984
|
+
rolePermissions: (a == null ? void 0 : a.rolePermissions) ?? []
|
|
985
|
+
}, _ = D(
|
|
973
986
|
{
|
|
974
987
|
accessControl: {
|
|
975
|
-
controlPlaneTenantId:
|
|
976
|
-
requireOrganizationMatch:
|
|
977
|
-
defaultPermissions:
|
|
988
|
+
controlPlaneTenantId: f,
|
|
989
|
+
requireOrganizationMatch: c,
|
|
990
|
+
defaultPermissions: s
|
|
978
991
|
}
|
|
979
992
|
},
|
|
980
|
-
{ tenants:
|
|
981
|
-
), { app:
|
|
982
|
-
dataAdapter:
|
|
983
|
-
...
|
|
993
|
+
{ tenants: b }
|
|
994
|
+
), { app: v } = ne({
|
|
995
|
+
dataAdapter: m,
|
|
996
|
+
...i,
|
|
984
997
|
entityHooks: A,
|
|
985
998
|
managementApiExtensions: [
|
|
986
999
|
...o,
|
|
987
|
-
{ path: "/tenants", router:
|
|
1000
|
+
{ path: "/tenants", router: _ }
|
|
988
1001
|
]
|
|
989
1002
|
});
|
|
990
|
-
return
|
|
1003
|
+
return v.use("/api/v2/*", ye(f)), g && v.use("/api/v2/*", we()), { app: v, controlPlaneTenantId: f };
|
|
991
1004
|
}
|
|
992
|
-
function
|
|
993
|
-
const e =
|
|
1005
|
+
function ze(t) {
|
|
1006
|
+
const e = j(t);
|
|
994
1007
|
return {
|
|
995
1008
|
name: "multi-tenancy",
|
|
996
1009
|
// Apply multi-tenancy middleware for subdomain routing, database resolution, etc.
|
|
997
|
-
middleware:
|
|
1010
|
+
middleware: V(t),
|
|
998
1011
|
// Provide lifecycle hooks
|
|
999
1012
|
hooks: e,
|
|
1000
1013
|
// Mount tenant management routes
|
|
1001
1014
|
routes: [
|
|
1002
1015
|
{
|
|
1003
1016
|
path: "/management",
|
|
1004
|
-
handler:
|
|
1017
|
+
handler: D(t, e)
|
|
1005
1018
|
}
|
|
1006
1019
|
],
|
|
1007
1020
|
// Called when plugin is registered
|
|
@@ -1014,46 +1027,61 @@ function Ie(t) {
|
|
|
1014
1027
|
}
|
|
1015
1028
|
};
|
|
1016
1029
|
}
|
|
1017
|
-
function
|
|
1018
|
-
const e = t.accessControl ?
|
|
1030
|
+
function j(t) {
|
|
1031
|
+
const e = t.accessControl ? se(t.accessControl) : {}, n = t.databaseIsolation ? oe(t.databaseIsolation) : {}, r = ce(t);
|
|
1019
1032
|
return {
|
|
1020
1033
|
...e,
|
|
1021
1034
|
...n,
|
|
1022
|
-
tenants:
|
|
1035
|
+
tenants: r
|
|
1023
1036
|
};
|
|
1024
1037
|
}
|
|
1025
|
-
function
|
|
1026
|
-
const e = new
|
|
1027
|
-
return e.route("/tenants",
|
|
1038
|
+
function _e(t) {
|
|
1039
|
+
const e = new Z(), n = j(t);
|
|
1040
|
+
return e.route("/tenants", D(t, n)), e;
|
|
1028
1041
|
}
|
|
1029
|
-
function
|
|
1042
|
+
function Re(t) {
|
|
1030
1043
|
return {
|
|
1031
|
-
hooks:
|
|
1032
|
-
middleware:
|
|
1033
|
-
app:
|
|
1034
|
-
config: t
|
|
1044
|
+
hooks: j(t),
|
|
1045
|
+
middleware: V(t),
|
|
1046
|
+
app: _e(t),
|
|
1047
|
+
config: t,
|
|
1048
|
+
/**
|
|
1049
|
+
* Wraps data adapters with runtime fallback from the control plane.
|
|
1050
|
+
* Uses the controlPlaneTenantId from the multi-tenancy config.
|
|
1051
|
+
*
|
|
1052
|
+
* @param adapters - Base data adapters to wrap
|
|
1053
|
+
* @param additionalConfig - Additional config (controlPlaneClientId, etc.)
|
|
1054
|
+
* @returns Wrapped adapters with runtime fallback
|
|
1055
|
+
*/
|
|
1056
|
+
wrapAdapters: (e, n) => {
|
|
1057
|
+
var r;
|
|
1058
|
+
return q(e, {
|
|
1059
|
+
controlPlaneTenantId: (r = t.accessControl) == null ? void 0 : r.controlPlaneTenantId,
|
|
1060
|
+
controlPlaneClientId: n == null ? void 0 : n.controlPlaneClientId
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1035
1063
|
};
|
|
1036
1064
|
}
|
|
1037
1065
|
export {
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1066
|
+
se as createAccessControlHooks,
|
|
1067
|
+
he as createAccessControlMiddleware,
|
|
1068
|
+
ye as createControlPlaneTenantMiddleware,
|
|
1069
|
+
oe as createDatabaseHooks,
|
|
1070
|
+
Te as createDatabaseMiddleware,
|
|
1071
|
+
_e as createMultiTenancy,
|
|
1072
|
+
j as createMultiTenancyHooks,
|
|
1073
|
+
V as createMultiTenancyMiddleware,
|
|
1074
|
+
ze as createMultiTenancyPlugin,
|
|
1075
|
+
we as createProtectSyncedMiddleware,
|
|
1076
|
+
ce as createProvisioningHooks,
|
|
1077
|
+
Q as createRuntimeFallbackAdapter,
|
|
1078
|
+
Pe as createSettingsInheritanceAdapter,
|
|
1079
|
+
ve as createSubdomainMiddleware,
|
|
1080
|
+
fe as createSyncHooks,
|
|
1081
|
+
D as createTenantsOpenAPIRouter,
|
|
1082
|
+
$e as initMultiTenant,
|
|
1083
|
+
Re as setupMultiTenancy,
|
|
1084
|
+
ae as validateTenantAccess,
|
|
1085
|
+
q as withRuntimeFallback,
|
|
1086
|
+
Se as withSettingsInheritance
|
|
1059
1087
|
};
|