(janitor/dedupe): consolidate Kilo user JWT auth middleware onto worker-utils - #5791
Open
kilo-code-bot[bot] wants to merge 1 commit into
Open
(janitor/dedupe): consolidate Kilo user JWT auth middleware onto worker-utils#5791kilo-code-bot[bot] wants to merge 1 commit into
kilo-code-bot[bot] wants to merge 1 commit into
Conversation
…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.
Contributor
Author
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryShared Files Reviewed (9 files)
Reviewed by grok-4.6 · Input: 168.1K · Output: 12.7K · Cached: 291.6K Review guidance: REVIEW.md from base branch |
pandemicsyn
approved these changes
Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Consolidate the near-identical Kilo user JWT auth middleware that
wastelandandgastowneach carried into a single owner in@kilocode/worker-utils.What changed
packages/worker-utils/kilo-auth-middleware— newcreateKiloAuthMiddlewarefactory owning the shared core: bearer-token extraction,NEXTAUTH_SECRETresolution,verifyKiloToken, Hono context population (kiloUserId,kiloIsAdmin,kiloApiTokenPepper,kiloGastownAccess,kiloOrgMemberships), and 401/500 error handling.resolveSecretstays service-local (injected), avoiding churn across the ~25 existing import sites in each service.onAuthenticatedhook preserves each service's structured-logger user tagging.services/wasteland/services/gastown— delete the duplicatedmiddleware/kilo-auth.middleware.tsand build the middleware from the shared factory with their own Env + logger.worker-utilsalongside the new owner and now also assertskiloGastownAccesspropagation.Why
The two copies were byte-equivalent apart from gastown's extra
kiloGastownAccessset (an optional claim onKiloTokenPayload) 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
kiloGastownAccesscontext variable (itsAuthVariablesnow includes it).Verification
pnpm --filter @kilocode/worker-utils exec tsgo --noEmit— passpnpm --filter cloudflare-wasteland exec tsgo --noEmit— passpnpm --filter cloudflare-gastown exec tsgo --noEmit— passpnpm --filter @kilocode/worker-utils exec vitest run— 360 passedoxlint+oxfmt --checkon changed files — clean