@getstrata/core 0.5.52 → 0.5.53
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/README.md +1 -1
- package/dist/entries/auth/accessControl.js +60 -1
- package/dist/entries/auth/membershipMiddleware.js +21 -0
- package/dist/entries/auth/membershipScope.js +336 -1
- package/dist/entries/auth/membershipService.js +304 -1
- package/dist/entries/auth/policy.js +81 -1
- package/dist/entries/database/repositoryQuery.js +655 -0
- package/dist/entries/database/whereBuilder.js +32 -0
- package/dist/entries/http/formRequest.js +102 -0
- package/dist/entries/http/pagination.js +102 -0
- package/dist/entries/http/routeModelBinding.js +102 -0
- package/dist/entries/http/securedRouteModelBinding.js +102 -0
- package/dist/entries/http/validation.js +145 -0
- package/dist/entries/http/webFormRequest.js +102 -0
- package/package.json +17 -2
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ bun run verify:shared-subpaths # after build: confirm singleton shims
|
|
|
49
49
|
|
|
50
50
|
## Subpath imports
|
|
51
51
|
|
|
52
|
-
`@getstrata/core` publishes **
|
|
52
|
+
`@getstrata/core` publishes **143+ subpaths** (for example `@getstrata/core/http/authMiddleware`,
|
|
53
53
|
`@getstrata/core/database/migrations`). Prefer subpaths over the root import in apps, bootstrap, and tests.
|
|
54
54
|
|
|
55
55
|
Some subpaths **re-export the main bundle** so singleton state stays shared (database pool binding,
|
|
@@ -1 +1,60 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/auth/accessControl.ts
|
|
3
|
+
import { ForbiddenError } from "@getstrata/core/errors/http";
|
|
4
|
+
|
|
5
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
6
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
7
|
+
function createAsyncContextStore(key) {
|
|
8
|
+
const symbol = Symbol.for(key);
|
|
9
|
+
const globalRecord = globalThis;
|
|
10
|
+
const existing = globalRecord[symbol];
|
|
11
|
+
if (existing) {
|
|
12
|
+
return existing;
|
|
13
|
+
}
|
|
14
|
+
const store = new AsyncLocalStorage;
|
|
15
|
+
globalRecord[symbol] = store;
|
|
16
|
+
return store;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ../../src/core/auth/authContext.ts
|
|
20
|
+
var authContext = createAsyncContextStore("@getstrata/authContext");
|
|
21
|
+
function currentAuthUser() {
|
|
22
|
+
return authContext.getStore() ?? null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ../../src/core/auth/accessControl.ts
|
|
26
|
+
var ROLE_RANK = {
|
|
27
|
+
member: 1,
|
|
28
|
+
admin: 2,
|
|
29
|
+
owner: 3
|
|
30
|
+
};
|
|
31
|
+
function isGlobalAdmin(user) {
|
|
32
|
+
return user?.role === "admin";
|
|
33
|
+
}
|
|
34
|
+
function hasMinimumOrgRole(role, minimum) {
|
|
35
|
+
if (!role) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return ROLE_RANK[role] >= ROLE_RANK[minimum];
|
|
39
|
+
}
|
|
40
|
+
function requireAuthenticatedUser() {
|
|
41
|
+
const user = currentAuthUser();
|
|
42
|
+
if (!user) {
|
|
43
|
+
throw new ForbiddenError("Authentication required.");
|
|
44
|
+
}
|
|
45
|
+
return user;
|
|
46
|
+
}
|
|
47
|
+
function resolveUserId(user) {
|
|
48
|
+
const userId = typeof user.id === "number" ? user.id : Number(user.id);
|
|
49
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
50
|
+
throw new ForbiddenError("Invalid authenticated user.");
|
|
51
|
+
}
|
|
52
|
+
return userId;
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
resolveUserId,
|
|
56
|
+
requireAuthenticatedUser,
|
|
57
|
+
isGlobalAdmin,
|
|
58
|
+
hasMinimumOrgRole,
|
|
59
|
+
ROLE_RANK
|
|
60
|
+
};
|
|
@@ -183,9 +183,27 @@ function currentAuthUser() {
|
|
|
183
183
|
}
|
|
184
184
|
|
|
185
185
|
// ../../src/core/auth/accessControl.ts
|
|
186
|
+
var ROLE_RANK = {
|
|
187
|
+
member: 1,
|
|
188
|
+
admin: 2,
|
|
189
|
+
owner: 3
|
|
190
|
+
};
|
|
186
191
|
function isGlobalAdmin(user) {
|
|
187
192
|
return user?.role === "admin";
|
|
188
193
|
}
|
|
194
|
+
function hasMinimumOrgRole(role, minimum) {
|
|
195
|
+
if (!role) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
return ROLE_RANK[role] >= ROLE_RANK[minimum];
|
|
199
|
+
}
|
|
200
|
+
function requireAuthenticatedUser() {
|
|
201
|
+
const user = currentAuthUser();
|
|
202
|
+
if (!user) {
|
|
203
|
+
throw new ForbiddenError("Authentication required.");
|
|
204
|
+
}
|
|
205
|
+
return user;
|
|
206
|
+
}
|
|
189
207
|
function resolveUserId(user) {
|
|
190
208
|
const userId = typeof user.id === "number" ? user.id : Number(user.id);
|
|
191
209
|
if (!Number.isInteger(userId) || userId <= 0) {
|
|
@@ -209,6 +227,9 @@ async function runWithMembershipContext(callback) {
|
|
|
209
227
|
};
|
|
210
228
|
return await membershipContext.run(context, callback);
|
|
211
229
|
}
|
|
230
|
+
function currentOrganizationIds() {
|
|
231
|
+
return membershipContext.getStore()?.organizationIds ?? [];
|
|
232
|
+
}
|
|
212
233
|
|
|
213
234
|
// ../../src/core/auth/membershipContextMiddleware.ts
|
|
214
235
|
function createMembershipContextMiddleware() {
|
|
@@ -1 +1,336 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/auth/membershipScope.ts
|
|
3
|
+
import { NotFoundError } from "@getstrata/core/errors/http";
|
|
4
|
+
import { currentTenantId } from "@getstrata/core/tenant/tenantContext";
|
|
5
|
+
|
|
6
|
+
// ../../src/core/auth/accessControl.ts
|
|
7
|
+
import { ForbiddenError } from "@getstrata/core/errors/http";
|
|
8
|
+
|
|
9
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
10
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
11
|
+
function createAsyncContextStore(key) {
|
|
12
|
+
const symbol = Symbol.for(key);
|
|
13
|
+
const globalRecord = globalThis;
|
|
14
|
+
const existing = globalRecord[symbol];
|
|
15
|
+
if (existing) {
|
|
16
|
+
return existing;
|
|
17
|
+
}
|
|
18
|
+
const store = new AsyncLocalStorage;
|
|
19
|
+
globalRecord[symbol] = store;
|
|
20
|
+
return store;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ../../src/core/auth/authContext.ts
|
|
24
|
+
var authContext = createAsyncContextStore("@getstrata/authContext");
|
|
25
|
+
function currentAuthUser() {
|
|
26
|
+
return authContext.getStore() ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ../../src/core/auth/accessControl.ts
|
|
30
|
+
var ROLE_RANK = {
|
|
31
|
+
member: 1,
|
|
32
|
+
admin: 2,
|
|
33
|
+
owner: 3
|
|
34
|
+
};
|
|
35
|
+
function isGlobalAdmin(user) {
|
|
36
|
+
return user?.role === "admin";
|
|
37
|
+
}
|
|
38
|
+
function hasMinimumOrgRole(role, minimum) {
|
|
39
|
+
if (!role) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return ROLE_RANK[role] >= ROLE_RANK[minimum];
|
|
43
|
+
}
|
|
44
|
+
function requireAuthenticatedUser() {
|
|
45
|
+
const user = currentAuthUser();
|
|
46
|
+
if (!user) {
|
|
47
|
+
throw new ForbiddenError("Authentication required.");
|
|
48
|
+
}
|
|
49
|
+
return user;
|
|
50
|
+
}
|
|
51
|
+
function resolveUserId(user) {
|
|
52
|
+
const userId = typeof user.id === "number" ? user.id : Number(user.id);
|
|
53
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
54
|
+
throw new ForbiddenError("Invalid authenticated user.");
|
|
55
|
+
}
|
|
56
|
+
return userId;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ../../src/config/database.ts
|
|
60
|
+
function readInteger(name, fallback) {
|
|
61
|
+
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
62
|
+
return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
|
|
63
|
+
}
|
|
64
|
+
var databaseConfig = {
|
|
65
|
+
url: process.env.DATABASE_URL ?? "",
|
|
66
|
+
poolMax: readInteger("DB_POOL_MAX", 10),
|
|
67
|
+
idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
|
|
68
|
+
maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
|
|
69
|
+
connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// ../../src/core/database/connectionContext.ts
|
|
73
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
74
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
75
|
+
return activeConnection.run(connection, callback);
|
|
76
|
+
}
|
|
77
|
+
function getActiveDatabaseConnection(fallback) {
|
|
78
|
+
return activeConnection.getStore() ?? fallback;
|
|
79
|
+
}
|
|
80
|
+
function hasActiveDatabaseConnection() {
|
|
81
|
+
return activeConnection.getStore() !== undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ../../src/core/database/queryProxy.ts
|
|
85
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
86
|
+
function createDatabaseQueryProxy(pool) {
|
|
87
|
+
function resolveDatabase() {
|
|
88
|
+
return getActiveDatabaseConnection(pool);
|
|
89
|
+
}
|
|
90
|
+
function resolveDatabaseForProperty(property) {
|
|
91
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
92
|
+
return pool;
|
|
93
|
+
}
|
|
94
|
+
return resolveDatabase();
|
|
95
|
+
}
|
|
96
|
+
return new Proxy(function database() {}, {
|
|
97
|
+
apply(_target, _thisArg, args) {
|
|
98
|
+
return resolveDatabase()(...args);
|
|
99
|
+
},
|
|
100
|
+
get(_target, property) {
|
|
101
|
+
const connection = resolveDatabaseForProperty(property);
|
|
102
|
+
const value = connection[property];
|
|
103
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ../../src/core/database/defaultConnection.ts
|
|
109
|
+
var defaultPool = {
|
|
110
|
+
connection: null
|
|
111
|
+
};
|
|
112
|
+
var defaultQuery = {
|
|
113
|
+
connection: null
|
|
114
|
+
};
|
|
115
|
+
function registerDefaultDatabasePool(connection) {
|
|
116
|
+
defaultPool.connection = connection;
|
|
117
|
+
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
118
|
+
}
|
|
119
|
+
function getDefaultDatabaseQuery() {
|
|
120
|
+
if (!defaultQuery.connection) {
|
|
121
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
122
|
+
}
|
|
123
|
+
return defaultQuery.connection;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ../../src/db/connection/createConnection.ts
|
|
127
|
+
var {SQL } = globalThis.Bun;
|
|
128
|
+
function createDatabaseConnection(config) {
|
|
129
|
+
if (!config.url) {
|
|
130
|
+
throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
|
|
131
|
+
}
|
|
132
|
+
return new SQL({
|
|
133
|
+
url: config.url,
|
|
134
|
+
max: config.poolMax,
|
|
135
|
+
idleTimeout: config.idleTimeoutSeconds,
|
|
136
|
+
maxLifetime: config.maxLifetimeSeconds,
|
|
137
|
+
connectionTimeout: config.connectionTimeoutSeconds
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ../../src/db/connection/index.ts
|
|
142
|
+
var connectionHolder = {
|
|
143
|
+
connection: null
|
|
144
|
+
};
|
|
145
|
+
function getDatabase() {
|
|
146
|
+
if (!connectionHolder.connection) {
|
|
147
|
+
connectionHolder.connection = createDatabaseConnection(databaseConfig);
|
|
148
|
+
registerDefaultDatabasePool(connectionHolder.connection);
|
|
149
|
+
}
|
|
150
|
+
return connectionHolder.connection;
|
|
151
|
+
}
|
|
152
|
+
function getDb() {
|
|
153
|
+
getDatabase();
|
|
154
|
+
return getDefaultDatabaseQuery();
|
|
155
|
+
}
|
|
156
|
+
var db = new Proxy(function database() {}, {
|
|
157
|
+
apply(_target, _thisArg, args) {
|
|
158
|
+
return getDb()(...args);
|
|
159
|
+
},
|
|
160
|
+
get(_target, property) {
|
|
161
|
+
const connection = getDb();
|
|
162
|
+
const value = connection[property];
|
|
163
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
var connection_default = db;
|
|
167
|
+
|
|
168
|
+
// ../../src/modules/organization/memberRepository.ts
|
|
169
|
+
class OrganizationMemberRepository {
|
|
170
|
+
constructor() {}
|
|
171
|
+
async findMembership(userId, organizationId) {
|
|
172
|
+
const rows = await connection_default`
|
|
173
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
174
|
+
FROM organization_member
|
|
175
|
+
WHERE user_id = ${userId} AND organization_id = ${organizationId}
|
|
176
|
+
LIMIT 1
|
|
177
|
+
`;
|
|
178
|
+
return rows[0] ?? null;
|
|
179
|
+
}
|
|
180
|
+
async listForUser(userId) {
|
|
181
|
+
return await connection_default`
|
|
182
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
183
|
+
FROM organization_member
|
|
184
|
+
WHERE user_id = ${userId}
|
|
185
|
+
ORDER BY organization_id
|
|
186
|
+
`;
|
|
187
|
+
}
|
|
188
|
+
async listForOrganization(organizationId) {
|
|
189
|
+
return await connection_default`
|
|
190
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
191
|
+
FROM organization_member
|
|
192
|
+
WHERE organization_id = ${organizationId}
|
|
193
|
+
ORDER BY id
|
|
194
|
+
`;
|
|
195
|
+
}
|
|
196
|
+
async addMember(input) {
|
|
197
|
+
const rows = await connection_default`
|
|
198
|
+
INSERT INTO organization_member (organization_id, user_id, role)
|
|
199
|
+
VALUES (${input.organizationId}, ${input.userId}, ${input.role ?? "member"})
|
|
200
|
+
RETURNING id, organization_id, user_id, role, created_at
|
|
201
|
+
`;
|
|
202
|
+
const row = rows[0];
|
|
203
|
+
if (!row) {
|
|
204
|
+
throw new Error("Organization member insert did not return a row.");
|
|
205
|
+
}
|
|
206
|
+
return row;
|
|
207
|
+
}
|
|
208
|
+
async removeMember(organizationId, userId) {
|
|
209
|
+
const rows = await connection_default`
|
|
210
|
+
DELETE FROM organization_member
|
|
211
|
+
WHERE organization_id = ${organizationId} AND user_id = ${userId}
|
|
212
|
+
RETURNING id
|
|
213
|
+
`;
|
|
214
|
+
return rows.length > 0;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
var memberRepository_default = OrganizationMemberRepository;
|
|
218
|
+
|
|
219
|
+
// ../../src/core/auth/membershipContext.ts
|
|
220
|
+
var membershipContext = createAsyncContextStore("@getstrata/membershipContext");
|
|
221
|
+
var membershipRepository = new memberRepository_default;
|
|
222
|
+
async function runWithMembershipContext(callback) {
|
|
223
|
+
const user = currentAuthUser();
|
|
224
|
+
if (!user || isGlobalAdmin(user)) {
|
|
225
|
+
return await callback();
|
|
226
|
+
}
|
|
227
|
+
const memberships = await membershipRepository.listForUser(resolveUserId(user));
|
|
228
|
+
const context = {
|
|
229
|
+
organizationIds: memberships.map((membership) => membership.organization_id),
|
|
230
|
+
rolesByOrganizationId: new Map(memberships.map((membership) => [membership.organization_id, membership.role]))
|
|
231
|
+
};
|
|
232
|
+
return await membershipContext.run(context, callback);
|
|
233
|
+
}
|
|
234
|
+
function currentOrganizationIds() {
|
|
235
|
+
return membershipContext.getStore()?.organizationIds ?? [];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ../../src/core/auth/membershipScope.ts
|
|
239
|
+
function resolveOrganizationScope() {
|
|
240
|
+
const user = currentAuthUser();
|
|
241
|
+
if (!user) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
if (isGlobalAdmin(user)) {
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
return currentOrganizationIds();
|
|
248
|
+
}
|
|
249
|
+
function scopedOrganizationIds(requestedOrganizationId) {
|
|
250
|
+
const scope = resolveOrganizationScope();
|
|
251
|
+
if (scope === null) {
|
|
252
|
+
return requestedOrganizationId === undefined ? null : [requestedOrganizationId];
|
|
253
|
+
}
|
|
254
|
+
if (requestedOrganizationId !== undefined) {
|
|
255
|
+
return scope.includes(requestedOrganizationId) ? [requestedOrganizationId] : [];
|
|
256
|
+
}
|
|
257
|
+
return scope;
|
|
258
|
+
}
|
|
259
|
+
function appendOrganizationScope(where, requestedOrganizationId) {
|
|
260
|
+
const organizationIds = scopedOrganizationIds(requestedOrganizationId);
|
|
261
|
+
if (organizationIds === null) {
|
|
262
|
+
return where;
|
|
263
|
+
}
|
|
264
|
+
if (organizationIds.length === 0) {
|
|
265
|
+
return {
|
|
266
|
+
...where,
|
|
267
|
+
organization_id: [-1]
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
...where,
|
|
272
|
+
organization_id: organizationIds.length === 1 ? organizationIds[0] : organizationIds
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function appendProjectScope(where, accessibleProjectIds, requestedProjectId) {
|
|
276
|
+
if (accessibleProjectIds === null) {
|
|
277
|
+
if (requestedProjectId === undefined) {
|
|
278
|
+
return where;
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
...where,
|
|
282
|
+
project_id: requestedProjectId
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
if (accessibleProjectIds.length === 0) {
|
|
286
|
+
return {
|
|
287
|
+
...where,
|
|
288
|
+
project_id: [-1]
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
if (requestedProjectId !== undefined) {
|
|
292
|
+
return {
|
|
293
|
+
...where,
|
|
294
|
+
project_id: accessibleProjectIds.includes(requestedProjectId) ? requestedProjectId : -1
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
...where,
|
|
299
|
+
project_id: accessibleProjectIds
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function emptyPaginateResult(page, perPage) {
|
|
303
|
+
return {
|
|
304
|
+
data: [],
|
|
305
|
+
meta: {
|
|
306
|
+
page,
|
|
307
|
+
per_page: perPage,
|
|
308
|
+
total: 0,
|
|
309
|
+
last_page: 1
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
function assertResourceInCurrentTenant(resourceTenantId, resourceLabel, resourceId) {
|
|
314
|
+
if (resourceTenantId !== currentTenantId()) {
|
|
315
|
+
throw new NotFoundError(`${resourceLabel} ${resourceId} not found.`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function assertOrganizationReadable(organizationId) {
|
|
319
|
+
const user = currentAuthUser();
|
|
320
|
+
if (!user || isGlobalAdmin(user)) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const organizationIds = scopedOrganizationIds();
|
|
324
|
+
if (organizationIds !== null && !organizationIds.includes(organizationId)) {
|
|
325
|
+
throw new NotFoundError(`Organization ${organizationId} not found.`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
export {
|
|
329
|
+
scopedOrganizationIds,
|
|
330
|
+
resolveOrganizationScope,
|
|
331
|
+
emptyPaginateResult,
|
|
332
|
+
assertResourceInCurrentTenant,
|
|
333
|
+
assertOrganizationReadable,
|
|
334
|
+
appendProjectScope,
|
|
335
|
+
appendOrganizationScope
|
|
336
|
+
};
|