UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

212 lines • 9.32 kB
import { randomBytes, randomUUID } from "node:crypto"; import { accountEnrollmentTranscript, parseAccountEnrollment, } from "./mcp-presenter-enrollment.js"; import { accountLapse, AttestationExpiry } from "./mcp-presenter-account-lifetime.js"; import { boundedClaim, PresenterJtiCache } from "./mcp-presenter-attestation.js"; import { send, signed } from "./mcp-presenter-wire.js"; /** How the settle audit names an account principal: by its verified attestation, never by `remoteOrigin` (D11 2.5). */ export function accountIdentity(principal) { return { principalKind: "account", dev: principal.dev, dname: principal.dname }; } function refuse(stream, reason) { stream.end(`${JSON.stringify({ type: "account_refused", version: 1, reason })}\n`, () => stream.destroy()); } /** * `account_enroll` and `account_reattest` on the presenter control endpoint (D11 S3-2). The pre-auth exchange is one * attempt within the control connection's 30 s window: attestation, then a signature over the host's transcript. */ export class McpPresenterAccounts { constructor(hostInstance, principals, authority, revokeStream) { this.jtis = new PresenterJtiCache(); this.attempts = new WeakMap(); this.started = new WeakSet(); this.verifying = new WeakSet(); this.closing = new WeakSet(); this.expiry = new AttestationExpiry(); this.hostInstance = hostInstance; this.principals = principals; this.authority = authority; this.revokeStream = revokeStream; // A sign-out or account switch ends every account principal now (D11 S3-4). this.unsubscribe = authority?.onAccountChange(() => { for (const principal of [...this.principals.values()]) if (principal.kind === "account") this.revoke(principal, accountLapse(principal, this.authority) ?? "presenter_account_mismatch"); }); } /** Takes account frames; `fresh` is a control stream that has sent nothing else. False leaves the frame alone. */ receive(stream, value, fresh, authenticated) { if (this.closing.has(stream)) return true; if (this.verifying.has(stream)) { stream.destroy(); return true; } const attempt = this.attempts.get(stream); if (attempt) { this.attempts.delete(stream); this.prove(stream, value, attempt, authenticated); return true; } if (value.type === "account_enroll") { if (!fresh || this.started.has(stream)) stream.destroy(); else void this.enroll(stream, value); return true; } if (value.type !== "account_reattest") return false; const principal = this.principalOn(stream); if (!principal || Object.keys(value).length !== 2 || typeof value.attestation !== "string") stream.destroy(); else void this.reattest(stream, principal, value.attestation); return true; } /** Revokes an account principal whose host account or attestation is no longer current; true when it did. */ lapsed(principal) { if (principal.kind !== "account") return false; const reason = accountLapse(principal, this.authority); if (reason !== undefined) this.revoke(principal, reason); return reason !== undefined; } dispose() { this.unsubscribe?.(); this.expiry.dispose(); this.jtis.clear(); } async enroll(stream, value) { this.started.add(stream); const request = parseAccountEnrollment(value); if (!request) return void stream.destroy(); this.verifying.add(stream); const attested = await this.attest(stream, request.attestation, request.key); this.verifying.delete(stream); if (attested === undefined) return; if (typeof attested === "string") return this.refuse(stream, attested); const transcript = accountEnrollmentTranscript(this.hostInstance, attested.acc, attested, request.installationKeyId, randomBytes(32).toString("hex")); this.attempts.set(stream, { request, attestation: attested, transcript }); send(stream, { type: "challenge", version: 1, transcript, hostInstance: this.hostInstance }); } prove(stream, value, attempt, authenticated) { const { request, attestation } = attempt; if (!signed(value, attempt.transcript, request.key)) return this.refuse(stream, "presenter_proof_invalid"); if (this.authority?.hostAccount() !== attestation.acc) return this.refuse(stream, "presenter_account_mismatch"); for (const old of this.principals.values()) { if (old.kind === "account" && old.acc === attestation.acc && old.dev === attestation.dev && old.installationKeyId === request.installationKeyId) this.revoke(old); } const principal = { kind: "account", id: randomUUID(), key: request.key, acc: attestation.acc, dev: attestation.dev, dname: attestation.dname, installationKeyId: request.installationKeyId, attestedUntil: attestation.exp, stream, sessions: new WeakSet(), }; this.principals.set(principal.id, principal); this.expireAt(principal); stream.once("close", () => { this.expiry.clear(principal); if (this.principals.get(principal.id) === principal) this.principals.delete(principal.id); }); authenticated(); send(stream, { type: "account_enrolled", version: 1, principalId: principal.id, hostInstance: this.hostInstance, attestedUntil: principal.attestedUntil, }); } async reattest(stream, principal, token) { let attested = await this.attest(stream, token, principal.key); if (attested === undefined || this.principals.get(principal.id) !== principal) return; if (typeof attested !== "string" && attested.acc !== principal.acc) attested = "presenter_account_mismatch"; if (typeof attested !== "string") { principal.attestedUntil = attested.exp; this.expireAt(principal); return send(stream, { type: "account_reattested", version: 1, attestedUntil: attested.exp }); } if (attested === "attestation_key_mismatch" || attested === "presenter_account_mismatch" || attested === "presenter_account_unavailable") return this.revoke(principal, attested); send(stream, { type: "account_refused", version: 1, reason: attested }); } async attest(stream, token, key) { const authority = this.authority; if (!authority || authority.hostAccount() === undefined) return "presenter_account_unavailable"; const nowS = Math.floor(Date.now() / 1000); let result; try { result = await authority.verify(token, key.export({ type: "spki", format: "der" }), nowS); } catch { // A verifier fault is not the client's refusal: fail closed without a reason. stream.destroy(); return undefined; } if (stream.destroyed) return undefined; if (!result.ok) return result.reason; const { attestation } = result; if (![attestation.dev, attestation.dname, attestation.jti].every(boundedClaim) || !Number.isSafeInteger(attestation.exp) || attestation.exp > nowS + 960) return "attestation_malformed"; const hostAccount = authority.hostAccount(); if (hostAccount === undefined) return "presenter_account_unavailable"; if (attestation.acc !== hostAccount) return "presenter_account_mismatch"; const admitted = this.jtis.admit(attestation.jti, attestation.exp, nowS); return admitted === "ok" ? attestation : admitted; } expireAt(principal) { this.expiry.arm(principal, () => { if (this.principals.get(principal.id) === principal) this.revoke(principal, "attestation_expired"); }); } principalOn(stream) { for (const principal of this.principals.values()) if (principal.kind === "account" && principal.stream === stream) return principal; return undefined; } /** Revokes the principal's registrations now, then closes its stream: with the refusal flushed, if one is given. */ revoke(principal, reason) { this.expiry.clear(principal); this.principals.delete(principal.id); this.revokeStream(principal.stream); if (reason) this.refuse(principal.stream, reason); else principal.stream.destroy(); } refuse(stream, reason) { this.closing.add(stream); refuse(stream, reason); } } //# sourceMappingURL=mcp-presenter-account.js.map