~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
303 lines · 13.6 KB Raw History
/**
 * oauth-scope — single source of ATProto OAuth scope tokens for the market
 * protocol. Every scope string across the polyrepo derives from the canonical
 * registry here; no repo hardcodes a scope list.
 *
 * Unified form (per atproto permission spec):
 *   collections:  repo:<collection>?action=<v1>&action=<v2>   (verbs repeated)
 *   endpoints:    rpc:<nsid>?aud=*                            (never repo-form)
 *   base:         atproto                                     (required)
 *
 * Role sets (REQUESTER_OAUTH_SCOPE, BIDDER_OAUTH_SCOPE, DESKTOP_OAUTH_SCOPE,
 * COMPUTE_SPA_OAUTH_SCOPE, DID_KEY_ASSOCIATOR_OAUTH_SCOPE) are derived from the
 * registry, never duplicated, so a new collection/verb added here flows to
 * every consumer and can't drift.
 */

/** Collection NSIDs that appear in any consumer's scope, keyed by short name. */
const C = {
  RBAC_DID: "com.publicdomainrelay.temp.auth.allowlist.rbacDid",
  TANGLED_VOUCH: "sh.tangled.graph.vouch",
  OFFERING: "com.publicdomainrelay.temp.market.offering",
  BID: "com.publicdomainrelay.temp.market.bid",
  BIDS_FREE: "com.publicdomainrelay.temp.market.bids.free",
  BIDS_X402: "com.publicdomainrelay.temp.market.bids.x402",
  RECEIPT: "com.publicdomainrelay.temp.market.receipt",
  RECEIPTS_FREE: "com.publicdomainrelay.temp.market.receipts.free",
  RECEIPTS_X402: "com.publicdomainrelay.temp.market.receipts.x402",
  EVENT: "com.publicdomainrelay.temp.market.event",
  CONFIG_WIF_SIMPLE: "com.publicdomainrelay.temp.compute.config.wif.simple",
  COMPUTE_VM: "com.publicdomainrelay.temp.compute.vm",
  RFP: "com.publicdomainrelay.temp.market.rfp",
  ACCEPT: "com.publicdomainrelay.temp.market.accept",
  VM_DELETE: "com.publicdomainrelay.temp.compute.events.vm.delete",
  VM_ON_NETWORK: "com.publicdomainrelay.temp.compute.events.vm.onNetwork",
  VM_TERMINATED: "com.publicdomainrelay.temp.compute.events.vm.terminated",
  VM_HOST_KEYS: "com.publicdomainrelay.temp.compute.events.vm.hostKeys",
  GUEST_PUBLIC_KEY: "com.publicdomainrelay.temp.compute.events.guest.publicKey",
  GUEST_SIGNAL_WEBRTC: "com.publicdomainrelay.temp.compute.events.guest.signalWebRTC",
  GUEST_SECRETS: "com.publicdomainrelay.temp.compute.events.guest.secrets",
  GUEST_SECRETS_APPLIED: "com.publicdomainrelay.temp.compute.events.guest.secretsApplied",
  GUEST_SECRETS_REQUESTED: "com.publicdomainrelay.temp.compute.events.guest.secretsRequested",
  GUEST_AGENT_START: "com.publicdomainrelay.temp.compute.events.guest.agentStart",
  GUEST_REQUESTER_DID: "com.publicdomainrelay.temp.compute.events.guest.requesterDid",
  GUEST_SECRETS_UPDATED: "com.publicdomainrelay.temp.compute.events.guest.secretsUpdated",
  GUEST_R2_START: "com.publicdomainrelay.temp.compute.events.guest.r2Start",
  GUEST_R2_RELEASE: "com.publicdomainrelay.temp.compute.events.guest.r2Release",
  GUEST_R2_RELEASED: "com.publicdomainrelay.temp.compute.events.guest.r2Released",
  GUEST_MEMORY_PRESSURE: "com.publicdomainrelay.temp.compute.events.guest.memoryPressure",
  GUEST_OOM_KILL: "com.publicdomainrelay.temp.compute.events.guest.oomKill",
  GUEST_IMAGE: "com.publicdomainrelay.temp.compute.events.guest.image",
  BADGE_BLUE_KEYS: "com.publicdomainrelay.temp.badgeBlueKeys",
  HEARTBEAT: "com.publicdomainrelay.temp.market.heartbeat",
  FEDPROXY_RBAC: "com.fedproxy.rbac",
  FEDPROXY_SSH_PUBLIC_KEY: "com.fedproxy.sshPublicKey",
  BIDDER_ASSOCIATION: "com.publicdomainrelay.temp.market.bidderAssociation",
  ASSOCIATION: "com.publicdomainrelay.temp.market.association",
  ASSOCIATION_REQUEST: "com.publicdomainrelay.temp.market.associationRequest",
  POLICY_GHA_LITE: "computer.socialweb.temp.policy.ghalite",
  POLICY_TYPESCRIPT: "computer.socialweb.temp.policy.typescript",
  INFRA: "computer.socialweb.infra",
  EPHEMERAL_INFRA: "computer.socialweb.ephemeralInfra",
} as const;

/** RPC endpoint scopes, already in unified `rpc:<nsid>?aud=*` form. */
const R = {
  SUBMIT_RFP: "rpc:com.publicdomainrelay.temp.market.submitRfp?aud=*",
  SUBMIT_ACCEPT: "rpc:com.publicdomainrelay.temp.market.submitAccept?aud=*",
  SUBMIT_BID: "rpc:com.publicdomainrelay.temp.market.submitBid?aud=*",
  SUBMIT_EVENT: "rpc:com.publicdomainrelay.temp.market.submitEvent?aud=*",
  ASSOCIATE_CONFIRM: "rpc:com.publicdomainrelay.temp.requester.associateConfirm?aud=*",
  CLUSTER_CREATE: "rpc:com.publicdomainrelay.temp.cluster.createCluster?aud=*",
  CLUSTER_LIST: "rpc:com.publicdomainrelay.temp.cluster.listClusters?aud=*",
  CLUSTER_KUBECONFIG: "rpc:com.publicdomainrelay.temp.cluster.getKubeconfig?aud=*",
  CLUSTER_UP: "rpc:com.publicdomainrelay.temp.cluster.controlPlaneUp?aud=*",
  CLUSTER_DOWN: "rpc:com.publicdomainrelay.temp.cluster.controlPlaneDown?aud=*",
  CLUSTER_DELETE: "rpc:com.publicdomainrelay.temp.cluster.deleteCluster?aud=*",
  ATIRC_MINT: "rpc:chat.atirc.mint?aud=did:web:irc.fedfork.com%23atirc",
  ATIRC_REVOKE: "rpc:chat.atirc.revoke?aud=did:web:irc.fedfork.com%23atirc",
} as const;

/**
 * Canonical registry: collection → verbs it is ever granted (union across all
 * consumers). A verb listed here is granted to every set that includes the
 * collection — additive, never narrowing a consumer's existing grant.
 */
const COLLECTIONS: Record<string, readonly string[]> = {
  [C.RBAC_DID]: ["create", "update"],
  [C.OFFERING]: ["create", "update"],
  [C.BID]: ["create", "update"],
  [C.BIDS_FREE]: ["create", "update"],
  [C.BIDS_X402]: ["create", "update"],
  [C.RECEIPT]: ["create", "update"],
  [C.RECEIPTS_FREE]: ["create", "update"],
  [C.RECEIPTS_X402]: ["create", "update"],
  [C.EVENT]: ["create", "update"],
  [C.CONFIG_WIF_SIMPLE]: ["create", "update"],
  [C.COMPUTE_VM]: ["create", "update"],
  [C.RFP]: ["create", "update"],
  [C.ACCEPT]: ["create", "update"],
  [C.VM_DELETE]: ["create", "update"],
  [C.VM_ON_NETWORK]: ["create", "update"],
  [C.VM_TERMINATED]: ["create"],
  [C.VM_HOST_KEYS]: ["create"],
  [C.GUEST_PUBLIC_KEY]: ["create", "update"],
  [C.GUEST_SIGNAL_WEBRTC]: ["create", "update"],
  [C.GUEST_SECRETS]: ["create", "update"],
  [C.GUEST_SECRETS_APPLIED]: ["create"],
  [C.GUEST_SECRETS_REQUESTED]: ["create"],
  [C.GUEST_AGENT_START]: ["create", "update"],
  [C.GUEST_REQUESTER_DID]: ["create"],
  [C.GUEST_SECRETS_UPDATED]: ["create", "update"],
  [C.GUEST_R2_START]: ["create", "update"],
  [C.GUEST_R2_RELEASE]: ["create", "update"],
  [C.GUEST_R2_RELEASED]: ["create"],
  [C.GUEST_MEMORY_PRESSURE]: ["create"],
  [C.GUEST_OOM_KILL]: ["create"],
  [C.GUEST_IMAGE]: ["create"],
  [C.BADGE_BLUE_KEYS]: ["create", "update", "delete"],
  [C.FEDPROXY_RBAC]: ["create", "update", "delete"],
  [C.FEDPROXY_SSH_PUBLIC_KEY]: ["create"],
  [C.BIDDER_ASSOCIATION]: ["create", "update"],
  [C.ASSOCIATION]: ["create", "update", "delete"],
  [C.ASSOCIATION_REQUEST]: ["create", "update", "delete"],
  [C.POLICY_GHA_LITE]: ["create", "update"],
  [C.POLICY_TYPESCRIPT]: ["create", "update"],
  [C.INFRA]: ["create", "update", "delete"],
  [C.EPHEMERAL_INFRA]: ["create", "update", "delete"],
};

/**
 * Render a collection's verbs as one unified scope token. `verbs` overrides
 * the registry's union verbs so each role requests only what it actually does
 * (e.g. a bidder requests create on `market.bid` even though desktop also
 * updates it — role subsets must not inherit every consumer's verbs).
 */
function collectionScope(nsid: string, verbs?: readonly string[]): string {
  const v = verbs ?? COLLECTIONS[nsid];
  if (!v) throw new Error(`oauth-scope: unknown collection "${nsid}"`);
  return `repo:${nsid}?action=${v.join("&action=")}`;
}

function collectionScopes(nsids: string[], verbs?: readonly string[]): string[] {
  return nsids.map((n) => collectionScope(n, verbs));
}

const CREATE = ["create"] as const;
const CREATE_UPDATE = ["create", "update"] as const;

/** Concatenate scope sets, dropping duplicates while preserving order. */
export function dedupeScopes(...sets: readonly (readonly string[])[]): string[] {
  const seen = new Set<string>();
  const out: string[] = [];
  for (const set of sets) {
    for (const token of set) {
      if (!seen.has(token)) {
        seen.add(token);
        out.push(token);
      }
    }
  }
  return out;
}

/**
 * requester: mirrors RequesterOAuthScope in socialweb-computer-kcp
 * internal/atproto/oauth/scope.go token for token; the requester's identity check
 * refuses a session whose granted scope lacks any of these.
 */
/** Optional requester scopes (RequesterOptionalScopes in scope.go): deletes of its own records, ingress offers, its webrtc ingress listen and offer records, peer records, the ident.link endorsement. */
export const REQUESTER_OPTIONAL_SCOPES: string[] = [
  ...collectionScopes(
    [
      C.COMPUTE_VM, C.RFP, C.ACCEPT, C.EVENT, C.VM_DELETE, C.VM_ON_NETWORK,
      C.GUEST_PUBLIC_KEY, C.GUEST_SIGNAL_WEBRTC, C.GUEST_SECRETS, C.GUEST_AGENT_START,
      C.GUEST_SECRETS_UPDATED, C.GUEST_R2_START, C.GUEST_R2_RELEASE, C.POLICY_GHA_LITE, C.POLICY_TYPESCRIPT,
    ],
    ["delete"],
  ),
  "repo:computer.socialweb.ingress.offer?action=create&action=delete",
  "repo:com.publicdomainrelay.temp.ingress.webRTC?action=create",
  "repo:computer.socialweb.peer?action=create&action=update&action=delete",
  "repo:link.ident.endorsement?action=create&action=delete",
];

export const REQUESTER_OAUTH_SCOPE: string[] = dedupeScopes(
  ["atproto"],
  collectionScopes(
    [
      C.COMPUTE_VM, C.RFP, C.ACCEPT, C.EVENT, C.VM_DELETE, C.VM_ON_NETWORK,
      C.GUEST_PUBLIC_KEY, C.GUEST_SIGNAL_WEBRTC, C.GUEST_SECRETS,
      C.GUEST_AGENT_START, C.GUEST_SECRETS_UPDATED, C.GUEST_R2_START, C.GUEST_R2_RELEASE,
      C.POLICY_GHA_LITE, C.POLICY_TYPESCRIPT,
    ],
    CREATE_UPDATE,
  ),
  [collectionScope(C.FEDPROXY_RBAC, ["create", "update", "delete"])],
  [collectionScope(C.BADGE_BLUE_KEYS, ["create", "update", "delete"])],
  [collectionScope(C.ASSOCIATION, ["create", "update", "delete"])],
  [collectionScope(C.ASSOCIATION_REQUEST, ["create", "update", "delete"])],
  collectionScopes([C.INFRA, C.EPHEMERAL_INFRA], ["create", "update", "delete"]),
  [R.SUBMIT_RFP, R.SUBMIT_ACCEPT, R.SUBMIT_BID, R.SUBMIT_EVENT],
  [R.ATIRC_MINT, R.ATIRC_REVOKE],
  [collectionScope(C.TANGLED_VOUCH, ["create", "update"])],
  REQUESTER_OPTIONAL_SCOPES,
);

/** bidder (hono-bidder, digitalocean-bidder): create everywhere except offering (create+update). */
export const BIDDER_OAUTH_SCOPE: string[] = dedupeScopes(
  ["atproto"],
  collectionScopes(
    [
      C.RBAC_DID, C.BID, C.BIDS_FREE, C.RECEIPT, C.EVENT, C.CONFIG_WIF_SIMPLE,
      C.COMPUTE_VM, C.RFP, C.ACCEPT, C.VM_DELETE, C.VM_ON_NETWORK, C.VM_TERMINATED, C.VM_HOST_KEYS,
      C.GUEST_PUBLIC_KEY, C.GUEST_SIGNAL_WEBRTC, C.GUEST_SECRETS_APPLIED, C.GUEST_SECRETS_REQUESTED,
      C.GUEST_REQUESTER_DID, C.GUEST_R2_RELEASED,
      C.GUEST_MEMORY_PRESSURE, C.GUEST_OOM_KILL, C.GUEST_IMAGE,
      C.FEDPROXY_RBAC, C.BIDDER_ASSOCIATION,
      C.POLICY_GHA_LITE, C.POLICY_TYPESCRIPT,
    ],
    CREATE,
  ),
  [collectionScope(C.OFFERING, CREATE_UPDATE)],
  [collectionScope(C.HEARTBEAT, CREATE_UPDATE)],
  [collectionScope(C.BADGE_BLUE_KEYS, ["create", "update", "delete"])],
  [collectionScope(C.ASSOCIATION, ["create", "update", "delete"])],
  [R.SUBMIT_RFP, R.SUBMIT_ACCEPT, R.SUBMIT_BID, R.SUBMIT_EVENT],
  [
    "repo:computer.socialweb.ingress.listen?action=create&action=update",
    "repo:computer.socialweb.ingress.answer?action=create&action=delete",
    "repo:com.publicdomainrelay.temp.ingress.webRTC?action=create",
  ],
);

/** desktop (hono-desktop / hono-macos-runner-desktop): create+update on its collections. */
export const DESKTOP_OAUTH_SCOPE: string[] = dedupeScopes(
  ["atproto"],
  collectionScopes(
    [
      C.RBAC_DID, C.OFFERING, C.BID, C.BIDS_FREE, C.BIDS_X402, C.RECEIPT,
      C.RECEIPTS_FREE, C.RECEIPTS_X402, C.EVENT, C.CONFIG_WIF_SIMPLE,
      C.FEDPROXY_RBAC, C.BIDDER_ASSOCIATION,
    ],
    CREATE_UPDATE,
  ),
  [collectionScope(C.BADGE_BLUE_KEYS, CREATE)],
  [R.SUBMIT_BID, R.SUBMIT_EVENT],
);

/** compute-spa (browser requester): minimal requester-shaped set, create only. */
export const COMPUTE_SPA_OAUTH_SCOPE: string[] = dedupeScopes(
  ["atproto"],
  collectionScopes(
    [C.COMPUTE_VM, C.BADGE_BLUE_KEYS, C.FEDPROXY_RBAC, C.FEDPROXY_SSH_PUBLIC_KEY],
    CREATE,
  ),
  [R.SUBMIT_RFP, R.SUBMIT_ACCEPT],
);

/**
 * did-key-associator (registered QR client at qr.fedfork.com). The QR login
 * serves both requester and bidder flows, so its scope is the union of both,
 * plus the app's own associateConfirm RPC. Must ⊇ REQUESTER and ⊇ BIDDER.
 */
export const DID_KEY_ASSOCIATOR_OAUTH_SCOPE: string[] = dedupeScopes(
  BIDDER_OAUTH_SCOPE,
  REQUESTER_OAUTH_SCOPE,
  [R.ASSOCIATE_CONFIRM],
);

/**
 * k8s door (socialweb-computer-k8s): the requester set, because a cluster's
 * nodes are provisioned through the market as the signed-in account, plus the
 * cluster lifecycle RPCs the door calls on the account's behalf.
 */
export const K8S_OAUTH_SCOPE: string[] = dedupeScopes(
  REQUESTER_OAUTH_SCOPE,
  [R.CLUSTER_CREATE, R.CLUSTER_LIST, R.CLUSTER_KUBECONFIG, R.CLUSTER_UP, R.CLUSTER_DOWN, R.CLUSTER_DELETE],
);

/**
 * The full canonical union of every scope token in the system — every role's
 * set (with its own verbs), plus every RPC endpoint. Each role ⊆ OAUTH_SCOPE.
 */
export const OAUTH_SCOPE: string[] = dedupeScopes(
  ["atproto"],
  REQUESTER_OAUTH_SCOPE,
  BIDDER_OAUTH_SCOPE,
  DESKTOP_OAUTH_SCOPE,
  COMPUTE_SPA_OAUTH_SCOPE,
  DID_KEY_ASSOCIATOR_OAUTH_SCOPE,
  K8S_OAUTH_SCOPE,
);

/** `OAUTH_SCOPE` joined into the space-separated form used in OAuth requests. */
export const OAUTH_SCOPE_STRING = OAUTH_SCOPE.join(" ");

/**
 * Small localhost-only scope used by did-key-associator's dynamic
 * `http://localhost?...` client id. Preserved verbatim (existing behavior).
 */
export const OAUTH_SCOPE_LOCALHOST =
  "atproto repo:com.publicdomainrelay.temp.badgeBlueKeys?action=create,update,delete " +
  "rpc:com.publicdomainrelay.temp.requester.associateConfirm?aud=*";