@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
182 lines • 7.34 kB
JavaScript
import { accountLapse } from "./mcp-presenter-account-lifetime.js";
import { PresenterJtiCache } from "./mcp-presenter-attestation.js";
import { SecretHostTokens } from "./secret-host-token.js";
import { checkSealedAnswer } from "./secret-sealed-answers.js";
import { secretBinding, secretRequestHash } from "./secret-sealed.js";
/** The control's sealed holders: eligibility, signed requests and token refreshes, all synchronous. */
export class SealedSecretHolders {
/** `undefined` when the authority cannot mint host tokens and verify step-up tokens: no sealed route at all. */
static create(host, account, principals) {
const tokens = SecretHostTokens.create(host.authority.instance, account);
return tokens && account ? new SealedSecretHolders(host, account, tokens, principals) : undefined;
}
constructor(host, account, tokens, principals) {
this.offered = new Map();
this.listeners = new Set();
/** Step-up `jti`s of this host incarnation (6.5): kept until `exp + 60`, refusing rather than evicting at 4096. */
this.jtis = new PresenterJtiCache();
this.watched = new WeakSet();
this.host = host;
this.authority = host.authority;
this.account = account;
this.tokens = tokens;
this.principals = principals;
this.unsubscribe = tokens.onToken((durableSessionId, token) => {
for (const offered of this.offered.values())
if (offered.durableSessionId === durableSessionId && !offered.settled)
for (const holder of offered.holders)
if (this.current(holder))
holder.registration.connection.send({
type: "mcp_presenter_secret_token",
v: 1,
hostInstance: this.authority.instance,
durableSessionId,
hostToken: token.token,
});
});
this.detach = host.attachSealedSecrets?.(this);
}
holders(sessionId) {
const binding = this.host.getMcpFormBinding(sessionId);
if (!binding)
return new Set();
if (binding.subscribe && !this.watched.has(binding)) {
this.watched.add(binding);
binding.subscribe(() => {
for (const listener of [...this.listeners])
listener();
});
}
return new Set(this.authority.secretCandidates(binding).filter((grant) => this.eligible(grant)));
}
eligible(holder) {
const binding = holder.registration.binding;
return (this.authority.secretCandidates(binding).includes(holder) &&
this.unlapsed(holder) !== undefined &&
this.tokens.current(holder.scope.durableSessionId) !== undefined);
}
principalOf(holder) {
return this.unlapsed(holder)?.id;
}
async answer(holder, frame) {
const offered = this.offered.get(frame.uiRequestId);
const principal = this.unlapsed(holder);
if (!offered || !offered.holders.has(holder) || !principal)
return { error: "secret_answer_refused" };
const { req } = offered;
const checked = await checkSealedAnswer({
frame,
principal,
req,
deadlineAtMs: offered.deadlineAtMs,
hostToken: this.tokens.current(offered.durableSessionId),
account: this.account,
});
if (!checked.ok)
return { error: checked.error };
const { jti, exp } = checked;
const value = frame.value;
return {
uiRequestId: frame.uiRequestId,
principal: principal.id,
grantId: frame.grantId,
answerId: frame.answerId,
req,
...(value === undefined || jti === undefined || exp === undefined
? {}
: { value: { jti, exp, enc: value.enc.toString("base64url"), ct: value.ct.toString("base64url") } }),
};
}
/** The host incarnation's step-up `jti` cache (6.5), for in-process latches. */
admitJti(jti, exp) {
return this.jtis.admit(jti, exp, Math.floor(Date.now() / 1000)) === "ok";
}
settle(holders, uiRequestId, outcome, winnerGrant) {
const offered = this.offered.get(uiRequestId);
if (!offered)
return;
for (const holder of holders) {
if (!offered.holders.has(holder))
continue;
holder.registration.connection.send({
type: "mcp_presenter_secret_settled",
v: 1,
uiRequestId,
outcome,
elsewhere: holder.id !== winnerGrant,
});
}
}
current(holder) {
return this.authority.isCurrent(holder);
}
connectionOf(holder) {
return holder.registration.connection.id;
}
offer(sessionId, holders, offer) {
const first = [...holders][0];
if (!first)
return;
const durableSessionId = first.scope.durableSessionId;
const token = this.tokens.current(durableSessionId);
if (!token)
return;
const binding = secretBinding({
hostInstance: this.authority.instance,
hostJkt: this.tokens.hostJkt,
durableSessionId,
...offer,
});
const sig = this.tokens.signRequest(binding);
this.offered.set(offer.uiRequestId, {
durableSessionId,
holders,
req: secretRequestHash(binding),
deadlineAtMs: offer.deadlineAtMs,
});
for (const holder of holders)
holder.registration.connection.send({
type: "mcp_presenter_secret_request",
v: 1,
sessionId,
durableSessionId,
uiRequestId: offer.uiRequestId,
title: offer.title,
...(offer.placeholder === undefined ? {} : { placeholder: offer.placeholder }),
deadlineAtMs: offer.deadlineAtMs,
hostInstance: this.authority.instance,
hpkePk: offer.hpkePk,
hostPk: this.tokens.hostPk,
hostToken: token.token,
sig,
});
}
withdraw(uiRequestId) {
this.offered.delete(uiRequestId);
}
retire(uiRequestId) {
const offered = this.offered.get(uiRequestId);
if (offered)
offered.settled = true;
}
onChange(listener) {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
dispose() {
this.detach?.();
this.unsubscribe();
this.listeners.clear();
this.offered.clear();
this.jtis.clear();
this.tokens.dispose();
}
/** 5.1 condition 4: the grant's account principal when it has not lapsed. */
unlapsed(grant) {
const principal = this.principals.get(grant.registration.principal);
return principal?.kind === "account" && accountLapse(principal, this.account) === undefined
? principal
: undefined;
}
}
//# sourceMappingURL=secret-sealed-holders.js.map