feat(infra): payments (Stripe), auth scaffold, engine bridge, embeds, .env.example
Env-guarded so build stays green: /api/payments/[order_id]/deposit+release + /api/webhooks/stripe (lazy Stripe, 50/50 escrow, 501 until keys); /api/auth login|me|logout (HMAC signed-session scaffold, guarded dev login); lib/engine bridges edge->FastAPI (estimates defers to ENGINE_URL when set); components/embed for NocoDB/Metabase signed URLs; .env.example full inventory. tsc 0, build green. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
// Lightweight signed-session scaffold (HMAC-SHA256 via Web Crypto, no deps).
|
||||
// Production auth = Auth.js + Postgres adapter + OAuth (docs/02) — wired once the
|
||||
// database is provisioned. This provides the session shape + a guarded dev login.
|
||||
const enc = new TextEncoder();
|
||||
export const SESSION_COOKIE = "0pt_session";
|
||||
|
||||
function b64url(buf: ArrayBuffer | Uint8Array): string {
|
||||
const b = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
|
||||
let s = "";
|
||||
for (const x of b) s += String.fromCharCode(x);
|
||||
return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
||||
}
|
||||
function fromB64url(s: string): Uint8Array {
|
||||
s = s.replace(/-/g, "+").replace(/_/g, "/");
|
||||
s += "=".repeat((4 - (s.length % 4)) % 4);
|
||||
const bin = atob(s);
|
||||
const u = new Uint8Array(bin.length);
|
||||
for (let i = 0; i < bin.length; i++) u[i] = bin.charCodeAt(i);
|
||||
return u;
|
||||
}
|
||||
async function hmacKey(): Promise<CryptoKey> {
|
||||
return crypto.subtle.importKey("raw", enc.encode(process.env.AUTH_SECRET ?? ""), { name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"]);
|
||||
}
|
||||
|
||||
export async function signSession(payload: Record<string, unknown>): Promise<string> {
|
||||
const body = b64url(enc.encode(JSON.stringify(payload)));
|
||||
const sig = await crypto.subtle.sign("HMAC", await hmacKey(), enc.encode(body));
|
||||
return `${body}.${b64url(sig)}`;
|
||||
}
|
||||
|
||||
export async function verifySession(token: string | undefined | null): Promise<Record<string, unknown> | null> {
|
||||
if (!token || !process.env.AUTH_SECRET) return null;
|
||||
const [body, sig] = token.split(".");
|
||||
if (!body || !sig) return null;
|
||||
const ok = await crypto.subtle.verify("HMAC", await hmacKey(), fromB64url(sig), enc.encode(body));
|
||||
if (!ok) return null;
|
||||
try {
|
||||
return JSON.parse(new TextDecoder().decode(fromB64url(body)));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Bridge from the Vercel edge (Next route handlers) to the FastAPI data engine
|
||||
// (services/api) running on the VPS. When ENGINE_URL is set, the edge defers to
|
||||
// the engine as the single source of truth; otherwise it computes locally.
|
||||
export function hasEngine(): boolean {
|
||||
return Boolean(process.env.ENGINE_URL);
|
||||
}
|
||||
|
||||
export async function engineFetch(path: string, init?: RequestInit): Promise<Response | null> {
|
||||
const base = process.env.ENGINE_URL;
|
||||
if (!base) return null;
|
||||
try {
|
||||
return await fetch(`${base}${path}`, {
|
||||
...init,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Internal-Token": process.env.INTERNAL_TOKEN ?? "",
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
cache: "no-store",
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import Stripe from "stripe";
|
||||
|
||||
// Lazy, env-guarded Stripe client. Returns null when STRIPE_SECRET_KEY is unset
|
||||
// so the app builds/runs without keys; payment routes 501 until configured.
|
||||
let _stripe: Stripe | null = null;
|
||||
|
||||
export function getStripe(): Stripe | null {
|
||||
const key = process.env.STRIPE_SECRET_KEY;
|
||||
if (!key) return null;
|
||||
if (!_stripe) _stripe = new Stripe(key);
|
||||
return _stripe;
|
||||
}
|
||||
Reference in New Issue
Block a user