@assinafy/cli 2.4.2 → 2.5.1
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/.env.example +4 -2
- package/README.en.md +46 -21
- package/README.md +34 -15
- package/dist/api.cjs +295 -55
- package/dist/api.cjs.map +1 -1
- package/dist/api.js +295 -55
- package/dist/api.js.map +1 -1
- package/dist/cli.cjs +635 -100
- package/dist/types/api/errors.d.ts +9 -0
- package/dist/types/api/errors.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +1 -1
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/resources/authentication.d.ts +33 -3
- package/dist/types/api/resources/authentication.d.ts.map +1 -1
- package/dist/types/api/resources/documents.d.ts +4 -3
- package/dist/types/api/resources/documents.d.ts.map +1 -1
- package/dist/types/api/resources/oauth.d.ts +1 -1
- package/dist/types/api/resources/oauth.d.ts.map +1 -1
- package/dist/types/api/resources/signer-documents.d.ts +9 -4
- package/dist/types/api/resources/signer-documents.d.ts.map +1 -1
- package/dist/types/api/resources/webhooks.d.ts +39 -6
- package/dist/types/api/resources/webhooks.d.ts.map +1 -1
- package/dist/types/api/support/webhook-verifier.d.ts +31 -14
- package/dist/types/api/support/webhook-verifier.d.ts.map +1 -1
- package/dist/types/api/types.d.ts +110 -21
- package/dist/types/api/types.d.ts.map +1 -1
- package/dist/types/api/utils.d.ts.map +1 -1
- package/docs/README.md +1 -1
- package/docs/api-operations.json +105 -1
- package/docs/api-reference.md +2380 -2057
- package/docs/auth.md +125 -4
- package/docs/documents.md +5 -6
- package/docs/migration-v2.md +1 -1
- package/docs/sdk-reference.md +84 -23
- package/docs/signer.md +4 -2
- package/docs/webhooks.md +185 -20
- package/package.json +2 -2
package/dist/api.js
CHANGED
|
@@ -17,6 +17,8 @@ var ApiError = class _ApiError extends AssinafyError {
|
|
|
17
17
|
wwwAuthenticate;
|
|
18
18
|
/** Server retry delay (seconds or HTTP date), when provided. */
|
|
19
19
|
retryAfter;
|
|
20
|
+
/** Rate-limit window reported by the API, when the headers are present. */
|
|
21
|
+
rateLimit;
|
|
20
22
|
constructor(message, statusCode, responseData = null, options) {
|
|
21
23
|
super(message, { statusCode, responseData }, options);
|
|
22
24
|
this.name = "ApiError";
|
|
@@ -24,6 +26,7 @@ var ApiError = class _ApiError extends AssinafyError {
|
|
|
24
26
|
this.responseData = responseData;
|
|
25
27
|
this.wwwAuthenticate = options?.wwwAuthenticate;
|
|
26
28
|
this.retryAfter = options?.retryAfter;
|
|
29
|
+
this.rateLimit = options?.rateLimit;
|
|
27
30
|
}
|
|
28
31
|
static fromResponse(statusCode, responseData, headers = {}) {
|
|
29
32
|
if (Buffer.isBuffer(responseData) || responseData instanceof ArrayBuffer) {
|
|
@@ -37,13 +40,23 @@ var ApiError = class _ApiError extends AssinafyError {
|
|
|
37
40
|
const data = responseData ?? {};
|
|
38
41
|
const rawMessage = data.message;
|
|
39
42
|
const rawError = data.error;
|
|
40
|
-
const
|
|
43
|
+
const description = data.error_description;
|
|
44
|
+
const message = typeof rawMessage === "string" && rawMessage.length > 0 ? rawMessage : typeof rawError === "string" ? typeof description === "string" && description.length > 0 ? `${rawError}: ${description}` : rawError : "API request failed";
|
|
41
45
|
return new _ApiError(message, statusCode, responseData, {
|
|
42
46
|
wwwAuthenticate: typeof headers["www-authenticate"] === "string" ? headers["www-authenticate"] : void 0,
|
|
43
|
-
retryAfter: typeof headers["retry-after"] === "string" ? headers["retry-after"] : void 0
|
|
47
|
+
retryAfter: typeof headers["retry-after"] === "string" ? headers["retry-after"] : void 0,
|
|
48
|
+
rateLimit: parseRateLimit(headers)
|
|
44
49
|
});
|
|
45
50
|
}
|
|
46
51
|
};
|
|
52
|
+
function parseRateLimit(headers) {
|
|
53
|
+
const info = {};
|
|
54
|
+
for (const key of ["limit", "remaining", "reset"]) {
|
|
55
|
+
const value = Number.parseInt(String(headers[`x-rate-limit-${key}`]), 10);
|
|
56
|
+
if (Number.isFinite(value)) info[key] = value;
|
|
57
|
+
}
|
|
58
|
+
return Object.keys(info).length > 0 ? info : void 0;
|
|
59
|
+
}
|
|
47
60
|
var ValidationError = class extends AssinafyError {
|
|
48
61
|
errors;
|
|
49
62
|
constructor(message = "Validation failed", errors = {}) {
|
|
@@ -197,6 +210,16 @@ function validateSignerOptions(input) {
|
|
|
197
210
|
`notification_methods must contain only: ${NOTIFICATION_METHODS.join(", ")}`
|
|
198
211
|
);
|
|
199
212
|
}
|
|
213
|
+
const notifications = input.notification_methods ?? [];
|
|
214
|
+
if (notifications.length > 1) {
|
|
215
|
+
throw new ValidationError("notification_methods accepts exactly one method per signer");
|
|
216
|
+
}
|
|
217
|
+
const [notification] = notifications;
|
|
218
|
+
if (notification !== void 0 && (input.verification_method === "Email" || input.verification_method === "Whatsapp") && notification !== input.verification_method) {
|
|
219
|
+
throw new ValidationError(
|
|
220
|
+
`${input.verification_method} verification requires the ${input.verification_method} notification method`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
200
223
|
if (input.step !== void 0 && !Number.isInteger(input.step)) {
|
|
201
224
|
throw new ValidationError("step must be an integer");
|
|
202
225
|
}
|
|
@@ -530,6 +553,12 @@ var AssignmentResource = class extends BaseResource {
|
|
|
530
553
|
};
|
|
531
554
|
|
|
532
555
|
// src/api/resources/authentication.ts
|
|
556
|
+
function requireReauth(payload) {
|
|
557
|
+
if (!payload?.password && !payload?.code) {
|
|
558
|
+
throw new ValidationError("password or code is required");
|
|
559
|
+
}
|
|
560
|
+
return payload;
|
|
561
|
+
}
|
|
533
562
|
var AuthenticationResource = class extends BaseResource {
|
|
534
563
|
/** `POST /login` — exchange email + password for a JWT access token. */
|
|
535
564
|
async login(email, password) {
|
|
@@ -540,6 +569,70 @@ var AuthenticationResource = class extends BaseResource {
|
|
|
540
569
|
() => this.http.post("/login", { email, password }, publicRequestConfig())
|
|
541
570
|
);
|
|
542
571
|
}
|
|
572
|
+
/**
|
|
573
|
+
* `POST /authentication/mfa/verify` — exchange the login `mfa_token` and an
|
|
574
|
+
* authenticator or recovery code for an access token. The challenge is
|
|
575
|
+
* single-use and expires 5 minutes after login.
|
|
576
|
+
*/
|
|
577
|
+
async verifyMfa(payload) {
|
|
578
|
+
if (!payload?.mfa_token) throw new ValidationError("mfa_token is required");
|
|
579
|
+
if (!payload.code?.trim()) throw new ValidationError("code is required");
|
|
580
|
+
return this.call(
|
|
581
|
+
"Two-factor verification failed",
|
|
582
|
+
() => this.http.post(
|
|
583
|
+
"/authentication/mfa/verify",
|
|
584
|
+
{ mfa_token: payload.mfa_token, code: payload.code.trim() },
|
|
585
|
+
publicRequestConfig()
|
|
586
|
+
)
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
/** `GET /users/self/mfa` — enrolled two-factor methods and remaining recovery codes. */
|
|
590
|
+
async listMfaMethods() {
|
|
591
|
+
return this.call("Failed to list two-factor methods", () => this.http.get("/users/self/mfa"));
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* `POST /users/self/mfa/totp` — start authenticator enrollment. The secret
|
|
595
|
+
* and provisioning URI are returned only once; two-factor stays inactive
|
|
596
|
+
* until {@link confirmTotpEnrollment}.
|
|
597
|
+
*/
|
|
598
|
+
async startTotpEnrollment(label) {
|
|
599
|
+
return this.call(
|
|
600
|
+
"Failed to start authenticator enrollment",
|
|
601
|
+
() => this.http.post("/users/self/mfa/totp", label ? { label } : {})
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* `PUT /users/self/mfa/totp/confirm` — activate the method with a live code
|
|
606
|
+
* and receive one-time recovery codes. Replacing a confirmed method also
|
|
607
|
+
* requires `password` or `reauth_code`.
|
|
608
|
+
*/
|
|
609
|
+
async confirmTotpEnrollment(payload) {
|
|
610
|
+
if (!payload?.id) throw new ValidationError("id is required");
|
|
611
|
+
if (!payload.code?.trim()) throw new ValidationError("code is required");
|
|
612
|
+
return this.call(
|
|
613
|
+
"Failed to confirm authenticator enrollment",
|
|
614
|
+
() => this.http.put("/users/self/mfa/totp/confirm", payload)
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
/** `POST /users/self/mfa/recovery-codes` — issue ten new recovery codes, invalidating the old set. */
|
|
618
|
+
async regenerateRecoveryCodes(payload) {
|
|
619
|
+
return this.call(
|
|
620
|
+
"Failed to regenerate recovery codes",
|
|
621
|
+
() => this.http.post("/users/self/mfa/recovery-codes", requireReauth(payload))
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* `DELETE /users/self/mfa/{methodId}` — remove a method. Removing the last
|
|
626
|
+
* one also discards the recovery codes.
|
|
627
|
+
*/
|
|
628
|
+
async removeMfaMethod(methodId, payload) {
|
|
629
|
+
const id = this.requireId(methodId, "Method ID");
|
|
630
|
+
const data = requireReauth(payload);
|
|
631
|
+
return this.call(
|
|
632
|
+
"Failed to remove two-factor method",
|
|
633
|
+
() => this.http.delete(`/users/self/mfa/${id}`, { data })
|
|
634
|
+
);
|
|
635
|
+
}
|
|
543
636
|
/** `POST /authentication/social-login` — exchange a provider token for an Assinafy JWT. */
|
|
544
637
|
async socialLogin(payload) {
|
|
545
638
|
if (payload.provider !== "google") throw new ValidationError("provider must be google");
|
|
@@ -863,7 +956,7 @@ var DocumentResource = class extends BaseResource {
|
|
|
863
956
|
async createFromTemplate(templateId, signers, options = {}, accountId) {
|
|
864
957
|
const tmplId = this.requireId(templateId, "Template ID");
|
|
865
958
|
const accId = this.accountId(accountId);
|
|
866
|
-
validateTemplateSigners(signers, { requireId: true
|
|
959
|
+
validateTemplateSigners(signers, { requireId: true });
|
|
867
960
|
if (options.expires_at !== void 0) requireIso8601(options.expires_at, "expires_at");
|
|
868
961
|
if (options.editor_fields !== void 0 && (!Array.isArray(options.editor_fields) || options.editor_fields.some((field) => !field?.field_id || typeof field.value !== "string"))) {
|
|
869
962
|
throw new ValidationError("editor_fields must contain field_id and value");
|
|
@@ -882,7 +975,7 @@ var DocumentResource = class extends BaseResource {
|
|
|
882
975
|
async estimateCostFromTemplate(templateId, signers, accountId) {
|
|
883
976
|
const tmplId = this.requireId(templateId, "Template ID");
|
|
884
977
|
const accId = this.accountId(accountId);
|
|
885
|
-
validateTemplateSigners(signers, { requireId: false
|
|
978
|
+
validateTemplateSigners(signers, { requireId: false });
|
|
886
979
|
return this.call(
|
|
887
980
|
"Failed to estimate cost from template",
|
|
888
981
|
() => this.http.post(`/accounts/${accId}/templates/${tmplId}/documents/estimate-cost`, { signers })
|
|
@@ -922,33 +1015,21 @@ var DocumentResource = class extends BaseResource {
|
|
|
922
1015
|
);
|
|
923
1016
|
}
|
|
924
1017
|
/**
|
|
925
|
-
* `PUT /public/documents/{document_id}/send-token` —
|
|
926
|
-
*
|
|
927
|
-
*
|
|
1018
|
+
* `PUT /public/documents/{document_id}/send-token` — email a one-time access
|
|
1019
|
+
* token to a signer who has not signed a pending document yet. The API sends
|
|
1020
|
+
* `{ channel: 'email', recipient }`; only the `email` channel is accepted.
|
|
1021
|
+
* The `{ email }` form is kept for existing callers and sends the same body.
|
|
928
1022
|
*/
|
|
929
1023
|
async sendToken(documentId, recipient, channel = "email") {
|
|
930
1024
|
const id = this.requireId(documentId, "Document ID");
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
"Failed to send signing token",
|
|
935
|
-
() => this.http.put(
|
|
936
|
-
`/public/documents/${id}/send-token`,
|
|
937
|
-
{ email: recipient.email },
|
|
938
|
-
publicRequestConfig()
|
|
939
|
-
)
|
|
940
|
-
);
|
|
941
|
-
}
|
|
942
|
-
if (!recipient) throw new ValidationError("recipient is required");
|
|
943
|
-
if (channel !== "email" && channel !== "whatsapp") {
|
|
944
|
-
throw new ValidationError("channel must be email or whatsapp");
|
|
945
|
-
}
|
|
946
|
-
if (channel === "email") requireEmail(recipient);
|
|
1025
|
+
const email = typeof recipient === "object" ? recipient?.email : recipient;
|
|
1026
|
+
if (channel !== "email") throw new ValidationError("channel must be email");
|
|
1027
|
+
requireEmail(email);
|
|
947
1028
|
return this.call(
|
|
948
1029
|
"Failed to send signing token",
|
|
949
1030
|
() => this.http.put(
|
|
950
1031
|
`/public/documents/${id}/send-token`,
|
|
951
|
-
{
|
|
1032
|
+
{ channel, recipient: email },
|
|
952
1033
|
publicRequestConfig()
|
|
953
1034
|
)
|
|
954
1035
|
);
|
|
@@ -991,9 +1072,6 @@ function validateTemplateSigners(signers, options) {
|
|
|
991
1072
|
if (options.requireId && signer.step !== void 0 && Number(signer.step) < 1) {
|
|
992
1073
|
throw new ValidationError("template signer step must be a positive integer");
|
|
993
1074
|
}
|
|
994
|
-
if (options.singleNotification && Array.isArray(signer.notification_methods) && signer.notification_methods.length > 1) {
|
|
995
|
-
throw new ValidationError("template signers accept at most one notification method");
|
|
996
|
-
}
|
|
997
1075
|
}
|
|
998
1076
|
if (options.requireId) {
|
|
999
1077
|
validateSigningSteps(signers);
|
|
@@ -1278,7 +1356,7 @@ var OAuthResource = class extends BaseResource {
|
|
|
1278
1356
|
});
|
|
1279
1357
|
}
|
|
1280
1358
|
/**
|
|
1281
|
-
* `POST /oauth/token` — code exchange or
|
|
1359
|
+
* `POST /oauth/token` — code exchange, refresh, or RFC 8693 token exchange; returns the flat OAuth JSON body.
|
|
1282
1360
|
* A refresh must return a new `refresh_token`; otherwise this throws `ValidationError`.
|
|
1283
1361
|
*/
|
|
1284
1362
|
async token(payload) {
|
|
@@ -1295,8 +1373,20 @@ var OAuthResource = class extends BaseResource {
|
|
|
1295
1373
|
}
|
|
1296
1374
|
} else if (payload.grant_type === "refresh_token") {
|
|
1297
1375
|
required(payload.refresh_token, "refresh_token");
|
|
1376
|
+
} else if (payload.grant_type === TOKEN_EXCHANGE) {
|
|
1377
|
+
required(payload.client_secret, "client_secret");
|
|
1378
|
+
httpsUrl(required(payload.resource, "resource"), "resource");
|
|
1379
|
+
required(payload.subject_token, "subject_token");
|
|
1380
|
+
for (const field of ["subject_token_type", "requested_token_type"]) {
|
|
1381
|
+
const value = payload[field];
|
|
1382
|
+
if ((value !== void 0 || field === "subject_token_type") && value !== ACCESS_TOKEN_TYPE) {
|
|
1383
|
+
throw new ValidationError(`${field} must be ${ACCESS_TOKEN_TYPE}`);
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1298
1386
|
} else {
|
|
1299
|
-
throw new ValidationError(
|
|
1387
|
+
throw new ValidationError(
|
|
1388
|
+
`grant_type must be authorization_code, refresh_token or ${TOKEN_EXCHANGE}`
|
|
1389
|
+
);
|
|
1300
1390
|
}
|
|
1301
1391
|
const tokens = await this.call(
|
|
1302
1392
|
"OAuth token request failed",
|
|
@@ -1329,6 +1419,8 @@ var OAuthResource = class extends BaseResource {
|
|
|
1329
1419
|
return this.call("Failed to fetch OAuth userinfo", () => this.http.get("/oauth/userinfo"));
|
|
1330
1420
|
}
|
|
1331
1421
|
};
|
|
1422
|
+
var TOKEN_EXCHANGE = "urn:ietf:params:oauth:grant-type:token-exchange";
|
|
1423
|
+
var ACCESS_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token";
|
|
1332
1424
|
function required(value, name) {
|
|
1333
1425
|
if (typeof value !== "string" || !value.trim()) throw new ValidationError(`${name} is required`);
|
|
1334
1426
|
return value;
|
|
@@ -1357,27 +1449,32 @@ var SignerDocumentsResource = class extends BaseResource {
|
|
|
1357
1449
|
() => this.http.get(`/signers/${sid}/document`, signerAccessConfig(code))
|
|
1358
1450
|
);
|
|
1359
1451
|
}
|
|
1360
|
-
/**
|
|
1452
|
+
/**
|
|
1453
|
+
* `GET /signers/{signer_id}/documents` — with a signer access code, or with
|
|
1454
|
+
* the client's owner credentials when the code is omitted (the signer must
|
|
1455
|
+
* belong to that workspace).
|
|
1456
|
+
*/
|
|
1361
1457
|
async list(signerId, signerAccessCode, params = {}) {
|
|
1362
1458
|
const sid = this.requireId(signerId, "Signer ID");
|
|
1363
|
-
const
|
|
1459
|
+
const config = this.signerOrOwnerConfig(signerAccessCode, { ...params });
|
|
1364
1460
|
return this.callList(
|
|
1365
1461
|
"Failed to list signer documents",
|
|
1366
|
-
() => this.http.get(
|
|
1367
|
-
`/signers/${sid}/documents`,
|
|
1368
|
-
signerAccessConfig(code, params)
|
|
1369
|
-
)
|
|
1462
|
+
() => this.http.get(`/signers/${sid}/documents`, config)
|
|
1370
1463
|
);
|
|
1371
1464
|
}
|
|
1372
|
-
/** `GET /signers/{signer_id}/documents/search?
|
|
1465
|
+
/** `GET /signers/{signer_id}/documents/search?search=…` — access code or owner credentials. */
|
|
1373
1466
|
async search(signerId, search, signerAccessCode) {
|
|
1374
1467
|
const sid = this.requireId(signerId, "Signer ID");
|
|
1375
|
-
const
|
|
1468
|
+
const config = this.signerOrOwnerConfig(signerAccessCode, { search });
|
|
1376
1469
|
return this.callList(
|
|
1377
1470
|
"Failed to search signer documents",
|
|
1378
|
-
() => this.http.get(`/signers/${sid}/documents/search`,
|
|
1471
|
+
() => this.http.get(`/signers/${sid}/documents/search`, config)
|
|
1379
1472
|
);
|
|
1380
1473
|
}
|
|
1474
|
+
signerOrOwnerConfig(signerAccessCode, params) {
|
|
1475
|
+
if (signerAccessCode === void 0) return { params: cleanParams(params) };
|
|
1476
|
+
return signerAccessConfig(this.requireId(signerAccessCode, "signer-access-code"), params);
|
|
1477
|
+
}
|
|
1381
1478
|
/**
|
|
1382
1479
|
* Public `GET /signers/{signer_id}/documents/{document_id}/download/{artifact}`.
|
|
1383
1480
|
* When an access code is supplied, the SDK first verifies that it belongs to
|
|
@@ -1921,10 +2018,8 @@ var DEFAULT_EVENTS = [
|
|
|
1921
2018
|
"signer_rejected_document",
|
|
1922
2019
|
"document_processing_failed"
|
|
1923
2020
|
];
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
async register(payload, accountId) {
|
|
1927
|
-
if (!payload.url) throw new ValidationError("Webhook URL is required");
|
|
2021
|
+
function validateWebhookFields(payload) {
|
|
2022
|
+
if (payload.url !== void 0) {
|
|
1928
2023
|
let webhookUrl;
|
|
1929
2024
|
try {
|
|
1930
2025
|
webhookUrl = new URL(payload.url);
|
|
@@ -1934,13 +2029,29 @@ var WebhookResource = class extends BaseResource {
|
|
|
1934
2029
|
if (webhookUrl.protocol !== "http:" && webhookUrl.protocol !== "https:") {
|
|
1935
2030
|
throw new ValidationError("Webhook URL must be a valid HTTP(S) URL");
|
|
1936
2031
|
}
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
2032
|
+
}
|
|
2033
|
+
if (payload.email !== void 0) requireEmail(payload.email);
|
|
2034
|
+
if (payload.events !== void 0 && (!Array.isArray(payload.events) || payload.events.some((event) => typeof event !== "string" || !event))) {
|
|
2035
|
+
throw new ValidationError("Webhook events must contain non-empty strings");
|
|
2036
|
+
}
|
|
2037
|
+
if (payload.name !== void 0 && typeof payload.name !== "string") {
|
|
2038
|
+
throw new ValidationError("Webhook name must be a string");
|
|
2039
|
+
}
|
|
2040
|
+
for (const key of ["is_active", "signing_enabled"]) {
|
|
2041
|
+
if (payload[key] !== void 0 && typeof payload[key] !== "boolean") {
|
|
2042
|
+
throw new ValidationError(`Webhook ${key} must be a boolean`);
|
|
1943
2043
|
}
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
var WebhookResource = class extends BaseResource {
|
|
2047
|
+
/**
|
|
2048
|
+
* `PUT /accounts/{accountId}/webhooks/subscriptions` — create or replace the
|
|
2049
|
+
* account's oldest webhook endpoint. Use the endpoint methods for accounts
|
|
2050
|
+
* with several endpoints or for signed deliveries.
|
|
2051
|
+
*/
|
|
2052
|
+
async register(payload, accountId) {
|
|
2053
|
+
if (!payload.url) throw new ValidationError("Webhook URL is required");
|
|
2054
|
+
validateWebhookFields({ ...payload, email: payload.email ?? "" });
|
|
1944
2055
|
const id = this.accountId(accountId);
|
|
1945
2056
|
const body = {
|
|
1946
2057
|
url: payload.url,
|
|
@@ -1956,7 +2067,7 @@ var WebhookResource = class extends BaseResource {
|
|
|
1956
2067
|
() => this.http.put(`/accounts/${id}/webhooks/subscriptions`, body)
|
|
1957
2068
|
);
|
|
1958
2069
|
}
|
|
1959
|
-
/** Fetch the
|
|
2070
|
+
/** Fetch the account's oldest webhook endpoint. Returns `null` if none exists. */
|
|
1960
2071
|
async get(accountId) {
|
|
1961
2072
|
const id = this.accountId(accountId);
|
|
1962
2073
|
return this.callOptional(
|
|
@@ -1964,7 +2075,7 @@ var WebhookResource = class extends BaseResource {
|
|
|
1964
2075
|
() => this.http.get(`/accounts/${id}/webhooks/subscriptions`)
|
|
1965
2076
|
);
|
|
1966
2077
|
}
|
|
1967
|
-
/** Inactivate the
|
|
2078
|
+
/** Inactivate the account's oldest webhook endpoint without deleting it. */
|
|
1968
2079
|
async inactivate(accountId) {
|
|
1969
2080
|
const id = this.accountId(accountId);
|
|
1970
2081
|
this.logger.info("Inactivating webhook subscription");
|
|
@@ -1973,6 +2084,91 @@ var WebhookResource = class extends BaseResource {
|
|
|
1973
2084
|
() => this.http.put(`/accounts/${id}/webhooks/inactivate`)
|
|
1974
2085
|
);
|
|
1975
2086
|
}
|
|
2087
|
+
/** `GET /accounts/{accountId}/webhooks/endpoints` — list webhook endpoints, oldest first. */
|
|
2088
|
+
async listEndpoints(accountId) {
|
|
2089
|
+
const id = this.accountId(accountId);
|
|
2090
|
+
return this.call(
|
|
2091
|
+
"Failed to list webhook endpoints",
|
|
2092
|
+
() => this.http.get(`/accounts/${id}/webhooks/endpoints`)
|
|
2093
|
+
);
|
|
2094
|
+
}
|
|
2095
|
+
/**
|
|
2096
|
+
* `POST /accounts/{accountId}/webhooks/endpoints` — register another
|
|
2097
|
+
* endpoint. Accounts allow 1 endpoint, or up to 3 on paid plans (`403` past
|
|
2098
|
+
* the limit); URLs must be unique per workspace. Omitted `events` uses the
|
|
2099
|
+
* same default set as {@link register}.
|
|
2100
|
+
*/
|
|
2101
|
+
async createEndpoint(payload, accountId) {
|
|
2102
|
+
if (!payload.url) throw new ValidationError("Webhook URL is required");
|
|
2103
|
+
validateWebhookFields({ ...payload, email: payload.email ?? "" });
|
|
2104
|
+
const id = this.accountId(accountId);
|
|
2105
|
+
const body = { ...payload, events: payload.events ?? DEFAULT_EVENTS };
|
|
2106
|
+
this.logger.info("Creating webhook endpoint", { eventCount: body.events.length });
|
|
2107
|
+
return this.call(
|
|
2108
|
+
"Failed to create webhook endpoint",
|
|
2109
|
+
() => this.http.post(`/accounts/${id}/webhooks/endpoints`, body)
|
|
2110
|
+
);
|
|
2111
|
+
}
|
|
2112
|
+
/** `GET /accounts/{accountId}/webhooks/endpoints/{endpointId}` — fetch one endpoint. */
|
|
2113
|
+
async getEndpoint(endpointId, accountId) {
|
|
2114
|
+
const id = this.accountId(accountId);
|
|
2115
|
+
const eid = this.requireId(endpointId, "Endpoint ID");
|
|
2116
|
+
return this.call(
|
|
2117
|
+
"Failed to fetch webhook endpoint",
|
|
2118
|
+
() => this.http.get(`/accounts/${id}/webhooks/endpoints/${eid}`)
|
|
2119
|
+
);
|
|
2120
|
+
}
|
|
2121
|
+
/**
|
|
2122
|
+
* `PUT /accounts/{accountId}/webhooks/endpoints/{endpointId}` — update only
|
|
2123
|
+
* the fields sent. Enabling signing creates a secret when none exists;
|
|
2124
|
+
* disabling it discards the secret.
|
|
2125
|
+
*/
|
|
2126
|
+
async updateEndpoint(endpointId, payload, accountId) {
|
|
2127
|
+
const id = this.accountId(accountId);
|
|
2128
|
+
const eid = this.requireId(endpointId, "Endpoint ID");
|
|
2129
|
+
if (!payload || typeof payload !== "object" || Object.keys(payload).length === 0) {
|
|
2130
|
+
throw new ValidationError("At least one webhook endpoint field is required");
|
|
2131
|
+
}
|
|
2132
|
+
validateWebhookFields(payload);
|
|
2133
|
+
return this.call(
|
|
2134
|
+
"Failed to update webhook endpoint",
|
|
2135
|
+
() => this.http.put(`/accounts/${id}/webhooks/endpoints/${eid}`, payload)
|
|
2136
|
+
);
|
|
2137
|
+
}
|
|
2138
|
+
/** `DELETE /accounts/{accountId}/webhooks/endpoints/{endpointId}` — delete an endpoint and free its slot. */
|
|
2139
|
+
async deleteEndpoint(endpointId, accountId) {
|
|
2140
|
+
const id = this.accountId(accountId);
|
|
2141
|
+
const eid = this.requireId(endpointId, "Endpoint ID");
|
|
2142
|
+
return this.call(
|
|
2143
|
+
"Failed to delete webhook endpoint",
|
|
2144
|
+
() => this.http.delete(`/accounts/${id}/webhooks/endpoints/${eid}`)
|
|
2145
|
+
);
|
|
2146
|
+
}
|
|
2147
|
+
/**
|
|
2148
|
+
* `GET …/endpoints/{endpointId}/secret` — the `whsec_` signing secret.
|
|
2149
|
+
* Returns `400` when signing is disabled. Not available to OAuth applications.
|
|
2150
|
+
*/
|
|
2151
|
+
async getEndpointSecret(endpointId, accountId) {
|
|
2152
|
+
const id = this.accountId(accountId);
|
|
2153
|
+
const eid = this.requireId(endpointId, "Endpoint ID");
|
|
2154
|
+
return this.call(
|
|
2155
|
+
"Failed to fetch webhook endpoint secret",
|
|
2156
|
+
() => this.http.get(`/accounts/${id}/webhooks/endpoints/${eid}/secret`)
|
|
2157
|
+
);
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* `POST …/endpoints/{endpointId}/secret/rotate` — replace the signing secret.
|
|
2161
|
+
* The old secret stops working immediately. Not available to OAuth applications.
|
|
2162
|
+
*/
|
|
2163
|
+
async rotateEndpointSecret(endpointId, accountId) {
|
|
2164
|
+
const id = this.accountId(accountId);
|
|
2165
|
+
const eid = this.requireId(endpointId, "Endpoint ID");
|
|
2166
|
+
this.logger.info("Rotating webhook endpoint secret");
|
|
2167
|
+
return this.call(
|
|
2168
|
+
"Failed to rotate webhook endpoint secret",
|
|
2169
|
+
() => this.http.post(`/accounts/${id}/webhooks/endpoints/${eid}/secret/rotate`)
|
|
2170
|
+
);
|
|
2171
|
+
}
|
|
1976
2172
|
/** List currently supported webhook event types. */
|
|
1977
2173
|
async listEventTypes() {
|
|
1978
2174
|
return this.call(
|
|
@@ -1980,7 +2176,7 @@ var WebhookResource = class extends BaseResource {
|
|
|
1980
2176
|
() => this.http.get("/webhooks/event-types")
|
|
1981
2177
|
);
|
|
1982
2178
|
}
|
|
1983
|
-
/** List webhook delivery history for the workspace. */
|
|
2179
|
+
/** List webhook delivery history for the workspace, optionally for one endpoint. */
|
|
1984
2180
|
async listDispatches(params = {}, accountId) {
|
|
1985
2181
|
const id = this.accountId(accountId);
|
|
1986
2182
|
if ("search" in params) throw new ValidationError("webhook dispatch search is not supported");
|
|
@@ -1992,7 +2188,7 @@ var WebhookResource = class extends BaseResource {
|
|
|
1992
2188
|
})
|
|
1993
2189
|
);
|
|
1994
2190
|
}
|
|
1995
|
-
/** Retry delivery of a specific webhook dispatch. */
|
|
2191
|
+
/** Retry delivery of a specific webhook dispatch to that entry's endpoint. */
|
|
1996
2192
|
async retryDispatch(dispatchId, accountId) {
|
|
1997
2193
|
const id = this.accountId(accountId);
|
|
1998
2194
|
const did = this.requireId(dispatchId, "Dispatch ID");
|
|
@@ -2101,6 +2297,17 @@ function validateNotificationSender(value) {
|
|
|
2101
2297
|
|
|
2102
2298
|
// src/api/support/webhook-verifier.ts
|
|
2103
2299
|
import { createHmac, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
2300
|
+
function readHeader(headers, name) {
|
|
2301
|
+
if (typeof headers.get === "function") {
|
|
2302
|
+
return headers.get(name) ?? void 0;
|
|
2303
|
+
}
|
|
2304
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
2305
|
+
if (key.toLowerCase() !== name) continue;
|
|
2306
|
+
if (Array.isArray(value)) return value.length === 1 ? value[0] : void 0;
|
|
2307
|
+
return value;
|
|
2308
|
+
}
|
|
2309
|
+
return void 0;
|
|
2310
|
+
}
|
|
2104
2311
|
var WebhookVerifier = class {
|
|
2105
2312
|
constructor(webhookSecret, options = {}) {
|
|
2106
2313
|
this.webhookSecret = webhookSecret;
|
|
@@ -2110,15 +2317,48 @@ var WebhookVerifier = class {
|
|
|
2110
2317
|
webhookSecret;
|
|
2111
2318
|
algorithm;
|
|
2112
2319
|
encoding;
|
|
2113
|
-
/**
|
|
2320
|
+
/**
|
|
2321
|
+
* Returns `true` when `headers` carry a Standard Webhooks signature that
|
|
2322
|
+
* matches the raw `payload` under this verifier's `whsec_` secret and the
|
|
2323
|
+
* `webhook-timestamp` is within the tolerance window. Pass the body exactly
|
|
2324
|
+
* as received; re-serialized JSON does not verify.
|
|
2325
|
+
*/
|
|
2326
|
+
verifyDelivery(payload, headers, options = {}) {
|
|
2327
|
+
if (!this.webhookSecret || !headers) return false;
|
|
2328
|
+
const id = readHeader(headers, "webhook-id");
|
|
2329
|
+
const timestamp = readHeader(headers, "webhook-timestamp");
|
|
2330
|
+
const signatures = readHeader(headers, "webhook-signature");
|
|
2331
|
+
if (!id || !timestamp || !signatures || !/^\d+$/.test(timestamp)) return false;
|
|
2332
|
+
const tolerance = options.toleranceSeconds ?? 300;
|
|
2333
|
+
const now = Math.floor((options.now ?? Date.now()) / 1e3);
|
|
2334
|
+
if (Math.abs(now - Number(timestamp)) > tolerance) return false;
|
|
2335
|
+
const secret = this.webhookSecret.startsWith("whsec_") ? this.webhookSecret.slice("whsec_".length) : this.webhookSecret;
|
|
2336
|
+
const key = Buffer.from(secret, "base64");
|
|
2337
|
+
if (key.length === 0) return false;
|
|
2338
|
+
const expected = Buffer.from(
|
|
2339
|
+
createHmac("sha256", key).update(`${id}.${timestamp}.`).update(typeof payload === "string" ? Buffer.from(payload, "utf8") : payload).digest("base64")
|
|
2340
|
+
);
|
|
2341
|
+
return signatures.split(" ").some((entry) => {
|
|
2342
|
+
const [version, value] = entry.split(",", 2);
|
|
2343
|
+
if (version !== "v1" || !value) return false;
|
|
2344
|
+
const provided = Buffer.from(value);
|
|
2345
|
+
return provided.length === expected.length && timingSafeEqual2(provided, expected);
|
|
2346
|
+
});
|
|
2347
|
+
}
|
|
2348
|
+
/**
|
|
2349
|
+
* Returns `true` if `signature` is an HMAC of `payload` keyed with the raw
|
|
2350
|
+
* secret string under the configured algorithm/encoding.
|
|
2351
|
+
*
|
|
2352
|
+
* @deprecated Assinafy signs deliveries with Standard Webhooks headers; use
|
|
2353
|
+
* {@link verifyDelivery}. Retained for existing callers.
|
|
2354
|
+
*/
|
|
2114
2355
|
verify(payload, signature) {
|
|
2115
2356
|
if (!this.webhookSecret || !signature) return false;
|
|
2116
2357
|
try {
|
|
2117
2358
|
const buf = typeof payload === "string" ? Buffer.from(payload, "utf8") : payload;
|
|
2118
2359
|
const expected = createHmac(this.algorithm, this.webhookSecret).update(buf).digest(this.encoding);
|
|
2119
|
-
const provided = signature.trim();
|
|
2120
2360
|
const a = Buffer.from(expected, "utf8");
|
|
2121
|
-
const b = Buffer.from(
|
|
2361
|
+
const b = Buffer.from(signature.trim(), "utf8");
|
|
2122
2362
|
if (a.length !== b.length) return false;
|
|
2123
2363
|
return timingSafeEqual2(a, b);
|
|
2124
2364
|
} catch {
|