Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 3 additions & 55 deletions packages/kilo-gateway/src/auth/device-auth-tui.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,11 @@
import { execFile } from "child_process"
import type { DeviceAuthInitiateResponse, DeviceAuthPollResponse } from "../types.js"
import type { DeviceAuthPollResponse } from "../types.js"
import { poll } from "./polling.js"
import { initiateDeviceAuth, pollDeviceAuth } from "./device.js"
import { getKiloProfile, getKiloDefaultModel, defaultOrganizationId } from "../api/profile.js"
import { KILO_API_BASE, POLL_INTERVAL_MS } from "../api/constants.js"
import { POLL_INTERVAL_MS } from "../api/constants.js"
import type { AuthOuathResult } from "@kilocode/plugin"

/**
* Initiate device authorization flow
* @returns Device authorization details
* @throws Error if initiation fails
*/
async function initiateDeviceAuth(): Promise<DeviceAuthInitiateResponse> {
const response = await fetch(`${KILO_API_BASE}/api/device-auth/codes`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
})

if (!response.ok) {
if (response.status === 429) {
throw new Error("Too many pending authorization requests. Please try again later.")
}
throw new Error(`Failed to initiate device authorization: ${response.status}`)
}

const data = await response.json()
return data as DeviceAuthInitiateResponse
}

/**
* Poll for device authorization status
* @param code The verification code
* @returns Poll response with status and optional token
* @throws Error if polling fails
*/
async function pollDeviceAuth(code: string): Promise<DeviceAuthPollResponse> {
const response = await fetch(`${KILO_API_BASE}/api/device-auth/codes/${code}`)

if (response.status === 202) {
return { status: "pending" }
}

if (response.status === 403) {
return { status: "denied" }
}

if (response.status === 410) {
return { status: "expired" }
}

if (!response.ok) {
throw new Error(`Failed to poll device authorization: ${response.status}`)
}

const data = await response.json()
return data as DeviceAuthPollResponse
}

/**
* TUI-compatible device authorization flow
*
Expand Down
59 changes: 2 additions & 57 deletions packages/kilo-gateway/src/auth/device-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,9 @@ import open from "open"
import { spinner } from "@clack/prompts"
import type { DeviceAuthInitiateResponse, DeviceAuthPollResponse } from "../types.js"
import { poll, formatTimeRemaining } from "./polling.js"
import { initiateDeviceAuth, pollDeviceAuth } from "./device.js"
import { getKiloProfile, getKiloDefaultModel, promptOrganizationSelection } from "../api/profile.js"
import { KILO_API_BASE, POLL_INTERVAL_MS } from "../api/constants.js"

/**
* Initiate device authorization flow
* @returns Device authorization details
* @throws Error if initiation fails
*/
async function initiateDeviceAuth(): Promise<DeviceAuthInitiateResponse> {
const response = await fetch(`${KILO_API_BASE}/api/device-auth/codes`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
})

if (!response.ok) {
if (response.status === 429) {
throw new Error("Too many pending authorization requests. Please try again later.")
}
throw new Error(`Failed to initiate device authorization: ${response.status}`)
}

const data = await response.json()
return data as DeviceAuthInitiateResponse
}

/**
* Poll for device authorization status
* @param code The verification code
* @returns Poll response with status and optional token
* @throws Error if polling fails
*/
async function pollDeviceAuth(code: string): Promise<DeviceAuthPollResponse> {
const response = await fetch(`${KILO_API_BASE}/api/device-auth/codes/${code}`)

if (response.status === 202) {
// Still pending
return { status: "pending" }
}

if (response.status === 403) {
// Denied by user
return { status: "denied" }
}

if (response.status === 410) {
// Code expired
return { status: "expired" }
}

if (!response.ok) {
throw new Error(`Failed to poll device authorization: ${response.status}`)
}

const data = await response.json()
return data as DeviceAuthPollResponse
}
import { POLL_INTERVAL_MS } from "../api/constants.js"

export interface DeviceAuthResult {
token: string
Expand Down
44 changes: 44 additions & 0 deletions packages/kilo-gateway/src/auth/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { DeviceAuthInitiateResponse, DeviceAuthPollResponse } from "../types.js"
import { KILO_API_BASE } from "../api/constants.js"

export async function initiateDeviceAuth(): Promise<DeviceAuthInitiateResponse> {
const response = await fetch(`${KILO_API_BASE}/api/device-auth/codes`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
})

if (!response.ok) {
if (response.status === 429) {
throw new Error("Too many pending authorization requests. Please try again later.")
}
throw new Error(`Failed to initiate device authorization: ${response.status}`)
}

const data = await response.json()
return data as DeviceAuthInitiateResponse
}

export async function pollDeviceAuth(code: string): Promise<DeviceAuthPollResponse> {
const response = await fetch(`${KILO_API_BASE}/api/device-auth/codes/${code}`)

if (response.status === 202) {
return { status: "pending" }
}

if (response.status === 403) {
return { status: "denied" }
}

if (response.status === 410) {
return { status: "expired" }
}

if (!response.ok) {
throw new Error(`Failed to poll device authorization: ${response.status}`)
}

const data = await response.json()
return data as DeviceAuthPollResponse
}
35 changes: 35 additions & 0 deletions packages/kilo-gateway/test/auth/device.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { afterEach, expect, mock, spyOn, test } from "bun:test"
import { KILO_API_BASE } from "../../src/api/constants.js"
import { initiateDeviceAuth, pollDeviceAuth } from "../../src/auth/device.js"

afterEach(() => mock.restore())

test("device initiation preserves the POST, JSON, and rate-limit error", async () => {
const data = { code: "ABCD", verificationUrl: "https://example.com/device", expiresIn: 600, extra: true }
const request = spyOn(globalThis, "fetch").mockResolvedValue(Response.json(data))

await expect(initiateDeviceAuth()).resolves.toEqual(data)
expect(request.mock.calls).toStrictEqual([
[`${KILO_API_BASE}/api/device-auth/codes`, { method: "POST", headers: { "Content-Type": "application/json" } }],
])
request.mockResolvedValue(new Response(null, { status: 429 }))
await expect(initiateDeviceAuth()).rejects.toThrow("Too many pending authorization requests. Please try again later.")
})

test("device polling preserves the bare GET and JSON", async () => {
const data = { status: "approved", token: "token", userEmail: "user@example.com", extra: true }
const request = spyOn(globalThis, "fetch").mockResolvedValue(Response.json(data))

await expect(pollDeviceAuth("code/with space")).resolves.toEqual(data)
expect(request.mock.calls).toStrictEqual([[`${KILO_API_BASE}/api/device-auth/codes/code/with space`]])
})

test.each([
[202, "pending"],
[403, "denied"],
[410, "expired"],
])("device polling maps HTTP %i to %s without parsing JSON", async (status, state) => {
spyOn(globalThis, "fetch").mockResolvedValue(new Response("not json", { status }))

await expect(pollDeviceAuth("code")).resolves.toEqual({ status: state })
})
9 changes: 0 additions & 9 deletions script/kilocode-duplication-allowlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,6 @@
"owner": "kilo-console",
"reason": "Existing duplication before the ratchet; remove through a focused, behavior-preserving extraction."
},
{
"files": ["packages/kilo-gateway/src/auth/device-auth-tui.ts", "packages/kilo-gateway/src/auth/device-auth.ts"],
"fingerprint": "e6ac9a2221c1deb4",
"maxMatches": 1,
"maxTokens": 194,
"kind": "legacy",
"owner": "kilo-gateway",
"reason": "Existing duplication before the ratchet; remove through a focused, behavior-preserving extraction."
},
{
"files": [
"packages/kilo-indexing/src/indexing/embedders/openai-compatible.ts",
Expand Down
Loading