Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import type { TFunction } from "i18next";
|
||||
import { useActionData } from "react-router";
|
||||
import { ActionFunction, LoaderFunctionArgs, MetaFunction } from "react-router";
|
||||
import { getUserByEmail, updateUserVerifyToken } from "~/utils/db/users.db.server";
|
||||
@@ -19,6 +18,12 @@ import {
|
||||
} from "~/services/auth/password-reset-token.server";
|
||||
import { withMetrics } from "~/lib/metrics/ssr";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
import { loadRequiredWebConfig } from "@hivegrid/config";
|
||||
import {
|
||||
AdminFallbackLoginRejected,
|
||||
assertAdminFallbackLoginAllowedForIdentity,
|
||||
} from "~/services/auth/admin-fallback.server";
|
||||
import { writeAdminSsoAuditEvent } from "~/modules/admin-sso/audit.server";
|
||||
|
||||
export const meta: MetaFunction<typeof loader> = ({ data }) => data?.metatags || [];
|
||||
export const loader = withMetrics("forgot-password", async ({ request }: LoaderFunctionArgs) => {
|
||||
@@ -34,25 +39,30 @@ type ActionData = {
|
||||
error?: string;
|
||||
};
|
||||
const badRequest = (data: ActionData) => Response.json(data, { status: 400 });
|
||||
const translateActionText = (t: TFunction, key: string, fallback: string) => {
|
||||
const translated = t(key, { defaultValue: fallback });
|
||||
return translated === key ? fallback : translated;
|
||||
};
|
||||
const FORGOT_PASSWORD_RESPONSE_FLOOR_MS = 1500;
|
||||
|
||||
async function waitForForgotPasswordResponseFloor(startedAt: number): Promise<void> {
|
||||
const remaining = FORGOT_PASSWORD_RESPONSE_FLOOR_MS - (Date.now() - startedAt);
|
||||
if (remaining > 0) {
|
||||
await new Promise((resolve) => setTimeout(resolve, remaining));
|
||||
}
|
||||
}
|
||||
|
||||
export const action: ActionFunction = async ({ request }) => {
|
||||
const { t } = await getTranslations(request);
|
||||
await validateCSRFToken(request);
|
||||
const form = await request.formData();
|
||||
const email = form.get("email")?.toString().toLowerCase().trim();
|
||||
const mode = form.get("mode")?.toString() === "magic" ? "magic" : "reset";
|
||||
const emailSentMessage = translateActionText(t, "account.reset.emailSent", "Email sent");
|
||||
const emailSentMessage = t("account.reset.emailSent");
|
||||
|
||||
if (!email) {
|
||||
return badRequest({
|
||||
error: translateActionText(t, "account.reset.errors.emailRequired", "Email required"),
|
||||
error: t("account.reset.errors.emailRequired"),
|
||||
});
|
||||
}
|
||||
|
||||
const rateLimit = checkAuthRateLimit({
|
||||
const rateLimit = await checkAuthRateLimit({
|
||||
request,
|
||||
action: "forgot-password",
|
||||
identifier: email,
|
||||
@@ -63,14 +73,35 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
return Response.json({ success: emailSentMessage }, { headers: { "Retry-After": String(rateLimit.retryAfterSeconds) } });
|
||||
}
|
||||
|
||||
const startedAt = Date.now();
|
||||
if (mode === "magic" && !loadRequiredWebConfig().magicLink.enabled) {
|
||||
await waitForForgotPasswordResponseFloor(startedAt);
|
||||
return Response.json({ success: emailSentMessage });
|
||||
}
|
||||
|
||||
const user = await getUserByEmail(email);
|
||||
if (!user) {
|
||||
// Anti-enumeration: same shape of success response for unknown emails.
|
||||
await new Promise((resolve) => setTimeout(resolve, 1500));
|
||||
// Anti-enumeration: same shape and minimum latency for unknown emails.
|
||||
await waitForForgotPasswordResponseFloor(startedAt);
|
||||
return Response.json({ success: emailSentMessage });
|
||||
}
|
||||
|
||||
if (mode === "magic") {
|
||||
try {
|
||||
assertAdminFallbackLoginAllowedForIdentity(user);
|
||||
} catch (error) {
|
||||
if (!(error instanceof AdminFallbackLoginRejected)) {
|
||||
throw error;
|
||||
}
|
||||
await writeAdminSsoAuditEvent({
|
||||
action: "admin.login.fallback.failure",
|
||||
actorAdminUserId: user.admin?.userId,
|
||||
request,
|
||||
meta: { reason: error.reason, email, method: "magic-link", stage: "issue" },
|
||||
});
|
||||
await waitForForgotPasswordResponseFloor(startedAt);
|
||||
return Response.json({ success: emailSentMessage });
|
||||
}
|
||||
await issueMagicLink({
|
||||
request,
|
||||
userId: user.id,
|
||||
@@ -78,6 +109,7 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
name: user.firstName,
|
||||
purpose: "login",
|
||||
});
|
||||
await waitForForgotPasswordResponseFloor(startedAt);
|
||||
return Response.json({ success: emailSentMessage });
|
||||
}
|
||||
|
||||
@@ -99,11 +131,14 @@ export const action: ActionFunction = async ({ request }) => {
|
||||
alias: "password-reset",
|
||||
subject: emailTemplate.subject,
|
||||
body: emailTemplate.body,
|
||||
text: emailTemplate.text,
|
||||
userId: user.id,
|
||||
data: emailData,
|
||||
priorityOverride: "critical",
|
||||
});
|
||||
|
||||
await waitForForgotPasswordResponseFloor(startedAt);
|
||||
|
||||
return Response.json({
|
||||
success: emailSentMessage,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user