@cosmicdrift/kumiko-bundled-features 0.415.0 → 0.416.0
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/agent-tools/tool-catalog.js +20 -1
- package/dist/agent-tools/tool-dispatch.js +14 -5
- package/dist/channel-whatsapp/constants.d.ts +1 -0
- package/dist/channel-whatsapp/constants.js +1 -0
- package/dist/channel-whatsapp/graph-client.d.ts +2 -0
- package/dist/channel-whatsapp/graph-client.js +29 -2
- package/dist/handbook-knowledge/constants.d.ts +6 -0
- package/dist/handbook-knowledge/constants.js +6 -0
- package/dist/handbook-knowledge/feature.d.ts +9 -0
- package/dist/handbook-knowledge/feature.js +17 -0
- package/dist/handbook-knowledge/handbook-index.d.ts +26 -0
- package/dist/handbook-knowledge/handbook-index.js +51 -0
- package/dist/handbook-knowledge/handlers.d.ts +28 -0
- package/dist/handbook-knowledge/handlers.js +67 -0
- package/dist/handbook-knowledge/index.d.ts +3 -0
- package/dist/handbook-knowledge/index.js +2 -0
- package/dist/signature/entity.d.ts +552 -0
- package/dist/signature/entity.js +156 -0
- package/dist/signature/feature.d.ts +3 -0
- package/dist/signature/feature.js +91 -0
- package/dist/signature/files.d.ts +23 -0
- package/dist/signature/files.js +64 -0
- package/dist/signature/grant.d.ts +32 -0
- package/dist/signature/grant.js +44 -0
- package/dist/signature/handlers/anonymous.write.d.ts +14 -0
- package/dist/signature/handlers/anonymous.write.js +52 -0
- package/dist/signature/handlers/cancel.write.d.ts +6 -0
- package/dist/signature/handlers/cancel.write.js +30 -0
- package/dist/signature/handlers/issue-link.write.d.ts +9 -0
- package/dist/signature/handlers/issue-link.write.js +57 -0
- package/dist/signature/handlers/lifecycle.write.d.ts +16 -0
- package/dist/signature/handlers/lifecycle.write.js +70 -0
- package/dist/signature/handlers/link-internal.write.d.ts +23 -0
- package/dist/signature/handlers/link-internal.write.js +186 -0
- package/dist/signature/handlers/request.write.d.ts +19 -0
- package/dist/signature/handlers/request.write.js +67 -0
- package/dist/signature/handlers/revoke-link.write.d.ts +6 -0
- package/dist/signature/handlers/revoke-link.write.js +18 -0
- package/dist/signature/handlers/sign-in-person.write.d.ts +8 -0
- package/dist/signature/handlers/sign-in-person.write.js +52 -0
- package/dist/signature/i18n.d.ts +6 -0
- package/dist/signature/i18n.js +49 -0
- package/dist/signature/index.d.ts +5 -0
- package/dist/signature/index.js +2 -0
- package/dist/signature/lib.d.ts +54 -0
- package/dist/signature/lib.js +68 -0
- package/dist/signature/options.d.ts +31 -0
- package/dist/signature/options.js +5 -0
- package/dist/signature/payloads.d.ts +60 -0
- package/dist/signature/payloads.js +53 -0
- package/dist/signature/png.d.ts +11 -0
- package/dist/signature/png.js +159 -0
- package/dist/signature/screens.d.ts +3 -0
- package/dist/signature/screens.js +95 -0
- package/dist/signature/signature-page.d.ts +25 -0
- package/dist/signature/signature-page.js +62 -0
- package/dist/signature/signing.d.ts +19 -0
- package/dist/signature/signing.js +46 -0
- package/dist/signature/static-sign.d.ts +17 -0
- package/dist/signature/static-sign.js +44 -0
- package/dist/signature/sweep.d.ts +3 -0
- package/dist/signature/sweep.js +113 -0
- package/dist/signature/user-data.d.ts +3 -0
- package/dist/signature/user-data.js +135 -0
- package/dist/signature/web/constants.d.ts +69 -0
- package/dist/signature/web/constants.js +68 -0
- package/dist/signature/web/index.d.ts +3 -0
- package/dist/signature/web/index.js +2 -0
- package/dist/signature/web/pad.d.ts +20 -0
- package/dist/signature/web/pad.js +104 -0
- package/dist/signature/web/signature-pad.d.ts +19 -0
- package/dist/signature/web/signature-pad.js +36 -0
- package/dist/subscription-stripe/feature.d.ts +6 -0
- package/dist/subscription-stripe/feature.js +3 -0
- package/dist/subscription-stripe/plugin-methods.d.ts +3 -1
- package/dist/subscription-stripe/plugin-methods.js +2 -1
- package/package.json +43 -11
- package/src/agent-tools/changes.json +6 -0
- package/src/channel-whatsapp/changes.json +6 -0
- package/src/handbook-knowledge/changes.json +8 -0
- package/src/signature/changes.json +15 -0
- package/src/subscription-stripe/changes.json +6 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { createEntity, createEntityExecutor, createLongTextField, createNumberField, createSelectField, createTextField, createTimestampField, } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { SIGNATURE_LEVEL, SIGNATURE_METHOD, SIGNATURE_REQUEST_ENTITY, SIGNATURE_REQUEST_STATUS, SIGNATURE_SIGNER_ENTITY, SIGNATURE_SIGNER_STATUS, } from "./web/constants.js";
|
|
3
|
+
const LEVEL_OPTIONS = [SIGNATURE_LEVEL.Simple];
|
|
4
|
+
const REQUEST_STATUS_OPTIONS = [
|
|
5
|
+
SIGNATURE_REQUEST_STATUS.Open,
|
|
6
|
+
SIGNATURE_REQUEST_STATUS.AllSigned,
|
|
7
|
+
SIGNATURE_REQUEST_STATUS.Completed,
|
|
8
|
+
SIGNATURE_REQUEST_STATUS.Declined,
|
|
9
|
+
SIGNATURE_REQUEST_STATUS.Expired,
|
|
10
|
+
SIGNATURE_REQUEST_STATUS.Cancelled,
|
|
11
|
+
];
|
|
12
|
+
const SIGNER_STATUS_OPTIONS = [
|
|
13
|
+
SIGNATURE_SIGNER_STATUS.Pending,
|
|
14
|
+
SIGNATURE_SIGNER_STATUS.Viewed,
|
|
15
|
+
SIGNATURE_SIGNER_STATUS.Signed,
|
|
16
|
+
SIGNATURE_SIGNER_STATUS.Declined,
|
|
17
|
+
];
|
|
18
|
+
const METHOD_OPTIONS = [SIGNATURE_METHOD.InPerson, SIGNATURE_METHOD.Link];
|
|
19
|
+
export const DEFAULT_SIGNATURE_RETENTION_KEEP_FOR = "10y";
|
|
20
|
+
// A signer has no user account, so the signer row itself is the crypto-shredding subject for the
|
|
21
|
+
// name, email, signature image and the audit values (IP, user agent) stored with the signature.
|
|
22
|
+
// find: "none" except the email, which the user-data hook looks up by exact match.
|
|
23
|
+
const SIGNER_PERSONAL = { personal: "self", find: "none" };
|
|
24
|
+
export function createSignatureRequestEntity(retention = { keepFor: DEFAULT_SIGNATURE_RETENTION_KEEP_FOR }) {
|
|
25
|
+
return createEntity({
|
|
26
|
+
table: "read_signature_requests",
|
|
27
|
+
description: "A request to sign one PDF document by several people, on a display in person or through a link, tracked through open, all-signed, completed, declined, expired and cancelled; the document is referenced by file id and hash and never changed.",
|
|
28
|
+
retention: { keepFor: retention.keepFor, strategy: "hardDelete", reference: "createdAt" },
|
|
29
|
+
fields: {
|
|
30
|
+
documentFileId: createTextField({
|
|
31
|
+
required: true,
|
|
32
|
+
personal: false,
|
|
33
|
+
reason: "technical_reference",
|
|
34
|
+
maxLength: 64,
|
|
35
|
+
}),
|
|
36
|
+
documentSha256: createTextField({
|
|
37
|
+
required: true,
|
|
38
|
+
personal: false,
|
|
39
|
+
reason: "is_document_hash",
|
|
40
|
+
maxLength: 64,
|
|
41
|
+
}),
|
|
42
|
+
title: createTextField({
|
|
43
|
+
required: true,
|
|
44
|
+
personal: false,
|
|
45
|
+
reason: "is_business_data",
|
|
46
|
+
searchable: true,
|
|
47
|
+
}),
|
|
48
|
+
level: createSelectField({ options: LEVEL_OPTIONS, required: true, filterable: true }),
|
|
49
|
+
subjectType: createTextField({
|
|
50
|
+
required: true,
|
|
51
|
+
personal: false,
|
|
52
|
+
reason: "technical_reference",
|
|
53
|
+
filterable: true,
|
|
54
|
+
maxLength: 100,
|
|
55
|
+
}),
|
|
56
|
+
subjectId: createTextField({
|
|
57
|
+
required: true,
|
|
58
|
+
personal: false,
|
|
59
|
+
reason: "technical_reference",
|
|
60
|
+
maxLength: 100,
|
|
61
|
+
}),
|
|
62
|
+
status: createSelectField({
|
|
63
|
+
options: REQUEST_STATUS_OPTIONS,
|
|
64
|
+
required: true,
|
|
65
|
+
default: SIGNATURE_REQUEST_STATUS.Open,
|
|
66
|
+
filterable: true,
|
|
67
|
+
sortable: true,
|
|
68
|
+
optionTones: {
|
|
69
|
+
[SIGNATURE_REQUEST_STATUS.Open]: "warn",
|
|
70
|
+
[SIGNATURE_REQUEST_STATUS.AllSigned]: "accent",
|
|
71
|
+
[SIGNATURE_REQUEST_STATUS.Completed]: "ok",
|
|
72
|
+
[SIGNATURE_REQUEST_STATUS.Declined]: "bad",
|
|
73
|
+
[SIGNATURE_REQUEST_STATUS.Expired]: "neutral",
|
|
74
|
+
[SIGNATURE_REQUEST_STATUS.Cancelled]: "neutral",
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
expiresAt: createTimestampField({
|
|
78
|
+
required: true,
|
|
79
|
+
personal: false,
|
|
80
|
+
reason: "is_deadline",
|
|
81
|
+
sortable: true,
|
|
82
|
+
}),
|
|
83
|
+
completedFileId: createTextField({
|
|
84
|
+
personal: false,
|
|
85
|
+
reason: "technical_reference",
|
|
86
|
+
maxLength: 64,
|
|
87
|
+
}),
|
|
88
|
+
completedAt: createTimestampField({
|
|
89
|
+
personal: false,
|
|
90
|
+
reason: "is_business_timestamp",
|
|
91
|
+
sortable: true,
|
|
92
|
+
}),
|
|
93
|
+
},
|
|
94
|
+
indexes: [
|
|
95
|
+
{ columns: ["tenantId", "status"] },
|
|
96
|
+
{ columns: ["tenantId", "subjectType", "subjectId"] },
|
|
97
|
+
],
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
export function createSignatureSignerEntity(retention = { keepFor: DEFAULT_SIGNATURE_RETENTION_KEEP_FOR }) {
|
|
101
|
+
return createEntity({
|
|
102
|
+
table: "read_signature_signers",
|
|
103
|
+
description: "One person who has to sign a signature request, with their way of signing (in person or link), the signature image and the audit values stored when they signed or declined; name, email, image, IP address and user agent are the signer's personal data.",
|
|
104
|
+
retention: { keepFor: retention.keepFor, strategy: "hardDelete", reference: "createdAt" },
|
|
105
|
+
fields: {
|
|
106
|
+
requestId: createTextField({
|
|
107
|
+
required: true,
|
|
108
|
+
personal: false,
|
|
109
|
+
reason: "technical_reference",
|
|
110
|
+
maxLength: 64,
|
|
111
|
+
}),
|
|
112
|
+
name: createTextField({ required: true, ...SIGNER_PERSONAL }),
|
|
113
|
+
role: createTextField({
|
|
114
|
+
personal: false,
|
|
115
|
+
reason: "is_business_data",
|
|
116
|
+
maxLength: 100,
|
|
117
|
+
}),
|
|
118
|
+
contactId: createTextField({
|
|
119
|
+
personal: false,
|
|
120
|
+
reason: "pseudonymous_fk",
|
|
121
|
+
maxLength: 64,
|
|
122
|
+
}),
|
|
123
|
+
email: createTextField({ personal: "self", find: "exact", maxLength: 320 }),
|
|
124
|
+
status: createSelectField({
|
|
125
|
+
options: SIGNER_STATUS_OPTIONS,
|
|
126
|
+
required: true,
|
|
127
|
+
default: SIGNATURE_SIGNER_STATUS.Pending,
|
|
128
|
+
filterable: true,
|
|
129
|
+
sortable: true,
|
|
130
|
+
}),
|
|
131
|
+
method: createSelectField({ options: METHOD_OPTIONS }),
|
|
132
|
+
signedAt: createTimestampField({
|
|
133
|
+
personal: false,
|
|
134
|
+
reason: "is_business_timestamp",
|
|
135
|
+
sortable: true,
|
|
136
|
+
}),
|
|
137
|
+
imageFileId: createTextField({ ...SIGNER_PERSONAL, maxLength: 64 }),
|
|
138
|
+
linkAnchor: createTextField({
|
|
139
|
+
personal: false,
|
|
140
|
+
reason: "is_grant_anchor",
|
|
141
|
+
maxLength: 64,
|
|
142
|
+
}),
|
|
143
|
+
linkSentAt: createTimestampField({ personal: false, reason: "is_business_timestamp" }),
|
|
144
|
+
viewedAt: createTimestampField({ personal: false, reason: "is_business_timestamp" }),
|
|
145
|
+
viewCount: createNumberField({ default: 0 }),
|
|
146
|
+
signedFromIp: createTextField({ ...SIGNER_PERSONAL, maxLength: 64 }),
|
|
147
|
+
signedUserAgent: createTextField({ ...SIGNER_PERSONAL, maxLength: 500 }),
|
|
148
|
+
declineReason: createLongTextField({ personal: "self", find: "none" }),
|
|
149
|
+
},
|
|
150
|
+
indexes: [{ columns: ["tenantId", "requestId"] }],
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
export const signatureRequestEntity = createSignatureRequestEntity();
|
|
154
|
+
export const signatureSignerEntity = createSignatureSignerEntity();
|
|
155
|
+
export const { executor: requestExecutor, table: signatureRequestTable } = createEntityExecutor(SIGNATURE_REQUEST_ENTITY, signatureRequestEntity);
|
|
156
|
+
export const { executor: signerExecutor, table: signatureSignerTable } = createEntityExecutor(SIGNATURE_SIGNER_ENTITY, signatureSignerEntity);
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { defineEntityDetailHandler, defineEntityListHandler, defineFeature, EXT_USER_DATA, i18nKey, } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { createSignatureRequestEntity, createSignatureSignerEntity } from "./entity.js";
|
|
3
|
+
import { createAnonymousLinkHandlers } from "./handlers/anonymous.write.js";
|
|
4
|
+
import { createCancelHandler } from "./handlers/cancel.write.js";
|
|
5
|
+
import { createIssueLinkHandler } from "./handlers/issue-link.write.js";
|
|
6
|
+
import { expireHandler, finishHandler, reconcileHandler } from "./handlers/lifecycle.write.js";
|
|
7
|
+
import { createLinkInternalHandlers } from "./handlers/link-internal.write.js";
|
|
8
|
+
import { createRequestHandler } from "./handlers/request.write.js";
|
|
9
|
+
import { createRevokeLinkHandler } from "./handlers/revoke-link.write.js";
|
|
10
|
+
import { createSignInPersonHandler } from "./handlers/sign-in-person.write.js";
|
|
11
|
+
import { SIGNATURE_I18N } from "./i18n.js";
|
|
12
|
+
import { DEFAULT_SIGNATURE_ACCESS } from "./lib.js";
|
|
13
|
+
import { LINK_RATE_LIMIT } from "./options.js";
|
|
14
|
+
import { SignatureCompletedEventSchema, SignatureDeclinedEventSchema, SignatureExpiredEventSchema, } from "./payloads.js";
|
|
15
|
+
import { createSignatureDetailScreen, createSignatureListScreen } from "./screens.js";
|
|
16
|
+
import { sweepSignatureRequests } from "./sweep.js";
|
|
17
|
+
import { signatureDeleteHook, signatureExportHook } from "./user-data.js";
|
|
18
|
+
import { SIGNATURE_FEATURE, SIGNATURE_REQUEST_ENTITY, SIGNATURE_SIGNER_ENTITY, } from "./web/constants.js";
|
|
19
|
+
export function createSignatureFeature(opts) {
|
|
20
|
+
const signedInAccess = opts.access ?? DEFAULT_SIGNATURE_ACCESS;
|
|
21
|
+
const requestEntity = createSignatureRequestEntity(opts.retention);
|
|
22
|
+
const signerEntity = createSignatureSignerEntity(opts.retention);
|
|
23
|
+
const anonymous = createAnonymousLinkHandlers(LINK_RATE_LIMIT);
|
|
24
|
+
const link = createLinkInternalHandlers(opts.linkSecret);
|
|
25
|
+
return defineFeature(SIGNATURE_FEATURE, (r) => {
|
|
26
|
+
r.describe("Signature requests: a signed-in user asks several people to sign one PDF of the tenant. Each signer draws a signature on a display in person or through a link in the URL fragment (14 days, limited opens, revocable, single-use, one answer for every rejection). The last signature moves the request to all-signed; a job renders a signature page through the injected print function, appends it to the unchanged original and completes the request. Events completed, declined and expired report the outcome; signer name, email, image, IP and user agent are personal data.");
|
|
27
|
+
r.uiHints({ displayLabel: "Signatures", category: "data", recommended: false });
|
|
28
|
+
r.requires({ features: ["files", "user-data-rights"] });
|
|
29
|
+
r.adoptsBundledLists();
|
|
30
|
+
r.translations({ keys: SIGNATURE_I18N });
|
|
31
|
+
r.entity({ name: "signatureRequest", ...requestEntity });
|
|
32
|
+
r.entity({ name: "signatureSigner", ...signerEntity });
|
|
33
|
+
r.defineEvent("completed", SignatureCompletedEventSchema, {
|
|
34
|
+
piiFields: "none",
|
|
35
|
+
});
|
|
36
|
+
r.defineEvent("declined", SignatureDeclinedEventSchema, {
|
|
37
|
+
piiFields: "none",
|
|
38
|
+
});
|
|
39
|
+
r.defineEvent("expired", SignatureExpiredEventSchema, {
|
|
40
|
+
piiFields: "none",
|
|
41
|
+
});
|
|
42
|
+
r.writeHandler(createRequestHandler(signedInAccess));
|
|
43
|
+
r.writeHandler(createSignInPersonHandler(signedInAccess));
|
|
44
|
+
r.writeHandler(createIssueLinkHandler(signedInAccess, opts));
|
|
45
|
+
r.writeHandler(createRevokeLinkHandler(signedInAccess));
|
|
46
|
+
r.writeHandler(createCancelHandler(signedInAccess));
|
|
47
|
+
r.writeHandler(anonymous.openByLink);
|
|
48
|
+
r.writeHandler(anonymous.signByLink);
|
|
49
|
+
r.writeHandler(anonymous.declineByLink);
|
|
50
|
+
r.writeHandler(link.linkOpen);
|
|
51
|
+
r.writeHandler(link.linkSign);
|
|
52
|
+
r.writeHandler(link.linkDecline);
|
|
53
|
+
r.writeHandler(finishHandler);
|
|
54
|
+
r.writeHandler(expireHandler);
|
|
55
|
+
r.writeHandler(reconcileHandler);
|
|
56
|
+
r.queryHandler(defineEntityListHandler(SIGNATURE_REQUEST_ENTITY, requestEntity, {
|
|
57
|
+
access: signedInAccess,
|
|
58
|
+
description: "Lists signature requests with status, deadline and the object they belong to; paging, search over the title and sorting.",
|
|
59
|
+
}));
|
|
60
|
+
r.queryHandler(defineEntityDetailHandler(SIGNATURE_REQUEST_ENTITY, requestEntity, {
|
|
61
|
+
access: signedInAccess,
|
|
62
|
+
description: "Returns one signature request.",
|
|
63
|
+
}));
|
|
64
|
+
r.queryHandler(defineEntityListHandler(SIGNATURE_SIGNER_ENTITY, signerEntity, {
|
|
65
|
+
access: signedInAccess,
|
|
66
|
+
agent: { expose: false },
|
|
67
|
+
description: "Lists the signers of requests with their status and way of signing; carries the signers' personal data.",
|
|
68
|
+
}));
|
|
69
|
+
r.useExtension(EXT_USER_DATA, SIGNATURE_SIGNER_ENTITY, {
|
|
70
|
+
export: signatureExportHook,
|
|
71
|
+
delete: signatureDeleteHook,
|
|
72
|
+
});
|
|
73
|
+
r.job({
|
|
74
|
+
name: "sweep",
|
|
75
|
+
trigger: { cron: "*/5 * * * *" },
|
|
76
|
+
perTenant: true,
|
|
77
|
+
concurrency: "skip",
|
|
78
|
+
handler: (_payload, ctx) => sweepSignatureRequests(ctx, opts),
|
|
79
|
+
});
|
|
80
|
+
r.screen(createSignatureListScreen(signedInAccess));
|
|
81
|
+
r.screen(createSignatureDetailScreen(signedInAccess));
|
|
82
|
+
r.nav({
|
|
83
|
+
id: "signature",
|
|
84
|
+
label: i18nKey("signature.nav.signature"),
|
|
85
|
+
screen: "signature:screen:signature-list",
|
|
86
|
+
icon: "file",
|
|
87
|
+
order: 40,
|
|
88
|
+
access: signedInAccess,
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type TenantDb } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import type { HandlerContext, Registry, SessionUser } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import { type CreatedFileRef } from "@cosmicdrift/kumiko-framework/files";
|
|
4
|
+
import type { WriteResult } from "@cosmicdrift/kumiko-types/handlers";
|
|
5
|
+
import { type FileRefContent } from "../file-foundation/index.ts";
|
|
6
|
+
export declare const PDF_MIME_TYPE = "application/pdf";
|
|
7
|
+
type FileContext = Pick<GeneratedFileContext, "db" | "services">;
|
|
8
|
+
export type GeneratedFileContext = {
|
|
9
|
+
readonly db: TenantDb;
|
|
10
|
+
readonly services: HandlerContext["services"];
|
|
11
|
+
readonly registry: Registry;
|
|
12
|
+
};
|
|
13
|
+
export declare function readTenantFile(ctx: FileContext, tenantId: string, fileRefId: string): Promise<FileRefContent | null>;
|
|
14
|
+
export declare function storeGeneratedFile(ctx: GeneratedFileContext, user: SessionUser, file: {
|
|
15
|
+
readonly fileName: string;
|
|
16
|
+
readonly mimeType: string;
|
|
17
|
+
readonly data: Uint8Array;
|
|
18
|
+
}): Promise<WriteResult<CreatedFileRef>>;
|
|
19
|
+
export declare function signedDocumentUrl(ctx: FileContext, user: SessionUser, fileRefId: string): Promise<string | null>;
|
|
20
|
+
export declare function sha256Hex(bytes: Uint8Array): string;
|
|
21
|
+
export declare function discardGeneratedFile(ctx: GeneratedFileContext, user: SessionUser, file: CreatedFileRef): Promise<void>;
|
|
22
|
+
export declare function deleteFileRef(db: TenantDb, user: SessionUser, id: string): Promise<void>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { createEventStoreExecutor, selectMany, } from "@cosmicdrift/kumiko-framework/db";
|
|
3
|
+
import { createFileRefFromBytes, fileRefEntity, fileRefsTable, } from "@cosmicdrift/kumiko-framework/files";
|
|
4
|
+
import { FileRefTooLargeError, fileContextForTenant, readFileRefContent, } from "../file-foundation/index.js";
|
|
5
|
+
export const PDF_MIME_TYPE = "application/pdf";
|
|
6
|
+
const MAX_DOCUMENT_BYTES = 25 * 1024 * 1024;
|
|
7
|
+
const SIGNED_URL_SECONDS = 15 * 60;
|
|
8
|
+
// The tenant's file refs only: a caller cannot point a request at another tenant's file.
|
|
9
|
+
export async function readTenantFile(ctx, tenantId, fileRefId) {
|
|
10
|
+
try {
|
|
11
|
+
return await readFileRefContent({
|
|
12
|
+
db: ctx.db,
|
|
13
|
+
filesForTenant: (ownerTenantId) => fileContextForTenant(ctx, tenantId, ownerTenantId),
|
|
14
|
+
fileRefId,
|
|
15
|
+
allowedTenantIds: [tenantId],
|
|
16
|
+
maxBytes: MAX_DOCUMENT_BYTES,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (error instanceof FileRefTooLargeError)
|
|
21
|
+
return null;
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function storeGeneratedFile(ctx, user, file) {
|
|
26
|
+
return createFileRefFromBytes({ ...ctx, user }, file);
|
|
27
|
+
}
|
|
28
|
+
// Only the opening page of a link gets a URL, and a short-lived one. Providers without presigned
|
|
29
|
+
// URLs (filesystem) yield null; the page then shows the document another way or not at all.
|
|
30
|
+
export async function signedDocumentUrl(ctx, user, fileRefId) {
|
|
31
|
+
const resolver = ctx.services._fileProviderResolver;
|
|
32
|
+
if (resolver === undefined)
|
|
33
|
+
return null;
|
|
34
|
+
const [row] = await selectMany(ctx.db, fileRefsTable, {
|
|
35
|
+
id: fileRefId,
|
|
36
|
+
tenantId: user.tenantId,
|
|
37
|
+
isDeleted: false,
|
|
38
|
+
});
|
|
39
|
+
if (row === undefined)
|
|
40
|
+
return null;
|
|
41
|
+
const provider = await resolver(user.tenantId);
|
|
42
|
+
if (provider.getSignedUrl === undefined)
|
|
43
|
+
return null;
|
|
44
|
+
return provider.getSignedUrl(row.storageKey, SIGNED_URL_SECONDS, {
|
|
45
|
+
contentDisposition: 'inline; filename="document.pdf"',
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
export function sha256Hex(bytes) {
|
|
49
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
50
|
+
}
|
|
51
|
+
const fileRefExecutor = createEventStoreExecutor(fileRefsTable, fileRefEntity, {
|
|
52
|
+
entityName: "fileRef",
|
|
53
|
+
});
|
|
54
|
+
// For a stored file whose owner row was never written (a lost sign race): removes ref and bytes so
|
|
55
|
+
// no signature image of a person who did not sign stays behind. The ledger entry is not touched:
|
|
56
|
+
// this runs inside the transaction that already took it, and a second connection deleting that
|
|
57
|
+
// row would wait on the lock this transaction holds.
|
|
58
|
+
export async function discardGeneratedFile(ctx, user, file) {
|
|
59
|
+
await deleteFileRef(ctx.db, user, file.id);
|
|
60
|
+
await ctx.services.files?.ref(file.storageKey).delete();
|
|
61
|
+
}
|
|
62
|
+
export async function deleteFileRef(db, user, id) {
|
|
63
|
+
await fileRefExecutor.delete({ id }, user, db);
|
|
64
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type TenantId } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { Temporal } from "@cosmicdrift/kumiko-types/temporal";
|
|
3
|
+
import { type RowBoundGrantResult, redeemRowBoundGrant } from "../shared/index.ts";
|
|
4
|
+
type AnchorCommit = Parameters<typeof redeemRowBoundGrant>[0]["commitAnchor"];
|
|
5
|
+
export type SignatureLinkSubject = {
|
|
6
|
+
readonly tenantId: TenantId;
|
|
7
|
+
readonly signerId: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function signatureLinkSubject(parts: {
|
|
10
|
+
readonly tenantId: string;
|
|
11
|
+
readonly signerId: string;
|
|
12
|
+
}): string;
|
|
13
|
+
export declare function parseSignatureLinkSubject(subject: string): SignatureLinkSubject | null;
|
|
14
|
+
export declare function peekSignatureLinkSubject(token: string): SignatureLinkSubject | null;
|
|
15
|
+
export declare function signSignatureLinkToken(args: {
|
|
16
|
+
readonly tenantId: string;
|
|
17
|
+
readonly signerId: string;
|
|
18
|
+
readonly linkAnchor: string;
|
|
19
|
+
readonly secret: string;
|
|
20
|
+
readonly now: Temporal.Instant;
|
|
21
|
+
}): {
|
|
22
|
+
readonly token: string;
|
|
23
|
+
readonly expiresAt: Temporal.Instant;
|
|
24
|
+
};
|
|
25
|
+
export declare function redeemSignatureLinkToken(args: {
|
|
26
|
+
readonly token: string;
|
|
27
|
+
readonly secret: string;
|
|
28
|
+
readonly now: Temporal.Instant;
|
|
29
|
+
readonly loadLinkAnchor: (signerId: string) => Promise<string | null>;
|
|
30
|
+
readonly commitAnchor: AnchorCommit;
|
|
31
|
+
}): Promise<RowBoundGrantResult>;
|
|
32
|
+
export type { RowBoundGrantResult };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isUuid, parseTenantId } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { peekTokenSubject, redeemRowBoundGrant, signRowBoundGrant, } from "../shared/index.js";
|
|
3
|
+
import { SIGNATURE_LINK_TTL_DAYS } from "./web/constants.js";
|
|
4
|
+
const SIGNATURE_LINK_PURPOSE = "signature-link";
|
|
5
|
+
const MINUTES_PER_DAY = 24 * 60;
|
|
6
|
+
export function signatureLinkSubject(parts) {
|
|
7
|
+
return `${parts.tenantId}_${parts.signerId}`;
|
|
8
|
+
}
|
|
9
|
+
export function parseSignatureLinkSubject(subject) {
|
|
10
|
+
const [rawTenantId, signerId, ...rest] = subject.split("_");
|
|
11
|
+
const tenantId = parseTenantId(rawTenantId);
|
|
12
|
+
if (rest.length > 0 || tenantId === null || !isUuid(signerId))
|
|
13
|
+
return null;
|
|
14
|
+
return { tenantId, signerId };
|
|
15
|
+
}
|
|
16
|
+
// Reads the unverified subject only to pick the tenant to dispatch into; the grant is still
|
|
17
|
+
// verified against the signer's current anchor there.
|
|
18
|
+
export function peekSignatureLinkSubject(token) {
|
|
19
|
+
const subject = peekTokenSubject(token);
|
|
20
|
+
return subject === null ? null : parseSignatureLinkSubject(subject);
|
|
21
|
+
}
|
|
22
|
+
export function signSignatureLinkToken(args) {
|
|
23
|
+
return signRowBoundGrant({
|
|
24
|
+
subject: signatureLinkSubject(args),
|
|
25
|
+
purpose: SIGNATURE_LINK_PURPOSE,
|
|
26
|
+
anchor: args.linkAnchor,
|
|
27
|
+
ttlMinutes: SIGNATURE_LINK_TTL_DAYS * MINUTES_PER_DAY,
|
|
28
|
+
secret: args.secret,
|
|
29
|
+
now: args.now,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export function redeemSignatureLinkToken(args) {
|
|
33
|
+
return redeemRowBoundGrant({
|
|
34
|
+
token: args.token,
|
|
35
|
+
purpose: SIGNATURE_LINK_PURPOSE,
|
|
36
|
+
secret: args.secret,
|
|
37
|
+
now: args.now,
|
|
38
|
+
commitAnchor: args.commitAnchor,
|
|
39
|
+
loadAnchor: (subject) => {
|
|
40
|
+
const parsed = parseSignatureLinkSubject(subject);
|
|
41
|
+
return parsed === null ? Promise.resolve(null) : args.loadLinkAnchor(parsed.signerId);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RateLimitDeclaration } from "@cosmicdrift/kumiko-types/handlers";
|
|
2
|
+
export declare function createAnonymousLinkHandlers(rateLimit: RateLimitDeclaration): {
|
|
3
|
+
openByLink: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"open-by-link", import("zod").ZodObject<{
|
|
4
|
+
token: import("zod").ZodString;
|
|
5
|
+
}, import("zod/v4/core").$strip>, unknown, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
6
|
+
signByLink: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"sign-by-link", import("zod").ZodObject<{
|
|
7
|
+
token: import("zod").ZodString;
|
|
8
|
+
imagePngBase64: import("zod").ZodString;
|
|
9
|
+
}, import("zod/v4/core").$strip>, unknown, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
10
|
+
declineByLink: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"decline-by-link", import("zod").ZodObject<{
|
|
11
|
+
token: import("zod").ZodString;
|
|
12
|
+
reason: import("zod").ZodOptional<import("zod").ZodString>;
|
|
13
|
+
}, import("zod/v4/core").$strip>, unknown, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
14
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createSystemUser, defineWriteHandler, } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { peekSignatureLinkSubject } from "../grant.js";
|
|
3
|
+
import { invalidLink } from "../lib.js";
|
|
4
|
+
import { DeclineByLinkSchema, OpenByLinkSchema, SignByLinkSchema } from "../payloads.js";
|
|
5
|
+
import { decodeSignatureImage, invalidImage } from "../signing.js";
|
|
6
|
+
import { SignatureInternalHandlers } from "../web/constants.js";
|
|
7
|
+
const TENANT_SWITCH_REASON = "the link holder arrives anonymously in the default tenant while the signer lives in the request's tenant; the tenant comes from the signed link subject and the internal handler verifies the grant there via ctx.dispatch.writeAs";
|
|
8
|
+
// One answer for every rejection, so a caller cannot tell forged from expired, revoked, used up or already signed.
|
|
9
|
+
async function forwardIntoLinkTenant(ctx, token, qn, payload) {
|
|
10
|
+
const subject = peekSignatureLinkSubject(token);
|
|
11
|
+
if (subject === null)
|
|
12
|
+
return invalidLink();
|
|
13
|
+
const result = await ctx.dispatch.writeAs(createSystemUser(subject.tenantId), qn, payload);
|
|
14
|
+
return result.isSuccess ? result : invalidLink();
|
|
15
|
+
}
|
|
16
|
+
export function createAnonymousLinkHandlers(rateLimit) {
|
|
17
|
+
const openByLink = defineWriteHandler({
|
|
18
|
+
name: "open-by-link",
|
|
19
|
+
schema: OpenByLinkSchema,
|
|
20
|
+
access: { roles: ["anonymous"], anySignedIn: true },
|
|
21
|
+
rateLimit,
|
|
22
|
+
escapeHatch: { grants: ["systemIdentity"], reason: TENANT_SWITCH_REASON },
|
|
23
|
+
agent: { expose: false },
|
|
24
|
+
description: "Anonymous: opens a signing link and returns the title, the signer's name and role and a short-lived document URL. Does not consume the link, counts the view up to a limit.",
|
|
25
|
+
handler: (event, ctx) => forwardIntoLinkTenant(ctx, event.payload.token, SignatureInternalHandlers.linkOpen, event.payload),
|
|
26
|
+
});
|
|
27
|
+
const signByLink = defineWriteHandler({
|
|
28
|
+
name: "sign-by-link",
|
|
29
|
+
schema: SignByLinkSchema,
|
|
30
|
+
access: { roles: ["anonymous"], personalData: "public-intake", anySignedIn: true },
|
|
31
|
+
rateLimit,
|
|
32
|
+
escapeHatch: { grants: ["systemIdentity"], reason: TENANT_SWITCH_REASON },
|
|
33
|
+
agent: { expose: false },
|
|
34
|
+
description: "Anonymous: signs through a link with a drawn PNG signature, exactly once; the link is consumed by the signature.",
|
|
35
|
+
handler: async (event, ctx) => {
|
|
36
|
+
if (decodeSignatureImage(event.payload.imagePngBase64) === null)
|
|
37
|
+
return invalidImage();
|
|
38
|
+
return forwardIntoLinkTenant(ctx, event.payload.token, SignatureInternalHandlers.linkSign, event.payload);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const declineByLink = defineWriteHandler({
|
|
42
|
+
name: "decline-by-link",
|
|
43
|
+
schema: DeclineByLinkSchema,
|
|
44
|
+
access: { roles: ["anonymous"], personalData: "public-intake", anySignedIn: true },
|
|
45
|
+
rateLimit,
|
|
46
|
+
escapeHatch: { grants: ["systemIdentity"], reason: TENANT_SWITCH_REASON },
|
|
47
|
+
agent: { expose: false },
|
|
48
|
+
description: "Anonymous: declines to sign through a link with an optional reason; the request becomes declined and every link of it stops working.",
|
|
49
|
+
handler: (event, ctx) => forwardIntoLinkTenant(ctx, event.payload.token, SignatureInternalHandlers.linkDecline, event.payload),
|
|
50
|
+
});
|
|
51
|
+
return { openByLink, signByLink, declineByLink };
|
|
52
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
export declare function createCancelHandler(access: AccessRule): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"cancel", import("zod").ZodObject<{
|
|
3
|
+
requestId: import("zod").ZodUUID;
|
|
4
|
+
}, import("zod/v4/core").$strip>, {
|
|
5
|
+
readonly cancelled: true;
|
|
6
|
+
}, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { signerExecutor } from "../entity.js";
|
|
3
|
+
import { isUnsignedSigner, listSigners, setRequestStatus } from "../lib.js";
|
|
4
|
+
import { RequestIdSchema } from "../payloads.js";
|
|
5
|
+
import { SIGNATURE_REQUEST_STATUS } from "../web/constants.js";
|
|
6
|
+
// Cancelling moves the request out of `open`, which already makes every link of it fail; the
|
|
7
|
+
// anchors are cleared as well so no token verifies against a cancelled request.
|
|
8
|
+
export function createCancelHandler(access) {
|
|
9
|
+
return defineWriteHandler({
|
|
10
|
+
name: "cancel",
|
|
11
|
+
schema: RequestIdSchema,
|
|
12
|
+
access,
|
|
13
|
+
agent: { risk: "mid" },
|
|
14
|
+
description: "Cancels an open signature request; all its signing links stop working. Signatures already given stay on record.",
|
|
15
|
+
handler: async (event, ctx) => {
|
|
16
|
+
const user = event.user;
|
|
17
|
+
const moved = await setRequestStatus(ctx, user, event.payload.requestId, SIGNATURE_REQUEST_STATUS.Open, { status: SIGNATURE_REQUEST_STATUS.Cancelled });
|
|
18
|
+
if (!moved.isSuccess)
|
|
19
|
+
return moved;
|
|
20
|
+
for (const signer of await listSigners(ctx, user, event.payload.requestId)) {
|
|
21
|
+
if (!isUnsignedSigner(signer) || signer.linkAnchor == null)
|
|
22
|
+
continue;
|
|
23
|
+
const cleared = await signerExecutor.update({ id: signer.id, changes: { linkAnchor: null } }, user, ctx.db, { skipOptimisticLock: true });
|
|
24
|
+
if (!cleared.isSuccess)
|
|
25
|
+
return cleared;
|
|
26
|
+
}
|
|
27
|
+
return { isSuccess: true, data: { cancelled: true } };
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { SignatureOptions } from "../options.ts";
|
|
3
|
+
export type IssueLinkData = {
|
|
4
|
+
readonly signerId: string;
|
|
5
|
+
readonly linkSentAt: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function createIssueLinkHandler(access: AccessRule, opts: Pick<SignatureOptions, "linkSecret" | "deliverLink" | "signaturePageUrl">): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"issue-link", import("zod").ZodObject<{
|
|
8
|
+
signerId: import("zod").ZodUUID;
|
|
9
|
+
}, import("zod/v4/core").$strip>, IssueLinkData, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { NotFoundError, UnprocessableError, writeFailure, } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import { generateId } from "@cosmicdrift/kumiko-framework/utils";
|
|
4
|
+
import { signerExecutor } from "../entity.js";
|
|
5
|
+
import { signSignatureLinkToken } from "../grant.js";
|
|
6
|
+
import { isOpenRequest, isUnsignedSigner, loadRequestOfSigner } from "../lib.js";
|
|
7
|
+
import { SignerIdSchema } from "../payloads.js";
|
|
8
|
+
// A new link replaces the previous one: the anchor moves on, so the old token stops verifying.
|
|
9
|
+
export function createIssueLinkHandler(access, opts) {
|
|
10
|
+
return defineWriteHandler({
|
|
11
|
+
name: "issue-link",
|
|
12
|
+
schema: SignerIdSchema,
|
|
13
|
+
access,
|
|
14
|
+
agent: { risk: "mid" },
|
|
15
|
+
description: "Sends a signing link to one signer of an open request, to the signer's own address; the link is never part of the result. An earlier link of that signer stops working. The link is valid for 14 days and a limited number of opens.",
|
|
16
|
+
handler: async (event, ctx) => {
|
|
17
|
+
const user = event.user;
|
|
18
|
+
const signer = await signerExecutor.detail({ id: event.payload.signerId }, user, ctx.db);
|
|
19
|
+
if (signer === null)
|
|
20
|
+
return writeFailure(new NotFoundError("signer", event.payload.signerId));
|
|
21
|
+
const request = await loadRequestOfSigner(ctx, user, signer);
|
|
22
|
+
const now = ctx.tz.now();
|
|
23
|
+
if (request === null || !isOpenRequest(request, now) || !isUnsignedSigner(signer)) {
|
|
24
|
+
return writeFailure(new UnprocessableError("link_not_possible"));
|
|
25
|
+
}
|
|
26
|
+
if (typeof signer.email !== "string" || signer.email === "") {
|
|
27
|
+
return writeFailure(new UnprocessableError("signer_without_email"));
|
|
28
|
+
}
|
|
29
|
+
const linkAnchor = generateId();
|
|
30
|
+
const updated = await signerExecutor.update({
|
|
31
|
+
id: signer.id,
|
|
32
|
+
changes: { linkAnchor, linkSentAt: now, viewCount: 0 },
|
|
33
|
+
}, user, ctx.db, { skipOptimisticLock: true });
|
|
34
|
+
if (!updated.isSuccess)
|
|
35
|
+
return updated;
|
|
36
|
+
const grant = signSignatureLinkToken({
|
|
37
|
+
tenantId: user.tenantId,
|
|
38
|
+
signerId: signer.id,
|
|
39
|
+
linkAnchor,
|
|
40
|
+
secret: opts.linkSecret,
|
|
41
|
+
now,
|
|
42
|
+
});
|
|
43
|
+
await opts.deliverLink(ctx, {
|
|
44
|
+
signerId: String(signer.id),
|
|
45
|
+
requestId: String(request.id),
|
|
46
|
+
name: signer.name,
|
|
47
|
+
email: signer.email,
|
|
48
|
+
url: `${opts.signaturePageUrl}#${grant.token}`,
|
|
49
|
+
issuedBy: user,
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
isSuccess: true,
|
|
53
|
+
data: { signerId: String(signer.id), linkSentAt: now.toString() },
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const finishHandler: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"finish", import("zod").ZodObject<{
|
|
2
|
+
requestId: import("zod").ZodUUID;
|
|
3
|
+
completedFileId: import("zod").ZodUUID;
|
|
4
|
+
}, import("zod/v4/core").$strip>, {
|
|
5
|
+
finished: boolean;
|
|
6
|
+
}, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
7
|
+
export declare const expireHandler: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"expire", import("zod").ZodObject<{
|
|
8
|
+
requestId: import("zod").ZodUUID;
|
|
9
|
+
}, import("zod/v4/core").$strip>, {
|
|
10
|
+
expired: boolean;
|
|
11
|
+
}, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
12
|
+
export declare const reconcileHandler: import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"reconcile", import("zod").ZodObject<{
|
|
13
|
+
requestId: import("zod").ZodUUID;
|
|
14
|
+
}, import("zod/v4/core").$strip>, {
|
|
15
|
+
advanced: boolean;
|
|
16
|
+
}, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|