AI Engineering Workspace
PartialChat, 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- ..
- ai.ts
- apps.ts
- audit.ts
- auth.ts
- central-core.d.mts
- central-core.mjs
- central.ts
- cloud.ts
- config.ts
- crypto.ts
- db.ts
- design.ts
- engines.ts
- files.ts
- gates.ts
- gateway.ts
- marketplace-core.d.mts
- marketplace-core.mjs
- metrics.ts
- modules.ts
- motion.ts
- nexus-core.d.mts
- nexus-core.mjs
- oidc-core.d.mts
- oidc-core.mjs
- oidc.ts
- providers.ts
- proxy-core.d.mts
- proxy-core.mjs
- pwauth-core.d.mts
- pwauth-core.mjs
- requirements.ts
- rls.sql.d.mts
- rls.sql.mjs
- services-content.ts
- ssrf.ts
- status.ts
- tenant.ts
- tlsgate.ts
- workspace.ts
app/src/app/auth/callback/route.ts
1import { cookies } from "next/headers";2import { asSystem, db, now, uid } from "@/lib/db";3import { audit } from "@/lib/audit";4import { createSession, httpsBase, isSecure } from "@/lib/auth";5import { customerOpsEnabled, GATE_MSG } from "@/lib/tenant";6import { userAdmitted } from "@/lib/gates";7import { exchangeCode, oidcConfig, withKeys } from "@/lib/oidc";8import { sha, verifyIdToken } from "@/lib/oidc-core.mjs";9export const dynamic = "force-dynamic";10const fail = async (msg: string, status = 400) => { await audit("anonymous", "auth.oidc.failed", "", { reason: msg.slice(0, 80) }); return Response.json({ error: msg }, { status }); };11export async function GET(req: Request) { return asSystem(() => handle(req)); } // identity bootstrap: explicit system scope until the user is resolved12async function handle(req: Request) {13 if (!(await isSecure())) return Response.json({ error: "sign-in requires HTTPS" }, { status: 403 });14 const u = new URL(req.url); const state = u.searchParams.get("state") ?? "", code = u.searchParams.get("code") ?? "";15 const ck = await cookies(); const bound = ck.get("zcs_oidc")?.value; ck.delete("zcs_oidc");16 if (u.searchParams.get("error")) return fail("identity provider refused the sign-in");17 if (!state || !code || !bound || bound !== state) return fail("invalid or expired sign-in state");18 const row = await db().prepare("DELETE FROM oidc_requests WHERE state=? AND created_at > ? RETURNING nonce,verifier").get(state, Date.now() - 600_000) as { nonce: string; verifier: string } | undefined; // single use19 if (!row) return fail("sign-in request expired or already used");20 const cfg = await oidcConfig(); if (!cfg.configured) return fail("Zoo IAM is not configured", 503);21 let id; try { const tok = await exchangeCode(cfg, code, row.verifier); id = await withKeys(cfg.issuer, k => verifyIdToken(tok.id_token, k, { issuer: cfg.issuer, clientId: cfg.clientId, nonce: row.nonce, requireAcr: cfg.requireAcr })); } catch (e) { return fail("identity verification failed: " + String((e as Error).message).slice(0, 80), 401); }22 let link = await db().prepare("SELECT user_id FROM external_identities WHERE issuer=? AND subject=?").get(id.iss, id.sub) as { user_id: string } | undefined; let userId = link?.user_id;23 if (!userId) { // first sight of this (iss,sub): a fresh user with NO organization and NO platform rights. E-mail never links to an existing account.24 userId = uid("usr_"); const ph = `idp-${sha(id.iss + id.sub).slice(0, 24)}@external.invalid`;25 await db().prepare("INSERT INTO users(id,email,name,platform_admin,status,created_at) VALUES(?,?,?,0,'active',?)").run(userId, ph, (id.name ?? "Zoo account").slice(0, 80), now());26 await db().prepare("INSERT INTO external_identities(issuer,subject,user_id,email,email_verified,display_name,picture,created_at) VALUES(?,?,?,?,?,?,?,?)").run(id.iss, id.sub, userId, id.email, id.emailVerified ? 1 : 0, id.name, id.picture, now());27 await audit("idp:" + id.sub.slice(0, 12), "auth.oidc.registered", userId, {});28 }29 const usr = await db().prepare("SELECT id,status,platform_admin FROM users WHERE id=?").get(userId) as any; if (!usr || usr.status !== "active") return fail("account disabled", 403);30 // accept pending invitations addressed to the VERIFIED e-mail of this identity (grants only the invited role in the inviting organization; never platform rights)31 if (id.emailVerified && id.email) {32 const inv = await db().prepare("SELECT id,org_id,role FROM invitations WHERE lower(email)=lower(?) AND accepted_at IS NULL AND expires_at > ?").all(id.email, Date.now()) as any[];33 for (const i of inv) { await db().prepare("INSERT INTO memberships(org_id,user_id,role,created_at) VALUES(?,?,?,?) ON CONFLICT(org_id,user_id) DO NOTHING").run(i.org_id, userId, i.role, now()); await db().prepare("UPDATE invitations SET accepted_at=? WHERE id=?").run(now(), i.id); await audit(id.email, "team.invite.accepted", i.id, { org: i.org_id, role: i.role }); }34 }35 await db().prepare("UPDATE external_identities SET last_login=?, email=?, email_verified=?, display_name=?, picture=? WHERE issuer=? AND subject=?").run(now(), id.email, id.emailVerified ? 1 : 0, id.name, id.picture, id.iss, id.sub);36 if (!usr.platform_admin && (!(await customerOpsEnabled()).on || !(await userAdmitted(usr.id)))) return Response.json({ error: GATE_MSG }, { status: 403 }); // customer gate: no session on untrusted TLS37 const old = (await cookies()).get("zcs_session")?.value; if (old) await db().prepare("DELETE FROM sessions WHERE id=?").run(old); // fixation defence: never reuse a pre-login session id38 await createSession(userId, { iss: id.iss, sid: id.sid, acr: id.acr }); await audit("idp:" + id.sub.slice(0, 12), "auth.oidc.login", userId, { acr: id.acr, amr: id.amr });39 return Response.redirect(`${await httpsBase()}/console`, 302);40}41
Conversation
PartialSign in to start AI sessions.
Task checklist
- Application shell deployed
- Persistent projects
- Agent runtime
- Sandboxed terminal
Execution activity
No agent sessions. Git HEAD fb22adb on master.
Approval requests
None pending.