Files

45 lines
2.1 KiB
TypeScript

// Standalone password manager — reveal ONE secret field of ONE entry. Resource
// route (action only): POST { entryId, field } -> { value }. Every reveal is
// access-checked + audited in the service BEFORE the value is returned. The UI
// copies the value and clears the clipboard after 30s (PASSWORD_SAFE_COPY_AUTO_
// CLEAR_MS). Tenant envelope crypto fails closed with the M002 catalog error
// when key material is unavailable.
import { type ActionFunctionArgs } from "react-router";
import { badRequestResponse, hrActionErrorResponse, loadHrContext, notFoundResponse, requireHrPermission } from "~/utils/integrations/hrAccess.server";
import { copyEntryValue, revealEntryValue, type PasswordSafeSecretField } from "~/utils/integrations/hrPasswordSafeService.server";
import { validateCSRFToken } from "~/utils/session.server";
const SECRET_FIELDS: ReadonlySet<string> = new Set<PasswordSafeSecretField>(["username", "password", "notes"]);
export const action = async ({ request, params }: ActionFunctionArgs) => {
const ctx = await loadHrContext({ request, params, requireModule: true });
requireHrPermission(ctx, "hr.passwordsafe.use");
const vaultId = params.vaultId ?? "";
const form = await request.formData();
await validateCSRFToken(request, { formData: form });
const entryId = form.get("entryId")?.toString() ?? "";
const field = form.get("field")?.toString() ?? "";
const intent = form.get("intent")?.toString() ?? "";
if (!entryId || !SECRET_FIELDS.has(field) || !["reveal", "copy"].includes(intent)) {
throw badRequestResponse();
}
try {
const command = intent === "copy" ? copyEntryValue : revealEntryValue;
const value = await command({ tenantId: ctx.tenantId, vaultId, entryId, userId: ctx.userId, field: field as PasswordSafeSecretField });
return Response.json({ ok: true, value });
} catch (error) {
if (error instanceof Response) {
throw error;
}
return hrActionErrorResponse(error);
}
};
// A direct GET on the reveal endpoint is meaningless and must never leak a value.
export const loader = () => {
throw notFoundResponse();
};