-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Device Auth Workflow #4178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Device Auth Workflow #4178
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
548f8ee
feat: device auth workflow
catrielmuller 414282a
refactor: add changeset
catrielmuller d261094
refactor: add translations for device auth
catrielmuller 50a6647
Update webview-ui/src/App.tsx
catrielmuller 314d490
refactor: isolate the deviceAuthHandler
catrielmuller 80e04db
refactor: isolate the changes
catrielmuller 830c8b7
refactor: add kilocode_change
catrielmuller 0dbbbda
refactor: refactor Device Auth Card
catrielmuller b4dc917
refactor: improve qrcode generation
catrielmuller b7b0f67
refactor: improve changelog
catrielmuller 9da4ece
refactor: rebase // fix conflicts
catrielmuller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "kilo-code": minor | ||
| --- | ||
|
|
||
| Added a new device authorization flow for Kilo Gateway that makes it easier to connect your editor to your Kilo account. Instead of manually copying API tokens, you can now: | ||
|
|
||
| - Scan a QR code with your phone or click to open the authorization page in your browser | ||
| - Approve the connection from your browser | ||
| - Automatically get authenticated without copying any tokens | ||
|
|
||
| This streamlined workflow provides a more secure and user-friendly way to authenticate, similar to how you connect devices to services like Netflix or YouTube. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { z } from "zod" | ||
|
|
||
| /** | ||
| * Device authorization response from initiate endpoint | ||
| */ | ||
| export const DeviceAuthInitiateResponseSchema = z.object({ | ||
| /** Verification code to display to user */ | ||
| code: z.string(), | ||
| /** URL for user to visit in browser */ | ||
| verificationUrl: z.string(), | ||
| /** Time in seconds until code expires */ | ||
| expiresIn: z.number(), | ||
| }) | ||
|
|
||
| export type DeviceAuthInitiateResponse = z.infer<typeof DeviceAuthInitiateResponseSchema> | ||
|
|
||
| /** | ||
| * Device authorization poll response | ||
| */ | ||
| export const DeviceAuthPollResponseSchema = z.object({ | ||
| /** Current status of the authorization */ | ||
| status: z.enum(["pending", "approved", "denied", "expired"]), | ||
| /** API token (only present when approved) */ | ||
| token: z.string().optional(), | ||
| /** User ID (only present when approved) */ | ||
| userId: z.string().optional(), | ||
| /** User email (only present when approved) */ | ||
| userEmail: z.string().optional(), | ||
| }) | ||
|
|
||
| export type DeviceAuthPollResponse = z.infer<typeof DeviceAuthPollResponseSchema> | ||
|
|
||
| /** | ||
| * Device auth state for UI | ||
| */ | ||
| export interface DeviceAuthState { | ||
| /** Current status of the auth flow */ | ||
| status: "idle" | "initiating" | "pending" | "polling" | "success" | "error" | "cancelled" | ||
| /** Verification code */ | ||
| code?: string | ||
| /** URL to visit for verification */ | ||
| verificationUrl?: string | ||
| /** Expiration time in seconds */ | ||
| expiresIn?: number | ||
| /** Error message if failed */ | ||
| error?: string | ||
| /** Time remaining in seconds */ | ||
| timeRemaining?: number | ||
| /** User email when successful */ | ||
| userEmail?: string | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import * as vscode from "vscode" | ||
| import { DeviceAuthService } from "../../../services/kilocode/DeviceAuthService" | ||
| import type { ExtensionMessage } from "../../../shared/ExtensionMessage" | ||
|
|
||
| /** | ||
| * Callbacks required by DeviceAuthHandler to communicate with the provider | ||
| */ | ||
| export interface DeviceAuthHandlerCallbacks { | ||
| postMessageToWebview: (message: ExtensionMessage) => Promise<void> | ||
| log: (message: string) => void | ||
| showInformationMessage: (message: string) => void | ||
| } | ||
|
|
||
| /** | ||
| * Handles device authorization flow for Kilo Code authentication | ||
| * This class encapsulates all device auth logic to keep ClineProvider clean | ||
| */ | ||
| export class DeviceAuthHandler { | ||
| private deviceAuthService?: DeviceAuthService | ||
| private callbacks: DeviceAuthHandlerCallbacks | ||
|
|
||
| constructor(callbacks: DeviceAuthHandlerCallbacks) { | ||
| this.callbacks = callbacks | ||
| } | ||
|
|
||
| /** | ||
| * Start the device authorization flow | ||
| */ | ||
| async startDeviceAuth(): Promise<void> { | ||
| try { | ||
| // Clean up any existing device auth service | ||
| if (this.deviceAuthService) { | ||
| this.deviceAuthService.dispose() | ||
| } | ||
|
|
||
| this.deviceAuthService = new DeviceAuthService() | ||
|
|
||
| // Set up event listeners | ||
| this.deviceAuthService.on("started", (data: any) => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthStarted", | ||
| deviceAuthCode: data.code, | ||
| deviceAuthVerificationUrl: data.verificationUrl, | ||
| deviceAuthExpiresIn: data.expiresIn, | ||
| }) | ||
| // Open browser automatically | ||
| vscode.env.openExternal(vscode.Uri.parse(data.verificationUrl)) | ||
| }) | ||
|
|
||
| this.deviceAuthService.on("polling", (timeRemaining: any) => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthPolling", | ||
| deviceAuthTimeRemaining: timeRemaining, | ||
| }) | ||
| }) | ||
|
|
||
| this.deviceAuthService.on("success", async (token: any, userEmail: any) => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthComplete", | ||
| deviceAuthToken: token, | ||
| deviceAuthUserEmail: userEmail, | ||
| }) | ||
|
|
||
| this.callbacks.showInformationMessage( | ||
| `Kilo Code successfully configured! Authenticated as ${userEmail}`, | ||
| ) | ||
|
|
||
| // Clean up | ||
| this.deviceAuthService?.dispose() | ||
| this.deviceAuthService = undefined | ||
| }) | ||
|
|
||
| this.deviceAuthService.on("denied", () => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthFailed", | ||
| deviceAuthError: "Authorization was denied", | ||
| }) | ||
|
|
||
| this.deviceAuthService?.dispose() | ||
| this.deviceAuthService = undefined | ||
| }) | ||
|
|
||
| this.deviceAuthService.on("expired", () => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthFailed", | ||
| deviceAuthError: "Authorization code expired. Please try again.", | ||
| }) | ||
|
|
||
| this.deviceAuthService?.dispose() | ||
| this.deviceAuthService = undefined | ||
| }) | ||
|
|
||
| this.deviceAuthService.on("error", (error: any) => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthFailed", | ||
| deviceAuthError: error.message, | ||
| }) | ||
|
|
||
| this.deviceAuthService?.dispose() | ||
| this.deviceAuthService = undefined | ||
| }) | ||
|
|
||
| this.deviceAuthService.on("cancelled", () => { | ||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthCancelled", | ||
| }) | ||
| }) | ||
|
|
||
| // Start the auth flow | ||
| await this.deviceAuthService.initiate() | ||
| } catch (error) { | ||
| this.callbacks.log(`Error starting device auth: ${error instanceof Error ? error.message : String(error)}`) | ||
|
|
||
| this.callbacks.postMessageToWebview({ | ||
| type: "deviceAuthFailed", | ||
| deviceAuthError: error instanceof Error ? error.message : "Failed to start authentication", | ||
| }) | ||
|
|
||
| this.deviceAuthService?.dispose() | ||
| this.deviceAuthService = undefined | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cancel the device authorization flow | ||
| */ | ||
| cancelDeviceAuth(): void { | ||
| if (this.deviceAuthService) { | ||
| this.deviceAuthService.cancel() | ||
| // Clean up the service after cancellation | ||
| // Use setTimeout to avoid disposing during event emission | ||
| setTimeout(() => { | ||
| if (this.deviceAuthService) { | ||
| this.deviceAuthService.dispose() | ||
| this.deviceAuthService = undefined | ||
| } | ||
| }, 0) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clean up resources | ||
| */ | ||
| dispose(): void { | ||
| if (this.deviceAuthService) { | ||
| this.deviceAuthService.dispose() | ||
| this.deviceAuthService = undefined | ||
| } | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.