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/lib/oidc-core.mjs

1// Zoo IAM OpenID Connect core: Authorization Code + PKCE (S256), state/nonce, JWKS signature verification, back-channel logout.
2// Pure functions (no I/O except an injectable fetch) so they are unit-tested against forged tokens without any live IdP.
3import crypto from "node:crypto";
4export const CLIENT_ID = "zoo-cloud-studio";
5export const DEFAULT_ISSUER = "https://iam.zoobd.com/realms/zoo";
6const b64u = b => Buffer.from(b).toString("base64url");
7export const rand = (n = 32) => crypto.randomBytes(n).toString("base64url");
8export const pkceChallenge = verifier => crypto.createHash("sha256").update(verifier).digest("base64url");
9export const newPkce = () => { const verifier = rand(48); return { verifier, challenge: pkceChallenge(verifier), method: "S256" }; };
10export const sha = s => crypto.createHash("sha256").update(s).digest("hex");
11
12/** Discovery document must declare exactly the configured issuer and https endpoints (loopback http only when explicitly allowed for tests). */
13export function validateDiscovery(doc, issuer, { allowHttpLoopback = false } = {}) {
14 if (!doc || doc.issuer !== issuer) throw new Error("discovery issuer mismatch");
15 const okUrl = u => { try { const x = new URL(u); return x.protocol === "https:" || (allowHttpLoopback && x.protocol === "http:" && ["127.0.0.1", "localhost"].includes(x.hostname)); } catch { return false; } };
16 for (const k of ["authorization_endpoint", "token_endpoint", "jwks_uri"]) if (!okUrl(doc[k])) throw new Error(`discovery ${k} missing or not https`);
17 if (doc.code_challenge_methods_supported && !doc.code_challenge_methods_supported.includes("S256")) throw new Error("IdP does not support PKCE S256");
18 return doc;
19}
20export function authorizationUrl(disc, { clientId = CLIENT_ID, redirectUri, state, nonce, challenge, acr }) {
21 const u = new URL(disc.authorization_endpoint);
22 for (const [k, v] of Object.entries({ response_type: "code", client_id: clientId, redirect_uri: redirectUri, scope: "openid profile email", state, nonce, code_challenge: challenge, code_challenge_method: "S256", ...(acr ? { acr_values: acr } : {}) })) u.searchParams.set(k, v);
23 return u.toString();
24}
25
26const ALGS = { RS256: { hash: "sha256", kty: "RSA" }, ES256: { hash: "sha256", kty: "EC", ieee: true } };
27export function decodeJwt(tok) {
28 const p = String(tok).split("."); if (p.length !== 3 || p.some(x => !/^[A-Za-z0-9_-]*$/.test(x)) || !p[2]) throw new Error("malformed JWT");
29 let header, payload; try { header = JSON.parse(Buffer.from(p[0], "base64url").toString()); payload = JSON.parse(Buffer.from(p[1], "base64url").toString()); } catch { throw new Error("malformed JWT"); }
30 return { header, payload, signingInput: p[0] + "." + p[1], sig: Buffer.from(p[2], "base64url") };
31}
32/** Verifies the signature against the issuer's JWKS. Only RS256/ES256; "none", HS* (key-confusion) and unknown kids are refused. */
33export function verifySignature(jwt, jwks) {
34 const alg = ALGS[jwt.header.alg]; if (!alg) throw new Error(`unsupported JWT alg ${String(jwt.header.alg).slice(0, 12)}`);
35 const keys = (jwks?.keys ?? []).filter(k => k.kty === alg.kty && (!k.use || k.use === "sig") && (jwt.header.kid ? k.kid === jwt.header.kid : (jwks.keys.length === 1)));
36 if (!keys.length) throw new Error("no matching signing key (kid)");
37 for (const k of keys) { const key = crypto.createPublicKey({ key: k, format: "jwk" }); if (crypto.verify(alg.hash, Buffer.from(jwt.signingInput), alg.ieee ? { key, dsaEncoding: "ieee-p1363" } : key, jwt.sig)) return true; }
38 throw new Error("invalid JWT signature");
39}
40const aud = a => Array.isArray(a) ? a : [a];
41export function verifyIdToken(token, jwks, { issuer, clientId = CLIENT_ID, nonce, now = Date.now() / 1000, skew = 60, requireAcr }) {
42 const jwt = decodeJwt(token); verifySignature(jwt, jwks); const c = jwt.payload;
43 if (c.iss !== issuer) throw new Error("issuer mismatch");
44 if (!aud(c.aud).includes(clientId)) throw new Error("audience mismatch");
45 if (aud(c.aud).length > 1 && c.azp !== clientId) throw new Error("azp mismatch");
46 if (typeof c.exp !== "number" || c.exp + skew < now) throw new Error("token expired");
47 if (typeof c.nbf === "number" && c.nbf - skew > now) throw new Error("token not yet valid");
48 if (typeof c.iat !== "number" || c.iat - skew > now) throw new Error("bad iat");
49 if (!nonce || c.nonce !== nonce) throw new Error("nonce mismatch");
50 if (typeof c.sub !== "string" || !c.sub) throw new Error("missing sub");
51 if (requireAcr && c.acr !== requireAcr) throw new Error("authentication context (MFA) requirement not met");
52 return { iss: c.iss, sub: c.sub, sid: c.sid ?? null, email: typeof c.email === "string" ? c.email : null, emailVerified: c.email_verified === true, name: typeof c.name === "string" ? c.name : null, picture: typeof c.picture === "string" ? c.picture : null, acr: c.acr ?? null, amr: Array.isArray(c.amr) ? c.amr : [], authTime: c.auth_time ?? null };
53}
54/** OIDC Back-Channel Logout 1.0 §2.6 */
55export function verifyLogoutToken(token, jwks, { issuer, clientId = CLIENT_ID, now = Date.now() / 1000, skew = 60 }) {
56 const jwt = decodeJwt(token); verifySignature(jwt, jwks); const c = jwt.payload;
57 if (c.iss !== issuer || !aud(c.aud).includes(clientId)) throw new Error("issuer/audience mismatch");
58 if (typeof c.iat !== "number" || Math.abs(now - c.iat) > 300 + skew) throw new Error("stale logout token");
59 if (!c.events || typeof c.events["http://schemas.openid.net/event/backchannel-logout"] !== "object") throw new Error("not a logout token");
60 if ("nonce" in c) throw new Error("logout token must not carry a nonce");
61 if (!c.sub && !c.sid) throw new Error("logout token needs sub or sid");
62 return { iss: c.iss, sub: c.sub ?? null, sid: c.sid ?? null, jti: c.jti ?? null };
63}
64/** The only stable external identity key. E-mail is display data and is never used to link or to grant roles. */
65export const identityKey = (iss, sub) => `${iss}\u0000${sub}`;
66

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 0eebc42 on master.

Approval requests

None pending.