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
+88
-4
@@ -7,10 +7,25 @@ import { getAbsoluteUrl } from "~/utils/url.server";
|
||||
import { queueTransactionalEmail } from "~/modules/emails/services/TransactionalEmailQueue.server";
|
||||
import { getAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { getMailTranslator, getMailTranslatorFromRequest } from "~/locale/mail-translator.server";
|
||||
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { localizedMailChrome } from "~/modules/emails/utils/LocalizedMailChrome";
|
||||
|
||||
export const MAGIC_LINK_TOKEN_TTL_MINUTES = 15;
|
||||
const MAGIC_LINK_TOKEN_VERSION = "v1";
|
||||
|
||||
export class MagicLinkDisabledError extends Error {
|
||||
constructor() {
|
||||
super("Magic-link authentication is disabled.");
|
||||
this.name = "MagicLinkDisabledError";
|
||||
}
|
||||
}
|
||||
|
||||
function assertMagicLinkEnabled(): void {
|
||||
if (!loadRequiredWebConfig().magicLink.enabled) {
|
||||
throw new MagicLinkDisabledError();
|
||||
}
|
||||
}
|
||||
|
||||
export type MagicLinkPurpose = "login" | "invitation" | "reset";
|
||||
|
||||
type MagicLinkPayload = {
|
||||
@@ -158,6 +173,29 @@ export async function issueMagicLink({
|
||||
redirectPath?: string;
|
||||
now?: Date;
|
||||
purpose?: MagicLinkPurpose;
|
||||
}) {
|
||||
// Keep the provider flag at the side-effect boundary as defense in depth:
|
||||
// no caller can create a token or enqueue mail while the provider is off.
|
||||
assertMagicLinkEnabled();
|
||||
return withUserRlsContext(userId, () => issueMagicLinkScoped({ request, userId, email, name, redirectPath, now, purpose }));
|
||||
}
|
||||
|
||||
async function issueMagicLinkScoped({
|
||||
request,
|
||||
userId,
|
||||
email,
|
||||
name,
|
||||
redirectPath,
|
||||
now,
|
||||
purpose,
|
||||
}: {
|
||||
request: Request;
|
||||
userId: string;
|
||||
email: string;
|
||||
name?: string | null;
|
||||
redirectPath: string;
|
||||
now: Date;
|
||||
purpose: MagicLinkPurpose;
|
||||
}) {
|
||||
const token = createSignedMagicLinkToken({ userId, now, purpose });
|
||||
await db.magicLinkToken.create({
|
||||
@@ -174,19 +212,35 @@ export async function issueMagicLink({
|
||||
const mailTranslator = await getMagicLinkMailTranslator(request, userId);
|
||||
const appConfiguration = await getAppConfiguration({ request });
|
||||
const brand = buildBrand(resolveTransactionalBrandTokens(toBrandConfiguration(appConfiguration)));
|
||||
const { html } = renderTransactionalEmail("magic-link", {
|
||||
const copyVariables = {
|
||||
productName: brand.productName,
|
||||
name: name ?? "",
|
||||
expiresInMinutes: MAGIC_LINK_TOKEN_TTL_MINUTES,
|
||||
};
|
||||
const { html, text } = renderTransactionalEmail("magic-link", {
|
||||
brand,
|
||||
...localizedMailChrome(mailTranslator.t, mailTranslator.locale),
|
||||
recipientName: name ?? undefined,
|
||||
actionUrl,
|
||||
expiresInMinutes: MAGIC_LINK_TOKEN_TTL_MINUTES,
|
||||
subject: mailTranslator.t("magicLink.subject", copyVariables),
|
||||
previewText: mailTranslator.t("magicLink.previewText", copyVariables),
|
||||
eyebrow: mailTranslator.t("magicLink.eyebrow", copyVariables),
|
||||
title: mailTranslator.t(name ? "magicLink.title_named" : "magicLink.title", copyVariables),
|
||||
intro: mailTranslator.t("magicLink.intro", copyVariables),
|
||||
actionLabel: mailTranslator.t("magicLink.actionLabel", copyVariables),
|
||||
outro: mailTranslator.t("magicLink.ignoreIfUnrequested", copyVariables),
|
||||
expiresText: mailTranslator.t("magicLink.outro", copyVariables),
|
||||
signoffThanks: mailTranslator.t("common.signoff.thanks"),
|
||||
});
|
||||
const subject = mailTranslator.t("magicLink.subject", { productName: brand.productName });
|
||||
const subject = mailTranslator.t("magicLink.subject", copyVariables);
|
||||
|
||||
await queueTransactionalEmail({
|
||||
to: email,
|
||||
alias: "magic-link",
|
||||
subject,
|
||||
body: html,
|
||||
text,
|
||||
userId,
|
||||
data: emailData,
|
||||
priorityOverride: "critical",
|
||||
@@ -199,6 +253,11 @@ export async function issueMagicLink({
|
||||
}
|
||||
|
||||
export async function consumeMagicLinkToken(token: string, options: { now?: Date; expectedPurpose?: MagicLinkPurpose } = {}): Promise<MagicLinkConsumeResult> {
|
||||
// Disabled providers must not consume outstanding tokens. Returning the
|
||||
// generic invalid reason avoids leaking runtime configuration to clients.
|
||||
if (!loadRequiredWebConfig().magicLink.enabled) {
|
||||
return { ok: false, reason: "invalid" };
|
||||
}
|
||||
const now = options.now ?? new Date();
|
||||
const payload = verifySignedMagicLinkToken(token);
|
||||
if (!payload) {
|
||||
@@ -208,6 +267,15 @@ export async function consumeMagicLinkToken(token: string, options: { now?: Date
|
||||
return { ok: false, reason: "expired" };
|
||||
}
|
||||
|
||||
return runWithRlsContext({ userId: payload.sub }, () => consumeMagicLinkTokenScoped(token, payload, now, options));
|
||||
}
|
||||
|
||||
async function consumeMagicLinkTokenScoped(
|
||||
token: string,
|
||||
payload: MagicLinkPayload,
|
||||
now: Date,
|
||||
options: { expectedPurpose?: MagicLinkPurpose }
|
||||
): Promise<MagicLinkConsumeResult> {
|
||||
const tokenHash = hashToken(token);
|
||||
const storedToken = await db.magicLinkToken.findUnique({ where: { id: payload.jti } });
|
||||
if (!storedToken || storedToken.userId !== payload.sub || storedToken.tokenHash !== tokenHash) {
|
||||
@@ -227,10 +295,26 @@ export async function consumeMagicLinkToken(token: string, options: { now?: Date
|
||||
return { ok: false, reason: "purpose-mismatch" };
|
||||
}
|
||||
|
||||
await db.magicLinkToken.update({
|
||||
where: { id: payload.jti },
|
||||
const consumed = await db.magicLinkToken.updateMany({
|
||||
where: {
|
||||
id: payload.jti,
|
||||
userId: payload.sub,
|
||||
tokenHash,
|
||||
consumedAt: null,
|
||||
expiresAt: { gt: now },
|
||||
},
|
||||
data: { consumedAt: now },
|
||||
});
|
||||
if (consumed.count !== 1) {
|
||||
return { ok: false, reason: "replayed" };
|
||||
}
|
||||
|
||||
return { ok: true, userId: payload.sub, tokenId: payload.jti, purpose: actualPurpose };
|
||||
}
|
||||
|
||||
function withUserRlsContext<T>(userId: string, operation: () => T): T {
|
||||
if (getRlsExecutionContext()?.userId) {
|
||||
return operation();
|
||||
}
|
||||
return runWithRlsContext({ userId }, operation);
|
||||
}
|
||||
Reference in new issue
Block a user