@getstrata/core 0.5.56 → 0.5.57
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/{domain/abilities.d.ts → core/auth/abilityCatalog.d.ts} +9 -1
- package/dist/core/auth/accessControl.d.ts +1 -1
- package/dist/core/auth/membershipContext.d.ts +6 -4
- package/dist/core/auth/membershipService.d.ts +5 -6
- package/dist/core/contracts/authUserDirectory.d.ts +11 -0
- package/dist/core/contracts/membership.d.ts +20 -0
- package/dist/core/http/clientIp.d.ts +3 -0
- package/dist/core/jobs/dispatchWebhookJob.d.ts +2 -0
- package/dist/core/tenant/tenancyConfig.d.ts +5 -0
- package/dist/core/tenant/tenantMiddleware.d.ts +1 -1
- package/dist/entries/auth/membershipMiddleware.js +21 -161
- package/dist/entries/auth/membershipScope.js +20 -160
- package/dist/entries/auth/membershipService.js +21 -161
- package/dist/entries/auth/scimAuthMiddleware.js +22 -4
- package/dist/entries/auth/sessionGuard.js +12 -258
- package/dist/entries/http/clientIp.js +19 -0
- package/dist/entries/http/csrfMiddleware.js +2 -1
- package/dist/entries/http/csrfToken.js +2 -1
- package/dist/entries/http/loginThrottleMiddleware.js +18 -1
- package/dist/entries/http/memoryThrottleMiddleware.js +16 -1
- package/dist/entries/http/response.js +28 -297
- package/dist/entries/http/scimThrottleMiddleware.js +40 -1
- package/dist/entries/http/throttleMiddleware.js +18 -1
- package/dist/entries/http/webErrorResponse.js +28 -297
- package/dist/entries/jobs/dispatchWebhookJob.js +111 -5
- package/dist/entries/logging/requestLoggingMiddleware.js +16 -1
- package/dist/entries/security/scimTenantTokens.js +1 -4
- package/dist/entries/tenant/tenancyConfig.js +12 -0
- package/dist/entries/tenant/tenantDatabaseScope.js +11 -0
- package/dist/entries/view.js +26 -295
- package/dist/framework/public-api.d.ts +4 -1
- package/dist/index.js +2186 -2243
- package/package.json +12 -2
- package/dist/config/database.d.ts +0 -10
- package/dist/db/connection/createConnection.d.ts +0 -6
- package/dist/db/connection/index.d.ts +0 -11
- package/dist/domain/auth.d.ts +0 -4
- package/dist/domain/scim.d.ts +0 -9
- package/dist/modules/organization/memberRepository.d.ts +0 -14
- package/dist/modules/organization/memberTypes.d.ts +0 -9
- package/dist/modules/user/apiTokenRepository.d.ts +0 -8
- package/dist/modules/user/apiTokenTable.d.ts +0 -3
- package/dist/modules/user/authService.d.ts +0 -23
- package/dist/modules/user/notificationRepository.d.ts +0 -6
- package/dist/modules/user/notificationService.d.ts +0 -25
- package/dist/modules/user/notificationTable.d.ts +0 -3
- package/dist/modules/user/notificationTypes.d.ts +0 -12
- package/dist/modules/user/oauthIdentityRepository.d.ts +0 -9
- package/dist/modules/user/provider.d.ts +0 -11
- package/dist/modules/user/repository.d.ts +0 -13
- package/dist/modules/user/table.d.ts +0 -3
- package/dist/modules/user/tokenService.d.ts +0 -29
- package/dist/modules/user/types.d.ts +0 -46
|
@@ -55,169 +55,29 @@ function resolveUserId(user) {
|
|
|
55
55
|
return userId;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
// ../../src/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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);
|
|
58
|
+
// ../../src/core/auth/membershipContext.ts
|
|
59
|
+
var membershipContext = createAsyncContextStore("@getstrata/membershipContext");
|
|
60
|
+
var uninitializedMembershipLookup = {
|
|
61
|
+
async listForUser() {
|
|
62
|
+
return [];
|
|
158
63
|
},
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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;
|
|
64
|
+
async findMembership() {
|
|
65
|
+
return null;
|
|
66
|
+
},
|
|
67
|
+
async listForOrganization() {
|
|
68
|
+
return [];
|
|
69
|
+
},
|
|
70
|
+
async addMember() {
|
|
71
|
+
throw new Error("configureMembershipLookup() must be called before mutating memberships.");
|
|
72
|
+
},
|
|
73
|
+
async removeMember() {
|
|
74
|
+
throw new Error("configureMembershipLookup() must be called before mutating memberships.");
|
|
214
75
|
}
|
|
76
|
+
};
|
|
77
|
+
var membershipRepository = uninitializedMembershipLookup;
|
|
78
|
+
function resolveMembershipLookup() {
|
|
79
|
+
return membershipRepository;
|
|
215
80
|
}
|
|
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
81
|
async function runWithMembershipContext(callback) {
|
|
222
82
|
const user = currentAuthUser();
|
|
223
83
|
if (!user || isGlobalAdmin(user)) {
|
|
@@ -247,7 +107,7 @@ function resolveMembershipService() {
|
|
|
247
107
|
// ../../src/core/auth/membershipService.ts
|
|
248
108
|
class MembershipService {
|
|
249
109
|
members;
|
|
250
|
-
constructor(members =
|
|
110
|
+
constructor(members = resolveMembershipLookup()) {
|
|
251
111
|
this.members = members;
|
|
252
112
|
}
|
|
253
113
|
async listOrganizationIdsForUser(userId) {
|
|
@@ -30,9 +30,6 @@ function hasActiveDatabaseConnection() {
|
|
|
30
30
|
return activeConnection.getStore() !== undefined;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
// ../../src/domain/scim.ts
|
|
34
|
-
var TEST_SCIM_BEARER_TOKEN = "workhub-scim-test-token";
|
|
35
|
-
|
|
36
33
|
// ../../src/core/security/timingSafeCompare.ts
|
|
37
34
|
import { timingSafeEqual } from "crypto";
|
|
38
35
|
function timingSafeCompareString(left, right) {
|
|
@@ -70,7 +67,7 @@ function resolveScimTenantFromToken(token) {
|
|
|
70
67
|
return tenantId;
|
|
71
68
|
}
|
|
72
69
|
}
|
|
73
|
-
const fallbackToken = process.env.SCIM_BEARER_TOKEN ??
|
|
70
|
+
const fallbackToken = process.env.SCIM_BEARER_TOKEN ?? "";
|
|
74
71
|
if (timingSafeCompareString(token, fallbackToken)) {
|
|
75
72
|
return 1;
|
|
76
73
|
}
|
|
@@ -79,7 +76,25 @@ function resolveScimTenantFromToken(token) {
|
|
|
79
76
|
|
|
80
77
|
// ../../src/core/tenant/resolveTenant.ts
|
|
81
78
|
import { repositoryConnection as db } from "@getstrata/core/database/repositoryConnection";
|
|
79
|
+
|
|
80
|
+
// ../../src/core/tenant/tenancyConfig.ts
|
|
81
|
+
function readTenancyDriver(env = process.env) {
|
|
82
|
+
return env.TENANCY_DRIVER === "none" ? "none" : "rls";
|
|
83
|
+
}
|
|
84
|
+
function isTenancyEnabled(env = process.env) {
|
|
85
|
+
return readTenancyDriver(env) !== "none";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ../../src/core/tenant/resolveTenant.ts
|
|
82
89
|
async function resolveTenant(tenantId) {
|
|
90
|
+
if (!isTenancyEnabled()) {
|
|
91
|
+
return {
|
|
92
|
+
id: tenantId,
|
|
93
|
+
slug: "default",
|
|
94
|
+
plan: "free",
|
|
95
|
+
region: "eu"
|
|
96
|
+
};
|
|
97
|
+
}
|
|
83
98
|
const rows = await db`
|
|
84
99
|
SELECT id, slug, plan, region
|
|
85
100
|
FROM tenant
|
|
@@ -108,6 +123,9 @@ async function applyTenantContextToTransaction(transaction, tenantId) {
|
|
|
108
123
|
await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
|
|
109
124
|
}
|
|
110
125
|
async function runWithTenantDatabase(tenant, callback) {
|
|
126
|
+
if (!isTenancyEnabled()) {
|
|
127
|
+
return await runWithTenant(tenant, callback);
|
|
128
|
+
}
|
|
111
129
|
if (hasActiveDatabaseConnection()) {
|
|
112
130
|
const activeConnection2 = getActiveDatabaseConnection(getDefaultDatabasePool());
|
|
113
131
|
await applyTenantContextToTransaction(activeConnection2, tenant.id);
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// ../../src/
|
|
2
|
+
// ../../src/core/contracts/serviceTokens.ts
|
|
3
|
+
var CORE_CONFIG_TOKEN = "core.config";
|
|
4
|
+
var CORE_CACHE_TOKEN = "core.cache";
|
|
5
|
+
var CORE_QUEUE_TOKEN = "core.queue";
|
|
6
|
+
var CORE_EVENT_BUS_TOKEN = "core.eventBus";
|
|
7
|
+
var CORE_POLICY_GATE_TOKEN = "core.policyGate";
|
|
8
|
+
var CORE_AUTH_TOKEN = "core.auth";
|
|
9
|
+
var CORE_TOKEN_SERVICE_TOKEN = "core.tokenService";
|
|
10
|
+
|
|
11
|
+
// ../../src/core/auth/abilityCatalog.ts
|
|
3
12
|
var MEMBER_ABILITIES = [
|
|
4
13
|
"organizations:read",
|
|
5
14
|
"projects:read",
|
|
@@ -37,261 +46,6 @@ function resolveAbilitiesForRole(role) {
|
|
|
37
46
|
return [...MEMBER_ABILITIES];
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
// ../../src/bootstrap/config.ts
|
|
41
|
-
import {
|
|
42
|
-
CORE_AUTH_TOKEN,
|
|
43
|
-
CORE_CACHE_TOKEN,
|
|
44
|
-
CORE_CONFIG_TOKEN,
|
|
45
|
-
CORE_EVENT_BUS_TOKEN,
|
|
46
|
-
CORE_POLICY_GATE_TOKEN,
|
|
47
|
-
CORE_QUEUE_TOKEN,
|
|
48
|
-
CORE_TOKEN_SERVICE_TOKEN
|
|
49
|
-
} from "@getstrata/core/contracts/serviceTokens";
|
|
50
|
-
var DEFAULT_QUEUE_DRIVER = "sync";
|
|
51
|
-
|
|
52
|
-
// ../../src/modules/user/provider.ts
|
|
53
|
-
import { OidcProvider } from "@getstrata/core/auth/oauth/oidcProvider";
|
|
54
|
-
import { GitHubOAuthProvider, MockOAuthProvider } from "@getstrata/core/auth/oauth/providers";
|
|
55
|
-
import { SamlProvider } from "@getstrata/core/auth/oauth/samlProvider";
|
|
56
|
-
|
|
57
|
-
// ../../src/config/features.ts
|
|
58
|
-
function readFeatureFlags() {
|
|
59
|
-
return {
|
|
60
|
-
webhooks: (process.env.FEATURE_WEBHOOKS ?? "true") !== "false",
|
|
61
|
-
fullTextSearch: (process.env.FEATURE_SEARCH ?? "true") !== "false",
|
|
62
|
-
auditLog: (process.env.FEATURE_AUDIT_LOG ?? "true") !== "false",
|
|
63
|
-
oauthLogin: (process.env.FEATURE_OAUTH ?? "true") !== "false",
|
|
64
|
-
samlLogin: (process.env.FEATURE_SAML ?? "false") === "true",
|
|
65
|
-
scim: (process.env.FEATURE_SCIM ?? "true") !== "false",
|
|
66
|
-
billing: (process.env.FEATURE_BILLING ?? "true") !== "false",
|
|
67
|
-
siemExport: (process.env.FEATURE_SIEM_EXPORT ?? "true") !== "false",
|
|
68
|
-
publicReads: (process.env.FEATURE_PUBLIC_READS ?? "true") !== "false",
|
|
69
|
-
emailVerification: (process.env.FEATURE_EMAIL_VERIFICATION ?? "false") === "true",
|
|
70
|
-
mfa: (process.env.FEATURE_MFA ?? "false") === "true"
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
var featureFlags = readFeatureFlags();
|
|
74
|
-
function isFeatureEnabled(feature) {
|
|
75
|
-
return readFeatureFlags()[feature];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// ../../src/modules/user/apiTokenRepository.ts
|
|
79
|
-
import { BaseRepository } from "@getstrata/core/database/baseRepository";
|
|
80
|
-
|
|
81
|
-
// ../../src/config/database.ts
|
|
82
|
-
function readInteger(name, fallback) {
|
|
83
|
-
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
84
|
-
return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
|
|
85
|
-
}
|
|
86
|
-
var databaseConfig = {
|
|
87
|
-
url: process.env.DATABASE_URL ?? "",
|
|
88
|
-
poolMax: readInteger("DB_POOL_MAX", 10),
|
|
89
|
-
idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
|
|
90
|
-
maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
|
|
91
|
-
connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
// ../../src/core/runtime/asyncContextStore.ts
|
|
95
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
96
|
-
function createAsyncContextStore(key) {
|
|
97
|
-
const symbol = Symbol.for(key);
|
|
98
|
-
const globalRecord = globalThis;
|
|
99
|
-
const existing = globalRecord[symbol];
|
|
100
|
-
if (existing) {
|
|
101
|
-
return existing;
|
|
102
|
-
}
|
|
103
|
-
const store = new AsyncLocalStorage;
|
|
104
|
-
globalRecord[symbol] = store;
|
|
105
|
-
return store;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// ../../src/core/database/connectionContext.ts
|
|
109
|
-
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
110
|
-
function runWithDatabaseConnection(connection, callback) {
|
|
111
|
-
return activeConnection.run(connection, callback);
|
|
112
|
-
}
|
|
113
|
-
function getActiveDatabaseConnection(fallback) {
|
|
114
|
-
return activeConnection.getStore() ?? fallback;
|
|
115
|
-
}
|
|
116
|
-
function hasActiveDatabaseConnection() {
|
|
117
|
-
return activeConnection.getStore() !== undefined;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// ../../src/core/database/queryProxy.ts
|
|
121
|
-
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
122
|
-
function createDatabaseQueryProxy(pool) {
|
|
123
|
-
function resolveDatabase() {
|
|
124
|
-
return getActiveDatabaseConnection(pool);
|
|
125
|
-
}
|
|
126
|
-
function resolveDatabaseForProperty(property) {
|
|
127
|
-
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
128
|
-
return pool;
|
|
129
|
-
}
|
|
130
|
-
return resolveDatabase();
|
|
131
|
-
}
|
|
132
|
-
return new Proxy(function database() {}, {
|
|
133
|
-
apply(_target, _thisArg, args) {
|
|
134
|
-
return resolveDatabase()(...args);
|
|
135
|
-
},
|
|
136
|
-
get(_target, property) {
|
|
137
|
-
const connection = resolveDatabaseForProperty(property);
|
|
138
|
-
const value = connection[property];
|
|
139
|
-
return typeof value === "function" ? value.bind(connection) : value;
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// ../../src/core/database/defaultConnection.ts
|
|
145
|
-
var defaultPool = {
|
|
146
|
-
connection: null
|
|
147
|
-
};
|
|
148
|
-
var defaultQuery = {
|
|
149
|
-
connection: null
|
|
150
|
-
};
|
|
151
|
-
function registerDefaultDatabasePool(connection) {
|
|
152
|
-
defaultPool.connection = connection;
|
|
153
|
-
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
154
|
-
}
|
|
155
|
-
function getDefaultDatabaseQuery() {
|
|
156
|
-
if (!defaultQuery.connection) {
|
|
157
|
-
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
158
|
-
}
|
|
159
|
-
return defaultQuery.connection;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// ../../src/db/connection/createConnection.ts
|
|
163
|
-
var {SQL } = globalThis.Bun;
|
|
164
|
-
function createDatabaseConnection(config) {
|
|
165
|
-
if (!config.url) {
|
|
166
|
-
throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
|
|
167
|
-
}
|
|
168
|
-
return new SQL({
|
|
169
|
-
url: config.url,
|
|
170
|
-
max: config.poolMax,
|
|
171
|
-
idleTimeout: config.idleTimeoutSeconds,
|
|
172
|
-
maxLifetime: config.maxLifetimeSeconds,
|
|
173
|
-
connectionTimeout: config.connectionTimeoutSeconds
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// ../../src/db/connection/index.ts
|
|
178
|
-
var connectionHolder = {
|
|
179
|
-
connection: null
|
|
180
|
-
};
|
|
181
|
-
function getDatabase() {
|
|
182
|
-
if (!connectionHolder.connection) {
|
|
183
|
-
connectionHolder.connection = createDatabaseConnection(databaseConfig);
|
|
184
|
-
registerDefaultDatabasePool(connectionHolder.connection);
|
|
185
|
-
}
|
|
186
|
-
return connectionHolder.connection;
|
|
187
|
-
}
|
|
188
|
-
function getDb() {
|
|
189
|
-
getDatabase();
|
|
190
|
-
return getDefaultDatabaseQuery();
|
|
191
|
-
}
|
|
192
|
-
var db = new Proxy(function database() {}, {
|
|
193
|
-
apply(_target, _thisArg, args) {
|
|
194
|
-
return getDb()(...args);
|
|
195
|
-
},
|
|
196
|
-
get(_target, property) {
|
|
197
|
-
const connection = getDb();
|
|
198
|
-
const value = connection[property];
|
|
199
|
-
return typeof value === "function" ? value.bind(connection) : value;
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
var connection_default = db;
|
|
203
|
-
|
|
204
|
-
// ../../src/modules/user/apiTokenTable.ts
|
|
205
|
-
import { defineTable } from "@getstrata/core/database/table";
|
|
206
|
-
var apiTokenTable = defineTable({
|
|
207
|
-
name: "api_token",
|
|
208
|
-
primaryKey: "id",
|
|
209
|
-
columns: [
|
|
210
|
-
"id",
|
|
211
|
-
"user_id",
|
|
212
|
-
"name",
|
|
213
|
-
"token_hash",
|
|
214
|
-
"abilities",
|
|
215
|
-
"last_used_at",
|
|
216
|
-
"expires_at",
|
|
217
|
-
"created_at"
|
|
218
|
-
],
|
|
219
|
-
defaultOrderBy: { column: "id", direction: "ASC" }
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// ../../src/modules/user/authService.ts
|
|
223
|
-
import { verifyPassword } from "@getstrata/core/auth/password";
|
|
224
|
-
import { revealMfaSecret } from "@getstrata/core/crypto/mfaSecret";
|
|
225
|
-
import { UnauthorizedError } from "@getstrata/core/errors/http";
|
|
226
|
-
import { logSecurityEvent } from "@getstrata/core/security/securityEvents";
|
|
227
|
-
import { resolveDefaultTokenExpiryDays } from "@getstrata/core/security/tokenExpiry";
|
|
228
|
-
import { verifyTotp } from "@getstrata/core/security/totp";
|
|
229
|
-
import { currentTenantId } from "@getstrata/core/tenant/tenantContext";
|
|
230
|
-
|
|
231
|
-
// ../../src/modules/user/notificationRepository.ts
|
|
232
|
-
import { BaseRepository as BaseRepository2 } from "@getstrata/core/database/baseRepository";
|
|
233
|
-
|
|
234
|
-
// ../../src/modules/user/notificationTable.ts
|
|
235
|
-
import { defineTable as defineTable2 } from "@getstrata/core/database/table";
|
|
236
|
-
var notificationTable = defineTable2({
|
|
237
|
-
name: "notification",
|
|
238
|
-
primaryKey: "id",
|
|
239
|
-
columns: ["id", "user_id", "tenant_id", "type", "title", "body", "data", "read_at", "created_at"],
|
|
240
|
-
defaultOrderBy: { column: "created_at", direction: "DESC" }
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// ../../src/modules/user/notificationService.ts
|
|
244
|
-
import { NotFoundError } from "@getstrata/core/errors/http";
|
|
245
|
-
|
|
246
|
-
// ../../src/modules/user/oauthIdentityRepository.ts
|
|
247
|
-
import { BaseRepository as BaseRepository3 } from "@getstrata/core/database/baseRepository";
|
|
248
|
-
import { defineTable as defineTable3 } from "@getstrata/core/database/table";
|
|
249
|
-
var oauthIdentityTable = defineTable3({
|
|
250
|
-
name: "oauth_identity",
|
|
251
|
-
primaryKey: "id",
|
|
252
|
-
columns: ["id", "user_id", "provider", "provider_user_id", "email", "created_at"]
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
// ../../src/modules/user/repository.ts
|
|
256
|
-
import {
|
|
257
|
-
emailLookupForQuery,
|
|
258
|
-
protectEmail,
|
|
259
|
-
revealEmail
|
|
260
|
-
} from "@getstrata/core/crypto/fieldEncryption";
|
|
261
|
-
import { revealMfaSecret as revealMfaSecret2 } from "@getstrata/core/crypto/mfaSecret";
|
|
262
|
-
import { BaseRepository as BaseRepository4 } from "@getstrata/core/database/baseRepository";
|
|
263
|
-
import { currentTenantId as currentTenantId2 } from "@getstrata/core/tenant/tenantContext";
|
|
264
|
-
|
|
265
|
-
// ../../src/modules/user/table.ts
|
|
266
|
-
import { defineTable as defineTable4 } from "@getstrata/core/database/table";
|
|
267
|
-
var userTable = defineTable4({
|
|
268
|
-
name: "users",
|
|
269
|
-
primaryKey: "id",
|
|
270
|
-
columns: [
|
|
271
|
-
"id",
|
|
272
|
-
"name",
|
|
273
|
-
"email",
|
|
274
|
-
"email_lookup",
|
|
275
|
-
"role",
|
|
276
|
-
"tenant_id",
|
|
277
|
-
"password_hash",
|
|
278
|
-
"email_verified_at",
|
|
279
|
-
"mfa_secret",
|
|
280
|
-
"mfa_enabled",
|
|
281
|
-
"created_at",
|
|
282
|
-
"updated_at"
|
|
283
|
-
],
|
|
284
|
-
defaultOrderBy: { column: "id", direction: "ASC" }
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
// ../../src/modules/user/tokenService.ts
|
|
288
|
-
import { hashApiToken } from "@getstrata/core/auth/tokenHash";
|
|
289
|
-
import { ForbiddenError, NotFoundError as NotFoundError2 } from "@getstrata/core/errors/http";
|
|
290
|
-
import { resolveDefaultTokenExpiryDays as resolveDefaultTokenExpiryDays2 } from "@getstrata/core/security/tokenExpiry";
|
|
291
|
-
|
|
292
|
-
// ../../src/modules/user/provider.ts
|
|
293
|
-
var tokenServiceToken = CORE_TOKEN_SERVICE_TOKEN;
|
|
294
|
-
|
|
295
49
|
// ../../src/core/auth/sessionCookie.ts
|
|
296
50
|
import { createHmac, timingSafeEqual } from "crypto";
|
|
297
51
|
var SESSION_COOKIE = "workhub_session";
|
|
@@ -371,10 +125,10 @@ class SessionGuard {
|
|
|
371
125
|
if (!userId) {
|
|
372
126
|
return null;
|
|
373
127
|
}
|
|
374
|
-
if (!this.container.has(
|
|
128
|
+
if (!this.container.has(CORE_TOKEN_SERVICE_TOKEN)) {
|
|
375
129
|
return null;
|
|
376
130
|
}
|
|
377
|
-
const tokenService = this.container.resolve(
|
|
131
|
+
const tokenService = this.container.resolve(CORE_TOKEN_SERVICE_TOKEN);
|
|
378
132
|
try {
|
|
379
133
|
const user = await tokenService.findByIdOrThrow(userId);
|
|
380
134
|
return {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/http/clientIp.ts
|
|
3
|
+
function trustForwardedFor(env = process.env) {
|
|
4
|
+
return (env.TRUST_FORWARDED_FOR ?? "false") === "true";
|
|
5
|
+
}
|
|
6
|
+
function readClientIp(request, env = process.env) {
|
|
7
|
+
if (!trustForwardedFor(env)) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const forwarded = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim();
|
|
11
|
+
if (forwarded) {
|
|
12
|
+
return forwarded;
|
|
13
|
+
}
|
|
14
|
+
return request.headers.get("x-real-ip")?.trim() || undefined;
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
readClientIp,
|
|
18
|
+
trustForwardedFor
|
|
19
|
+
};
|
|
@@ -78,9 +78,10 @@ function tokensMatch(left, right) {
|
|
|
78
78
|
}
|
|
79
79
|
function createCsrfTokenCookie() {
|
|
80
80
|
const token = Bun.CSRF.generate(resolveCsrfSecret(), { expiresIn: CSRF_TTL_MS });
|
|
81
|
+
const secure = process.env.APP_ENV === "production" ? "; Secure" : "";
|
|
81
82
|
return {
|
|
82
83
|
token,
|
|
83
|
-
cookie: `${CSRF_COOKIE}=${encodeURIComponent(token)}; Path=/; SameSite=Lax; Max-Age=${Math.floor(CSRF_TTL_MS / 1000)}`
|
|
84
|
+
cookie: `${CSRF_COOKIE}=${encodeURIComponent(token)}; Path=/; SameSite=Lax; Max-Age=${Math.floor(CSRF_TTL_MS / 1000)}${secure}`
|
|
84
85
|
};
|
|
85
86
|
}
|
|
86
87
|
function resolveCsrfToken(request) {
|
|
@@ -75,9 +75,10 @@ function tokensMatch(left, right) {
|
|
|
75
75
|
}
|
|
76
76
|
function createCsrfTokenCookie() {
|
|
77
77
|
const token = Bun.CSRF.generate(resolveCsrfSecret(), { expiresIn: CSRF_TTL_MS });
|
|
78
|
+
const secure = process.env.APP_ENV === "production" ? "; Secure" : "";
|
|
78
79
|
return {
|
|
79
80
|
token,
|
|
80
|
-
cookie: `${CSRF_COOKIE}=${encodeURIComponent(token)}; Path=/; SameSite=Lax; Max-Age=${Math.floor(CSRF_TTL_MS / 1000)}`
|
|
81
|
+
cookie: `${CSRF_COOKIE}=${encodeURIComponent(token)}; Path=/; SameSite=Lax; Max-Age=${Math.floor(CSRF_TTL_MS / 1000)}${secure}`
|
|
81
82
|
};
|
|
82
83
|
}
|
|
83
84
|
function resolveCsrfToken(request) {
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// ../../src/core/http/loginThrottleMiddleware.ts
|
|
3
3
|
var {RedisClient } = globalThis.Bun;
|
|
4
|
+
|
|
5
|
+
// ../../src/core/http/clientIp.ts
|
|
6
|
+
function trustForwardedFor(env = process.env) {
|
|
7
|
+
return (env.TRUST_FORWARDED_FOR ?? "false") === "true";
|
|
8
|
+
}
|
|
9
|
+
function readClientIp(request, env = process.env) {
|
|
10
|
+
if (!trustForwardedFor(env)) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const forwarded = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim();
|
|
14
|
+
if (forwarded) {
|
|
15
|
+
return forwarded;
|
|
16
|
+
}
|
|
17
|
+
return request.headers.get("x-real-ip")?.trim() || undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ../../src/core/http/loginThrottleMiddleware.ts
|
|
4
21
|
function resolveLoginIdentity(request) {
|
|
5
|
-
return request
|
|
22
|
+
return readClientIp(request) ?? "unknown";
|
|
6
23
|
}
|
|
7
24
|
async function resolveLoginEmail(request) {
|
|
8
25
|
const contentType = request.headers.get("content-type")?.toLowerCase() ?? "";
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// ../../src/core/http/clientIp.ts
|
|
3
|
+
function trustForwardedFor(env = process.env) {
|
|
4
|
+
return (env.TRUST_FORWARDED_FOR ?? "false") === "true";
|
|
5
|
+
}
|
|
6
|
+
function readClientIp(request, env = process.env) {
|
|
7
|
+
if (!trustForwardedFor(env)) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const forwarded = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim();
|
|
11
|
+
if (forwarded) {
|
|
12
|
+
return forwarded;
|
|
13
|
+
}
|
|
14
|
+
return request.headers.get("x-real-ip")?.trim() || undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
2
17
|
// ../../src/core/http/memoryThrottleMiddleware.ts
|
|
3
18
|
var buckets = new Map;
|
|
4
19
|
function createMemoryThrottleMiddleware(options) {
|
|
5
20
|
const prefix = options.keyPrefix ?? "workhub:memory-throttle:";
|
|
6
21
|
return async (request, next) => {
|
|
7
22
|
const path = new URL(request.url).pathname;
|
|
8
|
-
const identity = request
|
|
23
|
+
const identity = readClientIp(request) ?? request.headers.get("authorization")?.slice(0, 32) ?? "unknown";
|
|
9
24
|
const key = `${prefix}${identity}:${path}`;
|
|
10
25
|
const now = Date.now();
|
|
11
26
|
const existing = buckets.get(key);
|