The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 4 | export const COLINE_TIMESTAMP_HEADER = "x-coline-timestamp"; |
| 5 | export const COLINE_DELIVERY_HEADER = "x-coline-delivery-id"; |
| 6 |
| 7 | function normalizeSecret(secret: string): string { |
| 8 | if (!secret || secret.trim().length < 16) { |
| 9 | throw new Error("Signing secret must be at least 16 characters."); |
| 10 | } |
| 11 |
| 12 | return secret; |
| 13 | } |
| 14 |
| 15 | async function importHmacKey(secret: string) { |
| 16 | return crypto.subtle.importKey( |
| 17 | "raw", |
| 18 | encoder.encode(normalizeSecret(secret)), |
| 19 | { name: "HMAC", hash: "SHA-256" }, |
| 20 | false, |
| 21 | ["sign", "verify"], |
| 22 | ); |
| 23 | } |
| 24 |
| 25 | function toHex(buffer: ArrayBuffer): string { |
| 26 | return [...new Uint8Array(buffer)] |
| 27 | .map((byte) => byte.toString(16).padStart(2, "0")) |
| 28 | .join(""); |
| 29 | } |
| 30 |
| 31 | /** Raw HMAC-SHA256 over an arbitrary payload, hex-encoded. */ |
| 32 | export async function hmacSha256Hex(secret: string, payload: string): Promise<string> { |
| 33 | const key = await importHmacKey(secret); |
| 34 | const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(payload)); |
| 35 | return toHex(signature); |
| 36 | } |
| 37 |
| 38 | /** Constant-time string equality for signature comparison. */ |
| 39 | export function timingSafeEqualHex(a: string, b: string): boolean { |
| 40 | if (a.length !== b.length) { |
| 41 | return false; |
| 42 | } |
| 43 | let diff = 0; |
| 44 | for (let index = 0; index < a.length; index += 1) { |
| 45 | diff |= a.charCodeAt(index) ^ b.charCodeAt(index); |
| 46 | } |
| 47 | return diff === 0; |
| 48 | } |
| 49 |
| 50 | export function buildSigningPayload(params: { |
| 51 | timestamp: string; |
| 52 | body: string; |
| 53 | }): string { |
| 54 | return `${params.timestamp}.${params.body}`; |
| 55 | } |
| 56 |
| 57 | export async function signAppRequest(params: { |
| 58 | secret: string; |
| 59 | timestamp: string; |
| 60 | body: string; |
| 61 | }): Promise<string> { |
| 62 | const key = await importHmacKey(params.secret); |
| 63 | const payload = buildSigningPayload({ |
| 64 | timestamp: params.timestamp, |
| 65 | body: params.body, |
| 66 | }); |
| 67 |
| 68 | const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(payload)); |
| 69 | return toHex(signature); |
| 70 | } |
| 71 |
| 72 | export async function createSignedAppRequestHeaders(params: { |
| 73 | secret: string; |
| 74 | body: string; |
| 75 | deliveryId: string; |
| 76 | timestamp?: string; |
| 77 | }): Promise<Record<string, string>> { |
| 78 | const timestamp = params.timestamp ?? new Date().toISOString(); |
| 79 | const signature = await signAppRequest({ |
| 80 | secret: params.secret, |
| 81 | timestamp, |
| 82 | body: params.body, |
| 83 | }); |
| 84 |
| 85 | return { |
| 86 | [COLINE_SIGNATURE_HEADER]: signature, |
| 87 | [COLINE_TIMESTAMP_HEADER]: timestamp, |
| 88 | [COLINE_DELIVERY_HEADER]: params.deliveryId, |
| 89 | }; |
| 90 | } |
| 91 |
| 92 | export async function verifyAppRequestSignature(params: { |
| 93 | secret: string; |
| 94 | timestamp: string; |
| 95 | body: string; |
| 96 | signature: string; |
| 97 | }): Promise<boolean> { |
| 98 | const expected = await signAppRequest({ |
| 99 | secret: params.secret, |
| 100 | timestamp: params.timestamp, |
| 101 | body: params.body, |
| 102 | }); |
| 103 |
| 104 | return expected === params.signature; |
| 105 | } |
| 106 |