@getstrata/core 0.5.55 → 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 +3 -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/media/imageTransform.d.ts +13 -0
- package/dist/core/scheduler/osCron.d.ts +14 -0
- package/dist/core/tenant/tenancyConfig.d.ts +5 -0
- package/dist/core/tenant/tenantMiddleware.d.ts +1 -1
- package/dist/core/terminal/runShell.d.ts +6 -0
- package/dist/core/view/assertEtaHtmlSource.d.ts +5 -0
- package/dist/core/view/etaViewEngine.d.ts +4 -0
- package/dist/entries/audit/exportAuditLogs.js +2 -2
- package/dist/entries/audit/siemFormatter.js +2 -2
- package/dist/entries/auth/accessControl.js +4 -4
- package/dist/entries/auth/membershipMiddleware.js +21 -161
- package/dist/entries/auth/membershipScope.js +26 -166
- package/dist/entries/auth/membershipService.js +21 -161
- package/dist/entries/auth/oauth/providers.js +2 -2
- package/dist/entries/auth/password.js +2 -2
- package/dist/entries/auth/policy.js +2 -2
- package/dist/entries/auth/scimAuthMiddleware.js +22 -4
- package/dist/entries/auth/sessionCookie.js +4 -4
- package/dist/entries/auth/sessionGuard.js +12 -349
- package/dist/entries/auth/tokenHash.js +2 -2
- package/dist/entries/config/envSchema.js +2 -2
- package/dist/entries/contracts/container.js +2 -2
- package/dist/entries/contracts/di.js +2 -2
- package/dist/entries/contracts/serviceTokens.js +6 -6
- package/dist/entries/crypto/fieldEncryption.js +8 -8
- package/dist/entries/crypto/mfaSecret.js +2 -2
- package/dist/entries/database/errors.js +2 -2
- package/dist/entries/database/migrations.js +8 -8
- package/dist/entries/database/model.js +5 -5
- package/dist/entries/database/query.js +19 -19
- package/dist/entries/database/relationships.js +13 -13
- package/dist/entries/database/schema.js +13 -13
- package/dist/entries/database/seeders.js +2 -2
- package/dist/entries/facades.js +8 -8
- package/dist/entries/http/authMiddleware.js +2 -2
- package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
- package/dist/entries/http/clientIp.js +19 -0
- package/dist/entries/http/cookies.js +2 -2
- package/dist/entries/http/csrfMiddleware.js +2 -1
- package/dist/entries/http/csrfProtection.js +2 -2
- package/dist/entries/http/csrfToken.js +8 -7
- package/dist/entries/http/etag.js +8 -8
- package/dist/entries/http/flashSession.js +4 -4
- package/dist/entries/http/formRequest.js +2 -2
- package/dist/entries/http/loginThrottleMiddleware.js +20 -3
- package/dist/entries/http/memoryThrottleMiddleware.js +16 -1
- package/dist/entries/http/metricsMiddleware.js +2 -2
- package/dist/entries/http/pagination.js +4 -4
- package/dist/entries/http/parseFormBody.js +2 -2
- package/dist/entries/http/parseMultipartUpload.js +2 -2
- package/dist/entries/http/resources.js +2 -2
- package/dist/entries/http/response.js +214 -395
- package/dist/entries/http/scimThrottleMiddleware.js +40 -1
- package/dist/entries/http/securedRouteModelBinding.js +2 -2
- package/dist/entries/http/throttleMiddleware.js +20 -3
- package/dist/entries/http/validation.js +13 -13
- package/dist/entries/http/webErrorResponse.js +212 -393
- package/dist/entries/jobs/dispatchWebhookJob.js +111 -5
- package/dist/entries/lifecycle/gracefulShutdown.js +3 -3
- package/dist/entries/logging/logger.js +2 -2
- package/dist/entries/logging/requestLoggingMiddleware.js +16 -1
- package/dist/entries/mail/mailer.js +6 -6
- package/dist/entries/mail/markdownMail.js +3 -3
- package/dist/entries/mail/markdownMailable.js +2 -2
- package/dist/entries/media/imageTransform.js +40 -0
- package/dist/entries/metrics/prometheus.js +2 -2
- package/dist/entries/openapi/generator.js +2 -2
- package/dist/entries/queue/createAppQueue.js +4 -4
- package/dist/entries/queue/publicQueue.js +10 -10
- package/dist/entries/queue/queueMetrics.js +2 -2
- package/dist/entries/queue/redisQueue.js +7 -7
- package/dist/entries/queue.js +3 -3
- package/dist/entries/scheduler/osCron.js +27 -0
- package/dist/entries/scheduler/schedule.js +2 -2
- package/dist/entries/security/oauthState.js +3 -3
- package/dist/entries/security/publicReads.js +2 -2
- package/dist/entries/security/safeFetch.js +2 -2
- package/dist/entries/security/safeUrl.js +5 -5
- package/dist/entries/security/scimTenantTokens.js +3 -6
- package/dist/entries/security/totp.js +2 -2
- package/dist/entries/storage/storage.js +7 -7
- package/dist/entries/tenant/tenancyConfig.js +12 -0
- package/dist/entries/tenant/tenantDatabaseScope.js +13 -2
- package/dist/entries/terminal/runShell.js +18 -0
- package/dist/entries/validation/rules.js +12 -12
- package/dist/entries/view.js +212 -393
- package/dist/framework/public-api.d.ts +4 -1
- package/dist/index.js +2591 -2686
- package/package.json +27 -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);
|
|
@@ -67,9 +67,9 @@ function clearSessionCookie() {
|
|
|
67
67
|
return `${SESSION_COOKIE}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0${secure}`;
|
|
68
68
|
}
|
|
69
69
|
export {
|
|
70
|
-
|
|
71
|
-
createSessionCookie,
|
|
72
|
-
clearSessionCookie,
|
|
70
|
+
SESSION_COOKIE,
|
|
73
71
|
SESSION_TTL_SECONDS,
|
|
74
|
-
|
|
72
|
+
clearSessionCookie,
|
|
73
|
+
createSessionCookie,
|
|
74
|
+
readSessionUserId
|
|
75
75
|
};
|
|
@@ -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,352 +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
|
-
class AuthService {
|
|
231
|
-
users;
|
|
232
|
-
tokens;
|
|
233
|
-
oauthIdentities;
|
|
234
|
-
oauthProviders = new Map;
|
|
235
|
-
constructor(users, tokens, oauthIdentities) {
|
|
236
|
-
this.users = users;
|
|
237
|
-
this.tokens = tokens;
|
|
238
|
-
this.oauthIdentities = oauthIdentities;
|
|
239
|
-
}
|
|
240
|
-
registerOAuthProvider(provider) {
|
|
241
|
-
this.oauthProviders.set(provider.name, provider);
|
|
242
|
-
}
|
|
243
|
-
getOAuthProvider(name) {
|
|
244
|
-
return this.oauthProviders.get(name);
|
|
245
|
-
}
|
|
246
|
-
async loginWithPassword(email, password, options = {}) {
|
|
247
|
-
const user = await this.users.findByEmail(email);
|
|
248
|
-
if (!user?.password_hash) {
|
|
249
|
-
logSecurityEvent("auth_login_failed", { reason: "unknown_user", email });
|
|
250
|
-
throw new UnauthorizedError("Invalid credentials.");
|
|
251
|
-
}
|
|
252
|
-
const valid = await verifyPassword(password, user.password_hash);
|
|
253
|
-
if (!valid) {
|
|
254
|
-
logSecurityEvent("auth_login_failed", { reason: "invalid_password", user_id: user.id });
|
|
255
|
-
throw new UnauthorizedError("Invalid credentials.");
|
|
256
|
-
}
|
|
257
|
-
if (isFeatureEnabled("emailVerification") && !user.email_verified_at) {
|
|
258
|
-
logSecurityEvent("auth_login_blocked", { reason: "email_unverified", user_id: user.id });
|
|
259
|
-
throw new UnauthorizedError("Email address is not verified.");
|
|
260
|
-
}
|
|
261
|
-
if (isFeatureEnabled("mfa") && user.mfa_enabled) {
|
|
262
|
-
const mfaSecret = revealMfaSecret(user.mfa_secret);
|
|
263
|
-
if (!mfaSecret || !options.mfaCode || !verifyTotp(mfaSecret, options.mfaCode)) {
|
|
264
|
-
logSecurityEvent("auth_login_failed", { reason: "invalid_mfa", user_id: user.id });
|
|
265
|
-
throw new UnauthorizedError("Invalid MFA code.");
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
logSecurityEvent("auth_login_success", { user_id: user.id, method: "password" });
|
|
269
|
-
return await this.tokens.createToken(user.id, {
|
|
270
|
-
name: "password-login",
|
|
271
|
-
abilities: resolveAbilitiesForRole(user.role),
|
|
272
|
-
expiresInDays: resolveDefaultTokenExpiryDays() ?? undefined
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
async loginWithOAuth(providerName, code) {
|
|
276
|
-
const provider = this.oauthProviders.get(providerName);
|
|
277
|
-
if (!provider) {
|
|
278
|
-
throw new UnauthorizedError("Unsupported OAuth provider.");
|
|
279
|
-
}
|
|
280
|
-
const profile = await provider.exchangeCode(code);
|
|
281
|
-
const user = await this.findOrCreateOAuthUser(providerName, profile);
|
|
282
|
-
logSecurityEvent("auth_login_success", { user_id: user.id, method: `oauth:${providerName}` });
|
|
283
|
-
return await this.tokens.createToken(user.id, {
|
|
284
|
-
name: `${providerName}-oauth`,
|
|
285
|
-
abilities: resolveAbilitiesForRole(user.role),
|
|
286
|
-
expiresInDays: resolveDefaultTokenExpiryDays() ?? undefined
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
buildOAuthAuthorizationUrl(providerName, state) {
|
|
290
|
-
const provider = this.oauthProviders.get(providerName);
|
|
291
|
-
if (!provider) {
|
|
292
|
-
throw new UnauthorizedError("Unsupported OAuth provider.");
|
|
293
|
-
}
|
|
294
|
-
return provider.getAuthorizationUrl(state);
|
|
295
|
-
}
|
|
296
|
-
async findOrCreateOAuthUser(providerName, profile) {
|
|
297
|
-
const existingIdentity = await this.oauthIdentities.findByProviderUser(providerName, profile.providerUserId);
|
|
298
|
-
if (existingIdentity) {
|
|
299
|
-
return await this.users.findByIdOrThrow(existingIdentity.user_id);
|
|
300
|
-
}
|
|
301
|
-
const existingUser = await this.users.findByEmail(profile.email);
|
|
302
|
-
const user = existingUser ?? await this.users.create({
|
|
303
|
-
name: profile.name,
|
|
304
|
-
email: profile.email,
|
|
305
|
-
role: "member",
|
|
306
|
-
tenant_id: currentTenantId(),
|
|
307
|
-
email_verified_at: new Date,
|
|
308
|
-
created_at: new Date,
|
|
309
|
-
updated_at: new Date
|
|
310
|
-
});
|
|
311
|
-
await this.oauthIdentities.create({
|
|
312
|
-
user_id: user.id,
|
|
313
|
-
provider: providerName,
|
|
314
|
-
provider_user_id: profile.providerUserId,
|
|
315
|
-
email: profile.email,
|
|
316
|
-
created_at: new Date
|
|
317
|
-
});
|
|
318
|
-
return user;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// ../../src/modules/user/notificationRepository.ts
|
|
323
|
-
import { BaseRepository as BaseRepository2 } from "@getstrata/core/database/baseRepository";
|
|
324
|
-
|
|
325
|
-
// ../../src/modules/user/notificationTable.ts
|
|
326
|
-
import { defineTable as defineTable2 } from "@getstrata/core/database/table";
|
|
327
|
-
var notificationTable = defineTable2({
|
|
328
|
-
name: "notification",
|
|
329
|
-
primaryKey: "id",
|
|
330
|
-
columns: ["id", "user_id", "tenant_id", "type", "title", "body", "data", "read_at", "created_at"],
|
|
331
|
-
defaultOrderBy: { column: "created_at", direction: "DESC" }
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
// ../../src/modules/user/notificationService.ts
|
|
335
|
-
import { NotFoundError } from "@getstrata/core/errors/http";
|
|
336
|
-
|
|
337
|
-
// ../../src/modules/user/oauthIdentityRepository.ts
|
|
338
|
-
import { BaseRepository as BaseRepository3 } from "@getstrata/core/database/baseRepository";
|
|
339
|
-
import { defineTable as defineTable3 } from "@getstrata/core/database/table";
|
|
340
|
-
var oauthIdentityTable = defineTable3({
|
|
341
|
-
name: "oauth_identity",
|
|
342
|
-
primaryKey: "id",
|
|
343
|
-
columns: ["id", "user_id", "provider", "provider_user_id", "email", "created_at"]
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
// ../../src/modules/user/repository.ts
|
|
347
|
-
import {
|
|
348
|
-
emailLookupForQuery,
|
|
349
|
-
protectEmail,
|
|
350
|
-
revealEmail
|
|
351
|
-
} from "@getstrata/core/crypto/fieldEncryption";
|
|
352
|
-
import { revealMfaSecret as revealMfaSecret2 } from "@getstrata/core/crypto/mfaSecret";
|
|
353
|
-
import { BaseRepository as BaseRepository4 } from "@getstrata/core/database/baseRepository";
|
|
354
|
-
import { currentTenantId as currentTenantId2 } from "@getstrata/core/tenant/tenantContext";
|
|
355
|
-
|
|
356
|
-
// ../../src/modules/user/table.ts
|
|
357
|
-
import { defineTable as defineTable4 } from "@getstrata/core/database/table";
|
|
358
|
-
var userTable = defineTable4({
|
|
359
|
-
name: "users",
|
|
360
|
-
primaryKey: "id",
|
|
361
|
-
columns: [
|
|
362
|
-
"id",
|
|
363
|
-
"name",
|
|
364
|
-
"email",
|
|
365
|
-
"email_lookup",
|
|
366
|
-
"role",
|
|
367
|
-
"tenant_id",
|
|
368
|
-
"password_hash",
|
|
369
|
-
"email_verified_at",
|
|
370
|
-
"mfa_secret",
|
|
371
|
-
"mfa_enabled",
|
|
372
|
-
"created_at",
|
|
373
|
-
"updated_at"
|
|
374
|
-
],
|
|
375
|
-
defaultOrderBy: { column: "id", direction: "ASC" }
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
// ../../src/modules/user/tokenService.ts
|
|
379
|
-
import { hashApiToken } from "@getstrata/core/auth/tokenHash";
|
|
380
|
-
import { ForbiddenError, NotFoundError as NotFoundError2 } from "@getstrata/core/errors/http";
|
|
381
|
-
import { resolveDefaultTokenExpiryDays as resolveDefaultTokenExpiryDays2 } from "@getstrata/core/security/tokenExpiry";
|
|
382
|
-
|
|
383
|
-
// ../../src/modules/user/provider.ts
|
|
384
|
-
var tokenServiceToken = CORE_TOKEN_SERVICE_TOKEN;
|
|
385
|
-
|
|
386
49
|
// ../../src/core/auth/sessionCookie.ts
|
|
387
50
|
import { createHmac, timingSafeEqual } from "crypto";
|
|
388
51
|
var SESSION_COOKIE = "workhub_session";
|
|
@@ -462,10 +125,10 @@ class SessionGuard {
|
|
|
462
125
|
if (!userId) {
|
|
463
126
|
return null;
|
|
464
127
|
}
|
|
465
|
-
if (!this.container.has(
|
|
128
|
+
if (!this.container.has(CORE_TOKEN_SERVICE_TOKEN)) {
|
|
466
129
|
return null;
|
|
467
130
|
}
|
|
468
|
-
const tokenService = this.container.resolve(
|
|
131
|
+
const tokenService = this.container.resolve(CORE_TOKEN_SERVICE_TOKEN);
|
|
469
132
|
try {
|
|
470
133
|
const user = await tokenService.findByIdOrThrow(userId);
|
|
471
134
|
return {
|