File size: 21,729 Bytes
f606b10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// Package kiro provides custom protocol handler registration for Kiro OAuth.
// This enables the CLI to intercept kiro:// URIs for social authentication (Google/GitHub).
package kiro

import (
	"context"
	"fmt"
	"html"
	"net"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"sync"
	"time"

	log "github.com/sirupsen/logrus"
)

const (
	// KiroProtocol is the custom URI scheme used by Kiro
	KiroProtocol = "kiro"

	// KiroAuthority is the URI authority for authentication callbacks
	KiroAuthority = "kiro.kiroAgent"

	// KiroAuthPath is the path for successful authentication
	KiroAuthPath = "/authenticate-success"

	// KiroRedirectURI is the full redirect URI for social auth
	KiroRedirectURI = "kiro://kiro.kiroAgent/authenticate-success"

	// DefaultHandlerPort is the default port for the local callback server
	DefaultHandlerPort = 19876

	// HandlerTimeout is how long to wait for the OAuth callback
	HandlerTimeout = 10 * time.Minute
)

// ProtocolHandler manages the custom kiro:// protocol handler for OAuth callbacks.
type ProtocolHandler struct {
	port       int
	server     *http.Server
	listener   net.Listener
	resultChan chan *AuthCallback
	stopChan   chan struct{}
	mu         sync.Mutex
	running    bool
}

// AuthCallback contains the OAuth callback parameters.
type AuthCallback struct {
	Code  string
	State string
	Error string
}

// NewProtocolHandler creates a new protocol handler.
func NewProtocolHandler() *ProtocolHandler {
	return &ProtocolHandler{
		port:       DefaultHandlerPort,
		resultChan: make(chan *AuthCallback, 1),
		stopChan:   make(chan struct{}),
	}
}

// Start starts the local callback server that receives redirects from the protocol handler.
func (h *ProtocolHandler) Start(ctx context.Context) (int, error) {
	h.mu.Lock()
	defer h.mu.Unlock()

	if h.running {
		return h.port, nil
	}

	// Drain any stale results from previous runs
	select {
	case <-h.resultChan:
	default:
	}

	// Reset stopChan for reuse - close old channel first to unblock any waiting goroutines
	if h.stopChan != nil {
		select {
		case <-h.stopChan:
			// Already closed
		default:
			close(h.stopChan)
		}
	}
	h.stopChan = make(chan struct{})

	// Try ports in known range (must match handler script port range)
	var listener net.Listener
	var err error
	portRange := []int{DefaultHandlerPort, DefaultHandlerPort + 1, DefaultHandlerPort + 2, DefaultHandlerPort + 3, DefaultHandlerPort + 4}
	
	for _, port := range portRange {
		listener, err = net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
		if err == nil {
			break
		}
		log.Debugf("kiro protocol handler: port %d busy, trying next", port)
	}
	
	if listener == nil {
		return 0, fmt.Errorf("failed to start callback server: all ports %d-%d are busy", DefaultHandlerPort, DefaultHandlerPort+4)
	}

	h.listener = listener
	h.port = listener.Addr().(*net.TCPAddr).Port

	mux := http.NewServeMux()
	mux.HandleFunc("/oauth/callback", h.handleCallback)

	h.server = &http.Server{
		Handler:           mux,
		ReadHeaderTimeout: 10 * time.Second,
	}

	go func() {
		if err := h.server.Serve(listener); err != nil && err != http.ErrServerClosed {
			log.Debugf("kiro protocol handler server error: %v", err)
		}
	}()

	h.running = true
	log.Debugf("kiro protocol handler started on port %d", h.port)

	// Auto-shutdown after context done, timeout, or explicit stop
	// Capture references to prevent race with new Start() calls
	currentStopChan := h.stopChan
	currentServer := h.server
	currentListener := h.listener
	go func() {
		select {
		case <-ctx.Done():
		case <-time.After(HandlerTimeout):
		case <-currentStopChan:
			return // Already stopped, exit goroutine
		}
		// Only stop if this is still the current server/listener instance
		h.mu.Lock()
		if h.server == currentServer && h.listener == currentListener {
			h.mu.Unlock()
			h.Stop()
		} else {
			h.mu.Unlock()
		}
	}()

	return h.port, nil
}

// Stop stops the callback server.
func (h *ProtocolHandler) Stop() {
	h.mu.Lock()
	defer h.mu.Unlock()

	if !h.running {
		return
	}

	// Signal the auto-shutdown goroutine to exit.
	// This select pattern is safe because stopChan is only modified while holding h.mu,
	// and we hold the lock here. The select prevents panic from double-close.
	select {
	case <-h.stopChan:
		// Already closed
	default:
		close(h.stopChan)
	}

	if h.server != nil {
		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()
		_ = h.server.Shutdown(ctx)
	}

	h.running = false
	log.Debug("kiro protocol handler stopped")
}

// WaitForCallback waits for the OAuth callback and returns the result.
func (h *ProtocolHandler) WaitForCallback(ctx context.Context) (*AuthCallback, error) {
	select {
	case <-ctx.Done():
		return nil, ctx.Err()
	case <-time.After(HandlerTimeout):
		return nil, fmt.Errorf("timeout waiting for OAuth callback")
	case result := <-h.resultChan:
		return result, nil
	}
}

// GetPort returns the port the handler is listening on.
func (h *ProtocolHandler) GetPort() int {
	return h.port
}

// handleCallback processes the OAuth callback from the protocol handler script.
func (h *ProtocolHandler) handleCallback(w http.ResponseWriter, r *http.Request) {
	code := r.URL.Query().Get("code")
	state := r.URL.Query().Get("state")
	errParam := r.URL.Query().Get("error")

	result := &AuthCallback{
		Code:  code,
		State: state,
		Error: errParam,
	}

	// Send result
	select {
	case h.resultChan <- result:
	default:
		// Channel full, ignore duplicate callbacks
	}

	// Send success response
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	if errParam != "" {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<head><title>Login Failed</title></head>
<body>
<h1>Login Failed</h1>
<p>Error: %s</p>
<p>You can close this window.</p>
</body>
</html>`, html.EscapeString(errParam))
	} else {
		fmt.Fprint(w, `<!DOCTYPE html>
<html>
<head><title>Login Successful</title></head>
<body>
<h1>Login Successful!</h1>
<p>You can close this window and return to the terminal.</p>
<script>window.close();</script>
</body>
</html>`)
	}
}

// IsProtocolHandlerInstalled checks if the kiro:// protocol handler is installed.
func IsProtocolHandlerInstalled() bool {
	switch runtime.GOOS {
	case "linux":
		return isLinuxHandlerInstalled()
	case "windows":
		return isWindowsHandlerInstalled()
	case "darwin":
		return isDarwinHandlerInstalled()
	default:
		return false
	}
}

// InstallProtocolHandler installs the kiro:// protocol handler for the current platform.
func InstallProtocolHandler(handlerPort int) error {
	switch runtime.GOOS {
	case "linux":
		return installLinuxHandler(handlerPort)
	case "windows":
		return installWindowsHandler(handlerPort)
	case "darwin":
		return installDarwinHandler(handlerPort)
	default:
		return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
	}
}

// UninstallProtocolHandler removes the kiro:// protocol handler.
func UninstallProtocolHandler() error {
	switch runtime.GOOS {
	case "linux":
		return uninstallLinuxHandler()
	case "windows":
		return uninstallWindowsHandler()
	case "darwin":
		return uninstallDarwinHandler()
	default:
		return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
	}
}

// --- Linux Implementation ---

func getLinuxDesktopPath() string {
	homeDir, _ := os.UserHomeDir()
	return filepath.Join(homeDir, ".local", "share", "applications", "kiro-oauth-handler.desktop")
}

func getLinuxHandlerScriptPath() string {
	homeDir, _ := os.UserHomeDir()
	return filepath.Join(homeDir, ".local", "bin", "kiro-oauth-handler")
}

func isLinuxHandlerInstalled() bool {
	desktopPath := getLinuxDesktopPath()
	_, err := os.Stat(desktopPath)
	return err == nil
}

func installLinuxHandler(handlerPort int) error {
	// Create directories
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return err
	}

	binDir := filepath.Join(homeDir, ".local", "bin")
	appDir := filepath.Join(homeDir, ".local", "share", "applications")

	if err := os.MkdirAll(binDir, 0755); err != nil {
		return fmt.Errorf("failed to create bin directory: %w", err)
	}
	if err := os.MkdirAll(appDir, 0755); err != nil {
		return fmt.Errorf("failed to create applications directory: %w", err)
	}

	// Create handler script - tries multiple ports to handle dynamic port allocation
	scriptPath := getLinuxHandlerScriptPath()
	scriptContent := fmt.Sprintf(`#!/bin/bash
# Kiro OAuth Protocol Handler
# Handles kiro:// URIs - tries CLI first, then forwards to Kiro IDE

URL="$1"

# Check curl availability
if ! command -v curl &> /dev/null; then
    echo "Error: curl is required for Kiro OAuth handler" >&2
    exit 1
fi

# Extract code and state from URL
[[ "$URL" =~ code=([^&]+) ]] && CODE="${BASH_REMATCH[1]}"
[[ "$URL" =~ state=([^&]+) ]] && STATE="${BASH_REMATCH[1]}"
[[ "$URL" =~ error=([^&]+) ]] && ERROR="${BASH_REMATCH[1]}"

# Try CLI proxy on multiple possible ports (default + dynamic range)
CLI_OK=0
for PORT in %d %d %d %d %d; do
    if [ -n "$ERROR" ]; then
        curl -sf --connect-timeout 1 "http://127.0.0.1:$PORT/oauth/callback?error=$ERROR" && CLI_OK=1 && break
    elif [ -n "$CODE" ] && [ -n "$STATE" ]; then
        curl -sf --connect-timeout 1 "http://127.0.0.1:$PORT/oauth/callback?code=$CODE&state=$STATE" && CLI_OK=1 && break
    fi
done

# If CLI not available, forward to Kiro IDE
if [ $CLI_OK -eq 0 ] && [ -x "/usr/share/kiro/kiro" ]; then
    /usr/share/kiro/kiro --open-url "$URL" &
fi
`, handlerPort, handlerPort+1, handlerPort+2, handlerPort+3, handlerPort+4)

	if err := os.WriteFile(scriptPath, []byte(scriptContent), 0755); err != nil {
		return fmt.Errorf("failed to write handler script: %w", err)
	}

	// Create .desktop file
	desktopPath := getLinuxDesktopPath()
	desktopContent := fmt.Sprintf(`[Desktop Entry]
Name=Kiro OAuth Handler
Comment=Handle kiro:// protocol for CLI Proxy API authentication
Exec=%s %%u
Type=Application
Terminal=false
NoDisplay=true
MimeType=x-scheme-handler/kiro;
Categories=Utility;
`, scriptPath)

	if err := os.WriteFile(desktopPath, []byte(desktopContent), 0644); err != nil {
		return fmt.Errorf("failed to write desktop file: %w", err)
	}

	// Register handler with xdg-mime
	cmd := exec.Command("xdg-mime", "default", "kiro-oauth-handler.desktop", "x-scheme-handler/kiro")
	if err := cmd.Run(); err != nil {
		log.Warnf("xdg-mime registration failed (may need manual setup): %v", err)
	}

	// Update desktop database
	cmd = exec.Command("update-desktop-database", appDir)
	_ = cmd.Run() // Ignore errors, not critical

	log.Info("Kiro protocol handler installed for Linux")
	return nil
}

func uninstallLinuxHandler() error {
	desktopPath := getLinuxDesktopPath()
	scriptPath := getLinuxHandlerScriptPath()

	if err := os.Remove(desktopPath); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("failed to remove desktop file: %w", err)
	}
	if err := os.Remove(scriptPath); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("failed to remove handler script: %w", err)
	}

	log.Info("Kiro protocol handler uninstalled")
	return nil
}

// --- Windows Implementation ---

func isWindowsHandlerInstalled() bool {
	// Check registry key existence
	cmd := exec.Command("reg", "query", `HKCU\Software\Classes\kiro`, "/ve")
	return cmd.Run() == nil
}

func installWindowsHandler(handlerPort int) error {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		return err
	}

	// Create handler script (PowerShell)
	scriptDir := filepath.Join(homeDir, ".cliproxyapi")
	if err := os.MkdirAll(scriptDir, 0755); err != nil {
		return fmt.Errorf("failed to create script directory: %w", err)
	}

	scriptPath := filepath.Join(scriptDir, "kiro-oauth-handler.ps1")
	scriptContent := fmt.Sprintf(`# Kiro OAuth Protocol Handler for Windows
param([string]$url)

# Load required assembly for HttpUtility
Add-Type -AssemblyName System.Web

# Parse URL parameters
$uri = [System.Uri]$url
$query = [System.Web.HttpUtility]::ParseQueryString($uri.Query)
$code = $query["code"]
$state = $query["state"]
$errorParam = $query["error"]

# Try multiple ports (default + dynamic range)
$ports = @(%d, %d, %d, %d, %d)
$success = $false

foreach ($port in $ports) {
    if ($success) { break }
    $callbackUrl = "http://127.0.0.1:$port/oauth/callback"
    try {
        if ($errorParam) {
            $fullUrl = $callbackUrl + "?error=" + $errorParam
            Invoke-WebRequest -Uri $fullUrl -UseBasicParsing -TimeoutSec 1 -ErrorAction Stop | Out-Null
            $success = $true
        } elseif ($code -and $state) {
            $fullUrl = $callbackUrl + "?code=" + $code + "&state=" + $state
            Invoke-WebRequest -Uri $fullUrl -UseBasicParsing -TimeoutSec 1 -ErrorAction Stop | Out-Null
            $success = $true
        }
    } catch {
        # Try next port
    }
}
`, handlerPort, handlerPort+1, handlerPort+2, handlerPort+3, handlerPort+4)

	if err := os.WriteFile(scriptPath, []byte(scriptContent), 0644); err != nil {
		return fmt.Errorf("failed to write handler script: %w", err)
	}

	// Create batch wrapper
	batchPath := filepath.Join(scriptDir, "kiro-oauth-handler.bat")
	batchContent := fmt.Sprintf("@echo off\npowershell -ExecutionPolicy Bypass -File \"%s\" %%1\n", scriptPath)

	if err := os.WriteFile(batchPath, []byte(batchContent), 0644); err != nil {
		return fmt.Errorf("failed to write batch wrapper: %w", err)
	}

	// Register in Windows registry
	commands := [][]string{
		{"reg", "add", `HKCU\Software\Classes\kiro`, "/ve", "/d", "URL:Kiro Protocol", "/f"},
		{"reg", "add", `HKCU\Software\Classes\kiro`, "/v", "URL Protocol", "/d", "", "/f"},
		{"reg", "add", `HKCU\Software\Classes\kiro\shell`, "/f"},
		{"reg", "add", `HKCU\Software\Classes\kiro\shell\open`, "/f"},
		{"reg", "add", `HKCU\Software\Classes\kiro\shell\open\command`, "/ve", "/d", fmt.Sprintf("\"%s\" \"%%1\"", batchPath), "/f"},
	}

	for _, args := range commands {
		cmd := exec.Command(args[0], args[1:]...)
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("failed to run registry command: %w", err)
		}
	}

	log.Info("Kiro protocol handler installed for Windows")
	return nil
}

func uninstallWindowsHandler() error {
	// Remove registry keys
	cmd := exec.Command("reg", "delete", `HKCU\Software\Classes\kiro`, "/f")
	if err := cmd.Run(); err != nil {
		log.Warnf("failed to remove registry key: %v", err)
	}

	// Remove scripts
	homeDir, _ := os.UserHomeDir()
	scriptDir := filepath.Join(homeDir, ".cliproxyapi")
	_ = os.Remove(filepath.Join(scriptDir, "kiro-oauth-handler.ps1"))
	_ = os.Remove(filepath.Join(scriptDir, "kiro-oauth-handler.bat"))

	log.Info("Kiro protocol handler uninstalled")
	return nil
}

// --- macOS Implementation ---

func getDarwinAppPath() string {
	homeDir, _ := os.UserHomeDir()
	return filepath.Join(homeDir, "Applications", "KiroOAuthHandler.app")
}

func isDarwinHandlerInstalled() bool {
	appPath := getDarwinAppPath()
	_, err := os.Stat(appPath)
	return err == nil
}

func installDarwinHandler(handlerPort int) error {
	// Create app bundle structure
	appPath := getDarwinAppPath()
	contentsPath := filepath.Join(appPath, "Contents")
	macOSPath := filepath.Join(contentsPath, "MacOS")

	if err := os.MkdirAll(macOSPath, 0755); err != nil {
		return fmt.Errorf("failed to create app bundle: %w", err)
	}

	// Create Info.plist
	plistPath := filepath.Join(contentsPath, "Info.plist")
	plistContent := `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleIdentifier</key>
    <string>com.cliproxyapi.kiro-oauth-handler</string>
    <key>CFBundleName</key>
    <string>KiroOAuthHandler</string>
    <key>CFBundleExecutable</key>
    <string>kiro-oauth-handler</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>Kiro Protocol</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>kiro</string>
            </array>
        </dict>
    </array>
    <key>LSBackgroundOnly</key>
    <true/>
</dict>
</plist>`

	if err := os.WriteFile(plistPath, []byte(plistContent), 0644); err != nil {
		return fmt.Errorf("failed to write Info.plist: %w", err)
	}

	// Create executable script - tries multiple ports to handle dynamic port allocation
	execPath := filepath.Join(macOSPath, "kiro-oauth-handler")
	execContent := fmt.Sprintf(`#!/bin/bash
# Kiro OAuth Protocol Handler for macOS

URL="$1"

# Check curl availability (should always exist on macOS)
if [ ! -x /usr/bin/curl ]; then
    echo "Error: curl is required for Kiro OAuth handler" >&2
    exit 1
fi

# Extract code and state from URL
[[ "$URL" =~ code=([^&]+) ]] && CODE="${BASH_REMATCH[1]}"
[[ "$URL" =~ state=([^&]+) ]] && STATE="${BASH_REMATCH[1]}"
[[ "$URL" =~ error=([^&]+) ]] && ERROR="${BASH_REMATCH[1]}"

# Try multiple ports (default + dynamic range)
for PORT in %d %d %d %d %d; do
    if [ -n "$ERROR" ]; then
        /usr/bin/curl -sf --connect-timeout 1 "http://127.0.0.1:$PORT/oauth/callback?error=$ERROR" && exit 0
    elif [ -n "$CODE" ] && [ -n "$STATE" ]; then
        /usr/bin/curl -sf --connect-timeout 1 "http://127.0.0.1:$PORT/oauth/callback?code=$CODE&state=$STATE" && exit 0
    fi
done
`, handlerPort, handlerPort+1, handlerPort+2, handlerPort+3, handlerPort+4)

	if err := os.WriteFile(execPath, []byte(execContent), 0755); err != nil {
		return fmt.Errorf("failed to write executable: %w", err)
	}

	// Register the app with Launch Services
	cmd := exec.Command("/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister",
		"-f", appPath)
	if err := cmd.Run(); err != nil {
		log.Warnf("lsregister failed (handler may still work): %v", err)
	}

	log.Info("Kiro protocol handler installed for macOS")
	return nil
}

func uninstallDarwinHandler() error {
	appPath := getDarwinAppPath()

	// Unregister from Launch Services
	cmd := exec.Command("/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister",
		"-u", appPath)
	_ = cmd.Run()

	// Remove app bundle
	if err := os.RemoveAll(appPath); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("failed to remove app bundle: %w", err)
	}

	log.Info("Kiro protocol handler uninstalled")
	return nil
}

// ParseKiroURI parses a kiro:// URI and extracts the callback parameters.
func ParseKiroURI(rawURI string) (*AuthCallback, error) {
	u, err := url.Parse(rawURI)
	if err != nil {
		return nil, fmt.Errorf("invalid URI: %w", err)
	}

	if u.Scheme != KiroProtocol {
		return nil, fmt.Errorf("invalid scheme: expected %s, got %s", KiroProtocol, u.Scheme)
	}

	if u.Host != KiroAuthority {
		return nil, fmt.Errorf("invalid authority: expected %s, got %s", KiroAuthority, u.Host)
	}

	query := u.Query()
	return &AuthCallback{
		Code:  query.Get("code"),
		State: query.Get("state"),
		Error: query.Get("error"),
	}, nil
}

// GetHandlerInstructions returns platform-specific instructions for manual handler setup.
func GetHandlerInstructions() string {
	switch runtime.GOOS {
	case "linux":
		return `To manually set up the Kiro protocol handler on Linux:

1. Create ~/.local/share/applications/kiro-oauth-handler.desktop:
   [Desktop Entry]
   Name=Kiro OAuth Handler
   Exec=~/.local/bin/kiro-oauth-handler %u
   Type=Application
   Terminal=false
   MimeType=x-scheme-handler/kiro;

2. Create ~/.local/bin/kiro-oauth-handler (make it executable):
   #!/bin/bash
   URL="$1"
   # ... (see generated script for full content)

3. Run: xdg-mime default kiro-oauth-handler.desktop x-scheme-handler/kiro`

	case "windows":
		return `To manually set up the Kiro protocol handler on Windows:

1. Open Registry Editor (regedit.exe)
2. Create key: HKEY_CURRENT_USER\Software\Classes\kiro
3. Set default value to: URL:Kiro Protocol
4. Create string value "URL Protocol" with empty data
5. Create subkey: shell\open\command
6. Set default value to: "C:\path\to\handler.bat" "%1"`

	case "darwin":
		return `To manually set up the Kiro protocol handler on macOS:

1. Create ~/Applications/KiroOAuthHandler.app bundle
2. Add Info.plist with CFBundleURLTypes containing "kiro" scheme
3. Create executable in Contents/MacOS/
4. Run: /System/Library/.../lsregister -f ~/Applications/KiroOAuthHandler.app`

	default:
		return "Protocol handler setup is not supported on this platform."
	}
}

// SetupProtocolHandlerIfNeeded checks and installs the protocol handler if needed.
func SetupProtocolHandlerIfNeeded(handlerPort int) error {
	if IsProtocolHandlerInstalled() {
		log.Debug("Kiro protocol handler already installed")
		return nil
	}

	fmt.Println("\n╔══════════════════════════════════════════════════════════╗")
	fmt.Println("β•‘       Kiro Protocol Handler Setup Required                 β•‘")
	fmt.Println("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
	fmt.Println("\nTo enable Google/GitHub login, we need to install a protocol handler.")
	fmt.Println("This allows your browser to redirect back to the CLI after authentication.")
	fmt.Println("\nInstalling protocol handler...")

	if err := InstallProtocolHandler(handlerPort); err != nil {
		fmt.Printf("\n⚠ Automatic installation failed: %v\n", err)
		fmt.Println("\nManual setup instructions:")
		fmt.Println(strings.Repeat("-", 60))
		fmt.Println(GetHandlerInstructions())
		return err
	}

	fmt.Println("\nβœ“ Protocol handler installed successfully!")
	return nil
}