Skip to content

(janitor/dedupe): consolidate Kilo user JWT auth middleware onto worker-utils - #5791

Open
kilo-code-bot[bot] wants to merge 1 commit into
mainfrom
janitor/dedupe/kilo-auth-middleware
Open

(janitor/dedupe): consolidate Kilo user JWT auth middleware onto worker-utils#5791
kilo-code-bot[bot] wants to merge 1 commit into
mainfrom
janitor/dedupe/kilo-auth-middleware

Conversation

@kilo-code-bot

@kilo-code-bot kilo-code-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Consolidate the near-identical Kilo user JWT auth middleware that wasteland and gastown each carried into a single owner in @kilocode/worker-utils.

What changed

  • packages/worker-utils/kilo-auth-middleware — new createKiloAuthMiddleware factory owning the shared core: bearer-token extraction, NEXTAUTH_SECRET resolution, verifyKiloToken, Hono context population (kiloUserId, kiloIsAdmin, kiloApiTokenPepper, kiloGastownAccess, kiloOrgMemberships), and 401/500 error handling.
    • resolveSecret stays service-local (injected), avoiding churn across the ~25 existing import sites in each service.
    • Optional onAuthenticated hook preserves each service's structured-logger user tagging.
  • services/wasteland / services/gastown — delete the duplicated middleware/kilo-auth.middleware.ts and build the middleware from the shared factory with their own Env + logger.
  • The gastown middleware test moves to worker-utils alongside the new owner and now also asserts kiloGastownAccess propagation.

Why

The two copies were byte-equivalent apart from gastown's extra kiloGastownAccess set (an optional claim on KiloTokenPayload) and each service's Env type. A change to JWT claim handling or the error contract would have to be applied to both copies by hand. Both services already depend on @kilocode/worker-utils, so the dependency direction is clean.

Behavior

No behavior change. The shared core is the union of the two copies; wasteland gains an unused kiloGastownAccess context variable (its AuthVariables now includes it).

Verification

  • pnpm --filter @kilocode/worker-utils exec tsgo --noEmit — pass
  • pnpm --filter cloudflare-wasteland exec tsgo --noEmit — pass
  • pnpm --filter cloudflare-gastown exec tsgo --noEmit — pass
  • pnpm --filter @kilocode/worker-utils exec vitest run — 360 passed
  • oxlint + oxfmt --check on changed files — clean

…er-utils

wasteland and gastown each carried a near-identical `kiloAuthMiddleware`
(~48 lines): the same bearer-token extraction, NEXTAUTH_SECRET resolution,
Kilo token verification, Hono context population, and 401/500 error
handling. The only real difference was gastown additionally setting
`kiloGastownAccess` from the token's `gastownAccess` claim, plus each
service's own Env type and structured logger.

A change to Kilo JWT claim handling (e.g. a new required claim, a
different error contract, or a gastownAccess/orgMemberships semantics
fix) would have to be applied to both copies by hand.

Both services already depend on @kilocode/worker-utils (which owns
verifyKiloToken and extractBearerToken), so the dependency direction is
clean:

- packages/worker-utils/kilo-auth-middleware: single owner for the
  middleware core, exported as `createKiloAuthMiddleware`. It takes the
  service's existing `resolveSecret` (kept service-local so the many
  existing import sites don't churn) and an optional `onAuthenticated`
  hook for the service's structured-logger tagging. It always sets the
  full Kilo auth context (kiloUserId, kiloIsAdmin, kiloApiTokenPepper,
  kiloGastownAccess, kiloOrgMemberships); gastownAccess is an optional
  claim so setting it in wasteland is a no-op.
- wasteland/gastown: delete the duplicated middleware files and build the
  middleware from the shared factory with their own Env + logger.

The gastown middleware unit test moves to worker-utils alongside the new
owner and now also asserts gastownAccess context propagation.

No behavior change: the shared core is the union of the two copies, and
wasteland simply gains an unused `kiloGastownAccess` context variable.
@kilo-code-bot kilo-code-bot Bot added the janitor Automated dead-code/duplication cleanup label Sep 1, 2026
@kilo-code-bot

kilo-code-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

Shared createKiloAuthMiddleware preserves the prior wasteland/gastown JWT auth behavior, with types, context variables, and call sites aligned.

Files Reviewed (9 files)
  • packages/worker-utils/package.json
  • packages/worker-utils/src/index.ts
  • packages/worker-utils/src/kilo-auth-middleware.test.ts
  • packages/worker-utils/src/kilo-auth-middleware.ts
  • services/gastown/src/gastown.worker.ts
  • services/gastown/src/middleware/kilo-auth.middleware.ts
  • services/wasteland/src/middleware/auth.middleware.ts
  • services/wasteland/src/middleware/kilo-auth.middleware.ts
  • services/wasteland/src/wasteland.worker.ts

Reviewed by grok-4.6 · Input: 168.1K · Output: 12.7K · Cached: 291.6K

Review guidance: REVIEW.md from base branch main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

janitor Automated dead-code/duplication cleanup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant