Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

5.1 KB
Raw Blame
import { Hono, type MiddlewareHandler } from "hono";
import { z } from "zod";
import { type AuditStore, recordAuditEvent } from "../audit";
import type { AppVariables } from "../auth/guards";
import {
  githubAuthorizationUrl,
  githubRedirectUri,
  sealGithubState,
} from "./github-oauth";
import { PROVIDERS } from "./providers";
import type { IdentityStore } from "./store";

export type IdentityRouteOptions = {
  slackLinking?: boolean;
  github?: { clientId: string; publicUrl: string; encryptionKey: string };
};

/**
 * A person's own linked accounts.
 *
 * Every route is the asker's own: there is no administrator view of somebody else's links and no
 * way to link an account on another person's behalf. What leaves here is what the settings page
 * draws; realm, subject and credential stay on the server.
 */
export function identityRoutes(
  requireUser: MiddlewareHandler<{ Variables: AppVariables }>,
  store?: Pick<IdentityStore, "identitiesFor" | "unlink" | "issueChallenge">,
  auditStore?: AuditStore,
  options: IdentityRouteOptions = {},
) {
  const app = new Hono<{ Variables: AppVariables }>();
  app.use("*", async (context, next) => {
    context.header("cache-control", "no-store");
    await next();
  });
  app.use("*", requireUser);
  if (!store) {
    /*
     * The code is what tells the settings page "this deployment has no identity store" apart from a
     * proxy or platform 503, which must surface as an error rather than hide the section.
     */
    app.all("*", (context) =>
      context.json(
        {
          error: "Linked accounts are not available.",
          code: "identity_unavailable",
        },
        503,
      ),
    );
    return app;
  }
  const live = store;

  app.get("/providers", (context) =>
    context.json({
      providers: {
        slack: Boolean(options.slackLinking),
        github: Boolean(options.github),
      },
    }),
  );

  app.post("/challenges", async (context) => {
    if (!options.slackLinking)
      return context.json(
        {
          error: "That account type cannot be linked here.",
          code: "identity_provider_unavailable",
        },
        404,
      );
    const parsed = z
      .strictObject({ provider: z.literal("slack") })
      .safeParse(await context.req.json().catch(() => null));
    if (!parsed.success)
      return context.json(
        {
          error: "Choose an account type to link.",
          code: "identity_invalid_request",
        },
        400,
      );
    const { code, expiresAt } = await live.issueChallenge(
      context.var.actor.id,
      "slack",
    );
    return context.json({
      challenge: {
        code,
        expiresAt: expiresAt.toISOString(),
        instruction: PROVIDERS.slack.instruction?.(code) ?? "",
      },
    });
  });

  app.post("/github/connect", async (context) => {
    const github = options.github;
    if (!github)
      return context.json(
        {
          error: "That account type cannot be linked here.",
          code: "identity_provider_unavailable",
        },
        404,
      );
    const authorizationUrl = githubAuthorizationUrl({
      clientId: github.clientId,
      redirectUri: githubRedirectUri(github.publicUrl),
      state: await sealGithubState(context.var.actor.id, github.encryptionKey),
    });
    return context.json({ connect: { authorizationUrl } });
  });

  app.get("/links", async (context) => {
    const links = await live.identitiesFor(context.var.actor.id);
    return context.json({
      links: links.map((link) => ({
        id: link.id,
        provider: link.provider,
        title: PROVIDERS[link.provider].title,
        handle: link.handle,
        status: link.status,
        createdAt: link.createdAt.toISOString(),
      })),
    });
  });

  app.delete("/links/:id", async (context) => {
    const actorId = context.var.actor.id;
    const id = context.req.param("id");
    const removed = await live.unlink(actorId, id);
    /*
     * The code is what tells the settings page "this link is already gone" apart from a proxy,
     * missing-route or SPA-fallback 404, which must surface as a failed disconnect.
     */
    if (!removed)
      return context.json(
        { error: "Linked account not found.", code: "identity_link_not_found" },
        404,
      );
    /*
     * After the removal, and not caught: the same order and the same failure as the connected-account
     * disconnect (`mcp.account_disconnected`), where an audit write that throws fails the request.
     * The audit is written after the unlink commits, so such a failure leaves the link already gone
     * and the request failed; a retry answers 404 and writes no audit row.
     *
     * Names the link and its provider only. The subject, realm and credential id stay out of the
     * trail, as they stay out of every response here.
     */
    if (auditStore) {
      await recordAuditEvent(auditStore, {
        eventType: "identity.unlinked",
        targetType: "identity_link",
        targetId: id,
        actorUserId: actorId,
        payload: { actor: actorId, provider: removed.provider },
      });
    }
    return context.body(null, 204);
  });

  return app;
}