-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: add Kiro reauthentication action #2980
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d135aae
feat: add Settings toggle for Claude OAuth background token repair
Vit129 e70b03e
feat: add Codex Re-authenticate button in Settings
Vit129 874600c
feat: add Kiro Re-authenticate button in Settings
Vit129 4499e38
fix: surface Kiro device-flow URL/code before login times out
Vit129 e078fa8
fix: only refresh Kiro usage after a successful login
Vit129 dc0b9a4
test: cover Kiro login runner timeout, output-drain, and progress paths
Vit129 76c12ef
fix: remove duplicate Claude Keychain prompt-policy toggle
Vit129 edfca28
Merge branch 'main' into pr-reauth-buttons
steipete 6352ab7
feat: add Kiro reauthentication action
steipete 15e39ba
fix: localize Kiro reauthentication
steipete 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
37 changes: 37 additions & 0 deletions
37
Sources/CodexBar/Providers/Kiro/KiroLoginAlertPresentation.swift
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,37 @@ | ||
| import Foundation | ||
|
|
||
| enum KiroLoginAlertPresentation { | ||
| static func alertInfo(for result: KiroLoginRunner.Result) -> CodexLoginAlertInfo? { | ||
| switch result.outcome { | ||
| case .success: | ||
| return nil | ||
| case .missingBinary: | ||
| return CodexLoginAlertInfo( | ||
| title: L("Kiro CLI not found"), | ||
| message: L("Install kiro-cli and try again.")) | ||
| case let .launchFailed(message): | ||
| return CodexLoginAlertInfo(title: L("Could not start kiro-cli login"), message: message) | ||
| case .timedOut: | ||
| return CodexLoginAlertInfo( | ||
| title: L("Kiro login timed out"), | ||
| message: self.trimmedOutput(result.output)) | ||
| case let .failed(status): | ||
| let statusLine = String(format: L("kiro-cli login exited with status %d."), status) | ||
| let message = self.trimmedOutput(result.output.isEmpty ? statusLine : result.output) | ||
| return CodexLoginAlertInfo(title: L("Kiro login failed"), message: message) | ||
| } | ||
| } | ||
|
|
||
| private static func trimmedOutput(_ text: String) -> String { | ||
| let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let limit = 600 | ||
| if trimmed.isEmpty { | ||
| return L("No output captured.") | ||
| } | ||
| if trimmed.count <= limit { | ||
| return trimmed | ||
| } | ||
| let idx = trimmed.index(trimmed.startIndex, offsetBy: limit) | ||
| return "\(trimmed[..<idx])…" | ||
| } | ||
| } |
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,27 @@ | ||
| import CodexBarCore | ||
|
|
||
| @MainActor | ||
| extension StatusItemController { | ||
| @discardableResult | ||
| func runKiroLoginFlow() async -> Bool { | ||
| self.loginPhase = .requesting | ||
| defer { self.loginPhase = .idle } | ||
|
|
||
| let result = await KiroLoginRunner.run(timeout: 120) { [weak self] progressOutput in | ||
| Task { @MainActor in | ||
| self?.presentLoginAlert( | ||
| title: L("Complete Kiro login in your browser"), | ||
| message: progressOutput) | ||
| } | ||
| } | ||
| guard !Task.isCancelled else { return false } | ||
| if let info = KiroLoginAlertPresentation.alertInfo(for: result) { | ||
| self.presentLoginAlert(title: info.title, message: info.message) | ||
| } | ||
| let length = result.output.count | ||
| self.loginLogger.info("Kiro login", metadata: ["outcome": "\(result.outcome)", "length": "\(length)"]) | ||
| guard case .success = result.outcome else { return false } | ||
| self.postLoginNotification(for: .kiro) | ||
| return true | ||
| } | ||
| } |
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,226 @@ | ||
| import CodexBarCore | ||
| import Darwin | ||
| import Foundation | ||
|
|
||
| /// Spawns `kiro-cli login`, which opens a browser OAuth flow and blocks until it completes. | ||
| /// Mirrors ``CodexLoginRunner`` — same subprocess-lifecycle shape as `codex login`. | ||
| struct KiroLoginRunner { | ||
| struct Result: Equatable { | ||
| enum Outcome: Equatable { | ||
| case success | ||
| case timedOut | ||
| case failed(status: Int32) | ||
| case missingBinary | ||
| case launchFailed(String) | ||
| } | ||
|
|
||
| let outcome: Outcome | ||
| let output: String | ||
| } | ||
|
|
||
| /// Polling cadence while the login subprocess is still running, used to surface a | ||
| /// device-flow URL/code before the process exits (`kiro-cli login` prints them, then blocks | ||
| /// while polling for the browser approval). | ||
| private static let progressPollInterval: TimeInterval = 0.5 | ||
|
|
||
| static func run( | ||
| timeout: TimeInterval = 120, | ||
| outputDrainTimeout: TimeInterval = 3, | ||
| environment: [String: String] = ProcessInfo.processInfo.environment, | ||
| loginPATH: [String]? = LoginShellPathCache.shared.current, | ||
| onProgress: (@Sendable (String) -> Void)? = nil) async -> Result | ||
| { | ||
| await Task(priority: .userInitiated) { | ||
| var env = environment | ||
| env["PATH"] = PathBuilder.effectivePATH( | ||
| purposes: [.rpc, .tty, .nodeTooling], | ||
| env: env, | ||
| loginPATH: loginPATH) | ||
|
|
||
| guard let executable = BinaryLocator.resolveKiroCLIBinary(env: env, loginPATH: loginPATH) else { | ||
| return Result(outcome: .missingBinary, output: "") | ||
| } | ||
|
|
||
| let process = Process() | ||
| process.executableURL = URL(fileURLWithPath: "/usr/bin/env") | ||
| process.arguments = [executable, "login"] | ||
| process.environment = env | ||
|
|
||
| let stdout = Pipe() | ||
| let stderr = Pipe() | ||
| process.standardOutput = stdout | ||
| process.standardError = stderr | ||
| let stdoutCapture = ProcessPipeCapture(pipe: stdout) | ||
| let stderrCapture = ProcessPipeCapture(pipe: stderr) | ||
|
|
||
| let termination = ProcessTermination() | ||
| process.terminationHandler = { _ in | ||
| termination.resolve(timedOut: false) | ||
| } | ||
|
|
||
| var processGroup: pid_t? | ||
| do { | ||
| try process.run() | ||
| processGroup = self.attachProcessGroup(process) | ||
| } catch { | ||
| return Result(outcome: .launchFailed(error.localizedDescription), output: "") | ||
| } | ||
| stdoutCapture.start() | ||
| stderrCapture.start() | ||
|
|
||
| let progressTask = onProgress.map { onProgress in | ||
| Task.detached(priority: .userInitiated) { | ||
| await Self.pollProgress( | ||
| stdout: stdoutCapture, | ||
| stderr: stderrCapture, | ||
| interval: self.progressPollInterval, | ||
| onProgress: onProgress) | ||
| } | ||
| } | ||
|
|
||
| let timedOut = await self.wait(timeout: timeout, termination: termination) | ||
| progressTask?.cancel() | ||
| if timedOut { | ||
| self.terminate(process, processGroup: processGroup) | ||
| } | ||
|
|
||
| let output = await self.combinedOutput( | ||
| stdout: stdoutCapture, | ||
| stderr: stderrCapture, | ||
| timeout: outputDrainTimeout) | ||
| if timedOut { | ||
| return Result(outcome: .timedOut, output: output) | ||
| } | ||
|
|
||
| let status = process.terminationStatus | ||
| if status == 0 { | ||
| return Result(outcome: .success, output: output) | ||
| } | ||
| return Result(outcome: .failed(status: status), output: output) | ||
| }.value | ||
| } | ||
|
|
||
| private final class ProcessTermination: @unchecked Sendable { | ||
| private let lock = NSLock() | ||
| private var timedOut: Bool? | ||
| private var continuation: CheckedContinuation<Bool, Never>? | ||
|
|
||
| func resolve(timedOut: Bool) { | ||
| let continuation: CheckedContinuation<Bool, Never>? | ||
| self.lock.lock() | ||
| guard self.timedOut == nil else { | ||
| self.lock.unlock() | ||
| return | ||
| } | ||
| self.timedOut = timedOut | ||
| continuation = self.continuation | ||
| self.continuation = nil | ||
| self.lock.unlock() | ||
| continuation?.resume(returning: timedOut) | ||
| } | ||
|
|
||
| func wait() async -> Bool { | ||
| await withCheckedContinuation { continuation in | ||
| let timedOut: Bool? | ||
| self.lock.lock() | ||
| timedOut = self.timedOut | ||
| if timedOut == nil { | ||
| self.continuation = continuation | ||
| } | ||
| self.lock.unlock() | ||
|
|
||
| if let timedOut { | ||
| continuation.resume(returning: timedOut) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static func wait(timeout: TimeInterval, termination: ProcessTermination) async -> Bool { | ||
| let timeoutTask = Task.detached(priority: .userInitiated) { | ||
| try? await Task.sleep(nanoseconds: self.timeoutNanoseconds(timeout)) | ||
| if Task.isCancelled == false { | ||
| termination.resolve(timedOut: true) | ||
| } | ||
| } | ||
| let timedOut = await termination.wait() | ||
| timeoutTask.cancel() | ||
| return timedOut | ||
| } | ||
|
|
||
| private static func timeoutNanoseconds(_ timeout: TimeInterval) -> UInt64 { | ||
| guard timeout.isFinite else { return UInt64.max } | ||
| let seconds = max(0, min(timeout, Double(UInt64.max) / 1_000_000_000)) | ||
| return UInt64(seconds * 1_000_000_000) | ||
| } | ||
|
|
||
| private static func terminate(_ process: Process, processGroup: pid_t?) { | ||
| if let pgid = processGroup { | ||
| kill(-pgid, SIGTERM) | ||
| } | ||
| if process.isRunning { | ||
| process.terminate() | ||
| } | ||
|
|
||
| let deadline = Date().addingTimeInterval(2.0) | ||
| while process.isRunning, Date() < deadline { | ||
| usleep(100_000) | ||
| } | ||
|
|
||
| if process.isRunning { | ||
| if let pgid = processGroup { | ||
| kill(-pgid, SIGKILL) | ||
| } | ||
| kill(process.processIdentifier, SIGKILL) | ||
| } | ||
| } | ||
|
|
||
| private static func attachProcessGroup(_ process: Process) -> pid_t? { | ||
| let pid = process.processIdentifier | ||
| return setpgid(pid, pid) == 0 ? pid : nil | ||
| } | ||
|
|
||
| private static func combinedOutput( | ||
| stdout: ProcessPipeCapture, | ||
| stderr: ProcessPipeCapture, | ||
| timeout: TimeInterval) async -> String | ||
| { | ||
| let drainTimeout = Duration.seconds(max(0, timeout)) | ||
| async let outData = stdout.finish(timeout: drainTimeout) | ||
| async let errData = stderr.finish(timeout: drainTimeout) | ||
| let out = await self.decode(outData) | ||
| let err = await self.decode(errData) | ||
|
|
||
| let merged: String = if !out.isEmpty, !err.isEmpty { | ||
| [out, err].joined(separator: "\n") | ||
| } else { | ||
| out + err | ||
| } | ||
| let trimmed = merged.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let limited = trimmed.prefix(4000) | ||
| return limited.isEmpty ? L("No output captured.") : String(limited) | ||
| } | ||
|
|
||
| private static func decode(_ data: Data) -> String { | ||
| ProcessPipeCapture.decodeUTF8(data) | ||
| } | ||
|
|
||
| /// Polls the still-running subprocess's pipes for a device-flow URL/code and reports it once, | ||
| /// so the UI can show it before the timeout kills a login that's waiting on browser approval. | ||
| private static func pollProgress( | ||
| stdout: ProcessPipeCapture, | ||
| stderr: ProcessPipeCapture, | ||
| interval: TimeInterval, | ||
| onProgress: @escaping @Sendable (String) -> Void) async | ||
| { | ||
| while !Task.isCancelled { | ||
| let combined = self.decode(stdout.currentSnapshot()) + self.decode(stderr.currentSnapshot()) | ||
| let trimmed = combined.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| if trimmed.contains("http://") || trimmed.contains("https://") { | ||
| onProgress(trimmed) | ||
| return | ||
| } | ||
| try? await Task.sleep(nanoseconds: UInt64(max(0, interval) * 1_000_000_000)) | ||
| } | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If CodexBar shuts down while Kiro login is polling,
cancelShutdownTasks()cancelsloginTask, but this continuation wait never observes task cancellation and only callsterminateafter the 120-second timeout. Since application termination destroys that timeout task as well, the independently launchedkiro-cliprocess group can remain orphaned after CodexBar exits; make cancellation resolve the wait and run the existing process-group teardown.Useful? React with 👍 / 👎.