31 lines · 1.2 KB
import { scopedRepoPermission, TOKEN_AREAS } from "../token-scope";
import type { TokenArea } from "../token-scope";
import { requireScopedRepo } from "./http";
import type { ApiContext } from "./http";
import type { Permission } from "~/lib/types";

export function getCredential(ctx: ApiContext): Response {
  const grant = ctx.credential.grant;
  if (grant?.kind === "fine") {
    return Response.json({
      kind: "fine",
      token_id: grant.tokenId,
      all_repos: grant.allRepos,
      permissions: grant.permissions,
    });
  }
  return Response.json({ kind: grant?.kind ?? "none", token_id: grant?.tokenId ?? null });
}

/** The caller's effective permission per capability area, as enforced by every other repo endpoint. */
export async function getRepoPermissions(ctx: ApiContext, owner: string, name: string): Promise<Response> {
  const { repo } = await requireScopedRepo(ctx, owner, name, "contents", "none");
  const entries = await Promise.all(
    TOKEN_AREAS.map(async (area: TokenArea): Promise<[TokenArea, Permission]> => [
      area,
      await scopedRepoPermission(repo, ctx.credential, area),
    ]),
  );
  return Response.json(Object.fromEntries(entries));
}