Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Check identity again at each signature, hold the settings row while saving, keep the signature line on the page
Claude7976d91e6826
diff --git a/src/app/api/admin/settings/route.ts b/src/app/api/admin/settings/route.tsindex 0a11914..3eef699 100644--- a/src/app/api/admin/settings/route.ts+++ b/src/app/api/admin/settings/route.ts@@ -19,11 +19,7 @@ import {getRequestUserAgent,} from "@/lib/security/request";import { recordSystemAuditEvent } from "@/lib/services/system-audit";-import {- getSettings,- readStoredSettings,- resolveSettings,-} from "@/lib/settings";+import { readStoredSettings, resolveSettings } from "@/lib/settings";// The Organization tab in Settings. Administrators only.@@ -51,12 +47,19 @@ export async function PUT(request: Request) {const body: unknown = await request.json().catch(() => null);try {- const current = await getSettings();-// The change and its line in the install's audit trail land together.await withTransaction(async (tx) => {+ // Settings are one row. Holding it while the change is judged and+ // written keeps a Part 11 switch made meanwhile from being undone, or+ // a retention period from being set just as the mode goes on.+ await tx.query(`SELECT id FROM app_settings WHERE id FOR UPDATE`);+const before = await readStoredSettings(tx);- const after = await updateOrganizationSettings(body ?? {}, current, tx);+ const after = await updateOrganizationSettings(+ body ?? {},+ resolveSettings(before),+ tx,+ );const changes = diffSettings(before, after);if (Object.keys(changes).length > 0) {diff --git a/src/lib/part11.dbtest.ts b/src/lib/part11.dbtest.tsindex ee9af8f..9691fe9 100644--- a/src/lib/part11.dbtest.ts+++ b/src/lib/part11.dbtest.ts@@ -47,8 +47,9 @@ async function createUser(role: "admin" | "user" = "user") {createdUsers.push(id);await database.query(`- insert into public."user" (id, name, email, role, "emailVerified")- values ($1, $2, $3, $4, true)+ insert into public."user" (+ id, name, email, role, "emailVerified", identity_verified_at+ ) values ($1, $2, $3, $4, true, 1)`,[id, `Person ${id.slice(0, 4)}`, `${id}@example.test`, role],);@@ -273,3 +274,25 @@ test("a data request does not erase a finished Part 11 envelope", async () => {assert.equal(kept.rowCount, 1);await database.query(`delete from envelopes where id = $1`, [envelopeId]);});++test("an identity confirmation taken back after send stops the signature", async () => {+ const user = await createUser();++ await database.query(+ `update "user" set identity_verified_at = null where id = $1`,+ [user.id],+ );++ assert.deepEqual(+ await checkSigningCredentials(+ { email: user.email, password: "right", userId: user.id },+ { database, verifyPassword },+ ),+ {+ attemptsLeft: MAX_SIGNING_PASSWORD_ATTEMPTS,+ identityNotConfirmed: true,+ locked: false,+ ok: false,+ },+ );+});diff --git a/src/lib/part11.ts b/src/lib/part11.tsindex aa8b647..1cca82c 100644--- a/src/lib/part11.ts+++ b/src/lib/part11.ts@@ -312,7 +312,17 @@ async function defaultPasswordVerifier(input: {}export type SigningPasswordResult =- { ok: true } | { attemptsLeft: number; locked: boolean; ok: false };+ | { ok: true }+ | {+ attemptsLeft: number;+ /**+ * An administrator took back the identity confirmation, or the email+ * address is no longer confirmed, since the envelope was sent.+ */+ identityNotConfirmed?: true;+ locked: boolean;+ ok: false;+ };/*** The two parts of a Part 11 signature: the account's email address (its@@ -341,8 +351,10 @@ export async function checkSigningCredentials(const outcome = await withTransaction(async (tx) => {const account = await queryOne<{email: string;+ emailVerified: boolean;failedAttempts: number;hash: string | null;+ identityVerifiedAt: number | null;lockedAt: number | null;name: string;}>(@@ -353,6 +365,8 @@ export async function checkSigningCredentials(u.name,u.signing_failed_attempts AS "failedAttempts",u.signing_locked_at AS "lockedAt",+ u."emailVerified" AS "emailVerified",+ u.identity_verified_at AS "identityVerifiedAt",(SELECT a.password FROM account aWHERE a."userId" = u.id AND a."providerId" = 'credential'@@ -373,6 +387,20 @@ export async function checkSigningCredentials(return { attemptsLeft: 0, locked: true, ok: false as const };}+ // Checked again at every signature: what was true at send may not be+ // now. Not a wrong password, so it does not count towards the lock.+ if (!account.emailVerified || account.identityVerifiedAt === null) {+ return {+ attemptsLeft: Math.max(+ 0,+ MAX_SIGNING_PASSWORD_ATTEMPTS - account.failedAttempts,+ ),+ identityNotConfirmed: true as const,+ locked: false,+ ok: false as const,+ };+ }+const emailMatches =input.email.trim().toLowerCase() === account.email.toLowerCase();const passwordMatches =@@ -443,6 +471,9 @@ export async function checkSigningCredentials(return {attemptsLeft: outcome.attemptsLeft,+ ...("identityNotConfirmed" in outcome && outcome.identityNotConfirmed+ ? { identityNotConfirmed: true as const }+ : {}),locked: outcome.locked,ok: false,};diff --git a/src/lib/services/pdf-finalize.ts b/src/lib/services/pdf-finalize.tsindex 16a5566..8d7dd6b 100644--- a/src/lib/services/pdf-finalize.ts+++ b/src/lib/services/pdf-finalize.ts@@ -535,23 +535,52 @@ function stampSignatureManifestation(font: TextFace,text: string,): void {+ const pageWidth = page.getWidth();const rect = normalizedRectToPdfRect(field, {heightPt: page.getHeight(),- widthPt: page.getWidth(),+ widthPt: pageWidth,});- const maxWidth = Math.max(- rect.widthPt,- Math.min(240, page.getWidth() - rect.xPt - 4),+ const margin = 2;+ const fullWidth = pageWidth - margin * 2;+ const minimumSize = 5;+ // One line when it fits at a readable size, otherwise one line per part+ // (name, time, meaning), so nothing is shrunk away or runs off the page.+ const oneLineSize = fitTextSize(font, text, fullWidth, 7, 6.5);+ const lines =+ font.widthOfTextAtSize(text, oneLineSize) <= fullWidth &&+ oneLineSize >= minimumSize+ ? [text]+ : text.split(" | ");+ const size = Math.max(+ minimumSize,+ Math.min(+ ...lines.map((line) => fitTextSize(font, line, fullWidth, 7, 6.5)),+ ),);- const size = fitTextSize(font, text, maxWidth, 7, 6.5);- const below = rect.yPt - size - 1.5;- const y = below >= 2 ? below : rect.yPt + rect.heightPt + 1.5;+ const lineHeight = size + 1.5;+ const blockHeight = lineHeight * lines.length;+ const below = rect.yPt - blockHeight;+ // The top line's baseline. Below the field when there is room, above it+ // at the foot of the page.+ const top =+ below >= margin+ ? rect.yPt - lineHeight+ : Math.min(+ rect.yPt + rect.heightPt + blockHeight - size,+ page.getHeight() - size - margin,+ );- font.drawText(page, text, {- color: rgb(0.2, 0.2, 0.24),- size,- x: rect.xPt,- y,+ lines.forEach((line, index) => {+ const width = font.widthOfTextAtSize(line, size);+ // Starts under the field, moved left if it would run off the page.+ const x = Math.max(margin, Math.min(rect.xPt, pageWidth - width - margin));++ font.drawText(page, line, {+ color: rgb(0.2, 0.2, 0.24),+ size,+ x,+ y: top - index * lineHeight,+ });});}diff --git a/src/lib/signing.ts b/src/lib/signing.tsindex ab0e5af..2650c12 100644--- a/src/lib/signing.ts+++ b/src/lib/signing.ts@@ -1360,8 +1360,13 @@ export async function sendSignerEmailCode(*/function describeCredentialFailure(result: {attemptsLeft: number;+ identityNotConfirmed?: true;locked: boolean;}): string {+ if (result.identityNotConfirmed) {+ return "Your account cannot sign this document right now: an administrator has to confirm your identity, and your email address must be confirmed. Ask the sender or an administrator.";+ }+if (result.locked) {return `This account is locked for signing after ${MAX_SIGNING_PASSWORD_ATTEMPTS} wrong passwords. Ask an administrator to unlock it.`;}