Skip to content

Commit bb1098a

Browse files
author
gwizz
committed
fix: make kiro oauth polling cancelable
1 parent 2f47ea6 commit bb1098a

File tree

1 file changed

+84
-75
lines changed

1 file changed

+84
-75
lines changed

internal/api/handlers/management/auth_files.go

Lines changed: 84 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,95 +2424,104 @@ func (h *Handler) RequestKiroToken(c *gin.Context) {
24242424
waitFile := filepath.Join(h.cfg.AuthDir, fmt.Sprintf(".oauth-kiro-%s.oauth", state))
24252425
deadline := time.Now().Add(5 * time.Minute)
24262426

2427+
ticker := time.NewTicker(500 * time.Millisecond)
2428+
defer ticker.Stop()
2429+
24272430
for {
2428-
if time.Now().After(deadline) {
2429-
log.Error("oauth flow timed out")
2430-
setOAuthStatus(state, "OAuth flow timed out")
2431+
select {
2432+
case <-ctx.Done():
2433+
log.Error("oauth flow cancelled")
2434+
setOAuthStatus(state, "OAuth flow cancelled")
24312435
return
2432-
}
2433-
if data, errR := os.ReadFile(waitFile); errR == nil {
2434-
var m map[string]string
2435-
_ = json.Unmarshal(data, &m)
2436-
_ = os.Remove(waitFile)
2437-
if errStr := m["error"]; errStr != "" {
2438-
log.Errorf("Authentication failed: %s", errStr)
2439-
setOAuthStatus(state, "Authentication failed")
2440-
return
2441-
}
2442-
if m["state"] != state {
2443-
log.Errorf("State mismatch")
2444-
setOAuthStatus(state, "State mismatch")
2445-
return
2446-
}
2447-
code := m["code"]
2448-
if code == "" {
2449-
log.Error("No authorization code received")
2450-
setOAuthStatus(state, "No authorization code received")
2436+
case <-ticker.C:
2437+
if time.Now().After(deadline) {
2438+
log.Error("oauth flow timed out")
2439+
setOAuthStatus(state, "OAuth flow timed out")
24512440
return
24522441
}
2442+
if data, errR := os.ReadFile(waitFile); errR == nil {
2443+
var m map[string]string
2444+
_ = json.Unmarshal(data, &m)
2445+
_ = os.Remove(waitFile)
2446+
if errStr := m["error"]; errStr != "" {
2447+
log.Errorf("Authentication failed: %s", errStr)
2448+
setOAuthStatus(state, "Authentication failed")
2449+
return
2450+
}
2451+
if m["state"] != state {
2452+
log.Errorf("State mismatch")
2453+
setOAuthStatus(state, "State mismatch")
2454+
return
2455+
}
2456+
code := m["code"]
2457+
if code == "" {
2458+
log.Error("No authorization code received")
2459+
setOAuthStatus(state, "No authorization code received")
2460+
return
2461+
}
24532462

2454-
// Exchange code for tokens
2455-
tokenReq := &kiroauth.CreateTokenRequest{
2456-
Code: code,
2457-
CodeVerifier: codeVerifier,
2458-
RedirectURI: kiroauth.KiroRedirectURI,
2459-
}
2463+
// Exchange code for tokens
2464+
tokenReq := &kiroauth.CreateTokenRequest{
2465+
Code: code,
2466+
CodeVerifier: codeVerifier,
2467+
RedirectURI: kiroauth.KiroRedirectURI,
2468+
}
24602469

2461-
tokenResp, errToken := socialClient.CreateToken(ctx, tokenReq)
2462-
if errToken != nil {
2463-
log.Errorf("Failed to exchange code for tokens: %v", errToken)
2464-
setOAuthStatus(state, "Failed to exchange code for tokens")
2465-
return
2466-
}
2470+
tokenResp, errToken := socialClient.CreateToken(ctx, tokenReq)
2471+
if errToken != nil {
2472+
log.Errorf("Failed to exchange code for tokens: %v", errToken)
2473+
setOAuthStatus(state, "Failed to exchange code for tokens")
2474+
return
2475+
}
24672476

2468-
// Save the token
2469-
expiresIn := tokenResp.ExpiresIn
2470-
if expiresIn <= 0 {
2471-
expiresIn = 3600
2472-
}
2473-
expiresAt := time.Now().Add(time.Duration(expiresIn) * time.Second)
2474-
email := kiroauth.ExtractEmailFromJWT(tokenResp.AccessToken)
2477+
// Save the token
2478+
expiresIn := tokenResp.ExpiresIn
2479+
if expiresIn <= 0 {
2480+
expiresIn = 3600
2481+
}
2482+
expiresAt := time.Now().Add(time.Duration(expiresIn) * time.Second)
2483+
email := kiroauth.ExtractEmailFromJWT(tokenResp.AccessToken)
24752484

2476-
idPart := kiroauth.SanitizeEmailForFilename(email)
2477-
if idPart == "" {
2478-
idPart = fmt.Sprintf("%d", time.Now().UnixNano()%100000)
2479-
}
2485+
idPart := kiroauth.SanitizeEmailForFilename(email)
2486+
if idPart == "" {
2487+
idPart = fmt.Sprintf("%d", time.Now().UnixNano()%100000)
2488+
}
24802489

2481-
now := time.Now()
2482-
fileName := fmt.Sprintf("kiro-%s-%s.json", strings.ToLower(provider), idPart)
2490+
now := time.Now()
2491+
fileName := fmt.Sprintf("kiro-%s-%s.json", strings.ToLower(provider), idPart)
2492+
2493+
record := &coreauth.Auth{
2494+
ID: fileName,
2495+
Provider: "kiro",
2496+
FileName: fileName,
2497+
Metadata: map[string]any{
2498+
"type": "kiro",
2499+
"access_token": tokenResp.AccessToken,
2500+
"refresh_token": tokenResp.RefreshToken,
2501+
"profile_arn": tokenResp.ProfileArn,
2502+
"expires_at": expiresAt.Format(time.RFC3339),
2503+
"auth_method": "social",
2504+
"provider": provider,
2505+
"email": email,
2506+
"last_refresh": now.Format(time.RFC3339),
2507+
},
2508+
}
24832509

2484-
record := &coreauth.Auth{
2485-
ID: fileName,
2486-
Provider: "kiro",
2487-
FileName: fileName,
2488-
Metadata: map[string]any{
2489-
"type": "kiro",
2490-
"access_token": tokenResp.AccessToken,
2491-
"refresh_token": tokenResp.RefreshToken,
2492-
"profile_arn": tokenResp.ProfileArn,
2493-
"expires_at": expiresAt.Format(time.RFC3339),
2494-
"auth_method": "social",
2495-
"provider": provider,
2496-
"email": email,
2497-
"last_refresh": now.Format(time.RFC3339),
2498-
},
2499-
}
2510+
savedPath, errSave := h.saveTokenRecord(ctx, record)
2511+
if errSave != nil {
2512+
log.Errorf("Failed to save authentication tokens: %v", errSave)
2513+
setOAuthStatus(state, "Failed to save authentication tokens")
2514+
return
2515+
}
25002516

2501-
savedPath, errSave := h.saveTokenRecord(ctx, record)
2502-
if errSave != nil {
2503-
log.Errorf("Failed to save authentication tokens: %v", errSave)
2504-
setOAuthStatus(state, "Failed to save authentication tokens")
2517+
fmt.Printf("Authentication successful! Token saved to %s\n", savedPath)
2518+
if email != "" {
2519+
fmt.Printf("Authenticated as: %s\n", email)
2520+
}
2521+
deleteOAuthStatus(state)
25052522
return
25062523
}
2507-
2508-
fmt.Printf("Authentication successful! Token saved to %s\n", savedPath)
2509-
if email != "" {
2510-
fmt.Printf("Authenticated as: %s\n", email)
2511-
}
2512-
deleteOAuthStatus(state)
2513-
return
25142524
}
2515-
time.Sleep(500 * time.Millisecond)
25162525
}
25172526
}()
25182527

0 commit comments

Comments
 (0)