wishforge-ide/apps/api/src/internal-auth.ts
eust-w 6d2dcebe65 feat(ide): clone the project into IDE workspaces and push back through Git (FR-H2)
While someone has a workspace open, the gateway asks the API for a 15-minute
write token for the project's platform repository on behalf of that person
(membership and role checked each time) and delivers it to the workspace
backend. The backend writes it to a Git config file outside the repository
(a memory volume in Kubernetes) that the clone includes, and on the first
delivery clones the target branch into a folder named after the project. The
workspace cannot mint credentials itself, so access ends with the last
session.

- People commit and push to the target branch like with any Git server; the
  platform only accepts fast-forwards, and the next wish starts from that
  commit (covered end to end in the local loop test).
- Commits are authored by whoever opened the latest session, from the name and
  email in their sign-in; their own git config overrides it.
- Only the gateway can reach the delivery endpoint: it needs Theia's
  connection token, and the gateway refuses the path for browsers.
- The provisioner pins the workspace to its repository and lets the pod reach
  the Git service; the Kubernetes baseline admits that traffic. External
  repositories are not cloned.
- The local shared Theia clones each project into its own folder.
- The Theia image installs Git, and its stage is named theia: the default
  build target of that Dockerfile is the gateway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:52:37 +08:00

41 lines
2.1 KiB
TypeScript

import { DomainError, type Actor } from '@wishforge/contracts';
import type { Authenticator } from './auth.js';
/**
* Static service identities act for every run of a tenant, so each reaches only what its
* role needs: the workflow coordinator reads state, records waits and requests repairs;
* the deployer leases a built run and requests its release.
*/
const STATIC_ROUTES: Partial<Record<Actor['kind'], RegExp[]>> = {
worker: [/^\/runs\/[^/]+\/(state|wait|repair)$/],
deployer: [/^\/runs\/[^/]+\/(lease|lease\/release|transition)$/, /^\/releases$/],
};
/**
* Resolves the identity behind an `/internal` request and confines it: run-scoped
* execution tokens address only their own run, static service identities only their
* routes, the IDE gateway only workspace authorization and Git credentials, and environment runners only
* their deployment tasks.
*/
export async function internalActor(
auth: Authenticator,
request: { authorization?: string; tenant?: string; path: string; body?: unknown },
): Promise<Actor> {
const actor = await auth.internal(request.authorization, request.tenant);
if (actor.runId) {
const addressed =
/^\/runs\/([^/]+)\//.exec(request.path)?.[1] ??
(request.path === '/evidence' ? (request.body as { runId?: unknown } | undefined)?.runId : undefined);
if (addressed !== actor.runId) throw new DomainError('FORBIDDEN', 'Execution token is scoped to a single run', 403);
}
if (actor.userId === 'workspace-gateway') {
if (!/^\/workspaces\/[^/]+\/(authorize|git-credential)$/.test(request.path))
throw new DomainError('FORBIDDEN', 'Gateway identity is limited to workspace authorization', 403);
} else if (actor.userId.startsWith('runner:')) {
if (!request.path.startsWith('/runner/'))
throw new DomainError('FORBIDDEN', 'Runner identity is limited to assigned deployment tasks', 403);
} else if (!actor.runId && !(STATIC_ROUTES[actor.kind] ?? []).some(route => route.test(request.path)))
throw new DomainError('FORBIDDEN', `The ${actor.kind} service identity cannot call this route`, 403);
return actor;
}