railyard/apps/console/lib/standard-webhooks.ts
caelum0x 247e860a16 feat(console): Railyard console with Scribase sign-in and the Dodo test-mode money ladder (RYP-D1, Y-2)
Next.js console for Cloudflare Workers (OpenNext config, wrangler.jsonc)
with data in the console's own Scribase project, schema railyard
(migration 0001: orgs, org_members, apps, deploys, entitlements,
billing_events, api_tokens, device_codes; idempotent, RLS on, no anon
grants).

- Sign-in with Google or GitHub through Scribase Auth (PKCE), user read
  from the auth server, org and entitlements row created, signed
  HttpOnly session.
- Money ladder (pure, immutable): $1 card check credited back to the
  customer wallet, 14-day trial with the plan's limits, $19 a month or
  $190 a year, pause switch and one-click cancel (ends at the next
  billing date; a trial ends now).
- Webhook: Standard Webhooks signature and timestamp verified, replays
  ignored by unique message id, signed checkout metadata, other brands
  ignored, a refund of the card check reverses the credit and the
  trial, a failed wallet call releases the id for the provider's retry.
- Apps, Usage and Billing screens, one primary action each; Add app
  shows the deploy command and the Insights tag in two clicks.
- CLI device-flow login and org-scoped hashed tokens for Y-3.
- Test mode only: the live payment host is refused unless the owner
  sets RAILYARD_DODO_LIVE=1.

Tests (node --test, 25): journey from sign-in to entitlements and the
snippet, refund, pause and cancel, webhook security, ladder, tokens and
org isolation, PostgREST repo, a component test of Add app with the copy
lint, a next build grep for sentinel secrets in the client bundle, and
the migration applied twice on a scratch Postgres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 15:35:24 +03:00

42 lines
1.7 KiB
TypeScript

// Standard Webhooks verification (the scheme Dodo Payments signs with).
// Signed content: `${webhook-id}.${webhook-timestamp}.${body}`, HMAC-SHA256
// with the base64 key after the `whsec_` prefix. `webhook-signature` holds
// space-separated `v1,<base64>` values; any match is accepted. Timestamps
// more than five minutes away are refused. Replays of the same id are
// refused by the caller (billing_events.webhook_id is unique).
import { b64decode, b64encode, constantTimeEqual, hmacSha256 } from "./crypto.ts";
export const TOLERANCE_SECONDS = 300;
export interface WebhookHeaders {
id: string | null;
timestamp: string | null;
signature: string | null;
}
function key(secret: string): Uint8Array | null {
const raw = secret.trim().replace(/^whsec_/, "");
const k = b64decode(raw);
return k && k.length > 0 ? k : null;
}
export async function sign(secret: string, id: string, timestamp: string, body: string): Promise<string> {
const k = key(secret);
if (!k) throw new Error("webhook secret is not valid base64");
return `v1,${b64encode(await hmacSha256(k, `${id}.${timestamp}.${body}`))}`;
}
export async function verify(secret: string, h: WebhookHeaders, body: string, nowSeconds: number): Promise<boolean> {
if (!h.id || !h.timestamp || !h.signature) return false;
const sent = Number(h.timestamp.trim());
if (!Number.isInteger(sent) || Math.abs(nowSeconds - sent) > TOLERANCE_SECONDS) return false;
const k = key(secret);
if (!k) return false;
const expected = b64encode(await hmacSha256(k, `${h.id}.${h.timestamp.trim()}.${body}`));
return h.signature
.split(/\s+/)
.filter((c) => c.startsWith("v1,"))
.some((c) => constantTimeEqual(c.slice(3), expected));
}