create-strata 2.2.51 → 2.2.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +292 -258
- package/dist/templates/package.json +3 -3
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1201,6 +1201,254 @@ async function resolveStarterPlan(flags, injected) {
|
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
1203
|
|
|
1204
|
+
// ../strata-starter/src/auth/renderBrowserRoutes.ts
|
|
1205
|
+
function renderMfaChallengeRoutes() {
|
|
1206
|
+
return ` "/login/mfa": {
|
|
1207
|
+
GET: kernel.wrapWebGuest(async (request) => {
|
|
1208
|
+
const pending = readPendingMfaChallenge(request);
|
|
1209
|
+
const record = pending ? await starterAuthDirectory.findByIdOrThrow(pending.userId).catch(() => null) : null;
|
|
1210
|
+
if (!record?.is_active || record.auth_version !== pending?.authVersion) return redirectTo("/login");
|
|
1211
|
+
return views.render("mfa-challenge.eta", { layout: { title: "MFA" }, errors: {}, code: "" }, { request });
|
|
1212
|
+
}),
|
|
1213
|
+
POST: wrapWebLogin(
|
|
1214
|
+
kernel,
|
|
1215
|
+
async (request) => {
|
|
1216
|
+
const pending = readPendingMfaChallenge(request);
|
|
1217
|
+
if (!pending) {
|
|
1218
|
+
return redirectTo("/login");
|
|
1219
|
+
}
|
|
1220
|
+
const { fields } = await parseFormBody(request);
|
|
1221
|
+
const submitted = (fields.code ?? "").trim();
|
|
1222
|
+
return runWithLockedAuthUser(pending.userId, async (record) => {
|
|
1223
|
+
if (!record?.is_active || record.auth_version !== pending.authVersion) {
|
|
1224
|
+
const failed = redirectTo("/login");
|
|
1225
|
+
failed.headers.append("set-cookie", pendingMfaClearCookie());
|
|
1226
|
+
return failed;
|
|
1227
|
+
}
|
|
1228
|
+
const mfaResult = completePasswordLogin(record, { mfaCode: submitted });
|
|
1229
|
+
if (!mfaResult.ok) {
|
|
1230
|
+
return views.render("mfa-challenge.eta", { layout: { title: "MFA" }, errors: { code: "That code is not valid." }, code: "" }, { request });
|
|
1231
|
+
}
|
|
1232
|
+
await persistAuthRecovery(pending.userId, record.mfa_recovery_codes, mfaResult.consumedRecoveryHash);
|
|
1233
|
+
const signed = await auth.signInRedirect(sessionUser(record), "/");
|
|
1234
|
+
signed.headers.append("set-cookie", pendingMfaClearCookie());
|
|
1235
|
+
return signed;
|
|
1236
|
+
});
|
|
1237
|
+
},
|
|
1238
|
+
async (request) =>
|
|
1239
|
+
views.render("mfa-challenge.eta", { layout: { title: "MFA" }, errors: { code: "Too many login attempts. Try again shortly." }, code: "" }, { request, status: 429 }),
|
|
1240
|
+
),
|
|
1241
|
+
},
|
|
1242
|
+
`;
|
|
1243
|
+
}
|
|
1244
|
+
function renderBrowserAuthRoutes({
|
|
1245
|
+
cookie,
|
|
1246
|
+
mfa,
|
|
1247
|
+
verify
|
|
1248
|
+
}) {
|
|
1249
|
+
const mfaLoginBranch = `if (!mfaResult.ok) {
|
|
1250
|
+
${mfa ? `if (mfaResult.error === "mfa_required") {
|
|
1251
|
+
const pending = redirectTo("/login/mfa");
|
|
1252
|
+
pending.headers.append("set-cookie", pendingMfaSetCookie(mfaResult.user.id, mfaResult.user.auth_version));
|
|
1253
|
+
return pending;
|
|
1254
|
+
}` : ""}
|
|
1255
|
+
return views.render("login.eta", { layout: { title: "Sign in" }, errors: { email: "These credentials do not match our records." }, email, password: "" }, { request });
|
|
1256
|
+
}`;
|
|
1257
|
+
const registerSuccessExisting = `return flashResponse(
|
|
1258
|
+
redirectTo("/login"),
|
|
1259
|
+
{ level: "success", message: "If that email is available, continue from the sign-in page." },
|
|
1260
|
+
);`;
|
|
1261
|
+
const verifyRegisterBranch = `return flashResponse(
|
|
1262
|
+
redirectTo("/login"),
|
|
1263
|
+
{ level: "success", message: "If that email is available, continue from the sign-in page." },
|
|
1264
|
+
);`;
|
|
1265
|
+
const cookieRoutes = cookie ? `
|
|
1266
|
+
webRoutes({ kernel, dependencies }) {
|
|
1267
|
+
const auth = dependencies.container.resolve<CookieSessionAuthManager>(CORE_AUTH_TOKEN);
|
|
1268
|
+
const views = dependencies.container.resolve(CORE_VIEW_RENDERER_TOKEN).namespace("auth", { headers: { "Cache-Control": "private, no-store" } });
|
|
1269
|
+
return {
|
|
1270
|
+
"/login": {
|
|
1271
|
+
GET: kernel.wrapWebGuest(async (request) =>
|
|
1272
|
+
views.render("login.eta", { layout: { title: "Sign in" }, errors: {}, email: "", password: "" }, { request }),
|
|
1273
|
+
),
|
|
1274
|
+
POST: wrapWebLogin(
|
|
1275
|
+
kernel,
|
|
1276
|
+
async (request) => {
|
|
1277
|
+
const { fields } = await parseFormBody(request);
|
|
1278
|
+
const { email, password, mfa_code } = parseLoginCredentials(fields);
|
|
1279
|
+
return runWithLoginUser(email, async (candidate) => {
|
|
1280
|
+
const mfaResult = await authenticatePasswordUser(candidate, password, mfa_code);
|
|
1281
|
+
${mfaLoginBranch}
|
|
1282
|
+
const user = mfaResult.user;
|
|
1283
|
+
return auth.signInRedirect(sessionUser(user), "/");
|
|
1284
|
+
});
|
|
1285
|
+
},
|
|
1286
|
+
async (request) =>
|
|
1287
|
+
views.render("login.eta", { layout: { title: "Sign in" }, errors: { email: "Too many login attempts. Try again shortly." }, email: "", password: "" }, { request, status: 429 }),
|
|
1288
|
+
),
|
|
1289
|
+
},
|
|
1290
|
+
"/register": {
|
|
1291
|
+
GET: kernel.wrapWebGuest(async (request) => {
|
|
1292
|
+
if ((process.env.FEATURE_REGISTRATION ?? "true") === "false") {
|
|
1293
|
+
return new Response("Not found", { status: 404 });
|
|
1294
|
+
}
|
|
1295
|
+
return views.render("register.eta", { layout: { title: "Create account" }, errors: {}, name: "", email: "", password: "" }, { request });
|
|
1296
|
+
}),
|
|
1297
|
+
POST: wrapWebRegister(
|
|
1298
|
+
kernel,
|
|
1299
|
+
async (request) => {
|
|
1300
|
+
if ((process.env.FEATURE_REGISTRATION ?? "true") === "false") {
|
|
1301
|
+
return new Response("Not found", { status: 404 });
|
|
1302
|
+
}
|
|
1303
|
+
const { fields } = await parseFormBody(request);
|
|
1304
|
+
const { name, email, password } = parseRegisterCredentials(fields);
|
|
1305
|
+
const existing = await starterAuthDirectory.findByEmail?.(email);
|
|
1306
|
+
await hashPassword(password);
|
|
1307
|
+
if (existing) {
|
|
1308
|
+
${registerSuccessExisting}
|
|
1309
|
+
}
|
|
1310
|
+
const hashed = await hashPassword(password);
|
|
1311
|
+
${verify ? "const verificationMail = " : ""}await createRegistration({ name, email, password: hashed }${verify ? ', "/email/verify"' : ""});
|
|
1312
|
+
${verify ? `// SMTP follows the outermost commit, including request RLS.
|
|
1313
|
+
if (verificationMail) await afterCommit(() => mailer().send(verificationMail));` : ""}
|
|
1314
|
+
${verifyRegisterBranch}
|
|
1315
|
+
},
|
|
1316
|
+
async (request) =>
|
|
1317
|
+
views.render("register.eta", { layout: { title: "Create account" }, errors: { form: "Too many registration attempts. Try again shortly." }, name: "", email: "", password: "" }, { request, status: 429 }),
|
|
1318
|
+
),
|
|
1319
|
+
},
|
|
1320
|
+
"/forgot-password": {
|
|
1321
|
+
GET: kernel.wrapWebGuest(async (request) =>
|
|
1322
|
+
views.render("forgot-password.eta", { layout: { title: "Forgot password" }, errors: {}, email: "" }, { request }),
|
|
1323
|
+
),
|
|
1324
|
+
POST: kernel.wrapWeb(async (request) => {
|
|
1325
|
+
const { fields } = await parseFormBody(request);
|
|
1326
|
+
const email = parseForgotPasswordEmail(fields);
|
|
1327
|
+
const user = await starterAuthDirectory.findByEmail?.(email);
|
|
1328
|
+
if (user) {
|
|
1329
|
+
await issueSignedAuthMail(email, "Reset your password", "/reset-password", AUTH_ONE_TIME_PURPOSES.passwordReset, user.id, { email });
|
|
1330
|
+
}
|
|
1331
|
+
return flashResponse(redirectTo("/forgot-password"), {
|
|
1332
|
+
level: "success",
|
|
1333
|
+
message: "If that account exists, a reset link is on its way.",
|
|
1334
|
+
});
|
|
1335
|
+
}),
|
|
1336
|
+
},
|
|
1337
|
+
"/reset-password": {
|
|
1338
|
+
GET: kernel.wrapWeb(kernel.wrapSigned(async (request) => {
|
|
1339
|
+
const email = new URL(request.url).searchParams.get("email") ?? "";
|
|
1340
|
+
return views.render("reset-password.eta", { layout: { title: "Reset password" }, errors: {}, password: "", email, action: \`\${new URL(request.url).pathname}\${new URL(request.url).search}\` }, { request });
|
|
1341
|
+
})),
|
|
1342
|
+
POST: kernel.wrapWeb(kernel.wrapSigned(async (request) => {
|
|
1343
|
+
const { fields } = await parseFormBody(request);
|
|
1344
|
+
const email = new URL(request.url).searchParams.get("email") ?? fields.email ?? "";
|
|
1345
|
+
const password = fields.password ?? "";
|
|
1346
|
+
if (!email || !(await completeSignedPasswordReset(request, password))) {
|
|
1347
|
+
return views.render("reset-password.eta", { layout: { title: "Reset password" }, errors: { password: "Invalid or expired reset link." }, password: "", email, action: \`\${new URL(request.url).pathname}\${new URL(request.url).search}\` }, { request });
|
|
1348
|
+
}
|
|
1349
|
+
return flashResponse(redirectTo("/login"), { level: "success", message: "Password updated. Sign in." });
|
|
1350
|
+
})),
|
|
1351
|
+
},
|
|
1352
|
+
"/logout": {
|
|
1353
|
+
POST: kernel.wrapWebAuthenticatedAllowUnverified((request) => auth.signOutRedirect(request, "/")),
|
|
1354
|
+
},${verify ? `
|
|
1355
|
+
"/email/verify": {
|
|
1356
|
+
GET: kernel.wrapWeb(async (request) => {
|
|
1357
|
+
const url = new URL(request.url);
|
|
1358
|
+
if (url.searchParams.get("signature")) {
|
|
1359
|
+
assertValidSignature(request);
|
|
1360
|
+
if (await completeSignedEmailVerification(request)) {
|
|
1361
|
+
return flashResponse(
|
|
1362
|
+
redirectTo("/login"),
|
|
1363
|
+
{ level: "success", message: "Email verified. Sign in." },
|
|
1364
|
+
);
|
|
1365
|
+
}
|
|
1366
|
+
return flashResponse(
|
|
1367
|
+
redirectTo("/login"),
|
|
1368
|
+
{ level: "error", message: "Invalid or expired verification link." },
|
|
1369
|
+
);
|
|
1370
|
+
}
|
|
1371
|
+
return views.render("verify-email.eta", { layout: { title: "Verify email" } }, { request });
|
|
1372
|
+
}),
|
|
1373
|
+
},
|
|
1374
|
+
"/email/verification-notification": {
|
|
1375
|
+
POST: kernel.wrapWebAuthenticatedAllowUnverified(async (request) => {
|
|
1376
|
+
const user = await auth.user(request);
|
|
1377
|
+
if (user) {
|
|
1378
|
+
const record = await starterAuthDirectory.findByIdOrThrow(Number(user.id));
|
|
1379
|
+
await issueSignedAuthMail(record.email ?? "", "Verify your email", "/email/verify", AUTH_ONE_TIME_PURPOSES.emailVerify, Number(record.id), { id: String(record.id) });
|
|
1380
|
+
}
|
|
1381
|
+
return flashResponse(redirectTo("/email/verify"), { level: "info", message: "Verification link sent." });
|
|
1382
|
+
}),
|
|
1383
|
+
},` : ""}${mfa ? `
|
|
1384
|
+
${renderMfaChallengeRoutes()} "/confirm-password": {
|
|
1385
|
+
GET: kernel.wrapWebAuthenticated(async (request) =>
|
|
1386
|
+
views.render("confirm-password.eta", { layout: { title: "Confirm password" }, errors: {}, password: "" }, { request }),
|
|
1387
|
+
),
|
|
1388
|
+
POST: kernel.wrapWebAuthenticated(async (request) => {
|
|
1389
|
+
const user = await auth.user(request);
|
|
1390
|
+
if (!user) {
|
|
1391
|
+
return redirectTo("/login");
|
|
1392
|
+
}
|
|
1393
|
+
const { fields } = await parseFormBody(request);
|
|
1394
|
+
const password = fields.password ?? "";
|
|
1395
|
+
return runWithLockedAuthUser(Number(user.id), async (record) => {
|
|
1396
|
+
if (!record?.is_active || record.auth_version !== user.authVersion) return redirectTo("/login");
|
|
1397
|
+
if (!record.password || !(await verifyPassword(password, record.password))) {
|
|
1398
|
+
return views.render("confirm-password.eta", { layout: { title: "Confirm password" }, errors: { password: "That password is not correct." }, password: "" }, { request });
|
|
1399
|
+
}
|
|
1400
|
+
const next = sanitizeInternalPath(new URL(request.url).searchParams.get("redirect") ?? "/account/mfa");
|
|
1401
|
+
const confirmed = redirectTo(next);
|
|
1402
|
+
confirmed.headers.append("set-cookie", createPasswordConfirmCookie(Number(user.id), record.auth_version));
|
|
1403
|
+
return confirmed;
|
|
1404
|
+
});
|
|
1405
|
+
}),
|
|
1406
|
+
},
|
|
1407
|
+
"/account/mfa": {
|
|
1408
|
+
GET: kernel.wrapWebAuthenticated(async (request) => {
|
|
1409
|
+
const secret = generateTotpSecret();
|
|
1410
|
+
const user = await auth.user(request);
|
|
1411
|
+
const record = user ? await starterAuthDirectory.findByIdOrThrow(Number(user.id)) : null;
|
|
1412
|
+
const otpauth = buildOtpauthUrl({
|
|
1413
|
+
secret,
|
|
1414
|
+
account: record?.email ?? "user",
|
|
1415
|
+
issuer: process.env.APP_NAME ?? "Strata",
|
|
1416
|
+
});
|
|
1417
|
+
return views.render("mfa-setup.eta", { layout: { title: "MFA" }, errors: {}, code: "", secret, otpauth }, { request });
|
|
1418
|
+
}),
|
|
1419
|
+
POST: kernel.wrapWebPasswordConfirm(async (request) => {
|
|
1420
|
+
const user = await auth.user(request);
|
|
1421
|
+
if (!user) {
|
|
1422
|
+
return redirectTo("/login");
|
|
1423
|
+
}
|
|
1424
|
+
const { fields } = await parseFormBody(request);
|
|
1425
|
+
const secret = (fields.secret ?? "").trim();
|
|
1426
|
+
const submitted = (fields.code ?? "").trim();
|
|
1427
|
+
if (!secret || !verifyTotp(secret, submitted)) {
|
|
1428
|
+
return views.render("mfa-setup.eta", {
|
|
1429
|
+
layout: { title: "MFA" },
|
|
1430
|
+
errors: { code: "Could not confirm that code." },
|
|
1431
|
+
code: "",
|
|
1432
|
+
secret,
|
|
1433
|
+
otpauth: buildOtpauthUrl({ secret, account: "user", issuer: process.env.APP_NAME ?? "Strata" }),
|
|
1434
|
+
}, { request });
|
|
1435
|
+
}
|
|
1436
|
+
const recoveryCodes = await enrollMfa(user, secret);
|
|
1437
|
+
return views.render("mfa-setup.eta", {
|
|
1438
|
+
layout: { title: "MFA" },
|
|
1439
|
+
errors: {},
|
|
1440
|
+
code: "",
|
|
1441
|
+
secret,
|
|
1442
|
+
otpauth: buildOtpauthUrl({ secret, account: "user", issuer: process.env.APP_NAME ?? "Strata" }),
|
|
1443
|
+
recoveryCodes,
|
|
1444
|
+
}, { request });
|
|
1445
|
+
}),
|
|
1446
|
+
},` : ""}
|
|
1447
|
+
};
|
|
1448
|
+
},` : "";
|
|
1449
|
+
return cookieRoutes;
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1204
1452
|
// ../strata-starter/src/auth/renderApiRoutes.ts
|
|
1205
1453
|
function renderApiAuthRoutes({
|
|
1206
1454
|
layers,
|
|
@@ -1213,6 +1461,11 @@ function renderApiAuthRoutes({
|
|
|
1213
1461
|
jsonCookieAccounts,
|
|
1214
1462
|
jsonAccounts
|
|
1215
1463
|
}) {
|
|
1464
|
+
const browserMfa = cookie && mfa && (layers.frontend === "api" || layers.frontend === "spa-react");
|
|
1465
|
+
const mfaContinuation = browserMfa ? `
|
|
1466
|
+
...applyMiddlewareToRoutes({
|
|
1467
|
+
${renderMfaChallengeRoutes()}
|
|
1468
|
+
}, kernel.group("api")),` : "";
|
|
1216
1469
|
const tokenLogin = authUsesToken(layers.auth) ? `
|
|
1217
1470
|
"/api/v1/auth/login": {
|
|
1218
1471
|
POST: kernel.wrap("api", kernel.wrapLogin(withErrorHandling(async (request) => {
|
|
@@ -1577,256 +1830,14 @@ function renderApiAuthRoutes({
|
|
|
1577
1830
|
})),
|
|
1578
1831
|
},` : "";
|
|
1579
1832
|
const apiRoutes = bearerApi || cookie ? `
|
|
1580
|
-
routes({ kernel, dependencies }) {
|
|
1581
|
-
|
|
1582
|
-
};
|
|
1583
|
-
},` : "";
|
|
1584
|
-
return apiRoutes;
|
|
1585
|
-
}
|
|
1586
|
-
|
|
1587
|
-
// ../strata-starter/src/auth/renderBrowserRoutes.ts
|
|
1588
|
-
function renderBrowserAuthRoutes({
|
|
1589
|
-
cookie,
|
|
1590
|
-
mfa,
|
|
1591
|
-
verify
|
|
1592
|
-
}) {
|
|
1593
|
-
const mfaLoginBranch = `if (!mfaResult.ok) {
|
|
1594
|
-
${mfa ? `if (mfaResult.error === "mfa_required") {
|
|
1595
|
-
const pending = redirectTo("/login/mfa");
|
|
1596
|
-
pending.headers.append("set-cookie", pendingMfaSetCookie(mfaResult.user.id, mfaResult.user.auth_version));
|
|
1597
|
-
return pending;
|
|
1598
|
-
}` : ""}
|
|
1599
|
-
return views.render("login.eta", { layout: { title: "Sign in" }, errors: { email: "These credentials do not match our records." }, email, password: "" }, { request });
|
|
1600
|
-
}`;
|
|
1601
|
-
const registerSuccessExisting = `return flashResponse(
|
|
1602
|
-
redirectTo("/login"),
|
|
1603
|
-
{ level: "success", message: "If that email is available, continue from the sign-in page." },
|
|
1604
|
-
);`;
|
|
1605
|
-
const verifyRegisterBranch = `return flashResponse(
|
|
1606
|
-
redirectTo("/login"),
|
|
1607
|
-
{ level: "success", message: "If that email is available, continue from the sign-in page." },
|
|
1608
|
-
);`;
|
|
1609
|
-
const cookieRoutes = cookie ? `
|
|
1610
|
-
webRoutes({ kernel, dependencies }) {
|
|
1833
|
+
routes({ kernel, dependencies }) {${browserMfa ? `
|
|
1834
|
+
// Browser SSO requires only its continuation view, not general web routes.
|
|
1611
1835
|
const auth = dependencies.container.resolve<CookieSessionAuthManager>(CORE_AUTH_TOKEN);
|
|
1612
|
-
const views = dependencies.container.resolve(CORE_VIEW_RENDERER_TOKEN).namespace("auth", { headers: { "Cache-Control": "private, no-store" } })
|
|
1613
|
-
return {
|
|
1614
|
-
"/login": {
|
|
1615
|
-
GET: kernel.wrapWebGuest(async (request) =>
|
|
1616
|
-
views.render("login.eta", { layout: { title: "Sign in" }, errors: {}, email: "", password: "" }, { request }),
|
|
1617
|
-
),
|
|
1618
|
-
POST: wrapWebLogin(
|
|
1619
|
-
kernel,
|
|
1620
|
-
async (request) => {
|
|
1621
|
-
const { fields } = await parseFormBody(request);
|
|
1622
|
-
const { email, password, mfa_code } = parseLoginCredentials(fields);
|
|
1623
|
-
return runWithLoginUser(email, async (candidate) => {
|
|
1624
|
-
const mfaResult = await authenticatePasswordUser(candidate, password, mfa_code);
|
|
1625
|
-
${mfaLoginBranch}
|
|
1626
|
-
const user = mfaResult.user;
|
|
1627
|
-
return auth.signInRedirect(sessionUser(user), "/");
|
|
1628
|
-
});
|
|
1629
|
-
},
|
|
1630
|
-
async (request) =>
|
|
1631
|
-
views.render("login.eta", { layout: { title: "Sign in" }, errors: { email: "Too many login attempts. Try again shortly." }, email: "", password: "" }, { request, status: 429 }),
|
|
1632
|
-
),
|
|
1633
|
-
},
|
|
1634
|
-
"/register": {
|
|
1635
|
-
GET: kernel.wrapWebGuest(async (request) => {
|
|
1636
|
-
if ((process.env.FEATURE_REGISTRATION ?? "true") === "false") {
|
|
1637
|
-
return new Response("Not found", { status: 404 });
|
|
1638
|
-
}
|
|
1639
|
-
return views.render("register.eta", { layout: { title: "Create account" }, errors: {}, name: "", email: "", password: "" }, { request });
|
|
1640
|
-
}),
|
|
1641
|
-
POST: wrapWebRegister(
|
|
1642
|
-
kernel,
|
|
1643
|
-
async (request) => {
|
|
1644
|
-
if ((process.env.FEATURE_REGISTRATION ?? "true") === "false") {
|
|
1645
|
-
return new Response("Not found", { status: 404 });
|
|
1646
|
-
}
|
|
1647
|
-
const { fields } = await parseFormBody(request);
|
|
1648
|
-
const { name, email, password } = parseRegisterCredentials(fields);
|
|
1649
|
-
const existing = await starterAuthDirectory.findByEmail?.(email);
|
|
1650
|
-
await hashPassword(password);
|
|
1651
|
-
if (existing) {
|
|
1652
|
-
${registerSuccessExisting}
|
|
1653
|
-
}
|
|
1654
|
-
const hashed = await hashPassword(password);
|
|
1655
|
-
${verify ? "const verificationMail = " : ""}await createRegistration({ name, email, password: hashed }${verify ? ', "/email/verify"' : ""});
|
|
1656
|
-
${verify ? `// SMTP follows the outermost commit, including request RLS.
|
|
1657
|
-
if (verificationMail) await afterCommit(() => mailer().send(verificationMail));` : ""}
|
|
1658
|
-
${verifyRegisterBranch}
|
|
1659
|
-
},
|
|
1660
|
-
async (request) =>
|
|
1661
|
-
views.render("register.eta", { layout: { title: "Create account" }, errors: { form: "Too many registration attempts. Try again shortly." }, name: "", email: "", password: "" }, { request, status: 429 }),
|
|
1662
|
-
),
|
|
1663
|
-
},
|
|
1664
|
-
"/forgot-password": {
|
|
1665
|
-
GET: kernel.wrapWebGuest(async (request) =>
|
|
1666
|
-
views.render("forgot-password.eta", { layout: { title: "Forgot password" }, errors: {}, email: "" }, { request }),
|
|
1667
|
-
),
|
|
1668
|
-
POST: kernel.wrapWeb(async (request) => {
|
|
1669
|
-
const { fields } = await parseFormBody(request);
|
|
1670
|
-
const email = parseForgotPasswordEmail(fields);
|
|
1671
|
-
const user = await starterAuthDirectory.findByEmail?.(email);
|
|
1672
|
-
if (user) {
|
|
1673
|
-
await issueSignedAuthMail(email, "Reset your password", "/reset-password", AUTH_ONE_TIME_PURPOSES.passwordReset, user.id, { email });
|
|
1674
|
-
}
|
|
1675
|
-
return flashResponse(redirectTo("/forgot-password"), {
|
|
1676
|
-
level: "success",
|
|
1677
|
-
message: "If that account exists, a reset link is on its way.",
|
|
1678
|
-
});
|
|
1679
|
-
}),
|
|
1680
|
-
},
|
|
1681
|
-
"/reset-password": {
|
|
1682
|
-
GET: kernel.wrapWeb(kernel.wrapSigned(async (request) => {
|
|
1683
|
-
const email = new URL(request.url).searchParams.get("email") ?? "";
|
|
1684
|
-
return views.render("reset-password.eta", { layout: { title: "Reset password" }, errors: {}, password: "", email, action: \`\${new URL(request.url).pathname}\${new URL(request.url).search}\` }, { request });
|
|
1685
|
-
})),
|
|
1686
|
-
POST: kernel.wrapWeb(kernel.wrapSigned(async (request) => {
|
|
1687
|
-
const { fields } = await parseFormBody(request);
|
|
1688
|
-
const email = new URL(request.url).searchParams.get("email") ?? fields.email ?? "";
|
|
1689
|
-
const password = fields.password ?? "";
|
|
1690
|
-
if (!email || !(await completeSignedPasswordReset(request, password))) {
|
|
1691
|
-
return views.render("reset-password.eta", { layout: { title: "Reset password" }, errors: { password: "Invalid or expired reset link." }, password: "", email, action: \`\${new URL(request.url).pathname}\${new URL(request.url).search}\` }, { request });
|
|
1692
|
-
}
|
|
1693
|
-
return flashResponse(redirectTo("/login"), { level: "success", message: "Password updated. Sign in." });
|
|
1694
|
-
})),
|
|
1695
|
-
},
|
|
1696
|
-
"/logout": {
|
|
1697
|
-
POST: kernel.wrapWebAuthenticatedAllowUnverified((request) => auth.signOutRedirect(request, "/")),
|
|
1698
|
-
},${verify ? `
|
|
1699
|
-
"/email/verify": {
|
|
1700
|
-
GET: kernel.wrapWeb(async (request) => {
|
|
1701
|
-
const url = new URL(request.url);
|
|
1702
|
-
if (url.searchParams.get("signature")) {
|
|
1703
|
-
assertValidSignature(request);
|
|
1704
|
-
if (await completeSignedEmailVerification(request)) {
|
|
1705
|
-
return flashResponse(
|
|
1706
|
-
redirectTo("/login"),
|
|
1707
|
-
{ level: "success", message: "Email verified. Sign in." },
|
|
1708
|
-
);
|
|
1709
|
-
}
|
|
1710
|
-
return flashResponse(
|
|
1711
|
-
redirectTo("/login"),
|
|
1712
|
-
{ level: "error", message: "Invalid or expired verification link." },
|
|
1713
|
-
);
|
|
1714
|
-
}
|
|
1715
|
-
return views.render("verify-email.eta", { layout: { title: "Verify email" } }, { request });
|
|
1716
|
-
}),
|
|
1717
|
-
},
|
|
1718
|
-
"/email/verification-notification": {
|
|
1719
|
-
POST: kernel.wrapWebAuthenticatedAllowUnverified(async (request) => {
|
|
1720
|
-
const user = await auth.user(request);
|
|
1721
|
-
if (user) {
|
|
1722
|
-
const record = await starterAuthDirectory.findByIdOrThrow(Number(user.id));
|
|
1723
|
-
await issueSignedAuthMail(record.email ?? "", "Verify your email", "/email/verify", AUTH_ONE_TIME_PURPOSES.emailVerify, Number(record.id), { id: String(record.id) });
|
|
1724
|
-
}
|
|
1725
|
-
return flashResponse(redirectTo("/email/verify"), { level: "info", message: "Verification link sent." });
|
|
1726
|
-
}),
|
|
1727
|
-
},` : ""}${mfa ? `
|
|
1728
|
-
"/login/mfa": {
|
|
1729
|
-
GET: kernel.wrapWebGuest(async (request) => {
|
|
1730
|
-
const pending = readPendingMfaChallenge(request);
|
|
1731
|
-
const record = pending ? await starterAuthDirectory.findByIdOrThrow(pending.userId).catch(() => null) : null;
|
|
1732
|
-
if (!record?.is_active || record.auth_version !== pending?.authVersion) return redirectTo("/login");
|
|
1733
|
-
return views.render("mfa-challenge.eta", { layout: { title: "MFA" }, errors: {}, code: "" }, { request });
|
|
1734
|
-
}),
|
|
1735
|
-
POST: wrapWebLogin(
|
|
1736
|
-
kernel,
|
|
1737
|
-
async (request) => {
|
|
1738
|
-
const pending = readPendingMfaChallenge(request);
|
|
1739
|
-
if (!pending) {
|
|
1740
|
-
return redirectTo("/login");
|
|
1741
|
-
}
|
|
1742
|
-
const { fields } = await parseFormBody(request);
|
|
1743
|
-
const submitted = (fields.code ?? "").trim();
|
|
1744
|
-
return runWithLockedAuthUser(pending.userId, async (record) => {
|
|
1745
|
-
if (!record?.is_active || record.auth_version !== pending.authVersion) {
|
|
1746
|
-
const failed = redirectTo("/login");
|
|
1747
|
-
failed.headers.append("set-cookie", pendingMfaClearCookie());
|
|
1748
|
-
return failed;
|
|
1749
|
-
}
|
|
1750
|
-
const mfaResult = completePasswordLogin(record, { mfaCode: submitted });
|
|
1751
|
-
if (!mfaResult.ok) {
|
|
1752
|
-
return views.render("mfa-challenge.eta", { layout: { title: "MFA" }, errors: { code: "That code is not valid." }, code: "" }, { request });
|
|
1753
|
-
}
|
|
1754
|
-
await persistAuthRecovery(pending.userId, record.mfa_recovery_codes, mfaResult.consumedRecoveryHash);
|
|
1755
|
-
const signed = await auth.signInRedirect(sessionUser(record), "/");
|
|
1756
|
-
signed.headers.append("set-cookie", pendingMfaClearCookie());
|
|
1757
|
-
return signed;
|
|
1758
|
-
});
|
|
1759
|
-
},
|
|
1760
|
-
async (request) =>
|
|
1761
|
-
views.render("mfa-challenge.eta", { layout: { title: "MFA" }, errors: { code: "Too many login attempts. Try again shortly." }, code: "" }, { request, status: 429 }),
|
|
1762
|
-
),
|
|
1763
|
-
},
|
|
1764
|
-
"/confirm-password": {
|
|
1765
|
-
GET: kernel.wrapWebAuthenticated(async (request) =>
|
|
1766
|
-
views.render("confirm-password.eta", { layout: { title: "Confirm password" }, errors: {}, password: "" }, { request }),
|
|
1767
|
-
),
|
|
1768
|
-
POST: kernel.wrapWebAuthenticated(async (request) => {
|
|
1769
|
-
const user = await auth.user(request);
|
|
1770
|
-
if (!user) {
|
|
1771
|
-
return redirectTo("/login");
|
|
1772
|
-
}
|
|
1773
|
-
const { fields } = await parseFormBody(request);
|
|
1774
|
-
const password = fields.password ?? "";
|
|
1775
|
-
return runWithLockedAuthUser(Number(user.id), async (record) => {
|
|
1776
|
-
if (!record?.is_active || record.auth_version !== user.authVersion) return redirectTo("/login");
|
|
1777
|
-
if (!record.password || !(await verifyPassword(password, record.password))) {
|
|
1778
|
-
return views.render("confirm-password.eta", { layout: { title: "Confirm password" }, errors: { password: "That password is not correct." }, password: "" }, { request });
|
|
1779
|
-
}
|
|
1780
|
-
const next = sanitizeInternalPath(new URL(request.url).searchParams.get("redirect") ?? "/account/mfa");
|
|
1781
|
-
const confirmed = redirectTo(next);
|
|
1782
|
-
confirmed.headers.append("set-cookie", createPasswordConfirmCookie(Number(user.id), record.auth_version));
|
|
1783
|
-
return confirmed;
|
|
1784
|
-
});
|
|
1785
|
-
}),
|
|
1786
|
-
},
|
|
1787
|
-
"/account/mfa": {
|
|
1788
|
-
GET: kernel.wrapWebAuthenticated(async (request) => {
|
|
1789
|
-
const secret = generateTotpSecret();
|
|
1790
|
-
const user = await auth.user(request);
|
|
1791
|
-
const record = user ? await starterAuthDirectory.findByIdOrThrow(Number(user.id)) : null;
|
|
1792
|
-
const otpauth = buildOtpauthUrl({
|
|
1793
|
-
secret,
|
|
1794
|
-
account: record?.email ?? "user",
|
|
1795
|
-
issuer: process.env.APP_NAME ?? "Strata",
|
|
1796
|
-
});
|
|
1797
|
-
return views.render("mfa-setup.eta", { layout: { title: "MFA" }, errors: {}, code: "", secret, otpauth }, { request });
|
|
1798
|
-
}),
|
|
1799
|
-
POST: kernel.wrapWebPasswordConfirm(async (request) => {
|
|
1800
|
-
const user = await auth.user(request);
|
|
1801
|
-
if (!user) {
|
|
1802
|
-
return redirectTo("/login");
|
|
1803
|
-
}
|
|
1804
|
-
const { fields } = await parseFormBody(request);
|
|
1805
|
-
const secret = (fields.secret ?? "").trim();
|
|
1806
|
-
const submitted = (fields.code ?? "").trim();
|
|
1807
|
-
if (!secret || !verifyTotp(secret, submitted)) {
|
|
1808
|
-
return views.render("mfa-setup.eta", {
|
|
1809
|
-
layout: { title: "MFA" },
|
|
1810
|
-
errors: { code: "Could not confirm that code." },
|
|
1811
|
-
code: "",
|
|
1812
|
-
secret,
|
|
1813
|
-
otpauth: buildOtpauthUrl({ secret, account: "user", issuer: process.env.APP_NAME ?? "Strata" }),
|
|
1814
|
-
}, { request });
|
|
1815
|
-
}
|
|
1816
|
-
const recoveryCodes = await enrollMfa(user, secret);
|
|
1817
|
-
return views.render("mfa-setup.eta", {
|
|
1818
|
-
layout: { title: "MFA" },
|
|
1819
|
-
errors: {},
|
|
1820
|
-
code: "",
|
|
1821
|
-
secret,
|
|
1822
|
-
otpauth: buildOtpauthUrl({ secret, account: "user", issuer: process.env.APP_NAME ?? "Strata" }),
|
|
1823
|
-
recoveryCodes,
|
|
1824
|
-
}, { request });
|
|
1825
|
-
}),
|
|
1826
|
-
},` : ""}
|
|
1836
|
+
const views = dependencies.container.resolve(CORE_VIEW_RENDERER_TOKEN).namespace("auth", { headers: { "Cache-Control": "private, no-store" } });` : ""}
|
|
1837
|
+
return {${mfaContinuation}${csrfRoute}${samlRoutes}${githubRoutes}${oidcRoutes}${cookieJsonAuth}${tokenLogin}${jsonRegister}${jwtLogin}${apiUser}${jsonPassword}${jsonVerify}${jsonAccountManagement}
|
|
1827
1838
|
};
|
|
1828
1839
|
},` : "";
|
|
1829
|
-
return
|
|
1840
|
+
return apiRoutes;
|
|
1830
1841
|
}
|
|
1831
1842
|
|
|
1832
1843
|
// ../strata-starter/src/auth/renderImports.ts
|
|
@@ -1842,6 +1853,9 @@ function renderAuthImports({
|
|
|
1842
1853
|
tenantInsert
|
|
1843
1854
|
}) {
|
|
1844
1855
|
const imports = [];
|
|
1856
|
+
if (cookie && mfa && (layers.frontend === "api" || layers.frontend === "spa-react")) {
|
|
1857
|
+
imports.push(`import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";`);
|
|
1858
|
+
}
|
|
1845
1859
|
if (authUsesToken(layers.auth) || cookie) {
|
|
1846
1860
|
imports.push(`import { randomBytes } from "node:crypto";`);
|
|
1847
1861
|
}
|
|
@@ -3290,6 +3304,7 @@ import "./preload.ts";
|
|
|
3290
3304
|
import { createAppContext as createProviderAppContext, type InitializedAppContext } from "@getstrata/bootstrap/context";
|
|
3291
3305
|
import type { AppRouteMap } from "@getstrata/bootstrap/contracts";
|
|
3292
3306
|
import { mergeSpaRoutes } from "@getstrata/bootstrap/createSpaRoutes";
|
|
3307
|
+
import { createHttpKernel } from "@getstrata/bootstrap/httpKernel";
|
|
3293
3308
|
import {
|
|
3294
3309
|
configureModulesDirectory,
|
|
3295
3310
|
discoverModules,
|
|
@@ -3362,6 +3377,7 @@ ${layers.extras.metrics ? ` metricsRuntime = createMetricsRuntime(options.run
|
|
|
3362
3377
|
await migrate();
|
|
3363
3378
|
}
|
|
3364
3379
|
|
|
3380
|
+
const documentKernel = createHttpKernel(context.dependencies);
|
|
3365
3381
|
const routes = mergeSpaRoutes(context.dependencies, {
|
|
3366
3382
|
...createHealthRoutes(context.dependencies, {
|
|
3367
3383
|
pingOnHealth: true,
|
|
@@ -3375,6 +3391,7 @@ ${layers.extras.metrics ? ` metricsRuntime = createMetricsRuntime(options.run
|
|
|
3375
3391
|
...buildRoutes(context.dependencies),${metricsSpread}
|
|
3376
3392
|
}, {
|
|
3377
3393
|
distDirectory: join(import.meta.dir, "../../frontend/dist"),
|
|
3394
|
+
wrap: (handler) => documentKernel.wrapPublicDocument(handler),
|
|
3378
3395
|
});
|
|
3379
3396
|
|
|
3380
3397
|
return { context, routes, config: appConfig };
|
|
@@ -3439,7 +3456,8 @@ function renderViewProvider(layers) {
|
|
|
3439
3456
|
return `import { join } from "node:path";
|
|
3440
3457
|
import { createViewProvider } from "@getstrata/bootstrap/providers/view";
|
|
3441
3458
|
|
|
3442
|
-
export default createViewProvider({
|
|
3459
|
+
export default createViewProvider({${layers.extras.mfa && htmlAuthKit(layers.auth) && (layers.frontend === "api" || layers.frontend === "spa-react") ? `
|
|
3460
|
+
enabled: true,` : ""}
|
|
3443
3461
|
directory: join(import.meta.dir, "../../../views"),
|
|
3444
3462
|
data: {
|
|
3445
3463
|
userKey: "currentUser",${layers.extras.oidc ? `
|
|
@@ -4455,9 +4473,9 @@ function renderPackageJson(projectName, options = {}) {
|
|
|
4455
4473
|
"@getstrata/cli": "workspace:*",
|
|
4456
4474
|
"@getstrata/core": "workspace:*"
|
|
4457
4475
|
} : {
|
|
4458
|
-
"@getstrata/bootstrap": "^2.2.
|
|
4459
|
-
"@getstrata/cli": "^2.2.
|
|
4460
|
-
"@getstrata/core": "^2.2.
|
|
4476
|
+
"@getstrata/bootstrap": "^2.2.53",
|
|
4477
|
+
"@getstrata/cli": "^2.2.53",
|
|
4478
|
+
"@getstrata/core": "^2.2.53"
|
|
4461
4479
|
};
|
|
4462
4480
|
coreDeps.eta = "^4.6.0";
|
|
4463
4481
|
if (options.layers?.database === "mysql") {
|
|
@@ -5170,7 +5188,7 @@ function sqlParams(database, count) {
|
|
|
5170
5188
|
}
|
|
5171
5189
|
function renderDispatchOutboundWebhookJob(layers) {
|
|
5172
5190
|
const values = sqlParams(layers.database, 6);
|
|
5173
|
-
return `import { Job } from "@getstrata/core/queue";
|
|
5191
|
+
return `import { Job, type JobContext } from "@getstrata/core/queue";
|
|
5174
5192
|
import { isProductionEnv } from "@getstrata/core/runtime/appEnv";
|
|
5175
5193
|
import { webhookSignatureHeader } from "@getstrata/core/runtime/appKeyPrefix";
|
|
5176
5194
|
import { safeFetch } from "@getstrata/core/security/safeFetch";
|
|
@@ -5189,10 +5207,11 @@ interface DispatchOutboundWebhookPayload {
|
|
|
5189
5207
|
class DispatchOutboundWebhookJob extends Job<DispatchOutboundWebhookPayload> {
|
|
5190
5208
|
static readonly jobName = "webhook.dispatch";
|
|
5191
5209
|
|
|
5192
|
-
override async handle(payload: DispatchOutboundWebhookPayload): Promise<void> {
|
|
5210
|
+
override async handle(payload: DispatchOutboundWebhookPayload, context?: JobContext): Promise<void> {
|
|
5193
5211
|
const allowPrivate = !isProductionEnv() && process.env.WEBHOOK_ALLOW_PRIVATE === "true";
|
|
5194
5212
|
let responseStatus: number | null = null;
|
|
5195
5213
|
let error: string | null = null;
|
|
5214
|
+
let failure: Error | undefined;
|
|
5196
5215
|
try {
|
|
5197
5216
|
const response = await safeFetch(
|
|
5198
5217
|
payload.url,
|
|
@@ -5203,23 +5222,35 @@ class DispatchOutboundWebhookJob extends Job<DispatchOutboundWebhookPayload> {
|
|
|
5203
5222
|
[webhookSignatureHeader()]: signWebhookBody(payload.secret, payload.body),
|
|
5204
5223
|
},
|
|
5205
5224
|
body: payload.body,
|
|
5225
|
+
signal: context?.signal,
|
|
5206
5226
|
},
|
|
5207
5227
|
{ timeoutMs: 10_000, maxRedirects: 0, allowPrivate, allowHttp: allowPrivate },
|
|
5208
5228
|
);
|
|
5209
5229
|
responseStatus = response.status;
|
|
5210
5230
|
if (!response.ok) {
|
|
5211
5231
|
error = \`HTTP \${response.status}\`;
|
|
5232
|
+
failure = new Error(error);
|
|
5212
5233
|
}
|
|
5234
|
+
// Only the status is needed. Explicit cancellation releases safeFetch's
|
|
5235
|
+
// transport/body deadline even when the receiver sends an unread body.
|
|
5236
|
+
await response.body?.cancel();
|
|
5213
5237
|
} catch (caught) {
|
|
5214
5238
|
error = caught instanceof Error ? caught.message : "webhook dispatch failed";
|
|
5239
|
+
failure = caught instanceof Error ? caught : new Error(error, { cause: caught });
|
|
5215
5240
|
}
|
|
5216
5241
|
|
|
5217
|
-
await runWithMigrationBypass(async () => {
|
|
5242
|
+
try { await runWithMigrationBypass(async () => {
|
|
5218
5243
|
await getSql().unsafe(
|
|
5219
5244
|
"INSERT INTO webhook_deliveries (webhook_id, event, payload, response_status, attempt, error) VALUES (${values})",
|
|
5220
|
-
[payload.webhookId, payload.event, payload.body, responseStatus, 1, error],
|
|
5245
|
+
[payload.webhookId, payload.event, payload.body, responseStatus, context?.attempt ?? 1, error],
|
|
5221
5246
|
);
|
|
5222
|
-
});
|
|
5247
|
+
}); } catch (recordingFailure) {
|
|
5248
|
+
if (failure) throw new AggregateError([failure, recordingFailure], \`Webhook delivery failed: \${failure.message}; recording also failed.\`, { cause: failure });
|
|
5249
|
+
throw recordingFailure;
|
|
5250
|
+
}
|
|
5251
|
+
// Every transport/non-2xx failure uses the selected queue's bounded retry
|
|
5252
|
+
// and terminal recovery policy. Delivery remains at-least-once.
|
|
5253
|
+
if (failure) throw failure;
|
|
5223
5254
|
}
|
|
5224
5255
|
}
|
|
5225
5256
|
|
|
@@ -5240,12 +5271,15 @@ function registerNoteCreatedWebhookListener(): void {
|
|
|
5240
5271
|
url: string;
|
|
5241
5272
|
secret: string;
|
|
5242
5273
|
active: number | boolean;
|
|
5243
|
-
|
|
5274
|
+
events: string;
|
|
5275
|
+
}>("SELECT id, url, secret, active, events FROM webhooks");
|
|
5244
5276
|
const queue = resolveApplicationQueue();
|
|
5245
5277
|
const event = "notes.created";
|
|
5246
5278
|
const body = JSON.stringify({ event, data: payload });
|
|
5247
5279
|
for (const hook of rows) {
|
|
5248
|
-
|
|
5280
|
+
// The generated schema supports a literal event name or the explicit
|
|
5281
|
+
// wildcard. Other values do not implicitly subscribe to every event.
|
|
5282
|
+
if (!hook.active || (hook.events !== "*" && hook.events !== event)) {
|
|
5249
5283
|
continue;
|
|
5250
5284
|
}
|
|
5251
5285
|
await queue.dispatch(new DispatchOutboundWebhookJob(), {
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
"check": "tsc --noEmit"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@getstrata/bootstrap": "^2.2.
|
|
16
|
-
"@getstrata/cli": "^2.2.
|
|
17
|
-
"@getstrata/core": "^2.2.
|
|
15
|
+
"@getstrata/bootstrap": "^2.2.53",
|
|
16
|
+
"@getstrata/cli": "^2.2.53",
|
|
17
|
+
"@getstrata/core": "^2.2.53",
|
|
18
18
|
"eta": "^4.6.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|