Skip to content

Phase 2: Worker auth layer for GitHub App tokens (#432) #437

Closed

Description

@neuromechanist

Scope

Add the Worker-side machinery to mint and cache GitHub App installation tokens. No callsite changes yet — only the helper + env wiring + tests.

Implementation

  • New file `backend/src/services/github-auth.ts`:
    • `signAppJwt(appId, privateKeyPem): Promise` — RSASSA-PKCS1-v1_5 via `crypto.subtle.sign` on a 10-minute JWT (`iat`, `exp`, `iss=appId`).
    • `fetchInstallationToken(jwt, installationId): Promise<{ token, expiresAt }>` — POST to `/app/installations/:id/access_tokens`.
    • Module-level cache keyed by installation ID. Refresh ~5 min before `expiresAt`. Singleton in-flight Promise to coalesce parallel callers.
    • `getInstallationToken(env, installationId): Promise` — public entry point.
  • Discriminated source type:
    ```ts
    export type GitHubAuth =
    | { kind: "pat"; token: string }
    | { kind: "app"; installationId: string; getToken: () => Promise };
    ```
  • `getDefaultGitHubAuth(env)` — returns `{ kind: "app", ... }` if `GITHUB_APP_ID` + `GITHUB_APP_PRIVATE_KEY` + `GITHUB_APP_INSTALLATION_ID` (or per-org map) are all configured; otherwise `{ kind: "pat", token: env.GITHUB_ADMIN_PAT }`. Phase 3 swaps callers over to this helper.
  • `Bindings` interface in `backend/src/index.ts` gets the three new optional secrets.

Tests

  • `test/github-auth.test.ts` — unit tests for JWT structure, cache hit/miss, refresh-before-expiry, two-installation cache isolation. Use the project's no-mocks rule: stand up a tiny local HTTP server inside the test that returns a canned access-token response.

Acceptance

  • New helper compiles, types-check, tests pass.
  • Worker env on dev gets the three new secrets set (using the App from Phase 1) — verify with a one-shot `/admin/github-auth-debug` endpoint (or temporary log line) that the App path returns a valid token. Tear down the debug endpoint before merge.
  • `GITHUB_ADMIN_PAT` still used by every existing caller; this PR adds no behavior change to live flows.

Part of epic #432. Depends on Phase 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    infrastructure

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions