Skip to content
  1. Home
  2. workspace
Checking…
Public view — sanitized data only. Sign in over HTTPS to create projects and manage the workspace.

AI Engineering Workspace

Partial

Chat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.

Project files

read-only

app/src/app/api/auth/login/route.ts

1import crypto from "node:crypto";
2import { checkToken, createSession, isSecure, loginAllowed, sameOrigin, tokenConfigured } from "@/lib/auth";
3import { audit } from "@/lib/audit";
4import { asSystem, db } from "@/lib/db";
5import { customerOpsEnabled, GATE_MSG, hashToken } from "@/lib/tenant";
6import { userAdmitted } from "@/lib/gates";
7import { trustedConnection } from "@/lib/tlsgate";
8import { afterFailure, burn, hashPassword, policyError, verifyPassword, verifyTotp } from "@/lib/pwauth-core.mjs";
9export const dynamic = "force-dynamic";
10// The platform owner signs in with the admin token; customers with a personal access token issued by an org admin / super admin.
11// Dev-only stopgap until Zoo IAM (OIDC). Customer sign-in additionally requires trusted HTTPS (or the explicit dev override).
12export const POST = (req: Request) => asSystem(() => handle(req)); // identity is not known yet: explicit system scope for this bootstrap exchange
13async function handle(req: Request) {
14 if (!(await isSecure())) return Response.json({ error: "sign-in requires HTTPS" }, { status: 403 });
15 if (!(await sameOrigin())) return Response.json({ error: "cross-origin rejected" }, { status: 403 });
16 if (!loginAllowed()) return Response.json({ error: "too many attempts; wait a minute" }, { status: 429 });
17 const body0 = await req.clone().json().catch(() => ({}));
18 if (typeof body0.email === "string") return passwordLogin(body0);
19 if (!tokenConfigured()) return Response.json({ error: "access token not provisioned on server" }, { status: 503 });
20 const body = await req.json().catch(() => ({})); const token = String(body.token ?? "");
21 if (checkToken(token)) { await createSession("usr_owner"); await audit("owner@platform.local", "auth.login", "usr_owner", { org: "org_owner" }); return Response.json({ ok: true }); }
22 const u = token.length >= 20 ? await db().prepare("SELECT id,email,status,platform_admin FROM users WHERE token_hash=?").get(hashToken(token)) as any : null;
23 if (!u || u.status !== "active") { await audit("anonymous", "auth.login.failed"); return Response.json({ error: "invalid token" }, { status: 401 }); }
24 if (!u.platform_admin && (!(await customerOpsEnabled()).on || !(await userAdmitted(u.id)))) return Response.json({ error: GATE_MSG }, { status: 403 });
25 await createSession(u.id); await audit(u.email, "auth.login", u.id); return Response.json({ ok: true });
26}
27
28// E-mail + password. Credentials are only accepted over TRUSTED TLS (or the explicit, audited dev override); a temporary password must be changed in the same request.
29async function passwordLogin(b: any) {
30 const tc = await trustedConnection();
31 if (!tc.ok) return Response.json({ error: "Password sign-in is only available over verified trusted HTTPS on the canonical host (" + tc.reason + ")." }, { status: 403 });
32 const email = String(b.email ?? "").trim().toLowerCase(), pw = String(b.password ?? "");
33 const bad = () => Response.json({ error: "invalid e-mail or password" }, { status: 401 });
34 if (!email || !pw || pw.length > 128) { burn(pw); return bad(); }
35 const u = await db().prepare("SELECT u.id,u.email,u.status,u.platform_admin,c.pw_hash,c.must_change,c.failed,c.locked_until,c.mfa_secret,c.mfa_enabled FROM users u JOIN user_credentials c ON c.user_id=u.id WHERE lower(u.email)=?").get(email) as any;
36 if (!u || u.status !== "active" || !u.pw_hash) { burn(pw); await audit("anonymous", "auth.login.failed"); return bad(); }
37 if (!u.platform_admin && (!(await customerOpsEnabled()).on || !(await userAdmitted(u.id)))) { burn(pw); return bad(); } // customers stay behind the customer-operations gate
38 if (Number(u.locked_until) > Date.now()) { burn(pw); await audit(u.email, "auth.login.locked", u.id); return Response.json({ error: "account temporarily locked; try again later" }, { status: 429 }); }
39 if (!verifyPassword(pw, u.pw_hash)) { const n = afterFailure(u.failed); await db().prepare("UPDATE user_credentials SET failed=?, locked_until=? WHERE user_id=?").run(n.failed, n.lockedUntil, u.id); await audit(u.email, n.lockedUntil ? "auth.login.lockout" : "auth.login.failed", u.id); return bad(); }
40 if (u.mfa_enabled) { if (!b.totp) return Response.json({ error: "authentication code required", mfaRequired: true }, { status: 401 }); if (!verifyTotp(u.mfa_secret, b.totp)) { await audit(u.email, "auth.login.failed", u.id); return bad(); } }
41 if (u.must_change) {
42 if (!b.newPassword) return Response.json({ error: "password change required", mustChange: true }, { status: 428 }); // no session is created
43 const pe = policyError(String(b.newPassword), { email: u.email, old: pw }); if (pe) return Response.json({ error: pe, mustChange: true }, { status: 422 });
44 await db().prepare("UPDATE user_credentials SET pw_hash=?, must_change=0, failed=0, locked_until=0, changed_at=? WHERE user_id=?").run(hashPassword(String(b.newPassword)), new Date().toISOString(), u.id); // the temporary credential is now void
45 await db().prepare("DELETE FROM sessions WHERE user_id=?").run(u.id); await audit(u.email, "auth.password.changed", u.id);
46 } else await db().prepare("UPDATE user_credentials SET failed=0, locked_until=0 WHERE user_id=?").run(u.id);
47 await createSession(u.id); await audit(u.email, "auth.login", u.id, { method: "password" }); return Response.json({ ok: true });
48}
49

Conversation

Partial
Sign in to start AI sessions.

Task checklist

  • Application shell deployed
  • Persistent projects
  • Agent runtime
  • Sandboxed terminal

Execution activity

No agent sessions. Git HEAD 915def0 on master.

Approval requests

None pending.