@cosmicdrift/kumiko-bundled-features 0.416.0 → 0.417.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/helpdesk/screens.js +5 -1
- package/dist/signature/feature.js +17 -11
- package/dist/signature/handlers/cancel.write.d.ts +2 -1
- package/dist/signature/handlers/cancel.write.js +10 -3
- package/dist/signature/handlers/issue-link.write.d.ts +2 -1
- package/dist/signature/handlers/issue-link.write.js +4 -1
- package/dist/signature/handlers/request.write.d.ts +2 -1
- package/dist/signature/handlers/request.write.js +23 -4
- package/dist/signature/handlers/revoke-link.write.d.ts +2 -1
- package/dist/signature/handlers/revoke-link.write.js +10 -1
- package/dist/signature/handlers/sign-in-person.write.d.ts +2 -1
- package/dist/signature/handlers/sign-in-person.write.js +4 -1
- package/dist/signature/index.d.ts +1 -1
- package/dist/signature/options.d.ts +9 -0
- package/dist/signature/screens.js +2 -0
- package/dist/signature/static-sign.d.ts +2 -0
- package/dist/signature/static-sign.js +2 -1
- package/dist/signature/subject-queries.d.ts +5 -0
- package/dist/signature/subject-queries.js +113 -0
- package/dist/signature/subject-scope.d.ts +11 -0
- package/dist/signature/subject-scope.js +20 -0
- package/package.json +11 -11
- package/src/signature/changes.json +7 -0
package/dist/helpdesk/screens.js
CHANGED
|
@@ -286,7 +286,11 @@ function createRequestDetailScreen(opts, settings) {
|
|
|
286
286
|
field: "authorName",
|
|
287
287
|
label: `${HELPDESK_FEATURE}:entity:helpdesk-reply:field:authorName`,
|
|
288
288
|
},
|
|
289
|
-
{
|
|
289
|
+
{
|
|
290
|
+
field: "message",
|
|
291
|
+
label: `${HELPDESK_FEATURE}:entity:helpdesk-reply:field:message`,
|
|
292
|
+
multiline: true,
|
|
293
|
+
},
|
|
290
294
|
],
|
|
291
295
|
},
|
|
292
296
|
{
|
|
@@ -13,11 +13,14 @@ import { DEFAULT_SIGNATURE_ACCESS } from "./lib.js";
|
|
|
13
13
|
import { LINK_RATE_LIMIT } from "./options.js";
|
|
14
14
|
import { SignatureCompletedEventSchema, SignatureDeclinedEventSchema, SignatureExpiredEventSchema, } from "./payloads.js";
|
|
15
15
|
import { createSignatureDetailScreen, createSignatureListScreen } from "./screens.js";
|
|
16
|
+
import { scopeRequestDetail, scopeRequestList, scopeSignerList } from "./subject-queries.js";
|
|
17
|
+
import { createSubjectScope } from "./subject-scope.js";
|
|
16
18
|
import { sweepSignatureRequests } from "./sweep.js";
|
|
17
19
|
import { signatureDeleteHook, signatureExportHook } from "./user-data.js";
|
|
18
20
|
import { SIGNATURE_FEATURE, SIGNATURE_REQUEST_ENTITY, SIGNATURE_SIGNER_ENTITY, } from "./web/constants.js";
|
|
19
21
|
export function createSignatureFeature(opts) {
|
|
20
22
|
const signedInAccess = opts.access ?? DEFAULT_SIGNATURE_ACCESS;
|
|
23
|
+
const scope = createSubjectScope(opts);
|
|
21
24
|
const requestEntity = createSignatureRequestEntity(opts.retention);
|
|
22
25
|
const signerEntity = createSignatureSignerEntity(opts.retention);
|
|
23
26
|
const anonymous = createAnonymousLinkHandlers(LINK_RATE_LIMIT);
|
|
@@ -39,11 +42,11 @@ export function createSignatureFeature(opts) {
|
|
|
39
42
|
r.defineEvent("expired", SignatureExpiredEventSchema, {
|
|
40
43
|
piiFields: "none",
|
|
41
44
|
});
|
|
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));
|
|
45
|
+
r.writeHandler(createRequestHandler(signedInAccess, scope));
|
|
46
|
+
r.writeHandler(createSignInPersonHandler(signedInAccess, scope));
|
|
47
|
+
r.writeHandler(createIssueLinkHandler(signedInAccess, scope, opts));
|
|
48
|
+
r.writeHandler(createRevokeLinkHandler(signedInAccess, scope));
|
|
49
|
+
r.writeHandler(createCancelHandler(signedInAccess, scope));
|
|
47
50
|
r.writeHandler(anonymous.openByLink);
|
|
48
51
|
r.writeHandler(anonymous.signByLink);
|
|
49
52
|
r.writeHandler(anonymous.declineByLink);
|
|
@@ -53,19 +56,22 @@ export function createSignatureFeature(opts) {
|
|
|
53
56
|
r.writeHandler(finishHandler);
|
|
54
57
|
r.writeHandler(expireHandler);
|
|
55
58
|
r.writeHandler(reconcileHandler);
|
|
56
|
-
|
|
59
|
+
const requestList = defineEntityListHandler(SIGNATURE_REQUEST_ENTITY, requestEntity, {
|
|
57
60
|
access: signedInAccess,
|
|
58
61
|
description: "Lists signature requests with status, deadline and the object they belong to; paging, search over the title and sorting.",
|
|
59
|
-
})
|
|
60
|
-
|
|
62
|
+
});
|
|
63
|
+
const requestDetail = defineEntityDetailHandler(SIGNATURE_REQUEST_ENTITY, requestEntity, {
|
|
61
64
|
access: signedInAccess,
|
|
62
65
|
description: "Returns one signature request.",
|
|
63
|
-
})
|
|
64
|
-
|
|
66
|
+
});
|
|
67
|
+
const signerList = defineEntityListHandler(SIGNATURE_SIGNER_ENTITY, signerEntity, {
|
|
65
68
|
access: signedInAccess,
|
|
66
69
|
agent: { expose: false },
|
|
67
70
|
description: "Lists the signers of requests with their status and way of signing; carries the signers' personal data.",
|
|
68
|
-
})
|
|
71
|
+
});
|
|
72
|
+
r.queryHandler(scopeRequestList(requestList, scope));
|
|
73
|
+
r.queryHandler(scopeRequestDetail(requestDetail, scope));
|
|
74
|
+
r.queryHandler(scopeSignerList(signerList, scope));
|
|
69
75
|
r.useExtension(EXT_USER_DATA, SIGNATURE_SIGNER_ENTITY, {
|
|
70
76
|
export: signatureExportHook,
|
|
71
77
|
delete: signatureDeleteHook,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
-
|
|
2
|
+
import type { SubjectScope } from "../subject-scope.ts";
|
|
3
|
+
export declare function createCancelHandler(access: AccessRule, scope: SubjectScope): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"cancel", import("zod").ZodObject<{
|
|
3
4
|
requestId: import("zod").ZodUUID;
|
|
4
5
|
}, import("zod/v4/core").$strip>, {
|
|
5
6
|
readonly cancelled: true;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
-
import {
|
|
2
|
+
import { NotFoundError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import { requestExecutor, signerExecutor } from "../entity.js";
|
|
3
4
|
import { isUnsignedSigner, listSigners, setRequestStatus } from "../lib.js";
|
|
4
5
|
import { RequestIdSchema } from "../payloads.js";
|
|
5
|
-
import { SIGNATURE_REQUEST_STATUS } from "../web/constants.js";
|
|
6
|
+
import { SIGNATURE_REQUEST_ENTITY, SIGNATURE_REQUEST_STATUS } from "../web/constants.js";
|
|
6
7
|
// Cancelling moves the request out of `open`, which already makes every link of it fail; the
|
|
7
8
|
// anchors are cleared as well so no token verifies against a cancelled request.
|
|
8
|
-
export function createCancelHandler(access) {
|
|
9
|
+
export function createCancelHandler(access, scope) {
|
|
9
10
|
return defineWriteHandler({
|
|
10
11
|
name: "cancel",
|
|
11
12
|
schema: RequestIdSchema,
|
|
@@ -14,6 +15,12 @@ export function createCancelHandler(access) {
|
|
|
14
15
|
description: "Cancels an open signature request; all its signing links stop working. Signatures already given stay on record.",
|
|
15
16
|
handler: async (event, ctx) => {
|
|
16
17
|
const user = event.user;
|
|
18
|
+
if (scope.isScoped(user)) {
|
|
19
|
+
const request = await requestExecutor.detail({ id: event.payload.requestId }, user, ctx.db);
|
|
20
|
+
if (request === null || !(await scope.allowsRequest(ctx, user, request))) {
|
|
21
|
+
return writeFailure(new NotFoundError(SIGNATURE_REQUEST_ENTITY, event.payload.requestId));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
17
24
|
const moved = await setRequestStatus(ctx, user, event.payload.requestId, SIGNATURE_REQUEST_STATUS.Open, { status: SIGNATURE_REQUEST_STATUS.Cancelled });
|
|
18
25
|
if (!moved.isSuccess)
|
|
19
26
|
return moved;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
2
|
import type { SignatureOptions } from "../options.ts";
|
|
3
|
+
import type { SubjectScope } from "../subject-scope.ts";
|
|
3
4
|
export type IssueLinkData = {
|
|
4
5
|
readonly signerId: string;
|
|
5
6
|
readonly linkSentAt: string;
|
|
6
7
|
};
|
|
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
|
+
export declare function createIssueLinkHandler(access: AccessRule, scope: SubjectScope, opts: Pick<SignatureOptions, "linkSecret" | "deliverLink" | "signaturePageUrl">): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"issue-link", import("zod").ZodObject<{
|
|
8
9
|
signerId: import("zod").ZodUUID;
|
|
9
10
|
}, import("zod/v4/core").$strip>, IssueLinkData, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -6,7 +6,7 @@ import { signSignatureLinkToken } from "../grant.js";
|
|
|
6
6
|
import { isOpenRequest, isUnsignedSigner, loadRequestOfSigner } from "../lib.js";
|
|
7
7
|
import { SignerIdSchema } from "../payloads.js";
|
|
8
8
|
// A new link replaces the previous one: the anchor moves on, so the old token stops verifying.
|
|
9
|
-
export function createIssueLinkHandler(access, opts) {
|
|
9
|
+
export function createIssueLinkHandler(access, scope, opts) {
|
|
10
10
|
return defineWriteHandler({
|
|
11
11
|
name: "issue-link",
|
|
12
12
|
schema: SignerIdSchema,
|
|
@@ -19,6 +19,9 @@ export function createIssueLinkHandler(access, opts) {
|
|
|
19
19
|
if (signer === null)
|
|
20
20
|
return writeFailure(new NotFoundError("signer", event.payload.signerId));
|
|
21
21
|
const request = await loadRequestOfSigner(ctx, user, signer);
|
|
22
|
+
if (request !== null && !(await scope.allowsRequest(ctx, user, request))) {
|
|
23
|
+
return writeFailure(new NotFoundError("signer", event.payload.signerId));
|
|
24
|
+
}
|
|
22
25
|
const now = ctx.tz.now();
|
|
23
26
|
if (request === null || !isOpenRequest(request, now) || !isUnsignedSigner(signer)) {
|
|
24
27
|
return writeFailure(new UnprocessableError("link_not_possible"));
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { SubjectScope } from "../subject-scope.ts";
|
|
2
3
|
export type RequestSignatureData = {
|
|
3
4
|
readonly requestId: string;
|
|
4
5
|
readonly signerIds: readonly string[];
|
|
5
6
|
};
|
|
6
|
-
export declare function createRequestHandler(access: AccessRule): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"request", import("zod").ZodObject<{
|
|
7
|
+
export declare function createRequestHandler(access: AccessRule, scope: SubjectScope): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"request", import("zod").ZodObject<{
|
|
7
8
|
documentFileId: import("zod").ZodUUID;
|
|
8
9
|
title: import("zod").ZodString;
|
|
9
10
|
level: import("zod").ZodDefault<import("zod").ZodLiteral<"simple">>;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { selectMany } from "@cosmicdrift/kumiko-framework/db";
|
|
2
|
+
import { defineWriteHandler, isUuid, } from "@cosmicdrift/kumiko-framework/engine";
|
|
3
|
+
import { NotFoundError, UnprocessableError, writeFailure, } from "@cosmicdrift/kumiko-framework/errors";
|
|
4
|
+
import { canCallerReadFileRef, fileRefsTable, } from "@cosmicdrift/kumiko-framework/files";
|
|
3
5
|
import { PDFDocument } from "pdf-lib";
|
|
4
6
|
import { requestExecutor, signerExecutor } from "../entity.js";
|
|
5
7
|
import { readTenantFile, sha256Hex } from "../files.js";
|
|
6
8
|
import { RequestSignatureSchema } from "../payloads.js";
|
|
7
|
-
import { SIGNATURE_DEFAULT_VALIDITY_DAYS, SIGNATURE_REQUEST_STATUS, SIGNATURE_SIGNER_STATUS, } from "../web/constants.js";
|
|
9
|
+
import { SIGNATURE_DEFAULT_VALIDITY_DAYS, SIGNATURE_REQUEST_ENTITY, SIGNATURE_REQUEST_STATUS, SIGNATURE_SIGNER_STATUS, } from "../web/constants.js";
|
|
8
10
|
const MS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
9
11
|
// Encrypted or unparseable PDFs would fail later when the signature page is appended and leave the
|
|
10
12
|
// request stuck in all-signed.
|
|
@@ -17,7 +19,18 @@ async function isLoadablePdf(bytes) {
|
|
|
17
19
|
return false;
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
|
-
|
|
22
|
+
// A file the caller may not open through GET /files/:id answers like an unknown id, so ids cannot be probed.
|
|
23
|
+
async function isReadableTenantFile(ctx, fileRefId) {
|
|
24
|
+
if (!isUuid(fileRefId))
|
|
25
|
+
return false;
|
|
26
|
+
const [fileRef] = await selectMany(ctx.db, fileRefsTable, {
|
|
27
|
+
id: fileRefId,
|
|
28
|
+
tenantId: ctx.user.tenantId,
|
|
29
|
+
isDeleted: false,
|
|
30
|
+
});
|
|
31
|
+
return fileRef !== undefined && (await canCallerReadFileRef(ctx, fileRef));
|
|
32
|
+
}
|
|
33
|
+
export function createRequestHandler(access, scope) {
|
|
21
34
|
return defineWriteHandler({
|
|
22
35
|
name: "request",
|
|
23
36
|
schema: RequestSignatureSchema,
|
|
@@ -27,6 +40,12 @@ export function createRequestHandler(access) {
|
|
|
27
40
|
handler: async (event, ctx) => {
|
|
28
41
|
const payload = event.payload;
|
|
29
42
|
const user = event.user;
|
|
43
|
+
if (!(await scope.allows(ctx, user, payload.subjectType, payload.subjectId))) {
|
|
44
|
+
return writeFailure(new NotFoundError(SIGNATURE_REQUEST_ENTITY));
|
|
45
|
+
}
|
|
46
|
+
if (!(await isReadableTenantFile(ctx, payload.documentFileId))) {
|
|
47
|
+
return writeFailure(new UnprocessableError("document_not_a_pdf"));
|
|
48
|
+
}
|
|
30
49
|
const document = await readTenantFile(ctx, user.tenantId, payload.documentFileId);
|
|
31
50
|
if (document === null || !(await isLoadablePdf(document.content))) {
|
|
32
51
|
return writeFailure(new UnprocessableError("document_not_a_pdf"));
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
-
|
|
2
|
+
import type { SubjectScope } from "../subject-scope.ts";
|
|
3
|
+
export declare function createRevokeLinkHandler(access: AccessRule, scope: SubjectScope): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"revoke-link", import("zod").ZodObject<{
|
|
3
4
|
signerId: import("zod").ZodUUID;
|
|
4
5
|
}, import("zod/v4/core").$strip>, {
|
|
5
6
|
readonly revoked: true;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { NotFoundError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
|
|
2
3
|
import { signerExecutor } from "../entity.js";
|
|
4
|
+
import { loadRequestOfSigner } from "../lib.js";
|
|
3
5
|
import { SignerIdSchema } from "../payloads.js";
|
|
4
|
-
export function createRevokeLinkHandler(access) {
|
|
6
|
+
export function createRevokeLinkHandler(access, scope) {
|
|
5
7
|
return defineWriteHandler({
|
|
6
8
|
name: "revoke-link",
|
|
7
9
|
schema: SignerIdSchema,
|
|
@@ -9,6 +11,13 @@ export function createRevokeLinkHandler(access) {
|
|
|
9
11
|
agent: { expose: false },
|
|
10
12
|
description: "Revokes the signing link of one signer; the token stops working at once.",
|
|
11
13
|
handler: async (event, ctx) => {
|
|
14
|
+
if (scope.isScoped(event.user)) {
|
|
15
|
+
const signer = await signerExecutor.detail({ id: event.payload.signerId }, event.user, ctx.db);
|
|
16
|
+
const request = signer === null ? null : await loadRequestOfSigner(ctx, event.user, signer);
|
|
17
|
+
if (request === null || !(await scope.allowsRequest(ctx, event.user, request))) {
|
|
18
|
+
return writeFailure(new NotFoundError("signer", event.payload.signerId));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
12
21
|
const updated = await signerExecutor.update({ id: event.payload.signerId, changes: { linkAnchor: null } }, event.user, ctx.db, { skipOptimisticLock: true });
|
|
13
22
|
if (!updated.isSuccess)
|
|
14
23
|
return updated;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { type AccessRule } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { SubjectScope } from "../subject-scope.ts";
|
|
2
3
|
export type SignInPersonData = {
|
|
3
4
|
readonly requestStatus: string;
|
|
4
5
|
};
|
|
5
|
-
export declare function createSignInPersonHandler(access: AccessRule): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"sign-in-person", import("zod").ZodObject<{
|
|
6
|
+
export declare function createSignInPersonHandler(access: AccessRule, scope: SubjectScope): import("@cosmicdrift/kumiko-types/define-handler").WriteHandlerDefinition<"sign-in-person", import("zod").ZodObject<{
|
|
6
7
|
signerId: import("zod").ZodUUID;
|
|
7
8
|
imagePngBase64: import("zod").ZodString;
|
|
8
9
|
}, import("zod/v4/core").$strip>, SignInPersonData, import("@cosmicdrift/kumiko-types/event-type-map").KumikoEventTypeMap, undefined, undefined>;
|
|
@@ -6,7 +6,7 @@ import { advanceIfAllSigned, isOpenRequest, isUnsignedSigner, loadRequestOfSigne
|
|
|
6
6
|
import { SignInPersonSchema } from "../payloads.js";
|
|
7
7
|
import { decodeSignatureImage, invalidImage, recordInPersonSignature, storeSignatureImage, } from "../signing.js";
|
|
8
8
|
import { SIGNATURE_METHOD, SIGNATURE_REQUEST_STATUS } from "../web/constants.js";
|
|
9
|
-
export function createSignInPersonHandler(access) {
|
|
9
|
+
export function createSignInPersonHandler(access, scope) {
|
|
10
10
|
return defineWriteHandler({
|
|
11
11
|
name: "sign-in-person",
|
|
12
12
|
schema: SignInPersonSchema,
|
|
@@ -19,6 +19,9 @@ export function createSignInPersonHandler(access) {
|
|
|
19
19
|
if (signer === null)
|
|
20
20
|
return writeFailure(new NotFoundError("signer", event.payload.signerId));
|
|
21
21
|
const request = await loadRequestOfSigner(ctx, user, signer);
|
|
22
|
+
if (request !== null && !(await scope.allowsRequest(ctx, user, request))) {
|
|
23
|
+
return writeFailure(new NotFoundError("signer", event.payload.signerId));
|
|
24
|
+
}
|
|
22
25
|
const now = ctx.tz.now();
|
|
23
26
|
if (request === null || !isOpenRequest(request, now) || !isUnsignedSigner(signer)) {
|
|
24
27
|
return writeFailure(new UnprocessableError("signature_not_possible"));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { SignatureRetention } from "./entity.ts";
|
|
2
2
|
export { createSignatureFeature } from "./feature.ts";
|
|
3
|
-
export type { DeliverSignatureLink, RenderSignatureHtmlToPdf, SignatureLinkDelivery, SignatureOptions, } from "./options.ts";
|
|
3
|
+
export type { AuthorizeSignatureSubject, DeliverSignatureLink, RenderSignatureHtmlToPdf, SignatureLinkDelivery, SignatureOptions, SignatureSubject, } from "./options.ts";
|
|
4
4
|
export type { SignaturePageLabels } from "./signature-page.ts";
|
|
5
5
|
export { SIGNATURE_DEFAULT_VALIDITY_DAYS, SIGNATURE_EVENT_SHORT, SIGNATURE_FEATURE, SIGNATURE_INVALID_IMAGE_REASON, SIGNATURE_INVALID_LINK_REASON, SIGNATURE_LEVEL, SIGNATURE_LINK_TTL_DAYS, SIGNATURE_MAX_LINK_OPENS, SIGNATURE_METHOD, SIGNATURE_REQUEST_ENTITY, SIGNATURE_REQUEST_STATUS, SIGNATURE_SIGNER_ENTITY, SIGNATURE_SIGNER_STATUS, SignatureEvents, SignatureHandlers, SignatureInternalHandlers, SignatureJobs, type SignatureLevel, type SignatureMethod, SignatureQueries, type SignatureRequestStatus, type SignatureSignerStatus, } from "./web/constants.ts";
|
|
@@ -14,6 +14,11 @@ export type SignatureLinkDelivery = {
|
|
|
14
14
|
readonly issuedBy: SessionUser;
|
|
15
15
|
};
|
|
16
16
|
export type DeliverSignatureLink = (ctx: HandlerContext, delivery: SignatureLinkDelivery) => Promise<void>;
|
|
17
|
+
export type SignatureSubject = {
|
|
18
|
+
readonly subjectType: string;
|
|
19
|
+
readonly subjectId: string;
|
|
20
|
+
};
|
|
21
|
+
export type AuthorizeSignatureSubject = (ctx: HandlerContext, user: SessionUser, subject: SignatureSubject) => Promise<boolean>;
|
|
17
22
|
export type SignatureOptions = {
|
|
18
23
|
/** How long requests and signers are kept, data-retention keepFor syntax, counted from creation. */
|
|
19
24
|
readonly retention: SignatureRetention;
|
|
@@ -27,5 +32,9 @@ export type SignatureOptions = {
|
|
|
27
32
|
readonly signaturePageUrl: string;
|
|
28
33
|
/** Access rule of the signed-in handlers, queries, screens and nav. Default `{ roles: ["TenantAdmin", "TenantMember"] }`; set it when the app hands out other roles. The link handlers stay anonymous. */
|
|
29
34
|
readonly access?: AccessRule;
|
|
35
|
+
/** Runs for every signed-in handler and query on the subject of the request, except for `unscopedRoles`. `false` answers like "does not exist". Without it every user with `access` sees every request of the tenant. */
|
|
36
|
+
readonly authorizeSubject?: AuthorizeSignatureSubject;
|
|
37
|
+
/** Roles that skip `authorizeSubject`, e.g. admins. */
|
|
38
|
+
readonly unscopedRoles?: readonly string[];
|
|
30
39
|
};
|
|
31
40
|
export declare const LINK_RATE_LIMIT: RateLimitDeclaration;
|
|
@@ -27,6 +27,8 @@ export function createSignatureListScreen(access) {
|
|
|
27
27
|
{ field: "completedAt", hideOnNarrow: true },
|
|
28
28
|
],
|
|
29
29
|
defaultSort: { field: "expiresAt", dir: "desc" },
|
|
30
|
+
// A user bound by authorizeSubject gets no total, so the list pages by cursor.
|
|
31
|
+
pagination: "infinite",
|
|
30
32
|
access,
|
|
31
33
|
searchable: true,
|
|
32
34
|
rowActions: [
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type StaticRequestOptions, type StaticResult } from "@cosmicdrift/kumiko-bundled-features/auth-email-password/static-client";
|
|
2
|
+
import { SIGNATURE_INVALID_LINK_REASON } from "./web/constants.ts";
|
|
2
3
|
export { hrefWithoutUrlToken, readUrlToken, STATIC_NETWORK_REASON, STATIC_RATE_LIMITED_REASON, type StaticFailure, type StaticRequestOptions, type StaticResult, type StaticSuccess, } from "@cosmicdrift/kumiko-bundled-features/auth-email-password/static-client";
|
|
3
4
|
export { bytesToBase64, createSignaturePad, type SignaturePad, type SignaturePadCanvas, type SignaturePadOptions, } from "./web/pad.ts";
|
|
5
|
+
export { SIGNATURE_INVALID_LINK_REASON };
|
|
4
6
|
export type SignatureLinkView = {
|
|
5
7
|
readonly title: string;
|
|
6
8
|
readonly signerName: string;
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// a signer receives carries the token in the URL fragment (`#token=`), which browsers never send to a
|
|
3
3
|
// server or in a Referer.
|
|
4
4
|
import { dispatchWrite, staticFailure, staticSuccess, takeUrlToken, } from "@cosmicdrift/kumiko-bundled-features/auth-email-password/static-client";
|
|
5
|
-
import { SignatureHandlers } from "./web/constants.js";
|
|
5
|
+
import { SIGNATURE_INVALID_LINK_REASON, SignatureHandlers } from "./web/constants.js";
|
|
6
6
|
export { hrefWithoutUrlToken, readUrlToken, STATIC_NETWORK_REASON, STATIC_RATE_LIMITED_REASON, } from "@cosmicdrift/kumiko-bundled-features/auth-email-password/static-client";
|
|
7
7
|
export { bytesToBase64, createSignaturePad, } from "./web/pad.js";
|
|
8
|
+
export { SIGNATURE_INVALID_LINK_REASON };
|
|
8
9
|
/** Reads `#token=` from the current page and removes it from the address bar. Browser only. */
|
|
9
10
|
export function takeSignatureToken() {
|
|
10
11
|
return takeUrlToken("fragment");
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type QueryHandlerDef } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { SubjectScope } from "./subject-scope.ts";
|
|
3
|
+
export declare function scopeRequestList(base: QueryHandlerDef, scope: SubjectScope): QueryHandlerDef;
|
|
4
|
+
export declare function scopeRequestDetail(base: QueryHandlerDef, scope: SubjectScope): QueryHandlerDef;
|
|
5
|
+
export declare function scopeSignerList(base: QueryHandlerDef, scope: SubjectScope): QueryHandlerDef;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { DEFAULT_LIST_LIMIT, entityListSchema, MAX_LIST_LIMIT, } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import { AccessDeniedError } from "@cosmicdrift/kumiko-framework/errors";
|
|
3
|
+
import { requestExecutor } from "./entity.js";
|
|
4
|
+
function allFilters(payload) {
|
|
5
|
+
return [...(payload.filter === undefined ? [] : [payload.filter]), ...(payload.filters ?? [])];
|
|
6
|
+
}
|
|
7
|
+
function emptyPage(payload) {
|
|
8
|
+
return { rows: [], nextCursor: null, ...(payload.totalCount === true && { total: 0 }) };
|
|
9
|
+
}
|
|
10
|
+
function onlyFilterOn(payload, field) {
|
|
11
|
+
const matching = allFilters(payload).filter((filter) => filter.field === field);
|
|
12
|
+
const [first] = matching;
|
|
13
|
+
return matching.length === 1 && first !== undefined ? first : null;
|
|
14
|
+
}
|
|
15
|
+
function isRowsPage(value) {
|
|
16
|
+
return (typeof value === "object" && value !== null && "rows" in value && Array.isArray(value.rows));
|
|
17
|
+
}
|
|
18
|
+
function subjectKey(subject) {
|
|
19
|
+
return `${String(subject.subjectType)}\u0000${String(subject.subjectId)}`;
|
|
20
|
+
}
|
|
21
|
+
// ponytail: at most this many base pages per answer; a user who may see almost none of the
|
|
22
|
+
// tenant's requests gets short pages and scrolls on. Upgrade to a subject-joined query if that hurts.
|
|
23
|
+
const MAX_BASE_PAGES_PER_ANSWER = 10;
|
|
24
|
+
// authorizeSubject usually queries the database; a page of 200 subjects must not take the whole pool.
|
|
25
|
+
const SUBJECT_CHECKS_IN_PARALLEL = 10;
|
|
26
|
+
export function scopeRequestList(base, scope) {
|
|
27
|
+
return {
|
|
28
|
+
...base,
|
|
29
|
+
handler: async (query, ctx) => {
|
|
30
|
+
const user = query.user;
|
|
31
|
+
if (!scope.isScoped(user))
|
|
32
|
+
return base.handler(query, ctx);
|
|
33
|
+
const payload = entityListSchema.parse(query.payload);
|
|
34
|
+
// Group counts would include requests of foreign subjects, and an offset counts base rows,
|
|
35
|
+
// not the rows this user sees; the screen pages by cursor.
|
|
36
|
+
if (payload.aggregate !== undefined || (payload.offset ?? 0) > 0) {
|
|
37
|
+
throw new AccessDeniedError();
|
|
38
|
+
}
|
|
39
|
+
const limit = payload.limit ?? DEFAULT_LIST_LIMIT;
|
|
40
|
+
const verdicts = new Map();
|
|
41
|
+
const visibleRows = [];
|
|
42
|
+
let page = await base.handler(query, ctx);
|
|
43
|
+
for (let fetched = 1;; fetched++) {
|
|
44
|
+
if (!isRowsPage(page))
|
|
45
|
+
throw new Error("signature request list did not return a rows page");
|
|
46
|
+
const unknownSubjects = new Map(page.rows
|
|
47
|
+
.map((row) => [subjectKey(subjectOf(row)), subjectOf(row)])
|
|
48
|
+
.filter(([key]) => !verdicts.has(key)));
|
|
49
|
+
const pending = [...unknownSubjects];
|
|
50
|
+
for (let start = 0; start < pending.length; start += SUBJECT_CHECKS_IN_PARALLEL) {
|
|
51
|
+
await Promise.all(pending.slice(start, start + SUBJECT_CHECKS_IN_PARALLEL).map(async ([key, subject]) => {
|
|
52
|
+
verdicts.set(key, await scope.allowsRequest(ctx, user, subject));
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
visibleRows.push(...page.rows.filter((row) => verdicts.get(subjectKey(subjectOf(row)))));
|
|
56
|
+
const nextCursor = page["nextCursor"];
|
|
57
|
+
if (visibleRows.length >= limit ||
|
|
58
|
+
typeof nextCursor !== "string" ||
|
|
59
|
+
fetched >= MAX_BASE_PAGES_PER_ANSWER ||
|
|
60
|
+
verdicts.size >= MAX_LIST_LIMIT) {
|
|
61
|
+
// `total` would count requests of foreign subjects; the cursor continues after the last base page.
|
|
62
|
+
const { total: _total, rows: _rows, ...rest } = page;
|
|
63
|
+
return { ...rest, rows: visibleRows };
|
|
64
|
+
}
|
|
65
|
+
page = await base.handler({
|
|
66
|
+
...query,
|
|
67
|
+
payload: { ...payload, cursor: nextCursor, limit: limit - visibleRows.length },
|
|
68
|
+
}, ctx);
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
export function scopeRequestDetail(base, scope) {
|
|
74
|
+
return {
|
|
75
|
+
...base,
|
|
76
|
+
handler: async (query, ctx) => {
|
|
77
|
+
const row = await base.handler(query, ctx);
|
|
78
|
+
if (row === null || !scope.isScoped(query.user))
|
|
79
|
+
return row;
|
|
80
|
+
return (await scope.allowsRequest(ctx, query.user, subjectOf(row))) ? row : null;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function subjectOf(row) {
|
|
85
|
+
if (typeof row !== "object" || row === null)
|
|
86
|
+
return {};
|
|
87
|
+
return {
|
|
88
|
+
subjectType: "subjectType" in row ? row.subjectType : undefined,
|
|
89
|
+
subjectId: "subjectId" in row ? row.subjectId : undefined,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export function scopeSignerList(base, scope) {
|
|
93
|
+
return {
|
|
94
|
+
...base,
|
|
95
|
+
handler: async (query, ctx) => {
|
|
96
|
+
const user = query.user;
|
|
97
|
+
if (!scope.isScoped(user))
|
|
98
|
+
return base.handler(query, ctx);
|
|
99
|
+
const payload = entityListSchema.parse(query.payload);
|
|
100
|
+
const requestFilter = onlyFilterOn(payload, "requestId");
|
|
101
|
+
if (requestFilter === null ||
|
|
102
|
+
requestFilter.op !== "eq" ||
|
|
103
|
+
typeof requestFilter.value !== "string") {
|
|
104
|
+
throw new AccessDeniedError();
|
|
105
|
+
}
|
|
106
|
+
const request = await requestExecutor.detail({ id: requestFilter.value }, user, ctx.db);
|
|
107
|
+
if (request === null || !(await scope.allowsRequest(ctx, user, request))) {
|
|
108
|
+
return emptyPage(payload);
|
|
109
|
+
}
|
|
110
|
+
return base.handler(query, ctx);
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { HandlerContext, SessionUser } from "@cosmicdrift/kumiko-framework/engine";
|
|
2
|
+
import type { SignatureOptions } from "./options.ts";
|
|
3
|
+
export type SubjectScope = {
|
|
4
|
+
isScoped(user: SessionUser): boolean;
|
|
5
|
+
allows(ctx: HandlerContext, user: SessionUser, subjectType: unknown, subjectId: unknown): Promise<boolean>;
|
|
6
|
+
allowsRequest(ctx: HandlerContext, user: SessionUser, request: {
|
|
7
|
+
readonly subjectType?: unknown;
|
|
8
|
+
readonly subjectId?: unknown;
|
|
9
|
+
}): Promise<boolean>;
|
|
10
|
+
};
|
|
11
|
+
export declare function createSubjectScope(opts: Pick<SignatureOptions, "authorizeSubject" | "unscopedRoles">): SubjectScope;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function isNonEmptyString(value) {
|
|
2
|
+
return typeof value === "string" && value !== "";
|
|
3
|
+
}
|
|
4
|
+
export function createSubjectScope(opts) {
|
|
5
|
+
const { authorizeSubject } = opts;
|
|
6
|
+
const unscopedRoles = opts.unscopedRoles ?? [];
|
|
7
|
+
const isScoped = (user) => authorizeSubject !== undefined && !user.roles.some((role) => unscopedRoles.includes(role));
|
|
8
|
+
const allows = async (ctx, user, subjectType, subjectId) => {
|
|
9
|
+
if (authorizeSubject === undefined || !isScoped(user))
|
|
10
|
+
return true;
|
|
11
|
+
if (!isNonEmptyString(subjectType) || !isNonEmptyString(subjectId))
|
|
12
|
+
return false;
|
|
13
|
+
return authorizeSubject(ctx, user, { subjectType, subjectId });
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
isScoped,
|
|
17
|
+
allows,
|
|
18
|
+
allowsRequest: (ctx, user, request) => allows(ctx, user, request.subjectType, request.subjectId),
|
|
19
|
+
};
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-bundled-features",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.417.0",
|
|
4
4
|
"description": "Built-in features — tenant, user, auth, delivery. The stuff you'd rewrite anyway, already typed.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -642,9 +642,9 @@
|
|
|
642
642
|
}
|
|
643
643
|
},
|
|
644
644
|
"dependencies": {
|
|
645
|
-
"@cosmicdrift/kumiko-framework": "0.
|
|
646
|
-
"@cosmicdrift/kumiko-renderer": "0.
|
|
647
|
-
"@cosmicdrift/kumiko-types": "0.
|
|
645
|
+
"@cosmicdrift/kumiko-framework": "0.417.0",
|
|
646
|
+
"@cosmicdrift/kumiko-renderer": "0.417.0",
|
|
647
|
+
"@cosmicdrift/kumiko-types": "0.417.0",
|
|
648
648
|
"@mollie/api-client": "^4.6.0",
|
|
649
649
|
"@node-rs/argon2": "^2.2.2",
|
|
650
650
|
"@tiptap/core": "^3.32.2",
|
|
@@ -1310,19 +1310,19 @@
|
|
|
1310
1310
|
"LICENSE"
|
|
1311
1311
|
],
|
|
1312
1312
|
"devDependencies": {
|
|
1313
|
-
"@cosmicdrift/kumiko-locale-de": "0.
|
|
1314
|
-
"@cosmicdrift/kumiko-locale-es": "0.
|
|
1315
|
-
"@cosmicdrift/kumiko-locale-fr": "0.
|
|
1316
|
-
"@cosmicdrift/kumiko-locale-it": "0.
|
|
1317
|
-
"@cosmicdrift/kumiko-renderer-mail-html": "0.
|
|
1318
|
-
"@cosmicdrift/kumiko-testing": "0.
|
|
1313
|
+
"@cosmicdrift/kumiko-locale-de": "0.417.0",
|
|
1314
|
+
"@cosmicdrift/kumiko-locale-es": "0.417.0",
|
|
1315
|
+
"@cosmicdrift/kumiko-locale-fr": "0.417.0",
|
|
1316
|
+
"@cosmicdrift/kumiko-locale-it": "0.417.0",
|
|
1317
|
+
"@cosmicdrift/kumiko-renderer-mail-html": "0.417.0",
|
|
1318
|
+
"@cosmicdrift/kumiko-testing": "0.417.0",
|
|
1319
1319
|
"@testing-library/user-event": "^14.6.8",
|
|
1320
1320
|
"@types/sanitize-html": "^2.16.2",
|
|
1321
1321
|
"jsqr": "^1.4.0",
|
|
1322
1322
|
"unpdf": "^1.9.1"
|
|
1323
1323
|
},
|
|
1324
1324
|
"peerDependencies": {
|
|
1325
|
-
"@cosmicdrift/kumiko-testing": "0.
|
|
1325
|
+
"@cosmicdrift/kumiko-testing": "0.417.0"
|
|
1326
1326
|
},
|
|
1327
1327
|
"peerDependenciesMeta": {
|
|
1328
1328
|
"@cosmicdrift/kumiko-testing": {
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "0.417.0",
|
|
4
|
+
"type": "breaking",
|
|
5
|
+
"title": "Subject check for the signature feature and a readable-file check on requests",
|
|
6
|
+
"detail": "`createSignatureFeature({ ..., authorizeSubject, unscopedRoles })`: `authorizeSubject(ctx, user, { subjectType, subjectId })` runs for `request`, `cancel`, `issue-link`, `revoke-link`, `sign-in-person` and the queries, except for users holding a role of `unscopedRoles`; `false` answers like a missing record (not found, `null` or an empty page). `signatureRequest:list` keeps only the rows of allowed subjects for a scoped user and reads up to ten base pages to fill one answer (no 403; an answer can still hold fewer rows than `limit`, and it has no `total`); screens and nav stay at `access`. A scoped user must pass a `requestId` eq filter to `signatureSigner:list`, otherwise the query answers 403. `request` now also checks that the caller may read the document through the same decision as GET /files/:id; a file that is unknown or not readable answers `document_not_a_pdf`. `SIGNATURE_INVALID_LINK_REASON` is exported from `signature/static-sign`.",
|
|
7
|
+
"migration": "Apps that request a signature on a file the caller cannot open through GET /files/:id bind it to a record the caller can read first. Apps with authorizeSubject pass a requestId eq filter to signatureSigner:list and do not rely on `total` of signatureRequest:list for scoped users."
|
|
8
|
+
},
|
|
2
9
|
{
|
|
3
10
|
"version": "0.416.0",
|
|
4
11
|
"type": "improvement",
|