logicsrc/apps/pwa/src/lib/crypto.mjs
Anthony Ettinger 9ba044577f
Some checks failed
CI / build (push) Has been cancelled
test / test (push) Has been cancelled
feat(pwa): logicsrc credentials app — real auth + Turso, redesigned; retire commandboard-api credshare
Adds apps/pwa: an Express + libSQL/Turso app that is now the home of team
credential sharing, with the moshcode-style auth stack ported and reskinned to
match logicsrc.com (light theme, Inter, green accent).

apps/pwa
- auth: email/password (scrypt), passkeys (WebAuthn), CoinPay OAuth, cookie
  sessions, and lsk_ API keys for the CLI via a loopback OAuth-PKCE flow
  (/cli/authorize + /cli/token). Ported from the moshcode PWA.
- credshare API (/api/credshare/*): teams, members, invites, vaults, sealed
  grants, ciphertext secrets, audit — authed by session OR Bearer lsk_ key.
  Zero-knowledge: only ciphertext + sealed vault keys + public keys stored.
- teams dashboard, accept-invite, and settings (API keys) pages, server-rendered
  in the LogicSRC brand (lib/html.mjs).
- migrations (libSQL) 001_auth + 002_credshare, migrate-on-boot; Turso via
  TURSO_DATABASE_URL / TURSO_AUTH_TOKEN, or a local file db for dev.
- trimmed moshcode-specific approvals/credits/push/deliver.

CLI
- `logicsrc login` now does browser loopback OAuth-PKCE against the app and
  stores an lsk_ token (email-OTP removed); --token for CI. Client repointed.

Distribution
- install.sh (served at logicsrc.com/install.sh) installs the CLI from the
  GitHub repo: tarball -> npm install -> `npm run build:cli` -> logicsrc wrapper.
- root build:cli builds only the CLI's workspace chain (skips web/api/next).

Cleanup
- removed the commandboard-api credshare backend (superseded by the PWA) and its
  Supabase/Turso stores + libsql dep; commandboard-api tests green (40).
- removed the Next.js /teams page (the PWA is the web UI now).

Verified end-to-end: two accounts register on the PWA, mint lsk_ keys, CLI login
uploads identity keys, owner pushes an encrypted .env, teammate invited ->
accepted -> granted -> pulls the exact file. Server stores ciphertext only.
Full workspace build + tests green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-13 14:29:29 +00:00

41 lines
1.8 KiB
JavaScript

// Password hashing (scrypt) + signed cookies + tokens — all node:crypto, no deps.
import crypto from "node:crypto";
import { config } from "../config.mjs";
// ---- passwords (scrypt) ----
export function hashPassword(password) {
const salt = crypto.randomBytes(16);
const dk = crypto.scryptSync(String(password), salt, 32);
return `scrypt$${salt.toString("hex")}$${dk.toString("hex")}`;
}
export function verifyPassword(password, stored) {
if (!stored || !stored.startsWith("scrypt$")) return false;
const [, saltHex, hashHex] = stored.split("$");
const salt = Buffer.from(saltHex, "hex");
const expected = Buffer.from(hashHex, "hex");
const dk = crypto.scryptSync(String(password), salt, expected.length);
return dk.length === expected.length && crypto.timingSafeEqual(dk, expected);
}
// ---- ids / tokens ----
export const id = () => crypto.randomUUID();
export const token = (bytes = 32) => crypto.randomBytes(bytes).toString("base64url");
export const sha256 = (s) => crypto.createHash("sha256").update(s).digest("hex");
// ---- signed cookies (stateless ceremony state) ----
export function sign(value) {
const payload = Buffer.from(JSON.stringify(value)).toString("base64url");
const mac = crypto.createHmac("sha256", config.sessionSecret).update(payload).digest("base64url");
return `${payload}.${mac}`;
}
export function unsign(signed) {
if (!signed || typeof signed !== "string" || !signed.includes(".")) return null;
const [payload, mac] = signed.split(".");
const expected = crypto.createHmac("sha256", config.sessionSecret).update(payload).digest("base64url");
const a = Buffer.from(mac);
const b = Buffer.from(expected);
if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) return null;
try { return JSON.parse(Buffer.from(payload, "base64url").toString("utf8")); } catch { return null; }
}