@getstrata/bootstrap 2.2.60 → 3.0.4
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/CHANGELOG.md +24 -0
- package/dist/bootstrap/app.d.ts +18 -7
- package/dist/bootstrap/buildModuleRoutes.d.ts +3 -5
- package/dist/bootstrap/buildWebModuleRoutes.d.ts +2 -3
- package/dist/bootstrap/cache/modelCacheTags.d.ts +3 -2
- package/dist/bootstrap/cachedJson.d.ts +7 -2
- package/dist/bootstrap/config.d.ts +1 -1
- package/dist/bootstrap/context.d.ts +25 -7
- package/dist/bootstrap/contracts.d.ts +1 -0
- package/dist/bootstrap/createWebRoutes.d.ts +2 -1
- package/dist/bootstrap/databaseReadiness.d.ts +3 -0
- package/dist/bootstrap/dependencies.d.ts +3 -2
- package/dist/bootstrap/discoverJobs.d.ts +3 -2
- package/dist/bootstrap/discoverListeners.d.ts +10 -7
- package/dist/bootstrap/discoverModels.d.ts +3 -2
- package/dist/bootstrap/discoverModules.d.ts +7 -6
- package/dist/bootstrap/health.d.ts +3 -2
- package/dist/bootstrap/http/securedRouteModelBinding.d.ts +1 -1
- package/dist/bootstrap/httpKernel.d.ts +43 -22
- package/dist/bootstrap/httpPersistence.d.ts +4 -0
- package/dist/bootstrap/inProcessCron.d.ts +5 -5
- package/dist/bootstrap/listeners/invalidateCacheOnModelWrite.d.ts +13 -12
- package/dist/bootstrap/metricsRoutes.d.ts +6 -1
- package/dist/bootstrap/moduleRouteMiddleware.d.ts +4 -4
- package/dist/bootstrap/modules.d.ts +1 -1
- package/dist/bootstrap/ownedHttpRoute.d.ts +23 -0
- package/dist/bootstrap/persistenceContracts.d.ts +70 -0
- package/dist/bootstrap/persistenceInvocation.d.ts +4 -0
- package/dist/bootstrap/providers/auth.d.ts +21 -0
- package/dist/bootstrap/providers/cache.d.ts +9 -3
- package/dist/bootstrap/providers/cacheProvider.d.ts +3 -0
- package/dist/bootstrap/providers/config.d.ts +6 -3
- package/dist/bootstrap/providers/events.d.ts +5 -2
- package/dist/bootstrap/providers/index.d.ts +3 -2
- package/dist/bootstrap/providers/listeners.d.ts +3 -3
- package/dist/bootstrap/providers/mail.d.ts +15 -0
- package/dist/bootstrap/providers/queue.d.ts +3 -2
- package/dist/bootstrap/providers/storage.d.ts +11 -2
- package/dist/bootstrap/providers/tracing.d.ts +4 -2
- package/dist/bootstrap/providers/view.d.ts +8 -5
- package/dist/bootstrap/public-api.d.ts +10 -7
- package/dist/bootstrap/queue/defaultJobs.d.ts +4 -2
- package/dist/bootstrap/rateLimit.d.ts +13 -3
- package/dist/bootstrap/routeCompiler.d.ts +20 -0
- package/dist/bootstrap/routeMetadata.d.ts +3 -3
- package/dist/bootstrap/routePlan.d.ts +11 -0
- package/dist/bootstrap/schedule.d.ts +11 -2
- package/dist/bootstrap/secretsGuard.d.ts +2 -3
- package/dist/bootstrap/web/routing.d.ts +6 -3
- package/dist/bootstrap/web/server.d.ts +14 -4
- package/dist/bootstrap/web/session.d.ts +23 -16
- package/dist/entries/buildModuleRoutes.js +602 -384
- package/dist/entries/buildWebModuleRoutes.js +596 -347
- package/dist/entries/cache/modelCacheTags.js +8 -186
- package/dist/entries/cachedJson.js +22 -0
- package/dist/entries/config.js +1 -3
- package/dist/entries/context.js +665 -700
- package/dist/entries/createSpaRoutes.js +194 -5
- package/dist/entries/createWebRoutes.js +618 -367
- package/dist/entries/databaseReadiness.js +267 -0
- package/dist/entries/dependencies.js +666 -701
- package/dist/entries/discoverJobs.js +18 -16
- package/dist/entries/discoverListeners.js +27 -38
- package/dist/entries/discoverModels.js +4 -3
- package/dist/entries/discoverModules.js +29 -67
- package/dist/entries/health.js +14 -34
- package/dist/entries/httpKernel.js +405 -89
- package/dist/entries/listeners/invalidateCacheOnModelWrite.js +43 -289
- package/dist/entries/metricsRoutes.js +9 -5
- package/dist/entries/persistenceContracts.js +1 -0
- package/dist/entries/persistenceInvocation.js +17 -0
- package/dist/entries/providers/auth.js +114 -0
- package/dist/entries/providers/cache.js +35 -0
- package/dist/entries/providers/events.js +55 -0
- package/dist/entries/providers/view.js +34 -44
- package/dist/entries/providers.js +392 -561
- package/dist/entries/queue/defaultJobs.js +9 -6
- package/dist/entries/routePlan.js +200 -0
- package/dist/entries/schedule.js +37 -37
- package/dist/entries/secretsGuard.js +10 -13
- package/dist/entries/web/routing.js +412 -96
- package/dist/entries/web/server.js +461 -29
- package/dist/entries/web/session.js +61 -73
- package/dist/index.js +1520 -1085
- package/dist/internal/ownedHttpRoute.js +66 -0
- package/package.json +52 -15
- package/dist/bootstrap/applicationRegistry.d.ts +0 -1
- package/dist/bootstrap/routeRegistry.d.ts +0 -10
- package/dist/entries/applicationRegistry.js +0 -24
- package/dist/entries/routeRegistry.js +0 -29
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// ../../src/bootstrap/web/routing.ts
|
|
3
3
|
import { requestPrefersJson } from "@getstrata/core/http/contentNegotiation";
|
|
4
|
-
import {
|
|
4
|
+
import { identityModes as identityModes2, scimMiddleware as scimMiddleware2 } from "#strata-http-route";
|
|
5
5
|
|
|
6
6
|
// ../../src/bootstrap/http/securedRouteModelBinding.ts
|
|
7
7
|
import {
|
|
@@ -10,16 +10,15 @@ import {
|
|
|
10
10
|
} from "@getstrata/core/http/securedRouteModelBinding";
|
|
11
11
|
|
|
12
12
|
// ../../src/bootstrap/httpKernel.ts
|
|
13
|
+
import { establishedAuthReader as establishedAuthReader2 } from "@getstrata/core/auth/authReader";
|
|
14
|
+
import { hasVerifiedEmail } from "@getstrata/core/auth/emailVerification";
|
|
13
15
|
import {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from "@getstrata/core/
|
|
17
|
-
import { createMembershipMiddleware } from "@getstrata/core/auth/membershipMiddleware";
|
|
18
|
-
import { resolveAbilityChecker } from "@getstrata/core/contracts/serviceTokens";
|
|
19
|
-
import { createAuthMiddleware } from "@getstrata/core/http/authMiddleware";
|
|
16
|
+
CORE_TRACING_RUNTIME_TOKEN,
|
|
17
|
+
resolveAbilityChecker
|
|
18
|
+
} from "@getstrata/core/contracts/serviceTokens";
|
|
20
19
|
import { createAuthorizeMiddleware } from "@getstrata/core/http/authorizeMiddleware";
|
|
21
20
|
import { createBodySizeLimitMiddleware } from "@getstrata/core/http/bodySizeLimitMiddleware";
|
|
22
|
-
import { createCorsMiddleware } from "@getstrata/core/http/corsMiddleware";
|
|
21
|
+
import { createCorsMiddleware, resolveCorsConfig } from "@getstrata/core/http/corsMiddleware";
|
|
23
22
|
import { createCsrfMiddleware } from "@getstrata/core/http/csrfMiddleware";
|
|
24
23
|
import { createFlashMiddleware } from "@getstrata/core/http/flashMiddleware";
|
|
25
24
|
import { createLoginThrottleMiddleware } from "@getstrata/core/http/loginThrottleMiddleware";
|
|
@@ -32,21 +31,21 @@ import { createRequireGlobalAdminMiddleware } from "@getstrata/core/http/require
|
|
|
32
31
|
import { createRequirePasswordConfirmMiddleware } from "@getstrata/core/http/requirePasswordConfirmMiddleware";
|
|
33
32
|
import { createRequireVerifiedMiddleware } from "@getstrata/core/http/requireVerifiedMiddleware";
|
|
34
33
|
import { createRequireWebAuthMiddleware } from "@getstrata/core/http/requireWebAuthMiddleware";
|
|
35
|
-
import {
|
|
36
|
-
createJsonErrorMiddleware,
|
|
37
|
-
createWebErrorMiddleware,
|
|
38
|
-
withErrorHandling,
|
|
39
|
-
withJsonErrorHandling
|
|
40
|
-
} from "@getstrata/core/http/response";
|
|
34
|
+
import { createJsonErrorMiddleware, createWebErrorMiddleware } from "@getstrata/core/http/response";
|
|
41
35
|
import { withMiddleware } from "@getstrata/core/http/routeMiddleware";
|
|
42
36
|
import { createSecurityHeadersMiddleware } from "@getstrata/core/http/securityHeadersMiddleware";
|
|
43
37
|
import { createValidateSignatureMiddleware } from "@getstrata/core/http/signedUrl";
|
|
44
38
|
import { createThrottleMiddleware } from "@getstrata/core/http/throttleMiddleware";
|
|
45
39
|
import { createRequestLoggingMiddleware } from "@getstrata/core/logging/requestLoggingMiddleware";
|
|
46
|
-
import { isViewsEnabled } from "@getstrata/core/runtime/frontendMode";
|
|
47
|
-
import { isPublicReadsEnabled } from "@getstrata/core/security/publicReads";
|
|
48
|
-
import { createTenantMiddleware } from "@getstrata/core/tenant/tenantMiddleware";
|
|
49
40
|
import { createTracingMiddleware } from "@getstrata/core/tracing/tracingMiddleware";
|
|
41
|
+
import { CORE_WEB_ERROR_VIEW_TOKEN } from "@getstrata/core/view/tokens";
|
|
42
|
+
import {
|
|
43
|
+
assertHttpHandler,
|
|
44
|
+
identityModes,
|
|
45
|
+
ownedHttpRoute,
|
|
46
|
+
ownedPublicHttpRoute,
|
|
47
|
+
scimMiddleware
|
|
48
|
+
} from "#strata-http-route";
|
|
50
49
|
|
|
51
50
|
// ../../src/bootstrap/config.ts
|
|
52
51
|
import {
|
|
@@ -61,8 +60,7 @@ import {
|
|
|
61
60
|
CORE_PUBLIC_TENANCY_TOKEN,
|
|
62
61
|
CORE_QUEUE_TOKEN,
|
|
63
62
|
CORE_TENANT_RESOLVER_TOKEN,
|
|
64
|
-
CORE_THROTTLE_QUOTA_POLICY_TOKEN
|
|
65
|
-
CORE_TOKEN_SERVICE_TOKEN
|
|
63
|
+
CORE_THROTTLE_QUOTA_POLICY_TOKEN
|
|
66
64
|
} from "@getstrata/core/contracts/serviceTokens";
|
|
67
65
|
var APP_PORT_CONFIG_KEY = "app.port";
|
|
68
66
|
var CACHE_TTL_MS_CONFIG_KEY = "cache.ttlMs";
|
|
@@ -79,6 +77,189 @@ var DEFAULT_CACHE_DRIVER = "array";
|
|
|
79
77
|
var DEFAULT_API_TOKEN = "";
|
|
80
78
|
var DEFAULT_QUEUE_DRIVER = "sync";
|
|
81
79
|
|
|
80
|
+
// ../../src/bootstrap/httpPersistence.ts
|
|
81
|
+
import { establishedAuthReader, runWithEstablishedAuth } from "@getstrata/core/auth/authReader";
|
|
82
|
+
import { validatedAuthOutcome } from "@getstrata/core/auth/guard";
|
|
83
|
+
import { runWithMembershipSnapshot } from "@getstrata/core/auth/membershipContext";
|
|
84
|
+
import { InternalServerError, InvalidCredentialsError } from "@getstrata/core/errors/http";
|
|
85
|
+
import { currentRequestMeta } from "@getstrata/core/http/requestMetaContext";
|
|
86
|
+
import { errorResponse } from "@getstrata/core/http/response";
|
|
87
|
+
import { webErrorResponse } from "@getstrata/core/http/webErrorResponse";
|
|
88
|
+
import { createPublicTenantSelector } from "@getstrata/core/tenant/publicTenantHost";
|
|
89
|
+
import { runWithTenant } from "@getstrata/core/tenant/tenantContext";
|
|
90
|
+
import { DEFAULT_TENANT, resolveTenantForRequest } from "@getstrata/core/tenant/tenantMiddleware";
|
|
91
|
+
import { businessPorts } from "#strata-http-route";
|
|
92
|
+
import { HTTP_PERSISTENCE_OPTIONS } from "#strata-http-route";
|
|
93
|
+
function snapshotAuth(input) {
|
|
94
|
+
const auth = validatedAuthOutcome(input);
|
|
95
|
+
if (auth.status === "error")
|
|
96
|
+
throw Object.assign(new InternalServerError, { cause: auth.error });
|
|
97
|
+
if (auth.status === "invalid")
|
|
98
|
+
throw new InvalidCredentialsError;
|
|
99
|
+
const user = auth.user;
|
|
100
|
+
const name = user?.name;
|
|
101
|
+
const email = user?.email;
|
|
102
|
+
return Object.freeze({
|
|
103
|
+
credentialSource: auth.credentialSource,
|
|
104
|
+
user: user ? Object.freeze({
|
|
105
|
+
id: user.id,
|
|
106
|
+
...typeof name === "string" ? { name } : {},
|
|
107
|
+
...typeof email === "string" ? { email } : {},
|
|
108
|
+
...user.role !== undefined ? { role: user.role } : {},
|
|
109
|
+
...user.isActive !== undefined ? { isActive: user.isActive } : {},
|
|
110
|
+
...user.authVersion !== undefined ? { authVersion: user.authVersion } : {},
|
|
111
|
+
...user.tokenId !== undefined ? { tokenId: user.tokenId } : {},
|
|
112
|
+
...user.abilities ? { abilities: Object.freeze([...user.abilities]) } : {},
|
|
113
|
+
...user.emailVerifiedAt !== undefined ? {
|
|
114
|
+
emailVerifiedAt: user.emailVerifiedAt instanceof Date ? user.emailVerifiedAt.toISOString() : user.emailVerifiedAt
|
|
115
|
+
} : {}
|
|
116
|
+
}) : null
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function snapshotTenant(tenant) {
|
|
120
|
+
const snapshot = structuredClone(tenant);
|
|
121
|
+
if (!snapshot || typeof snapshot !== "object" || Array.isArray(snapshot) || !Number.isSafeInteger(snapshot.id) || snapshot.id < 1 || typeof snapshot.slug !== "string")
|
|
122
|
+
throw new TypeError("HTTP metadata must establish a valid tenant identity.");
|
|
123
|
+
return Object.freeze(snapshot);
|
|
124
|
+
}
|
|
125
|
+
function createHttpPersistence(invoke, options) {
|
|
126
|
+
const invalidSessionCookie = options.invalidSessionCookie ? Object.freeze({ ...options.invalidSessionCookie }) : undefined;
|
|
127
|
+
const selectPublic = options.publicTenancy ? createPublicTenantSelector(options.publicTenancy) : undefined;
|
|
128
|
+
return async (request, handler, identity, scim = false) => {
|
|
129
|
+
const externalId = currentRequestMeta().requestId;
|
|
130
|
+
const metadata = { routeTemplate: currentRequestMeta().routeTemplate ?? "", phase: "metadata" };
|
|
131
|
+
const established = await invoke({
|
|
132
|
+
kind: "request",
|
|
133
|
+
externalId,
|
|
134
|
+
signal: request.signal,
|
|
135
|
+
metadata,
|
|
136
|
+
actor: { kind: "system", policy: "http.identity-metadata" }
|
|
137
|
+
}, async (ports) => {
|
|
138
|
+
if (scim) {
|
|
139
|
+
if (!options.scim)
|
|
140
|
+
throw new Error("Explicit SCIM metadata authentication is required.");
|
|
141
|
+
const tenant = await options.scim(request, ports.database);
|
|
142
|
+
if (tenant instanceof Response)
|
|
143
|
+
return tenant;
|
|
144
|
+
return Object.freeze({
|
|
145
|
+
auth: Object.freeze({ user: null, credentialSource: "bearer" }),
|
|
146
|
+
memberships: Object.freeze([]),
|
|
147
|
+
tenant: snapshotTenant(tenant)
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const lookup = options.metadata(ports);
|
|
151
|
+
const outcome = validatedAuthOutcome(await lookup.authenticate(request));
|
|
152
|
+
if (outcome.status === "invalid" && outcome.credentialSource === "session" && invalidSessionCookie && request.headers.get("cookie")?.split(";").some((part) => part.trim().startsWith(`${invalidSessionCookie.name}=`))) {
|
|
153
|
+
const error = new InvalidCredentialsError;
|
|
154
|
+
const response = await webErrorResponse(error, request) ?? errorResponse(error);
|
|
155
|
+
response.headers.append("Set-Cookie", invalidSessionCookie.expiredHeader);
|
|
156
|
+
return response;
|
|
157
|
+
}
|
|
158
|
+
const auth = snapshotAuth(outcome);
|
|
159
|
+
const memberships = auth.user ? await lookup.memberships(auth.user) : [];
|
|
160
|
+
const resolveTenant = async (id) => {
|
|
161
|
+
const result = await lookup.tenant(id);
|
|
162
|
+
if (result === null)
|
|
163
|
+
return null;
|
|
164
|
+
const snapshot = snapshotTenant(result);
|
|
165
|
+
if (snapshot.id !== id)
|
|
166
|
+
throw new Error("Tenant resolver returned a different tenant identity.");
|
|
167
|
+
return snapshot;
|
|
168
|
+
};
|
|
169
|
+
const tenant = options.tenancy === "none" ? snapshotTenant(DEFAULT_TENANT) : await resolveTenantForRequest(request, {
|
|
170
|
+
auth,
|
|
171
|
+
config: {
|
|
172
|
+
enabled: true,
|
|
173
|
+
production: options.production,
|
|
174
|
+
allowDevHeaders: options.allowTenantDevHeaders
|
|
175
|
+
},
|
|
176
|
+
resolveTenant,
|
|
177
|
+
userTenantId: lookup.userTenantId
|
|
178
|
+
}, resolveTenant, selectPublic);
|
|
179
|
+
return Object.freeze({
|
|
180
|
+
auth,
|
|
181
|
+
tenant,
|
|
182
|
+
memberships: Object.freeze(memberships.map((row) => Object.freeze({ ...row })))
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
if (established instanceof Response)
|
|
186
|
+
return established;
|
|
187
|
+
const claims = {};
|
|
188
|
+
if (established.auth.user) {
|
|
189
|
+
for (const [key, value] of Object.entries(established.auth.user))
|
|
190
|
+
if (value !== undefined) {
|
|
191
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || Array.isArray(value))
|
|
192
|
+
claims[key] = value;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return invoke({
|
|
196
|
+
kind: "request",
|
|
197
|
+
externalId,
|
|
198
|
+
signal: request.signal,
|
|
199
|
+
metadata: {
|
|
200
|
+
...metadata,
|
|
201
|
+
phase: identity ? "identity-operation" : "business",
|
|
202
|
+
credentialSource: established.auth.credentialSource
|
|
203
|
+
},
|
|
204
|
+
...scim ? { actor: { kind: "system", policy: "http.scim" } } : identity ? { actor: { kind: "system", policy: "http.identity-operation" } } : established.auth.user ? { actor: { kind: "principal", claims } } : {},
|
|
205
|
+
tenant: { id: established.tenant.id, slug: established.tenant.slug }
|
|
206
|
+
}, (ports) => {
|
|
207
|
+
const policy = Object.freeze({
|
|
208
|
+
run: (selected, mode, work) => {
|
|
209
|
+
const allowed = mode.kind === "tenant" ? identity === undefined : identity?.includes(mode.kind) === true;
|
|
210
|
+
if (!allowed)
|
|
211
|
+
throw new Error("Database policy is not allowed by this route declaration.");
|
|
212
|
+
return ports.policy.run(selected, mode, work);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
const work = (database) => runWithEstablishedAuth(request, established.auth, () => runWithMembershipSnapshot(established.memberships, () => {
|
|
216
|
+
const execute = () => handler(request, businessPorts({
|
|
217
|
+
database,
|
|
218
|
+
auth: establishedAuthReader,
|
|
219
|
+
established,
|
|
220
|
+
policy
|
|
221
|
+
}));
|
|
222
|
+
return runWithTenant(established.tenant, execute);
|
|
223
|
+
}));
|
|
224
|
+
return !identity && options.tenancy === "rls" ? ports.policy.run(ports.database, { kind: "tenant", tenantId: established.tenant.id }, work) : work(ports.database);
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ../../src/bootstrap/moduleRouteMiddleware.ts
|
|
230
|
+
function enterModuleRoutes(routes, enter) {
|
|
231
|
+
const wrapped = {};
|
|
232
|
+
for (const [path, value] of Object.entries(routes)) {
|
|
233
|
+
if (value instanceof Response) {
|
|
234
|
+
const methods = {};
|
|
235
|
+
for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
|
|
236
|
+
methods[method] = enter(() => value.clone());
|
|
237
|
+
}
|
|
238
|
+
wrapped[path] = methods;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (typeof value === "function") {
|
|
242
|
+
wrapped[path] = enter(value);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
246
|
+
const entries = Object.entries(value);
|
|
247
|
+
if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
|
|
248
|
+
wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
|
|
249
|
+
const callback = handler instanceof Response ? () => handler.clone() : handler;
|
|
250
|
+
return [
|
|
251
|
+
method,
|
|
252
|
+
typeof callback === "function" ? enter(callback) : callback
|
|
253
|
+
];
|
|
254
|
+
}));
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
wrapped[path] = value;
|
|
259
|
+
}
|
|
260
|
+
return wrapped;
|
|
261
|
+
}
|
|
262
|
+
|
|
82
263
|
// ../../src/bootstrap/rateLimit.ts
|
|
83
264
|
var LOCAL_LOGIN_RATE_LIMIT = {
|
|
84
265
|
maxAttempts: 100,
|
|
@@ -88,9 +269,6 @@ var PRODUCTION_LOGIN_RATE_LIMIT = {
|
|
|
88
269
|
maxAttempts: 5,
|
|
89
270
|
decaySeconds: 900
|
|
90
271
|
};
|
|
91
|
-
function isLocalAppEnv() {
|
|
92
|
-
return (process.env.APP_ENV ?? "local") === "local";
|
|
93
|
-
}
|
|
94
272
|
function parsePositiveInt(value, fallback) {
|
|
95
273
|
const parsed = Number(value);
|
|
96
274
|
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
@@ -110,18 +288,18 @@ function parseWindowSeconds(secondsValue, msValue, fallback) {
|
|
|
110
288
|
}
|
|
111
289
|
return fallback;
|
|
112
290
|
}
|
|
113
|
-
function resolveLoginRateLimit() {
|
|
114
|
-
const defaults =
|
|
291
|
+
function resolveLoginRateLimit(env = {}) {
|
|
292
|
+
const defaults = env.APP_ENV === "local" ? LOCAL_LOGIN_RATE_LIMIT : PRODUCTION_LOGIN_RATE_LIMIT;
|
|
115
293
|
return {
|
|
116
|
-
maxAttempts: parsePositiveInt(
|
|
117
|
-
decaySeconds: parseWindowSeconds(
|
|
294
|
+
maxAttempts: parsePositiveInt(env.LOGIN_RATE_LIMIT_PER_WINDOW, defaults.maxAttempts),
|
|
295
|
+
decaySeconds: parseWindowSeconds(env.LOGIN_RATE_LIMIT_WINDOW_SECONDS, env.LOGIN_RATE_LIMIT_WINDOW_MS, defaults.decaySeconds)
|
|
118
296
|
};
|
|
119
297
|
}
|
|
120
|
-
function resolveRegisterRateLimit() {
|
|
121
|
-
const defaults =
|
|
298
|
+
function resolveRegisterRateLimit(env = {}) {
|
|
299
|
+
const defaults = env.APP_ENV === "local" ? { maxAttempts: 100, decaySeconds: 60 } : { maxAttempts: 10, decaySeconds: 60 };
|
|
122
300
|
return {
|
|
123
|
-
maxAttempts: parsePositiveInt(
|
|
124
|
-
decaySeconds: parseWindowSeconds(
|
|
301
|
+
maxAttempts: parsePositiveInt(env.REGISTER_RATE_LIMIT_PER_WINDOW, defaults.maxAttempts),
|
|
302
|
+
decaySeconds: parseWindowSeconds(env.REGISTER_RATE_LIMIT_WINDOW_SECONDS, env.REGISTER_RATE_LIMIT_WINDOW_MS, defaults.decaySeconds)
|
|
125
303
|
};
|
|
126
304
|
}
|
|
127
305
|
|
|
@@ -130,15 +308,78 @@ class HttpKernel {
|
|
|
130
308
|
dependencies;
|
|
131
309
|
ownedThrottles = new Set;
|
|
132
310
|
disposed = false;
|
|
311
|
+
emailVerificationRequired;
|
|
312
|
+
viewsEnabled;
|
|
313
|
+
publicReadsEnabled;
|
|
314
|
+
etagEnabled;
|
|
315
|
+
httpOptions;
|
|
316
|
+
responsePolicy;
|
|
133
317
|
configuredCsrfMiddleware;
|
|
318
|
+
responses;
|
|
319
|
+
persistence;
|
|
134
320
|
constructor(dependencies) {
|
|
135
321
|
this.dependencies = dependencies;
|
|
322
|
+
const http = dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS) : undefined;
|
|
323
|
+
this.httpOptions = http;
|
|
324
|
+
const config = dependencies.container.has(CORE_CONFIG_TOKEN) ? dependencies.container.resolve(CORE_CONFIG_TOKEN) : undefined;
|
|
325
|
+
const features = http?.features ?? config?.get("http.features");
|
|
326
|
+
this.emailVerificationRequired = features?.emailVerificationRequired ?? false;
|
|
327
|
+
this.viewsEnabled = features?.viewsEnabled ?? false;
|
|
328
|
+
this.publicReadsEnabled = features?.publicReadsEnabled ?? false;
|
|
329
|
+
this.etagEnabled = features?.etagEnabled ?? true;
|
|
330
|
+
const policy = http?.responsePolicy ?? config?.get("http.responsePolicy");
|
|
331
|
+
if (policy?.cors)
|
|
332
|
+
resolveCorsConfig(policy.cors);
|
|
333
|
+
this.responsePolicy = Object.freeze({
|
|
334
|
+
cors: policy?.cors ? {
|
|
335
|
+
allowedOrigins: [...policy.cors.allowedOrigins ?? []],
|
|
336
|
+
additionalAllowedHeaders: [...policy.cors.additionalAllowedHeaders ?? []]
|
|
337
|
+
} : undefined,
|
|
338
|
+
securityHeaders: policy?.securityHeaders ?? {
|
|
339
|
+
production: http?.production ?? true,
|
|
340
|
+
frontendMode: this.viewsEnabled ? "server-htmx" : "api"
|
|
341
|
+
},
|
|
342
|
+
maxBodyBytes: policy?.maxBodyBytes ?? 1048576,
|
|
343
|
+
rateLimitPerMinute: policy?.rateLimitPerMinute ?? 120,
|
|
344
|
+
namespace: policy?.namespace ?? "strata",
|
|
345
|
+
trustForwardedFor: policy?.trustForwardedFor ?? false,
|
|
346
|
+
loginRateLimit: Object.freeze({ ...policy?.loginRateLimit ?? resolveLoginRateLimit() }),
|
|
347
|
+
registerRateLimit: Object.freeze({
|
|
348
|
+
...policy?.registerRateLimit ?? resolveRegisterRateLimit()
|
|
349
|
+
})
|
|
350
|
+
});
|
|
136
351
|
const targetOrigin = dependencies.container.has(CORE_CONFIG_TOKEN) ? dependencies.container.resolve(CORE_CONFIG_TOKEN).get(CSRF_TARGET_ORIGIN_CONFIG_KEY) : undefined;
|
|
137
|
-
this.configuredCsrfMiddleware = createCsrfMiddleware({ targetOrigin });
|
|
352
|
+
this.configuredCsrfMiddleware = createCsrfMiddleware({ targetOrigin, ...http?.csrf });
|
|
353
|
+
this.responses = Object.freeze({
|
|
354
|
+
json: Object.freeze(this.responseMiddleware("json")),
|
|
355
|
+
web: Object.freeze(this.responseMiddleware("web"))
|
|
356
|
+
});
|
|
357
|
+
this.persistence = dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? createHttpPersistence(dependencies.invoke, dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS)) : undefined;
|
|
138
358
|
if (dependencies.container.has(CORE_HTTP_CLEANUP_TOKEN)) {
|
|
139
359
|
dependencies.container.resolve(CORE_HTTP_CLEANUP_TOKEN)(() => this.dispose());
|
|
140
360
|
}
|
|
141
361
|
}
|
|
362
|
+
requireFlashOptions() {
|
|
363
|
+
const options = this.httpOptions?.flash;
|
|
364
|
+
if (!options)
|
|
365
|
+
throw new TypeError("Explicit flash configuration is required for web routes.");
|
|
366
|
+
return options;
|
|
367
|
+
}
|
|
368
|
+
requireIntendedOptions() {
|
|
369
|
+
const options = this.httpOptions?.intendedUrl;
|
|
370
|
+
if (!options)
|
|
371
|
+
throw new TypeError("Explicit intended URL cookie configuration is required for web authentication.");
|
|
372
|
+
return options;
|
|
373
|
+
}
|
|
374
|
+
routeModelBindingPorts(auth) {
|
|
375
|
+
const gate = this.dependencies.container.resolve(CORE_POLICY_GATE_TOKEN);
|
|
376
|
+
const authorize = gate.authorize;
|
|
377
|
+
return Object.freeze({
|
|
378
|
+
auth,
|
|
379
|
+
authorize: (...args) => authorize.call(gate, ...args),
|
|
380
|
+
etagEnabled: this.etagEnabled
|
|
381
|
+
});
|
|
382
|
+
}
|
|
142
383
|
dispose() {
|
|
143
384
|
this.disposed = true;
|
|
144
385
|
const owned = [...this.ownedThrottles];
|
|
@@ -166,39 +407,73 @@ class HttpKernel {
|
|
|
166
407
|
return throttle;
|
|
167
408
|
}
|
|
168
409
|
globalMiddleware(errorFormat = "json") {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
410
|
+
return [...this.responses[errorFormat]];
|
|
411
|
+
}
|
|
412
|
+
run(request, handler, format = "json", identity, scim) {
|
|
413
|
+
assertHttpHandler(handler);
|
|
414
|
+
const execute = this.persistence;
|
|
415
|
+
if (!execute)
|
|
416
|
+
throw new Error("Explicit HTTP persistence composition is required.");
|
|
417
|
+
const outer = withMiddleware(...this.responses[format])((req) => withMiddleware(...scim ?? [])((req) => execute(req, handler, identity, scim !== undefined))(req));
|
|
418
|
+
return Promise.resolve(outer(request));
|
|
419
|
+
}
|
|
420
|
+
composed(handler, format, identity = identityModes(handler), scim = scimMiddleware(handler)) {
|
|
421
|
+
assertHttpHandler(handler);
|
|
422
|
+
if (identity !== undefined && scim !== undefined)
|
|
423
|
+
throw new TypeError("SCIM and identity operation declarations cannot be combined.");
|
|
424
|
+
return ownedHttpRoute(handler, (request, work) => this.run(request, work, format, identity, scim), identity, scim);
|
|
425
|
+
}
|
|
426
|
+
wrapIdentity(handler, modes = ["identifier"]) {
|
|
427
|
+
assertHttpHandler(handler);
|
|
428
|
+
if (scimMiddleware(handler) !== undefined)
|
|
429
|
+
throw new TypeError("SCIM and identity operation declarations cannot be combined.");
|
|
430
|
+
if (!Array.isArray(modes) || modes.some((mode) => mode !== "identifier" && mode !== "bypass"))
|
|
431
|
+
throw new TypeError("Invalid identity route policy modes.");
|
|
432
|
+
return this.composed(handler, "web", Object.freeze([...modes]));
|
|
433
|
+
}
|
|
434
|
+
wrapPasswordConfirm(handler) {
|
|
435
|
+
assertHttpHandler(handler);
|
|
436
|
+
const options = this.dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS).passwordConfirmation;
|
|
437
|
+
if (!options)
|
|
438
|
+
throw new Error("Explicit password confirmation configuration is required.");
|
|
439
|
+
return this.composed(withMiddleware(createRequirePasswordConfirmMiddleware(options))(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
440
|
+
}
|
|
441
|
+
wrapRouteMap(routes, middleware) {
|
|
442
|
+
return enterModuleRoutes(routes, (callback) => this.composed(withMiddleware(...middleware)(callback), "json", identityModes(callback), scimMiddleware(callback)));
|
|
443
|
+
}
|
|
444
|
+
wrapScim(handler, before = []) {
|
|
445
|
+
assertHttpHandler(handler);
|
|
446
|
+
if (identityModes(handler) !== undefined)
|
|
447
|
+
throw new TypeError("SCIM and identity operation declarations cannot be combined.");
|
|
448
|
+
return this.composed(handler, "json", undefined, Object.freeze([...before]));
|
|
179
449
|
}
|
|
180
450
|
responseMiddleware(errorFormat) {
|
|
181
451
|
return [
|
|
182
|
-
createCorsMiddleware(),
|
|
183
|
-
createSecurityHeadersMiddleware(),
|
|
184
|
-
createBodySizeLimitMiddleware(),
|
|
185
|
-
createTracingMiddleware(),
|
|
452
|
+
createCorsMiddleware(this.responsePolicy.cors),
|
|
453
|
+
createSecurityHeadersMiddleware(this.responsePolicy.securityHeaders),
|
|
454
|
+
createBodySizeLimitMiddleware(this.responsePolicy.maxBodyBytes),
|
|
455
|
+
...this.dependencies.container.has(CORE_TRACING_RUNTIME_TOKEN) ? [createTracingMiddleware(this.dependencies.container.resolve(CORE_TRACING_RUNTIME_TOKEN))] : [],
|
|
186
456
|
createMetricsMiddleware(),
|
|
187
|
-
createRequestLoggingMiddleware(),
|
|
457
|
+
createRequestLoggingMiddleware(this.responsePolicy.trustForwardedFor),
|
|
188
458
|
requestIdMiddleware,
|
|
189
|
-
errorFormat === "json" ? createJsonErrorMiddleware() : createWebErrorMiddleware(
|
|
459
|
+
errorFormat === "json" ? createJsonErrorMiddleware() : createWebErrorMiddleware({
|
|
460
|
+
enabled: this.viewsEnabled,
|
|
461
|
+
production: this.dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? this.dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS).production ?? true : true,
|
|
462
|
+
render: this.dependencies.container.has(CORE_WEB_ERROR_VIEW_TOKEN) ? this.dependencies.container.resolve(CORE_WEB_ERROR_VIEW_TOKEN) : undefined
|
|
463
|
+
})
|
|
190
464
|
];
|
|
191
465
|
}
|
|
192
466
|
wrapPublicDocument(handler) {
|
|
193
|
-
|
|
467
|
+
assertHttpHandler(handler);
|
|
468
|
+
return ownedPublicHttpRoute(handler, (request, work) => Promise.resolve(withMiddleware(...this.responses.web)(work)(request)));
|
|
194
469
|
}
|
|
195
470
|
group(name) {
|
|
196
|
-
const auth =
|
|
471
|
+
const auth = establishedAuthReader2;
|
|
197
472
|
switch (name) {
|
|
198
473
|
case "authenticated":
|
|
199
474
|
return [createRequireAuthMiddleware(auth)];
|
|
200
475
|
case "web":
|
|
201
|
-
return
|
|
476
|
+
return this.viewsEnabled ? [createFlashMiddleware(this.requireFlashOptions()), this.configuredCsrfMiddleware] : [];
|
|
202
477
|
case "api": {
|
|
203
478
|
const csrf = this.configuredCsrfMiddleware;
|
|
204
479
|
if (!this.dependencies.container.has(CORE_CONFIG_TOKEN)) {
|
|
@@ -208,9 +483,11 @@ class HttpKernel {
|
|
|
208
483
|
const config = this.dependencies.container.resolve(CORE_CONFIG_TOKEN);
|
|
209
484
|
const redisUrl = config.get(REDIS_URL_CONFIG_KEY)?.trim() ?? "";
|
|
210
485
|
if (!redisUrl) {
|
|
211
|
-
const maxAttempts =
|
|
486
|
+
const maxAttempts = this.responsePolicy.rateLimitPerMinute ?? 120;
|
|
212
487
|
return [
|
|
213
488
|
this.ownThrottle(createMemoryThrottleMiddleware({
|
|
489
|
+
namespace: this.responsePolicy.namespace,
|
|
490
|
+
trustForwardedFor: this.responsePolicy.trustForwardedFor,
|
|
214
491
|
maxAttempts: Number.isFinite(maxAttempts) ? maxAttempts : 120,
|
|
215
492
|
decaySeconds: 60,
|
|
216
493
|
quotaPolicy
|
|
@@ -218,9 +495,11 @@ class HttpKernel {
|
|
|
218
495
|
csrf
|
|
219
496
|
];
|
|
220
497
|
}
|
|
221
|
-
const maxAttempts =
|
|
498
|
+
const maxAttempts = this.responsePolicy.rateLimitPerMinute ?? 120;
|
|
222
499
|
return [
|
|
223
500
|
this.ownThrottle(createThrottleMiddleware({
|
|
501
|
+
namespace: this.responsePolicy.namespace,
|
|
502
|
+
trustForwardedFor: this.responsePolicy.trustForwardedFor,
|
|
224
503
|
redisUrl,
|
|
225
504
|
maxAttempts: Number.isFinite(maxAttempts) ? maxAttempts : 120,
|
|
226
505
|
decaySeconds: 60,
|
|
@@ -234,146 +513,183 @@ class HttpKernel {
|
|
|
234
513
|
}
|
|
235
514
|
}
|
|
236
515
|
wrap(groups, handler) {
|
|
516
|
+
assertHttpHandler(handler);
|
|
237
517
|
const names = Array.isArray(groups) ? groups : [groups];
|
|
238
518
|
const middleware = names.flatMap((name) => this.group(name));
|
|
239
519
|
const wrapped = middleware.length === 0 ? handler : withMiddleware(...middleware)(handler);
|
|
240
520
|
if (names.includes("api")) {
|
|
241
|
-
return
|
|
521
|
+
return this.composed(wrapped, "json", identityModes(handler), scimMiddleware(handler));
|
|
242
522
|
}
|
|
243
|
-
return wrapped;
|
|
523
|
+
return this.composed(wrapped, "web", identityModes(handler), scimMiddleware(handler));
|
|
244
524
|
}
|
|
245
525
|
wrapApi(handler) {
|
|
526
|
+
assertHttpHandler(handler);
|
|
246
527
|
return this.wrap(["api", "authenticated"], handler);
|
|
247
528
|
}
|
|
248
529
|
wrapWeb(handler) {
|
|
249
|
-
|
|
530
|
+
assertHttpHandler(handler);
|
|
531
|
+
return this.wrap("web", handler);
|
|
250
532
|
}
|
|
251
533
|
wrapWebGuest(handler, home = "/") {
|
|
252
|
-
|
|
253
|
-
|
|
534
|
+
assertHttpHandler(handler);
|
|
535
|
+
const auth = establishedAuthReader2;
|
|
536
|
+
const guest = this.wrapWeb(async (request, ports) => {
|
|
254
537
|
const user = await auth.resolve(request);
|
|
255
538
|
if (user) {
|
|
256
|
-
if (
|
|
539
|
+
if (this.emailVerificationRequired && !hasVerifiedEmail(user)) {
|
|
257
540
|
return Response.redirect("/email/verify", 302);
|
|
258
541
|
}
|
|
259
542
|
const location = typeof home === "function" ? await home(user) : home;
|
|
260
543
|
return Response.redirect(location, 302);
|
|
261
544
|
}
|
|
262
|
-
return handler(request);
|
|
545
|
+
return handler(request, ports);
|
|
263
546
|
});
|
|
547
|
+
return this.composed(guest, "web", identityModes(handler), scimMiddleware(handler));
|
|
264
548
|
}
|
|
265
549
|
wrapWebPublicRead(handler, options = {}) {
|
|
266
|
-
|
|
550
|
+
assertHttpHandler(handler);
|
|
551
|
+
if (options.allowAnonymous ?? this.publicReadsEnabled) {
|
|
267
552
|
return this.wrapWeb(handler);
|
|
268
553
|
}
|
|
269
554
|
return this.wrapWebAuthenticated(handler);
|
|
270
555
|
}
|
|
271
556
|
wrapWebAuthenticated(handler) {
|
|
557
|
+
assertHttpHandler(handler);
|
|
272
558
|
return this.wrapWebAuth(handler, { verified: true });
|
|
273
559
|
}
|
|
274
560
|
wrapWebAuthenticatedAllowUnverified(handler) {
|
|
561
|
+
assertHttpHandler(handler);
|
|
275
562
|
return this.wrapWebAuth(handler, { verified: false });
|
|
276
563
|
}
|
|
277
564
|
wrapWebVerified(handler) {
|
|
565
|
+
assertHttpHandler(handler);
|
|
278
566
|
return this.wrapWebAuth(handler, { verified: true });
|
|
279
567
|
}
|
|
280
568
|
wrapWebPasswordConfirm(handler) {
|
|
281
|
-
|
|
569
|
+
assertHttpHandler(handler);
|
|
570
|
+
const options = this.dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS).passwordConfirmation;
|
|
571
|
+
if (!options)
|
|
572
|
+
throw new Error("Explicit password confirmation configuration is required.");
|
|
573
|
+
return this.wrapWebAuth(this.composed(withMiddleware(createRequirePasswordConfirmMiddleware(options))(handler), "web", identityModes(handler), scimMiddleware(handler)), {
|
|
282
574
|
verified: true
|
|
283
575
|
});
|
|
284
576
|
}
|
|
285
577
|
wrapWebAbility(ability, handler) {
|
|
286
|
-
|
|
578
|
+
assertHttpHandler(handler);
|
|
579
|
+
const auth = establishedAuthReader2;
|
|
287
580
|
const abilityChecker = resolveAbilityChecker(this.dependencies.container);
|
|
288
|
-
const requireAbility = createRequireAbilityMiddleware(abilityChecker);
|
|
581
|
+
const requireAbility = createRequireAbilityMiddleware(abilityChecker, this.viewsEnabled);
|
|
289
582
|
const middleware = [
|
|
290
|
-
createRequireWebAuthMiddleware(auth),
|
|
583
|
+
createRequireWebAuthMiddleware(auth, this.requireIntendedOptions()),
|
|
291
584
|
...this.verifiedMiddleware(),
|
|
292
585
|
requireAbility(ability)
|
|
293
586
|
];
|
|
294
|
-
return this.wrapWeb(withMiddleware(...middleware)(handler));
|
|
587
|
+
return this.wrapWeb(this.composed(withMiddleware(...middleware)(handler), "web", identityModes(handler), scimMiddleware(handler)));
|
|
295
588
|
}
|
|
296
589
|
wrapWebGlobalAdmin(handler) {
|
|
297
|
-
|
|
590
|
+
assertHttpHandler(handler);
|
|
591
|
+
const auth = establishedAuthReader2;
|
|
298
592
|
const middleware = [
|
|
299
|
-
createRequireWebAuthMiddleware(auth),
|
|
593
|
+
createRequireWebAuthMiddleware(auth, this.requireIntendedOptions()),
|
|
300
594
|
...this.verifiedMiddleware(),
|
|
301
595
|
createRequireGlobalAdminMiddleware()
|
|
302
596
|
];
|
|
303
|
-
return this.wrapWeb(withMiddleware(...middleware)(handler));
|
|
597
|
+
return this.wrapWeb(this.composed(withMiddleware(...middleware)(handler), "web", identityModes(handler), scimMiddleware(handler)));
|
|
304
598
|
}
|
|
305
599
|
wrapAuthenticated(handler) {
|
|
600
|
+
assertHttpHandler(handler);
|
|
306
601
|
return this.wrap("authenticated", handler);
|
|
307
602
|
}
|
|
308
603
|
wrapVerified(handler) {
|
|
604
|
+
assertHttpHandler(handler);
|
|
309
605
|
const middleware = [...this.group("authenticated"), ...this.verifiedMiddleware()];
|
|
310
|
-
return withMiddleware(...middleware)(handler);
|
|
606
|
+
return this.composed(withMiddleware(...middleware)(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
311
607
|
}
|
|
312
608
|
wrapPublicRead(handler, options = {}) {
|
|
313
|
-
|
|
314
|
-
|
|
609
|
+
assertHttpHandler(handler);
|
|
610
|
+
if (options.allowAnonymous ?? this.publicReadsEnabled) {
|
|
611
|
+
return this.composed(handler, "json");
|
|
315
612
|
}
|
|
316
613
|
return this.wrapAuthenticated(handler);
|
|
317
614
|
}
|
|
318
615
|
wrapGlobalAdmin(handler) {
|
|
616
|
+
assertHttpHandler(handler);
|
|
319
617
|
const middleware = [...this.group("authenticated"), createRequireGlobalAdminMiddleware()];
|
|
320
|
-
return withMiddleware(...middleware)(handler);
|
|
618
|
+
return this.composed(withMiddleware(...middleware)(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
321
619
|
}
|
|
322
620
|
wrapAbility(ability, handler) {
|
|
621
|
+
assertHttpHandler(handler);
|
|
323
622
|
const abilityChecker = resolveAbilityChecker(this.dependencies.container);
|
|
324
|
-
const requireAbility = createRequireAbilityMiddleware(abilityChecker);
|
|
623
|
+
const requireAbility = createRequireAbilityMiddleware(abilityChecker, this.viewsEnabled);
|
|
325
624
|
const middleware = [...this.group("authenticated"), requireAbility(ability)];
|
|
326
|
-
return withMiddleware(...middleware)(handler);
|
|
625
|
+
return this.composed(withMiddleware(...middleware)(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
327
626
|
}
|
|
328
627
|
wrapPolicy(resource, action, handler) {
|
|
329
|
-
|
|
628
|
+
assertHttpHandler(handler);
|
|
629
|
+
const auth = establishedAuthReader2;
|
|
330
630
|
const gate = this.dependencies.container.resolve(CORE_POLICY_GATE_TOKEN);
|
|
331
|
-
return withMiddleware(createAuthorizeMiddleware(gate, auth, resource, action))(handler);
|
|
631
|
+
return this.composed(withMiddleware(createAuthorizeMiddleware(gate, auth, resource, action))(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
332
632
|
}
|
|
333
633
|
wrapLogin(handler) {
|
|
334
|
-
|
|
634
|
+
assertHttpHandler(handler);
|
|
635
|
+
return this.wrapThrottle("login", this.responsePolicy.loginRateLimit ?? resolveLoginRateLimit(), handler);
|
|
335
636
|
}
|
|
336
637
|
wrapSigned(handler) {
|
|
337
|
-
|
|
638
|
+
assertHttpHandler(handler);
|
|
639
|
+
const secret = this.dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS).signatureSecret;
|
|
640
|
+
if (!secret)
|
|
641
|
+
throw new Error("Explicit signed URL configuration is required.");
|
|
642
|
+
return this.composed(withMiddleware(createValidateSignatureMiddleware({ secret }))(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
338
643
|
}
|
|
339
644
|
wrapRegister(handler) {
|
|
340
|
-
|
|
645
|
+
assertHttpHandler(handler);
|
|
646
|
+
return this.wrapThrottle("register", this.responsePolicy.registerRateLimit ?? resolveRegisterRateLimit(), handler);
|
|
341
647
|
}
|
|
342
648
|
wrapWebAuth(handler, options) {
|
|
343
|
-
|
|
649
|
+
assertHttpHandler(handler);
|
|
650
|
+
const auth = establishedAuthReader2;
|
|
344
651
|
const middleware = [
|
|
345
|
-
createRequireWebAuthMiddleware(auth),
|
|
652
|
+
createRequireWebAuthMiddleware(auth, this.requireIntendedOptions()),
|
|
346
653
|
...options.verified ? this.verifiedMiddleware() : []
|
|
347
654
|
];
|
|
348
|
-
return this.wrapWeb(withMiddleware(...middleware)(handler));
|
|
655
|
+
return this.wrapWeb(this.composed(withMiddleware(...middleware)(handler), "web", identityModes(handler), scimMiddleware(handler)));
|
|
349
656
|
}
|
|
350
657
|
verifiedMiddleware() {
|
|
351
|
-
const auth =
|
|
352
|
-
return [
|
|
658
|
+
const auth = establishedAuthReader2;
|
|
659
|
+
return [
|
|
660
|
+
createRequireVerifiedMiddleware(auth, this.emailVerificationRequired, this.requireIntendedOptions())
|
|
661
|
+
];
|
|
353
662
|
}
|
|
354
663
|
wrapThrottle(scope, rateLimit, handler) {
|
|
664
|
+
assertHttpHandler(handler);
|
|
355
665
|
const memoryKeyPrefix = scope === "login" ? "login-throttle:" : "register-throttle:";
|
|
356
666
|
const redisUrl = this.dependencies.container.has(CORE_CONFIG_TOKEN) ? this.dependencies.container.resolve(CORE_CONFIG_TOKEN).get(REDIS_URL_CONFIG_KEY)?.trim() ?? "" : "";
|
|
357
667
|
if (scope === "login") {
|
|
358
|
-
return withMiddleware(this.ownThrottle(createLoginThrottleMiddleware({
|
|
668
|
+
return this.composed(withMiddleware(this.ownThrottle(createLoginThrottleMiddleware({
|
|
669
|
+
namespace: this.responsePolicy.namespace,
|
|
670
|
+
trustForwardedFor: this.responsePolicy.trustForwardedFor,
|
|
359
671
|
...redisUrl ? { redisUrl } : {},
|
|
360
672
|
maxAttempts: rateLimit.maxAttempts,
|
|
361
673
|
decaySeconds: rateLimit.decaySeconds
|
|
362
|
-
})))(handler);
|
|
674
|
+
})))(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
363
675
|
}
|
|
364
676
|
if (redisUrl) {
|
|
365
|
-
return withMiddleware(this.ownThrottle(createThrottleMiddleware({
|
|
677
|
+
return this.composed(withMiddleware(this.ownThrottle(createThrottleMiddleware({
|
|
678
|
+
namespace: this.responsePolicy.namespace,
|
|
679
|
+
trustForwardedFor: this.responsePolicy.trustForwardedFor,
|
|
366
680
|
redisUrl,
|
|
367
681
|
maxAttempts: rateLimit.maxAttempts,
|
|
368
682
|
decaySeconds: rateLimit.decaySeconds,
|
|
369
683
|
keyPrefix: memoryKeyPrefix
|
|
370
|
-
})))(handler);
|
|
684
|
+
})))(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
371
685
|
}
|
|
372
|
-
return withMiddleware(this.ownThrottle(createMemoryThrottleMiddleware({
|
|
686
|
+
return this.composed(withMiddleware(this.ownThrottle(createMemoryThrottleMiddleware({
|
|
687
|
+
namespace: this.responsePolicy.namespace,
|
|
688
|
+
trustForwardedFor: this.responsePolicy.trustForwardedFor,
|
|
373
689
|
maxAttempts: rateLimit.maxAttempts,
|
|
374
690
|
decaySeconds: rateLimit.decaySeconds,
|
|
375
691
|
keyPrefix: memoryKeyPrefix
|
|
376
|
-
})))(handler);
|
|
692
|
+
})))(handler), "json", identityModes(handler), scimMiddleware(handler));
|
|
377
693
|
}
|
|
378
694
|
}
|
|
379
695
|
function createHttpKernel(dependencies) {
|
|
@@ -397,8 +713,8 @@ function toRouteRequest(request) {
|
|
|
397
713
|
});
|
|
398
714
|
return request;
|
|
399
715
|
}
|
|
400
|
-
function wrapSecuredRouteModelByKey(param, resolver, authorization, handler) {
|
|
401
|
-
const bound =
|
|
716
|
+
function wrapSecuredRouteModelByKey(param, resolver, authorization, handler, ports) {
|
|
717
|
+
const bound = securedBindRouteModelByKey(param, resolver, authorization, handler, ports);
|
|
402
718
|
return async (request) => bound(toRouteRequest(request));
|
|
403
719
|
}
|
|
404
720
|
function wrapWebLogin(kernel, handler, onThrottled) {
|
|
@@ -409,13 +725,13 @@ function wrapWebRegister(kernel, handler, onThrottled) {
|
|
|
409
725
|
}
|
|
410
726
|
function wrapWebThrottle(kernel, scope, handler, onThrottled) {
|
|
411
727
|
const throttled = scope === "login" ? kernel.wrapLogin(handler) : kernel.wrapRegister(handler);
|
|
412
|
-
return async (request) => {
|
|
413
|
-
const response = await throttled(request);
|
|
728
|
+
return kernel.composed(async (request, ports) => {
|
|
729
|
+
const response = await throttled(request, ports);
|
|
414
730
|
if (response.status === 429 && !requestPrefersJson(request)) {
|
|
415
|
-
return onThrottled(request);
|
|
731
|
+
return onThrottled(request, ports);
|
|
416
732
|
}
|
|
417
733
|
return response;
|
|
418
|
-
};
|
|
734
|
+
}, "web", identityModes2(handler), scimMiddleware2(handler));
|
|
419
735
|
}
|
|
420
736
|
function createRouteKernel(dependencies) {
|
|
421
737
|
return createHttpKernel(dependencies);
|