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>
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
// WebCrypto helpers that run the same on Node and Cloudflare Workers.
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
export function b64encode(bytes: Uint8Array): string {
|
|
let s = "";
|
|
for (const b of bytes) s += String.fromCharCode(b);
|
|
return btoa(s);
|
|
}
|
|
|
|
export function b64decode(text: string): Uint8Array | null {
|
|
try {
|
|
const bin = atob(text);
|
|
const out = new Uint8Array(bin.length);
|
|
for (let i = 0; i < bin.length; i += 1) out[i] = bin.charCodeAt(i);
|
|
return out;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function b64url(bytes: Uint8Array): string {
|
|
return b64encode(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
}
|
|
|
|
export function hex(bytes: Uint8Array): string {
|
|
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
}
|
|
|
|
export async function hmacSha256(key: Uint8Array | string, data: Uint8Array | string): Promise<Uint8Array> {
|
|
const k = await crypto.subtle.importKey(
|
|
"raw",
|
|
typeof key === "string" ? enc.encode(key) : key,
|
|
{ name: "HMAC", hash: "SHA-256" },
|
|
false,
|
|
["sign"],
|
|
);
|
|
const sig = await crypto.subtle.sign("HMAC", k, typeof data === "string" ? enc.encode(data) : data);
|
|
return new Uint8Array(sig);
|
|
}
|
|
|
|
export async function sha256Hex(data: string): Promise<string> {
|
|
return hex(new Uint8Array(await crypto.subtle.digest("SHA-256", enc.encode(data))));
|
|
}
|
|
|
|
export async function sha256B64url(data: string): Promise<string> {
|
|
return b64url(new Uint8Array(await crypto.subtle.digest("SHA-256", enc.encode(data))));
|
|
}
|
|
|
|
export function randomToken(bytes = 32): string {
|
|
const buf = new Uint8Array(bytes);
|
|
crypto.getRandomValues(buf);
|
|
return b64url(buf);
|
|
}
|
|
|
|
export function constantTimeEqual(a: string, b: string): boolean {
|
|
if (a.length !== b.length) return false;
|
|
let diff = 0;
|
|
for (let i = 0; i < a.length; i += 1) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
return diff === 0;
|
|
}
|