@superjackfruitlabs/treaty-cloudevents 0.0.5 → 0.0.7
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/signature.d.ts +7 -0
- package/dist/signature.js +38 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,3 +6,4 @@ export { REQUIRED_ATTRIBUTES, SPEC_VERSION, type RequiredAttribute, } from "./co
|
|
|
6
6
|
export { CloudEventError, percentDecode, percentEncode } from "./encoding.ts";
|
|
7
7
|
export { fromBinary, toBinary, validate, type BinaryMessage, type Binding, type CloudEvent, } from "./codec.ts";
|
|
8
8
|
export { eventSubject, eventType, parseSubject, parseType, type EventName, } from "./naming.ts";
|
|
9
|
+
export { SIGNATURE_HEADER, signDelivery, verifyDelivery } from "./signature.ts";
|
package/dist/index.js
CHANGED
|
@@ -6,3 +6,4 @@ export { REQUIRED_ATTRIBUTES, SPEC_VERSION, } from "./constants.js";
|
|
|
6
6
|
export { CloudEventError, percentDecode, percentEncode } from "./encoding.js";
|
|
7
7
|
export { fromBinary, toBinary, validate, } from "./codec.js";
|
|
8
8
|
export { eventSubject, eventType, parseSubject, parseType, } from "./naming.js";
|
|
9
|
+
export { SIGNATURE_HEADER, signDelivery, verifyDelivery } from "./signature.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const SIGNATURE_HEADER = "x-treaty-signature";
|
|
2
|
+
export declare function signDelivery(secret: string, id: string, body: Uint8Array, nowMs?: number): Promise<string>;
|
|
3
|
+
/** Throws unless `header` signs this id and body with `secret`, within `toleranceSeconds` of now. */
|
|
4
|
+
export declare function verifyDelivery(secret: string, header: string | null, id: string, body: Uint8Array, { nowMs, toleranceSeconds, }?: {
|
|
5
|
+
nowMs?: number;
|
|
6
|
+
toleranceSeconds?: number;
|
|
7
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signed HTTP delivery: how the relay proves to a Worker that an event came from it.
|
|
3
|
+
*
|
|
4
|
+
* The header is `t=<unix seconds>,v1=<hex HMAC-SHA256>`, over `<t>.<event id>.<body>`. Binding
|
|
5
|
+
* the id stops a captured body from being replayed under another id, and the timestamp bounds
|
|
6
|
+
* how long a captured request is accepted. Web Crypto only, so it runs in Workers, Bun and Node.
|
|
7
|
+
*/
|
|
8
|
+
import { CloudEventError } from "./encoding.js";
|
|
9
|
+
export const SIGNATURE_HEADER = "x-treaty-signature";
|
|
10
|
+
const UTF8 = new TextEncoder();
|
|
11
|
+
async function hmac(secret, t, id, body) {
|
|
12
|
+
const key = await crypto.subtle.importKey("raw", UTF8.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
13
|
+
const prefix = UTF8.encode(`${t}.${id}.`);
|
|
14
|
+
const message = new Uint8Array(prefix.length + body.length);
|
|
15
|
+
message.set(prefix);
|
|
16
|
+
message.set(body, prefix.length);
|
|
17
|
+
const mac = new Uint8Array(await crypto.subtle.sign("HMAC", key, message));
|
|
18
|
+
return Array.from(mac, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
19
|
+
}
|
|
20
|
+
export async function signDelivery(secret, id, body, nowMs = Date.now()) {
|
|
21
|
+
const t = Math.floor(nowMs / 1000);
|
|
22
|
+
return `t=${t},v1=${await hmac(secret, t, id, body)}`;
|
|
23
|
+
}
|
|
24
|
+
/** Throws unless `header` signs this id and body with `secret`, within `toleranceSeconds` of now. */
|
|
25
|
+
export async function verifyDelivery(secret, header, id, body, { nowMs = Date.now(), toleranceSeconds = 300, } = {}) {
|
|
26
|
+
const match = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(header ?? "");
|
|
27
|
+
if (!match)
|
|
28
|
+
throw new CloudEventError("missing or malformed signature");
|
|
29
|
+
const t = Number(match[1]);
|
|
30
|
+
if (Math.abs(nowMs / 1000 - t) > toleranceSeconds)
|
|
31
|
+
throw new CloudEventError("signature timestamp outside tolerance");
|
|
32
|
+
const expected = await hmac(secret, t, id, body);
|
|
33
|
+
let diff = 0;
|
|
34
|
+
for (let i = 0; i < expected.length; i++)
|
|
35
|
+
diff |= expected.charCodeAt(i) ^ match[2].charCodeAt(i);
|
|
36
|
+
if (diff !== 0)
|
|
37
|
+
throw new CloudEventError("signature does not match");
|
|
38
|
+
}
|