@primitivedotdev/sdk 0.26.1 → 0.27.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/README.md +2 -2
- package/dist/api/index.d.ts +5 -3
- package/dist/api/index.js +4 -406
- package/dist/{api-CnUa6o4r.js → api-CZIBnM4Q.js} +118 -92
- package/dist/contract/index.d.ts +2 -2
- package/dist/contract/index.js +1 -1
- package/dist/{errors-x91I_yEt.js → errors-BPJGp9I6.js} +1 -1
- package/dist/{errors-C53fe686.d.ts → errors-T_0JE528.d.ts} +1 -1
- package/dist/{index-BbEVpN5e.d.ts → index-9Rqocr-c.d.ts} +87 -75
- package/dist/{index-Dbx9udpX.d.ts → index-EQZK4vWT.d.ts} +2 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/openapi/index.d.ts +1 -52
- package/dist/openapi/index.js +2 -8
- package/dist/operations.generated-BJERV_56.d.ts +53 -0
- package/dist/operations.generated-T3exFpgJ.js +7632 -0
- package/dist/parser/index.d.ts +1 -1
- package/dist/parser/index.js +1 -1
- package/dist/webhook/index.d.ts +3 -3
- package/dist/webhook/index.js +2 -2
- package/dist/{webhook-DJkfUnFZ.js → webhook-Bra-g1q8.js} +1 -1
- package/package.json +9 -69
- package/bin/run.js +0 -20
- package/dist/api/generated/client/client.gen.js +0 -235
- package/dist/api/generated/client/index.js +0 -6
- package/dist/api/generated/client/types.gen.js +0 -2
- package/dist/api/generated/client/utils.gen.js +0 -228
- package/dist/api/generated/client.gen.js +0 -3
- package/dist/api/generated/core/auth.gen.js +0 -14
- package/dist/api/generated/core/bodySerializer.gen.js +0 -57
- package/dist/api/generated/core/params.gen.js +0 -100
- package/dist/api/generated/core/pathSerializer.gen.js +0 -106
- package/dist/api/generated/core/queryKeySerializer.gen.js +0 -92
- package/dist/api/generated/core/serverSentEvents.gen.js +0 -132
- package/dist/api/generated/core/types.gen.js +0 -2
- package/dist/api/generated/core/utils.gen.js +0 -87
- package/dist/api/generated/index.js +0 -2
- package/dist/api/generated/sdk.gen.js +0 -878
- package/dist/api/generated/types.gen.js +0 -2
- package/dist/api/verify-signature.js +0 -198
- package/dist/oclif/api-command.js +0 -755
- package/dist/oclif/auth.js +0 -223
- package/dist/oclif/commands/emails-latest.js +0 -185
- package/dist/oclif/commands/emails-poll.js +0 -121
- package/dist/oclif/commands/emails-wait.js +0 -171
- package/dist/oclif/commands/emails-watch.js +0 -165
- package/dist/oclif/commands/functions-deploy.js +0 -124
- package/dist/oclif/commands/functions-init.js +0 -256
- package/dist/oclif/commands/functions-redeploy.js +0 -113
- package/dist/oclif/commands/functions-set-secret.js +0 -213
- package/dist/oclif/commands/login.js +0 -237
- package/dist/oclif/commands/logout.js +0 -88
- package/dist/oclif/commands/send.js +0 -222
- package/dist/oclif/commands/whoami.js +0 -95
- package/dist/oclif/fish-completion.js +0 -87
- package/dist/oclif/index.js +0 -167
- package/dist/oclif/lint/raw-send-mail-fetch.js +0 -98
- package/dist/openapi/openapi.generated.js +0 -5754
- package/dist/openapi/operations.generated.js +0 -4626
- package/dist/parser/address-parser.js +0 -129
- package/dist/types.generated.js +0 -7
- package/dist/types.js +0 -53
- package/dist/webhook/errors.js +0 -224
- package/dist/webhook/received-email.js +0 -82
- package/oclif.manifest.json +0 -4380
- /package/dist/{address-parser-BYn8oW5r.js → address-parser-CQbFjgRC.js} +0 -0
- /package/dist/{types-9vXGZjPd.d.ts → types-Nslo1CU0.d.ts} +0 -0
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Workers-safe webhook signature verification.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors `verifyWebhookSignature` from `@primitivedotdev/sdk` but
|
|
5
|
-
* implements the HMAC-SHA256 step with the Web Crypto API
|
|
6
|
-
* (`crypto.subtle`) instead of `node:crypto`. The Node version is
|
|
7
|
-
* still the right choice for server-side handlers running on Node
|
|
8
|
-
* (it's measurably faster and supports Buffer bodies); this one
|
|
9
|
-
* exists so a Primitive Function handler can bundle the verifier
|
|
10
|
-
* without dragging in a `node:crypto` polyfill that inflates the
|
|
11
|
-
* deploy artifact past the size cap.
|
|
12
|
-
*
|
|
13
|
-
* Available natively in Workers, Node 22+, browsers, Deno, and Bun.
|
|
14
|
-
* Zero polyfill weight, zero new runtime dependencies.
|
|
15
|
-
*
|
|
16
|
-
* Surface contract matches the Node verifier exactly: same input
|
|
17
|
-
* shape, same `WebhookVerificationError` class, same set of error
|
|
18
|
-
* codes. Existing callers can swap the import path with no other
|
|
19
|
-
* code changes:
|
|
20
|
-
*
|
|
21
|
-
* // Node (existing):
|
|
22
|
-
* import { verifyWebhookSignature } from '@primitivedotdev/sdk';
|
|
23
|
-
*
|
|
24
|
-
* // Workers / in-handler (this file):
|
|
25
|
-
* import { verifyWebhookSignature } from '@primitivedotdev/sdk/api';
|
|
26
|
-
*/
|
|
27
|
-
import { WebhookVerificationError } from "../webhook/errors.js";
|
|
28
|
-
// Header name carrying the timestamp + signature. Must match the
|
|
29
|
-
// constant of the same name in `../webhook/signing.ts`. Kept in two
|
|
30
|
-
// places intentionally so this file has no dependency on the Node
|
|
31
|
-
// signing module (which would drag `node:crypto` into the bundle).
|
|
32
|
-
export const PRIMITIVE_SIGNATURE_HEADER = "Primitive-Signature";
|
|
33
|
-
// Re-export so consumers can `import { verifyWebhookSignature,
|
|
34
|
-
// WebhookVerificationError } from '@primitivedotdev/sdk/api'`
|
|
35
|
-
// without a second import statement against `/webhook`.
|
|
36
|
-
export { WebhookVerificationError } from "../webhook/errors.js";
|
|
37
|
-
// 5 minute max-age tolerance matches `webhook/signing.ts`.
|
|
38
|
-
const DEFAULT_TOLERANCE_SECONDS = 5 * 60;
|
|
39
|
-
// 60 second future tolerance for clock skew.
|
|
40
|
-
const FUTURE_TOLERANCE_SECONDS = 60;
|
|
41
|
-
// HMAC-SHA256 hex digest is 64 characters. Accept either case to
|
|
42
|
-
// stay byte-for-byte compatible with the Node verifier in
|
|
43
|
-
// `../webhook/signing.ts`, which uses the same pattern with the `/i`
|
|
44
|
-
// flag. Canonical Primitive signers emit lowercase, but tolerating
|
|
45
|
-
// uppercase keeps third-party signers (and tests that hand-build
|
|
46
|
-
// fixtures) from silently failing through to SIGNATURE_MISMATCH.
|
|
47
|
-
const HEX_PATTERN = /^[0-9a-f]+$/i;
|
|
48
|
-
const HEX_LENGTH = 64;
|
|
49
|
-
const UNIX_SECONDS_PATTERN = /^\d{1,10}$/;
|
|
50
|
-
function parseSignatureHeader(signatureHeader) {
|
|
51
|
-
if (!signatureHeader || typeof signatureHeader !== "string") {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
const parts = signatureHeader.split(",");
|
|
55
|
-
let timestamp = null;
|
|
56
|
-
const signatures = [];
|
|
57
|
-
for (const part of parts) {
|
|
58
|
-
const idx = part.indexOf("=");
|
|
59
|
-
if (idx === -1)
|
|
60
|
-
continue;
|
|
61
|
-
const key = part.slice(0, idx).trim();
|
|
62
|
-
const value = part.slice(idx + 1).trim();
|
|
63
|
-
if (!key || !value)
|
|
64
|
-
continue;
|
|
65
|
-
if (key === "t") {
|
|
66
|
-
if (!UNIX_SECONDS_PATTERN.test(value))
|
|
67
|
-
continue;
|
|
68
|
-
const parsed = Number(value);
|
|
69
|
-
if (Number.isSafeInteger(parsed)) {
|
|
70
|
-
timestamp = parsed;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
else if (key === "v1") {
|
|
74
|
-
signatures.push(value);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (timestamp === null || signatures.length === 0) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
return { timestamp, signatures };
|
|
81
|
-
}
|
|
82
|
-
function isValidHex(str) {
|
|
83
|
-
return str.length === HEX_LENGTH && HEX_PATTERN.test(str);
|
|
84
|
-
}
|
|
85
|
-
function arrayBufferToHex(buffer) {
|
|
86
|
-
const bytes = new Uint8Array(buffer);
|
|
87
|
-
let hex = "";
|
|
88
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
89
|
-
// biome-ignore lint/style/noNonNullAssertion: bytes[i] is always defined for valid index
|
|
90
|
-
hex += bytes[i].toString(16).padStart(2, "0");
|
|
91
|
-
}
|
|
92
|
-
return hex;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Constant-time comparison of two equal-length hex strings. Returns
|
|
96
|
-
* false if lengths differ (intentionally not a security issue: lengths
|
|
97
|
-
* are public). Iterates the full length regardless of mismatch so the
|
|
98
|
-
* timing signal does not reveal the position of the first divergence.
|
|
99
|
-
*/
|
|
100
|
-
function timingSafeEqualHex(a, b) {
|
|
101
|
-
if (a.length !== b.length)
|
|
102
|
-
return false;
|
|
103
|
-
let diff = 0;
|
|
104
|
-
for (let i = 0; i < a.length; i++) {
|
|
105
|
-
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
106
|
-
}
|
|
107
|
-
return diff === 0;
|
|
108
|
-
}
|
|
109
|
-
async function computeHmacHex(secret, payload) {
|
|
110
|
-
const encoder = new TextEncoder();
|
|
111
|
-
const keyData = encoder.encode(secret);
|
|
112
|
-
const key = await crypto.subtle.importKey("raw", keyData, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
113
|
-
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(payload));
|
|
114
|
-
return arrayBufferToHex(signature);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Verify a webhook signature using the Web Crypto API.
|
|
118
|
-
*
|
|
119
|
-
* Throws `WebhookVerificationError` on failure with a specific error
|
|
120
|
-
* code matching the Node verifier's set. Returns `true` on success.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* import {
|
|
125
|
-
* verifyWebhookSignature,
|
|
126
|
-
* WebhookVerificationError,
|
|
127
|
-
* PRIMITIVE_SIGNATURE_HEADER,
|
|
128
|
-
* } from '@primitivedotdev/sdk/api';
|
|
129
|
-
*
|
|
130
|
-
* export default {
|
|
131
|
-
* async fetch(request: Request, env: { PRIMITIVE_WEBHOOK_SECRET: string }) {
|
|
132
|
-
* const rawBody = await request.text();
|
|
133
|
-
* try {
|
|
134
|
-
* await verifyWebhookSignature({
|
|
135
|
-
* rawBody,
|
|
136
|
-
* signatureHeader: request.headers.get(PRIMITIVE_SIGNATURE_HEADER) ?? '',
|
|
137
|
-
* secret: env.PRIMITIVE_WEBHOOK_SECRET,
|
|
138
|
-
* });
|
|
139
|
-
* } catch (err) {
|
|
140
|
-
* if (err instanceof WebhookVerificationError) {
|
|
141
|
-
* return new Response('invalid signature', { status: 401 });
|
|
142
|
-
* }
|
|
143
|
-
* throw err;
|
|
144
|
-
* }
|
|
145
|
-
* // ... process the webhook
|
|
146
|
-
* },
|
|
147
|
-
* };
|
|
148
|
-
* ```
|
|
149
|
-
*/
|
|
150
|
-
export async function verifyWebhookSignature(opts) {
|
|
151
|
-
const { rawBody, signatureHeader, secret, toleranceSeconds = DEFAULT_TOLERANCE_SECONDS, nowSeconds, } = opts;
|
|
152
|
-
// `secret` is typed as `string` here (Node verifier also accepts
|
|
153
|
-
// Buffer, but Buffer isn't a thing in Workers and we deliberately
|
|
154
|
-
// don't include it in the Web Crypto API surface). `!secret` already
|
|
155
|
-
// catches undefined, null, and "" cleanly; no extra type guard
|
|
156
|
-
// needed.
|
|
157
|
-
if (!secret) {
|
|
158
|
-
throw new WebhookVerificationError("MISSING_SECRET", "Webhook secret is required but was empty or not provided");
|
|
159
|
-
}
|
|
160
|
-
const parsed = parseSignatureHeader(signatureHeader);
|
|
161
|
-
if (!parsed) {
|
|
162
|
-
throw new WebhookVerificationError("INVALID_SIGNATURE_HEADER", "Invalid Primitive-Signature header format. Expected: t={timestamp},v1={signature}");
|
|
163
|
-
}
|
|
164
|
-
const { timestamp, signatures } = parsed;
|
|
165
|
-
const now = nowSeconds ?? Math.floor(Date.now() / 1000);
|
|
166
|
-
const age = now - timestamp;
|
|
167
|
-
if (age > toleranceSeconds) {
|
|
168
|
-
throw new WebhookVerificationError("TIMESTAMP_OUT_OF_RANGE", `Webhook timestamp too old (${age}s). Max age is ${toleranceSeconds}s.`);
|
|
169
|
-
}
|
|
170
|
-
if (age < -FUTURE_TOLERANCE_SECONDS) {
|
|
171
|
-
throw new WebhookVerificationError("TIMESTAMP_OUT_OF_RANGE", "Webhook timestamp is too far in the future. Check server clock sync.");
|
|
172
|
-
}
|
|
173
|
-
const signedPayloadString = `${timestamp}.${rawBody}`;
|
|
174
|
-
const expectedHex = await computeHmacHex(secret, signedPayloadString);
|
|
175
|
-
// Walk every provided signature so a key-rotation header carrying
|
|
176
|
-
// [old, new] still verifies once the new key is live. Constant-time
|
|
177
|
-
// comparison per candidate so a partial-match attacker can't binary
|
|
178
|
-
// search hex characters by timing.
|
|
179
|
-
//
|
|
180
|
-
// Lowercase the candidate before comparing: HEX_PATTERN accepts
|
|
181
|
-
// either case (to match the Node verifier, which decodes via
|
|
182
|
-
// `Buffer.from(str, "hex")` and is case-insensitive), but
|
|
183
|
-
// expectedHex from `arrayBufferToHex` is always lowercase.
|
|
184
|
-
// Comparing raw `charCodeAt` would treat "AB" and "ab" as
|
|
185
|
-
// different and silently fail through to SIGNATURE_MISMATCH.
|
|
186
|
-
let anyMatch = false;
|
|
187
|
-
for (const candidate of signatures) {
|
|
188
|
-
if (!isValidHex(candidate))
|
|
189
|
-
continue;
|
|
190
|
-
if (timingSafeEqualHex(candidate.toLowerCase(), expectedHex)) {
|
|
191
|
-
anyMatch = true;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
if (!anyMatch) {
|
|
195
|
-
throw new WebhookVerificationError("SIGNATURE_MISMATCH", "Webhook signature did not match. The body may have been modified in transit, or the secret may be out of date.");
|
|
196
|
-
}
|
|
197
|
-
return true;
|
|
198
|
-
}
|