@getstrata/core 0.5.51 → 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/facades.js +378 -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 +22 -2
|
@@ -1 +1,304 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/auth/membershipService.ts
|
|
3
|
+
import { ForbiddenError as ForbiddenError2 } from "@getstrata/core/errors/http";
|
|
4
|
+
|
|
5
|
+
// ../../src/core/auth/accessControl.ts
|
|
6
|
+
import { ForbiddenError } from "@getstrata/core/errors/http";
|
|
7
|
+
|
|
8
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
9
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
10
|
+
function createAsyncContextStore(key) {
|
|
11
|
+
const symbol = Symbol.for(key);
|
|
12
|
+
const globalRecord = globalThis;
|
|
13
|
+
const existing = globalRecord[symbol];
|
|
14
|
+
if (existing) {
|
|
15
|
+
return existing;
|
|
16
|
+
}
|
|
17
|
+
const store = new AsyncLocalStorage;
|
|
18
|
+
globalRecord[symbol] = store;
|
|
19
|
+
return store;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ../../src/core/auth/authContext.ts
|
|
23
|
+
var authContext = createAsyncContextStore("@getstrata/authContext");
|
|
24
|
+
function currentAuthUser() {
|
|
25
|
+
return authContext.getStore() ?? null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ../../src/core/auth/accessControl.ts
|
|
29
|
+
var ROLE_RANK = {
|
|
30
|
+
member: 1,
|
|
31
|
+
admin: 2,
|
|
32
|
+
owner: 3
|
|
33
|
+
};
|
|
34
|
+
function isGlobalAdmin(user) {
|
|
35
|
+
return user?.role === "admin";
|
|
36
|
+
}
|
|
37
|
+
function hasMinimumOrgRole(role, minimum) {
|
|
38
|
+
if (!role) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return ROLE_RANK[role] >= ROLE_RANK[minimum];
|
|
42
|
+
}
|
|
43
|
+
function requireAuthenticatedUser() {
|
|
44
|
+
const user = currentAuthUser();
|
|
45
|
+
if (!user) {
|
|
46
|
+
throw new ForbiddenError("Authentication required.");
|
|
47
|
+
}
|
|
48
|
+
return user;
|
|
49
|
+
}
|
|
50
|
+
function resolveUserId(user) {
|
|
51
|
+
const userId = typeof user.id === "number" ? user.id : Number(user.id);
|
|
52
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
53
|
+
throw new ForbiddenError("Invalid authenticated user.");
|
|
54
|
+
}
|
|
55
|
+
return userId;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ../../src/config/database.ts
|
|
59
|
+
function readInteger(name, fallback) {
|
|
60
|
+
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
61
|
+
return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
|
|
62
|
+
}
|
|
63
|
+
var databaseConfig = {
|
|
64
|
+
url: process.env.DATABASE_URL ?? "",
|
|
65
|
+
poolMax: readInteger("DB_POOL_MAX", 10),
|
|
66
|
+
idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
|
|
67
|
+
maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
|
|
68
|
+
connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// ../../src/core/database/connectionContext.ts
|
|
72
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
73
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
74
|
+
return activeConnection.run(connection, callback);
|
|
75
|
+
}
|
|
76
|
+
function getActiveDatabaseConnection(fallback) {
|
|
77
|
+
return activeConnection.getStore() ?? fallback;
|
|
78
|
+
}
|
|
79
|
+
function hasActiveDatabaseConnection() {
|
|
80
|
+
return activeConnection.getStore() !== undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ../../src/core/database/queryProxy.ts
|
|
84
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
85
|
+
function createDatabaseQueryProxy(pool) {
|
|
86
|
+
function resolveDatabase() {
|
|
87
|
+
return getActiveDatabaseConnection(pool);
|
|
88
|
+
}
|
|
89
|
+
function resolveDatabaseForProperty(property) {
|
|
90
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
91
|
+
return pool;
|
|
92
|
+
}
|
|
93
|
+
return resolveDatabase();
|
|
94
|
+
}
|
|
95
|
+
return new Proxy(function database() {}, {
|
|
96
|
+
apply(_target, _thisArg, args) {
|
|
97
|
+
return resolveDatabase()(...args);
|
|
98
|
+
},
|
|
99
|
+
get(_target, property) {
|
|
100
|
+
const connection = resolveDatabaseForProperty(property);
|
|
101
|
+
const value = connection[property];
|
|
102
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ../../src/core/database/defaultConnection.ts
|
|
108
|
+
var defaultPool = {
|
|
109
|
+
connection: null
|
|
110
|
+
};
|
|
111
|
+
var defaultQuery = {
|
|
112
|
+
connection: null
|
|
113
|
+
};
|
|
114
|
+
function registerDefaultDatabasePool(connection) {
|
|
115
|
+
defaultPool.connection = connection;
|
|
116
|
+
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
117
|
+
}
|
|
118
|
+
function getDefaultDatabaseQuery() {
|
|
119
|
+
if (!defaultQuery.connection) {
|
|
120
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
121
|
+
}
|
|
122
|
+
return defaultQuery.connection;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ../../src/db/connection/createConnection.ts
|
|
126
|
+
var {SQL } = globalThis.Bun;
|
|
127
|
+
function createDatabaseConnection(config) {
|
|
128
|
+
if (!config.url) {
|
|
129
|
+
throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
|
|
130
|
+
}
|
|
131
|
+
return new SQL({
|
|
132
|
+
url: config.url,
|
|
133
|
+
max: config.poolMax,
|
|
134
|
+
idleTimeout: config.idleTimeoutSeconds,
|
|
135
|
+
maxLifetime: config.maxLifetimeSeconds,
|
|
136
|
+
connectionTimeout: config.connectionTimeoutSeconds
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ../../src/db/connection/index.ts
|
|
141
|
+
var connectionHolder = {
|
|
142
|
+
connection: null
|
|
143
|
+
};
|
|
144
|
+
function getDatabase() {
|
|
145
|
+
if (!connectionHolder.connection) {
|
|
146
|
+
connectionHolder.connection = createDatabaseConnection(databaseConfig);
|
|
147
|
+
registerDefaultDatabasePool(connectionHolder.connection);
|
|
148
|
+
}
|
|
149
|
+
return connectionHolder.connection;
|
|
150
|
+
}
|
|
151
|
+
function getDb() {
|
|
152
|
+
getDatabase();
|
|
153
|
+
return getDefaultDatabaseQuery();
|
|
154
|
+
}
|
|
155
|
+
var db = new Proxy(function database() {}, {
|
|
156
|
+
apply(_target, _thisArg, args) {
|
|
157
|
+
return getDb()(...args);
|
|
158
|
+
},
|
|
159
|
+
get(_target, property) {
|
|
160
|
+
const connection = getDb();
|
|
161
|
+
const value = connection[property];
|
|
162
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
var connection_default = db;
|
|
166
|
+
|
|
167
|
+
// ../../src/modules/organization/memberRepository.ts
|
|
168
|
+
class OrganizationMemberRepository {
|
|
169
|
+
constructor() {}
|
|
170
|
+
async findMembership(userId, organizationId) {
|
|
171
|
+
const rows = await connection_default`
|
|
172
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
173
|
+
FROM organization_member
|
|
174
|
+
WHERE user_id = ${userId} AND organization_id = ${organizationId}
|
|
175
|
+
LIMIT 1
|
|
176
|
+
`;
|
|
177
|
+
return rows[0] ?? null;
|
|
178
|
+
}
|
|
179
|
+
async listForUser(userId) {
|
|
180
|
+
return await connection_default`
|
|
181
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
182
|
+
FROM organization_member
|
|
183
|
+
WHERE user_id = ${userId}
|
|
184
|
+
ORDER BY organization_id
|
|
185
|
+
`;
|
|
186
|
+
}
|
|
187
|
+
async listForOrganization(organizationId) {
|
|
188
|
+
return await connection_default`
|
|
189
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
190
|
+
FROM organization_member
|
|
191
|
+
WHERE organization_id = ${organizationId}
|
|
192
|
+
ORDER BY id
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
async addMember(input) {
|
|
196
|
+
const rows = await connection_default`
|
|
197
|
+
INSERT INTO organization_member (organization_id, user_id, role)
|
|
198
|
+
VALUES (${input.organizationId}, ${input.userId}, ${input.role ?? "member"})
|
|
199
|
+
RETURNING id, organization_id, user_id, role, created_at
|
|
200
|
+
`;
|
|
201
|
+
const row = rows[0];
|
|
202
|
+
if (!row) {
|
|
203
|
+
throw new Error("Organization member insert did not return a row.");
|
|
204
|
+
}
|
|
205
|
+
return row;
|
|
206
|
+
}
|
|
207
|
+
async removeMember(organizationId, userId) {
|
|
208
|
+
const rows = await connection_default`
|
|
209
|
+
DELETE FROM organization_member
|
|
210
|
+
WHERE organization_id = ${organizationId} AND user_id = ${userId}
|
|
211
|
+
RETURNING id
|
|
212
|
+
`;
|
|
213
|
+
return rows.length > 0;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
var memberRepository_default = OrganizationMemberRepository;
|
|
217
|
+
|
|
218
|
+
// ../../src/core/auth/membershipContext.ts
|
|
219
|
+
var membershipContext = createAsyncContextStore("@getstrata/membershipContext");
|
|
220
|
+
var membershipRepository = new memberRepository_default;
|
|
221
|
+
async function runWithMembershipContext(callback) {
|
|
222
|
+
const user = currentAuthUser();
|
|
223
|
+
if (!user || isGlobalAdmin(user)) {
|
|
224
|
+
return await callback();
|
|
225
|
+
}
|
|
226
|
+
const memberships = await membershipRepository.listForUser(resolveUserId(user));
|
|
227
|
+
const context = {
|
|
228
|
+
organizationIds: memberships.map((membership) => membership.organization_id),
|
|
229
|
+
rolesByOrganizationId: new Map(memberships.map((membership) => [membership.organization_id, membership.role]))
|
|
230
|
+
};
|
|
231
|
+
return await membershipContext.run(context, callback);
|
|
232
|
+
}
|
|
233
|
+
function currentOrganizationIds() {
|
|
234
|
+
return membershipContext.getStore()?.organizationIds ?? [];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ../../src/core/auth/resolveMembershipService.ts
|
|
238
|
+
import { resolveApplicationDependencies } from "@getstrata/core/runtime/applicationRegistry";
|
|
239
|
+
function resolveMembershipService() {
|
|
240
|
+
const dependencies = resolveApplicationDependencies();
|
|
241
|
+
if (dependencies.container.has("core.membership")) {
|
|
242
|
+
return dependencies.container.resolve("core.membership");
|
|
243
|
+
}
|
|
244
|
+
return new membershipService_default;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ../../src/core/auth/membershipService.ts
|
|
248
|
+
class MembershipService {
|
|
249
|
+
members;
|
|
250
|
+
constructor(members = membershipRepository) {
|
|
251
|
+
this.members = members;
|
|
252
|
+
}
|
|
253
|
+
async listOrganizationIdsForUser(userId) {
|
|
254
|
+
const memberships = await this.members.listForUser(userId);
|
|
255
|
+
return memberships.map((membership) => membership.organization_id);
|
|
256
|
+
}
|
|
257
|
+
async getOrgRole(userId, organizationId) {
|
|
258
|
+
const membership = await this.members.findMembership(userId, organizationId);
|
|
259
|
+
return membership?.role ?? null;
|
|
260
|
+
}
|
|
261
|
+
async requireOrgAccess(organizationId, minimumRole = "member", user = currentAuthUser()) {
|
|
262
|
+
if (!user) {
|
|
263
|
+
throw new ForbiddenError2("Authentication required.");
|
|
264
|
+
}
|
|
265
|
+
if (isGlobalAdmin(user)) {
|
|
266
|
+
return "owner";
|
|
267
|
+
}
|
|
268
|
+
const role = await this.getOrgRole(resolveUserId(user), organizationId);
|
|
269
|
+
if (!role || !hasMinimumOrgRole(role, minimumRole)) {
|
|
270
|
+
throw new ForbiddenError2("Organization membership required.");
|
|
271
|
+
}
|
|
272
|
+
return role;
|
|
273
|
+
}
|
|
274
|
+
async filterAccessibleOrganizationIds(organizationIds, user = currentAuthUser()) {
|
|
275
|
+
if (!user) {
|
|
276
|
+
return [];
|
|
277
|
+
}
|
|
278
|
+
if (isGlobalAdmin(user)) {
|
|
279
|
+
return organizationIds;
|
|
280
|
+
}
|
|
281
|
+
const allowed = new Set(await this.listOrganizationIdsForUser(resolveUserId(user)));
|
|
282
|
+
return organizationIds.filter((organizationId) => allowed.has(organizationId));
|
|
283
|
+
}
|
|
284
|
+
async addOwnerOnOrganizationCreate(organizationId, userId) {
|
|
285
|
+
await this.members.addMember({
|
|
286
|
+
organizationId,
|
|
287
|
+
userId,
|
|
288
|
+
role: "owner"
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
listMembersForOrganization(organizationId) {
|
|
292
|
+
return this.members.listForOrganization(organizationId);
|
|
293
|
+
}
|
|
294
|
+
addMember(input) {
|
|
295
|
+
return this.members.addMember(input);
|
|
296
|
+
}
|
|
297
|
+
removeMember(organizationId, userId) {
|
|
298
|
+
return this.members.removeMember(organizationId, userId);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
var membershipService_default = MembershipService;
|
|
302
|
+
export {
|
|
303
|
+
resolveMembershipService
|
|
304
|
+
};
|
|
@@ -1 +1,81 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/auth/policy.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/policy.ts
|
|
26
|
+
class Policy {
|
|
27
|
+
constructor() {}
|
|
28
|
+
view(_user, _resource) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
create(_user) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
update(_user, _resource) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
delete(_user, _resource) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
var BLOCKED_POLICY_ACTIONS = new Set([
|
|
42
|
+
"constructor",
|
|
43
|
+
"toString",
|
|
44
|
+
"valueOf",
|
|
45
|
+
"hasOwnProperty",
|
|
46
|
+
"isPrototypeOf",
|
|
47
|
+
"propertyIsEnumerable",
|
|
48
|
+
"__proto__"
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
class PolicyGate {
|
|
52
|
+
constructor() {}
|
|
53
|
+
policies = new Map;
|
|
54
|
+
register(resource, policy) {
|
|
55
|
+
this.policies.set(resource, policy);
|
|
56
|
+
}
|
|
57
|
+
allows(resource, action, user, model) {
|
|
58
|
+
const policy = this.policies.get(resource);
|
|
59
|
+
if (!policy) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (BLOCKED_POLICY_ACTIONS.has(action) || !(action in policy)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const handler = policy[action];
|
|
66
|
+
if (typeof handler !== "function") {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const resolvedUser = user === undefined ? currentAuthUser() : user;
|
|
70
|
+
return model === undefined ? handler.call(policy, resolvedUser) : handler.call(policy, resolvedUser, model);
|
|
71
|
+
}
|
|
72
|
+
authorize(resource, action, user, model) {
|
|
73
|
+
if (!this.allows(resource, action, user, model)) {
|
|
74
|
+
throw new ForbiddenError;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
PolicyGate,
|
|
80
|
+
Policy
|
|
81
|
+
};
|