@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
|
@@ -66,7 +66,7 @@ async function withDatabaseErrorHandling(operation) {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// ../../src/core/http/webErrorResponse.ts
|
|
69
|
-
import { HttpError as HttpError2, UnauthorizedError
|
|
69
|
+
import { HttpError as HttpError2, UnauthorizedError, ValidationError } from "@getstrata/core/errors/http";
|
|
70
70
|
|
|
71
71
|
// ../../src/config/frontend.ts
|
|
72
72
|
function readFrontendMode() {
|
|
@@ -313,298 +313,14 @@ function isHtmxRequest(request) {
|
|
|
313
313
|
import { currentAuthUser } from "@getstrata/core/auth/authContext";
|
|
314
314
|
import { currentRequestMeta as currentRequestMeta2 } from "@getstrata/core/http/requestMetaContext";
|
|
315
315
|
|
|
316
|
-
// ../../src/
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
CORE_TOKEN_SERVICE_TOKEN
|
|
325
|
-
} from "@getstrata/core/contracts/serviceTokens";
|
|
326
|
-
var DEFAULT_QUEUE_DRIVER = "sync";
|
|
327
|
-
|
|
328
|
-
// ../../src/modules/user/provider.ts
|
|
329
|
-
import { OidcProvider } from "@getstrata/core/auth/oauth/oidcProvider";
|
|
330
|
-
import { GitHubOAuthProvider, MockOAuthProvider } from "@getstrata/core/auth/oauth/providers";
|
|
331
|
-
import { SamlProvider } from "@getstrata/core/auth/oauth/samlProvider";
|
|
332
|
-
|
|
333
|
-
// ../../src/config/features.ts
|
|
334
|
-
function readFeatureFlags() {
|
|
335
|
-
return {
|
|
336
|
-
webhooks: (process.env.FEATURE_WEBHOOKS ?? "true") !== "false",
|
|
337
|
-
fullTextSearch: (process.env.FEATURE_SEARCH ?? "true") !== "false",
|
|
338
|
-
auditLog: (process.env.FEATURE_AUDIT_LOG ?? "true") !== "false",
|
|
339
|
-
oauthLogin: (process.env.FEATURE_OAUTH ?? "true") !== "false",
|
|
340
|
-
samlLogin: (process.env.FEATURE_SAML ?? "false") === "true",
|
|
341
|
-
scim: (process.env.FEATURE_SCIM ?? "true") !== "false",
|
|
342
|
-
billing: (process.env.FEATURE_BILLING ?? "true") !== "false",
|
|
343
|
-
siemExport: (process.env.FEATURE_SIEM_EXPORT ?? "true") !== "false",
|
|
344
|
-
publicReads: (process.env.FEATURE_PUBLIC_READS ?? "true") !== "false",
|
|
345
|
-
emailVerification: (process.env.FEATURE_EMAIL_VERIFICATION ?? "false") === "true",
|
|
346
|
-
mfa: (process.env.FEATURE_MFA ?? "false") === "true"
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
var featureFlags = readFeatureFlags();
|
|
350
|
-
function isFeatureEnabled(feature) {
|
|
351
|
-
return readFeatureFlags()[feature];
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// ../../src/modules/user/apiTokenRepository.ts
|
|
355
|
-
import { BaseRepository } from "@getstrata/core/database/baseRepository";
|
|
356
|
-
|
|
357
|
-
// ../../src/config/database.ts
|
|
358
|
-
function readInteger(name, fallback) {
|
|
359
|
-
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
360
|
-
return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
|
|
361
|
-
}
|
|
362
|
-
var databaseConfig = {
|
|
363
|
-
url: process.env.DATABASE_URL ?? "",
|
|
364
|
-
poolMax: readInteger("DB_POOL_MAX", 10),
|
|
365
|
-
idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
|
|
366
|
-
maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
|
|
367
|
-
connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
|
|
368
|
-
};
|
|
369
|
-
|
|
370
|
-
// ../../src/core/runtime/asyncContextStore.ts
|
|
371
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
372
|
-
function createAsyncContextStore(key) {
|
|
373
|
-
const symbol = Symbol.for(key);
|
|
374
|
-
const globalRecord = globalThis;
|
|
375
|
-
const existing = globalRecord[symbol];
|
|
376
|
-
if (existing) {
|
|
377
|
-
return existing;
|
|
378
|
-
}
|
|
379
|
-
const store = new AsyncLocalStorage;
|
|
380
|
-
globalRecord[symbol] = store;
|
|
381
|
-
return store;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// ../../src/core/database/connectionContext.ts
|
|
385
|
-
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
386
|
-
function runWithDatabaseConnection(connection, callback) {
|
|
387
|
-
return activeConnection.run(connection, callback);
|
|
388
|
-
}
|
|
389
|
-
function getActiveDatabaseConnection(fallback) {
|
|
390
|
-
return activeConnection.getStore() ?? fallback;
|
|
391
|
-
}
|
|
392
|
-
function hasActiveDatabaseConnection() {
|
|
393
|
-
return activeConnection.getStore() !== undefined;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
// ../../src/core/database/queryProxy.ts
|
|
397
|
-
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
398
|
-
function createDatabaseQueryProxy(pool) {
|
|
399
|
-
function resolveDatabase() {
|
|
400
|
-
return getActiveDatabaseConnection(pool);
|
|
401
|
-
}
|
|
402
|
-
function resolveDatabaseForProperty(property) {
|
|
403
|
-
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
404
|
-
return pool;
|
|
405
|
-
}
|
|
406
|
-
return resolveDatabase();
|
|
407
|
-
}
|
|
408
|
-
return new Proxy(function database() {}, {
|
|
409
|
-
apply(_target, _thisArg, args) {
|
|
410
|
-
return resolveDatabase()(...args);
|
|
411
|
-
},
|
|
412
|
-
get(_target, property) {
|
|
413
|
-
const connection = resolveDatabaseForProperty(property);
|
|
414
|
-
const value = connection[property];
|
|
415
|
-
return typeof value === "function" ? value.bind(connection) : value;
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// ../../src/core/database/defaultConnection.ts
|
|
421
|
-
var defaultPool = {
|
|
422
|
-
connection: null
|
|
423
|
-
};
|
|
424
|
-
var defaultQuery = {
|
|
425
|
-
connection: null
|
|
426
|
-
};
|
|
427
|
-
function registerDefaultDatabasePool(connection) {
|
|
428
|
-
defaultPool.connection = connection;
|
|
429
|
-
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
430
|
-
}
|
|
431
|
-
function getDefaultDatabaseQuery() {
|
|
432
|
-
if (!defaultQuery.connection) {
|
|
433
|
-
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
434
|
-
}
|
|
435
|
-
return defaultQuery.connection;
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// ../../src/db/connection/createConnection.ts
|
|
439
|
-
var {SQL } = globalThis.Bun;
|
|
440
|
-
function createDatabaseConnection(config) {
|
|
441
|
-
if (!config.url) {
|
|
442
|
-
throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
|
|
443
|
-
}
|
|
444
|
-
return new SQL({
|
|
445
|
-
url: config.url,
|
|
446
|
-
max: config.poolMax,
|
|
447
|
-
idleTimeout: config.idleTimeoutSeconds,
|
|
448
|
-
maxLifetime: config.maxLifetimeSeconds,
|
|
449
|
-
connectionTimeout: config.connectionTimeoutSeconds
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
// ../../src/db/connection/index.ts
|
|
454
|
-
var connectionHolder = {
|
|
455
|
-
connection: null
|
|
456
|
-
};
|
|
457
|
-
function getDatabase() {
|
|
458
|
-
if (!connectionHolder.connection) {
|
|
459
|
-
connectionHolder.connection = createDatabaseConnection(databaseConfig);
|
|
460
|
-
registerDefaultDatabasePool(connectionHolder.connection);
|
|
461
|
-
}
|
|
462
|
-
return connectionHolder.connection;
|
|
463
|
-
}
|
|
464
|
-
function getDb() {
|
|
465
|
-
getDatabase();
|
|
466
|
-
return getDefaultDatabaseQuery();
|
|
467
|
-
}
|
|
468
|
-
var db = new Proxy(function database() {}, {
|
|
469
|
-
apply(_target, _thisArg, args) {
|
|
470
|
-
return getDb()(...args);
|
|
471
|
-
},
|
|
472
|
-
get(_target, property) {
|
|
473
|
-
const connection = getDb();
|
|
474
|
-
const value = connection[property];
|
|
475
|
-
return typeof value === "function" ? value.bind(connection) : value;
|
|
476
|
-
}
|
|
477
|
-
});
|
|
478
|
-
var connection_default = db;
|
|
479
|
-
|
|
480
|
-
// ../../src/modules/user/apiTokenTable.ts
|
|
481
|
-
import { defineTable } from "@getstrata/core/database/table";
|
|
482
|
-
var apiTokenTable = defineTable({
|
|
483
|
-
name: "api_token",
|
|
484
|
-
primaryKey: "id",
|
|
485
|
-
columns: [
|
|
486
|
-
"id",
|
|
487
|
-
"user_id",
|
|
488
|
-
"name",
|
|
489
|
-
"token_hash",
|
|
490
|
-
"abilities",
|
|
491
|
-
"last_used_at",
|
|
492
|
-
"expires_at",
|
|
493
|
-
"created_at"
|
|
494
|
-
],
|
|
495
|
-
defaultOrderBy: { column: "id", direction: "ASC" }
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
// ../../src/modules/user/authService.ts
|
|
499
|
-
import { verifyPassword } from "@getstrata/core/auth/password";
|
|
500
|
-
import { revealMfaSecret } from "@getstrata/core/crypto/mfaSecret";
|
|
501
|
-
import { UnauthorizedError } from "@getstrata/core/errors/http";
|
|
502
|
-
import { logSecurityEvent } from "@getstrata/core/security/securityEvents";
|
|
503
|
-
import { resolveDefaultTokenExpiryDays } from "@getstrata/core/security/tokenExpiry";
|
|
504
|
-
import { verifyTotp } from "@getstrata/core/security/totp";
|
|
505
|
-
import { currentTenantId } from "@getstrata/core/tenant/tenantContext";
|
|
506
|
-
|
|
507
|
-
// ../../src/domain/abilities.ts
|
|
508
|
-
var MEMBER_ABILITIES = [
|
|
509
|
-
"organizations:read",
|
|
510
|
-
"projects:read",
|
|
511
|
-
"projects:create",
|
|
512
|
-
"tasks:read",
|
|
513
|
-
"tasks:create",
|
|
514
|
-
"comments:read",
|
|
515
|
-
"comments:create",
|
|
516
|
-
"attachments:read",
|
|
517
|
-
"attachments:create",
|
|
518
|
-
"auth:tokens:read",
|
|
519
|
-
"auth:tokens:write"
|
|
520
|
-
];
|
|
521
|
-
var ADMIN_ABILITIES = [
|
|
522
|
-
...MEMBER_ABILITIES,
|
|
523
|
-
"organizations:create",
|
|
524
|
-
"organizations:update",
|
|
525
|
-
"organizations:delete",
|
|
526
|
-
"projects:update",
|
|
527
|
-
"projects:delete",
|
|
528
|
-
"tasks:update",
|
|
529
|
-
"tasks:delete",
|
|
530
|
-
"comments:update",
|
|
531
|
-
"comments:delete",
|
|
532
|
-
"attachments:delete",
|
|
533
|
-
"webhooks:read",
|
|
534
|
-
"webhooks:write",
|
|
535
|
-
"audit:read"
|
|
536
|
-
];
|
|
537
|
-
var PLATFORM_ADMIN_ABILITIES = ["*"];
|
|
538
|
-
function resolveAbilitiesForRole(role) {
|
|
539
|
-
if (role === "admin") {
|
|
540
|
-
return [...PLATFORM_ADMIN_ABILITIES];
|
|
541
|
-
}
|
|
542
|
-
return [...MEMBER_ABILITIES];
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
// ../../src/modules/user/notificationRepository.ts
|
|
546
|
-
import { BaseRepository as BaseRepository2 } from "@getstrata/core/database/baseRepository";
|
|
547
|
-
|
|
548
|
-
// ../../src/modules/user/notificationTable.ts
|
|
549
|
-
import { defineTable as defineTable2 } from "@getstrata/core/database/table";
|
|
550
|
-
var notificationTable = defineTable2({
|
|
551
|
-
name: "notification",
|
|
552
|
-
primaryKey: "id",
|
|
553
|
-
columns: ["id", "user_id", "tenant_id", "type", "title", "body", "data", "read_at", "created_at"],
|
|
554
|
-
defaultOrderBy: { column: "created_at", direction: "DESC" }
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
// ../../src/modules/user/notificationService.ts
|
|
558
|
-
import { NotFoundError } from "@getstrata/core/errors/http";
|
|
559
|
-
|
|
560
|
-
// ../../src/modules/user/oauthIdentityRepository.ts
|
|
561
|
-
import { BaseRepository as BaseRepository3 } from "@getstrata/core/database/baseRepository";
|
|
562
|
-
import { defineTable as defineTable3 } from "@getstrata/core/database/table";
|
|
563
|
-
var oauthIdentityTable = defineTable3({
|
|
564
|
-
name: "oauth_identity",
|
|
565
|
-
primaryKey: "id",
|
|
566
|
-
columns: ["id", "user_id", "provider", "provider_user_id", "email", "created_at"]
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
// ../../src/modules/user/repository.ts
|
|
570
|
-
import {
|
|
571
|
-
emailLookupForQuery,
|
|
572
|
-
protectEmail,
|
|
573
|
-
revealEmail
|
|
574
|
-
} from "@getstrata/core/crypto/fieldEncryption";
|
|
575
|
-
import { revealMfaSecret as revealMfaSecret2 } from "@getstrata/core/crypto/mfaSecret";
|
|
576
|
-
import { BaseRepository as BaseRepository4 } from "@getstrata/core/database/baseRepository";
|
|
577
|
-
import { currentTenantId as currentTenantId2 } from "@getstrata/core/tenant/tenantContext";
|
|
578
|
-
|
|
579
|
-
// ../../src/modules/user/table.ts
|
|
580
|
-
import { defineTable as defineTable4 } from "@getstrata/core/database/table";
|
|
581
|
-
var userTable = defineTable4({
|
|
582
|
-
name: "users",
|
|
583
|
-
primaryKey: "id",
|
|
584
|
-
columns: [
|
|
585
|
-
"id",
|
|
586
|
-
"name",
|
|
587
|
-
"email",
|
|
588
|
-
"email_lookup",
|
|
589
|
-
"role",
|
|
590
|
-
"tenant_id",
|
|
591
|
-
"password_hash",
|
|
592
|
-
"email_verified_at",
|
|
593
|
-
"mfa_secret",
|
|
594
|
-
"mfa_enabled",
|
|
595
|
-
"created_at",
|
|
596
|
-
"updated_at"
|
|
597
|
-
],
|
|
598
|
-
defaultOrderBy: { column: "id", direction: "ASC" }
|
|
599
|
-
});
|
|
600
|
-
|
|
601
|
-
// ../../src/modules/user/tokenService.ts
|
|
602
|
-
import { hashApiToken } from "@getstrata/core/auth/tokenHash";
|
|
603
|
-
import { ForbiddenError, NotFoundError as NotFoundError2 } from "@getstrata/core/errors/http";
|
|
604
|
-
import { resolveDefaultTokenExpiryDays as resolveDefaultTokenExpiryDays2 } from "@getstrata/core/security/tokenExpiry";
|
|
605
|
-
|
|
606
|
-
// ../../src/modules/user/provider.ts
|
|
607
|
-
var tokenServiceToken = CORE_TOKEN_SERVICE_TOKEN;
|
|
316
|
+
// ../../src/core/contracts/serviceTokens.ts
|
|
317
|
+
var CORE_CONFIG_TOKEN = "core.config";
|
|
318
|
+
var CORE_CACHE_TOKEN = "core.cache";
|
|
319
|
+
var CORE_QUEUE_TOKEN = "core.queue";
|
|
320
|
+
var CORE_EVENT_BUS_TOKEN = "core.eventBus";
|
|
321
|
+
var CORE_POLICY_GATE_TOKEN = "core.policyGate";
|
|
322
|
+
var CORE_AUTH_TOKEN = "core.auth";
|
|
323
|
+
var CORE_TOKEN_SERVICE_TOKEN = "core.tokenService";
|
|
608
324
|
|
|
609
325
|
// ../../src/core/http/csrfToken.ts
|
|
610
326
|
import { timingSafeEqual } from "crypto";
|
|
@@ -637,6 +353,20 @@ function readBunRequestCookie(request, name) {
|
|
|
637
353
|
return request.cookies.get(name) ?? readRequestCookie(request, name);
|
|
638
354
|
}
|
|
639
355
|
|
|
356
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
357
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
358
|
+
function createAsyncContextStore(key) {
|
|
359
|
+
const symbol = Symbol.for(key);
|
|
360
|
+
const globalRecord = globalThis;
|
|
361
|
+
const existing = globalRecord[symbol];
|
|
362
|
+
if (existing) {
|
|
363
|
+
return existing;
|
|
364
|
+
}
|
|
365
|
+
const store = new AsyncLocalStorage;
|
|
366
|
+
globalRecord[symbol] = store;
|
|
367
|
+
return store;
|
|
368
|
+
}
|
|
369
|
+
|
|
640
370
|
// ../../src/core/http/requestMetaContext.ts
|
|
641
371
|
var requestMetaContext = createAsyncContextStore("@getstrata/requestMetaContext");
|
|
642
372
|
function runWithRequestMeta(meta, callback) {
|
|
@@ -668,9 +398,10 @@ function tokensMatch(left, right) {
|
|
|
668
398
|
}
|
|
669
399
|
function createCsrfTokenCookie() {
|
|
670
400
|
const token = Bun.CSRF.generate(resolveCsrfSecret(), { expiresIn: CSRF_TTL_MS });
|
|
401
|
+
const secure = process.env.APP_ENV === "production" ? "; Secure" : "";
|
|
671
402
|
return {
|
|
672
403
|
token,
|
|
673
|
-
cookie: `${CSRF_COOKIE}=${encodeURIComponent(token)}; Path=/; SameSite=Lax; Max-Age=${Math.floor(CSRF_TTL_MS / 1000)}`
|
|
404
|
+
cookie: `${CSRF_COOKIE}=${encodeURIComponent(token)}; Path=/; SameSite=Lax; Max-Age=${Math.floor(CSRF_TTL_MS / 1000)}${secure}`
|
|
674
405
|
};
|
|
675
406
|
}
|
|
676
407
|
function resolveCsrfToken(request) {
|
|
@@ -838,7 +569,7 @@ async function resolveWebLayoutData(container, request) {
|
|
|
838
569
|
if (!Number.isInteger(userId) || userId <= 0) {
|
|
839
570
|
return { authUser: null, csrfToken, flash };
|
|
840
571
|
}
|
|
841
|
-
if (!container.has(
|
|
572
|
+
if (!container.has(CORE_TOKEN_SERVICE_TOKEN)) {
|
|
842
573
|
return {
|
|
843
574
|
authUser: {
|
|
844
575
|
id: userId,
|
|
@@ -849,7 +580,7 @@ async function resolveWebLayoutData(container, request) {
|
|
|
849
580
|
flash
|
|
850
581
|
};
|
|
851
582
|
}
|
|
852
|
-
const tokenService = container.resolve(
|
|
583
|
+
const tokenService = container.resolve(CORE_TOKEN_SERVICE_TOKEN);
|
|
853
584
|
try {
|
|
854
585
|
const user = await tokenService.findByIdOrThrow(userId);
|
|
855
586
|
return {
|
|
@@ -910,7 +641,7 @@ function webErrorResponse(error, request) {
|
|
|
910
641
|
return null;
|
|
911
642
|
}
|
|
912
643
|
const mappedError = error instanceof HttpError2 ? error : mapDatabaseError(error);
|
|
913
|
-
if (mappedError instanceof
|
|
644
|
+
if (mappedError instanceof UnauthorizedError) {
|
|
914
645
|
const redirectTarget = encodeURIComponent(new URL(request.url).pathname);
|
|
915
646
|
return Response.redirect(`/login?redirect=${redirectTarget}`, 302);
|
|
916
647
|
}
|
|
@@ -1,10 +1,44 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// ../../src/core/http/scimThrottleMiddleware.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/scimThrottleMiddleware.ts
|
|
21
|
+
var memoryBuckets = new Map;
|
|
22
|
+
function resolveScimIdentity(request) {
|
|
23
|
+
return request.headers.get("authorization")?.slice("Bearer ".length, "Bearer ".length + 16) ?? readClientIp(request) ?? "unknown";
|
|
24
|
+
}
|
|
25
|
+
function throttleFromMemory(key, maxAttempts, decaySeconds) {
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const existing = memoryBuckets.get(key);
|
|
28
|
+
if (!existing || existing.resetAt <= now) {
|
|
29
|
+
memoryBuckets.set(key, { count: 1, resetAt: now + decaySeconds * 1000 });
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
existing.count += 1;
|
|
33
|
+
if (existing.count > maxAttempts) {
|
|
34
|
+
return Response.json({ error: "Too many SCIM requests." }, { status: 429, headers: { "retry-after": String(decaySeconds) } });
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
4
38
|
function createScimThrottleMiddleware(options) {
|
|
5
39
|
const client = options.redisUrl ? new RedisClient(options.redisUrl) : null;
|
|
6
40
|
return async (request, next) => {
|
|
7
|
-
const identity =
|
|
41
|
+
const identity = resolveScimIdentity(request);
|
|
8
42
|
const key = `workhub:scim-throttle:${identity}`;
|
|
9
43
|
if (client) {
|
|
10
44
|
const attempts = Number(await client.incr(key));
|
|
@@ -14,6 +48,11 @@ function createScimThrottleMiddleware(options) {
|
|
|
14
48
|
if (attempts > options.maxAttempts) {
|
|
15
49
|
return Response.json({ error: "Too many SCIM requests." }, { status: 429, headers: { "retry-after": String(options.decaySeconds) } });
|
|
16
50
|
}
|
|
51
|
+
return await next();
|
|
52
|
+
}
|
|
53
|
+
const limited = throttleFromMemory(key, options.maxAttempts, options.decaySeconds);
|
|
54
|
+
if (limited) {
|
|
55
|
+
return limited;
|
|
17
56
|
}
|
|
18
57
|
return await next();
|
|
19
58
|
};
|
|
@@ -3,6 +3,23 @@
|
|
|
3
3
|
import { currentAuthUser } from "@getstrata/core/auth/authContext";
|
|
4
4
|
import { currentTenant, rateLimitMultiplierForPlan } from "@getstrata/core/tenant/tenantContext";
|
|
5
5
|
var {RedisClient } = globalThis.Bun;
|
|
6
|
+
|
|
7
|
+
// ../../src/core/http/clientIp.ts
|
|
8
|
+
function trustForwardedFor(env = process.env) {
|
|
9
|
+
return (env.TRUST_FORWARDED_FOR ?? "false") === "true";
|
|
10
|
+
}
|
|
11
|
+
function readClientIp(request, env = process.env) {
|
|
12
|
+
if (!trustForwardedFor(env)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const forwarded = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim();
|
|
16
|
+
if (forwarded) {
|
|
17
|
+
return forwarded;
|
|
18
|
+
}
|
|
19
|
+
return request.headers.get("x-real-ip")?.trim() || undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ../../src/core/http/throttleMiddleware.ts
|
|
6
23
|
function resolveThrottleIdentity(request) {
|
|
7
24
|
const user = currentAuthUser();
|
|
8
25
|
if (user?.tokenId !== undefined) {
|
|
@@ -11,7 +28,7 @@ function resolveThrottleIdentity(request) {
|
|
|
11
28
|
if (user) {
|
|
12
29
|
return `user:${user.id}`;
|
|
13
30
|
}
|
|
14
|
-
return request
|
|
31
|
+
return readClientIp(request) ?? "unknown";
|
|
15
32
|
}
|
|
16
33
|
function createThrottleMiddleware(options) {
|
|
17
34
|
const client = new RedisClient(options.redisUrl);
|