diff --git a/changelog/unreleased/kiro-oauth-expiry.md b/changelog/unreleased/kiro-oauth-expiry.md new file mode 100644 index 00000000..ff8fb959 --- /dev/null +++ b/changelog/unreleased/kiro-oauth-expiry.md @@ -0,0 +1,7 @@ +### English + +- Refresh imported Kiro OAuth credentials that omit an expiry when a refresh token is available. Use the profile lookup page size accepted by Kiro so account usage and model discovery can proceed with the renewed credential. + +### 中文 + +- 导入的 Kiro OAuth 凭证缺少有效期但带有刷新令牌时,先换取新的访问令牌。按 Kiro 接受的分页大小查找 profile,以便后续读取会员额度和模型目录。 diff --git a/internal/providers/kiro/account.go b/internal/providers/kiro/account.go index c57b19b1..81baa798 100644 --- a/internal/providers/kiro/account.go +++ b/internal/providers/kiro/account.go @@ -200,7 +200,9 @@ func (c *Client) listProfilesInRegion(ctx context.Context, client *http.Client, var profiles []string next := "" for page := 0; page < maxProfilePages; page++ { - body := map[string]any{"maxResults": 50} + // ListAvailableProfiles currently rejects values above 10, even though the + // model endpoint accepts 50. Keep this request at the service limit. + body := map[string]any{"maxResults": 10} if next != "" { body["nextToken"] = next } diff --git a/internal/providers/kiro/account_test.go b/internal/providers/kiro/account_test.go index 4f5280f3..8dcbc88c 100644 --- a/internal/providers/kiro/account_test.go +++ b/internal/providers/kiro/account_test.go @@ -176,6 +176,13 @@ func TestEnsureProfileArnDiscoversPagesAndPersistsProfile(t *testing.T) { if r.Method != http.MethodPost || r.URL.Path != "/ListAvailableProfiles" { t.Fatalf("request = %s %s", r.Method, r.URL.Path) } + var requestBody map[string]any + if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil { + t.Fatal(err) + } + if requestBody["maxResults"] != float64(10) { + t.Fatalf("maxResults = %#v", requestBody["maxResults"]) + } calls++ if calls == 1 { _, _ = io.WriteString(w, `{"profiles":[{"arn":"arn:aws:codewhisperer:us-east-1:123456789012:profile/one"}],"nextToken":"next"}`) @@ -324,6 +331,73 @@ func TestProfileArnRegionBeatsOIDCRegion(t *testing.T) { } } +func TestImportedSocialCredentialRefreshesBeforeQuotaAndModels(t *testing.T) { + const profile = "arn:aws:codewhisperer:us-east-1:123456789012:profile/example" + store := &memStore{payload: []byte(`{"provider":"Github","accessToken":"old-access","refreshToken":"old-refresh"}`)} + var refreshes, lookups int + httpClient := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { + var body string + switch r.URL.String() { + case socialRefreshURL: + refreshes++ + var request struct { + RefreshToken string `json:"refreshToken"` + } + if err := json.NewDecoder(r.Body).Decode(&request); err != nil || request.RefreshToken != "old-refresh" { + t.Fatal("refresh did not use the imported refresh token") + } + body = `{"accessToken":"new-access","refreshToken":"new-refresh","expiresIn":3600}` + case kiroMetadataURL: + body = fmt.Sprintf(`{"currentRelease":%q}`, IDEVersion) + default: + if r.Header.Get("Authorization") != "Bearer new-access" { + t.Fatalf("%s did not use the refreshed access token", r.URL.Path) + } + if r.URL.Path != "/ListAvailableProfiles" && r.URL.Query().Get("profileArn") != profile { + t.Fatalf("%s did not use the discovered profile", r.URL.Path) + } + switch r.URL.Path { + case "/ListAvailableProfiles": + lookups++ + body = fmt.Sprintf(`{"profiles":[{"arn":%q}]}`, profile) + case "/getUsageLimits": + body = `{"subscriptionInfo":{"subscriptionTitle":"KIRO PRO"}}` + case "/ListAvailableModels": + body = `{"models":[{"modelId":"auto"}]}` + default: + t.Fatalf("unexpected path %s", r.URL.Path) + } + } + return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil + })} + client := &Client{store: store, http: httpClient} + ctx := context.Background() + quota, err := client.Quota(ctx, t.Name()) + if err != nil { + t.Fatal(err) + } + if quota.Plan != "PRO" { + t.Fatalf("plan = %q, want PRO", quota.Plan) + } + models, err := client.Models(ctx, t.Name()) + if err != nil { + t.Fatal(err) + } + if len(models) != 1 || models[0].NativeModel != "auto" { + t.Fatalf("models = %+v", models) + } + if refreshes != 1 || lookups != 1 { + t.Fatalf("refreshes = %d, profile lookups = %d, want one each", refreshes, lookups) + } + stored, err := DecodeCredential(store.payload) + if err != nil { + t.Fatal(err) + } + if stored.RefreshToken != "new-refresh" || stored.ExpiresAt <= time.Now().Unix() || stored.ProfileArn != profile { + t.Fatal("refreshed credential, expiry, or profile was not saved") + } +} + func TestRefreshDispatchesByAuthMethod(t *testing.T) { var got []string client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { diff --git a/internal/providers/kiro/credential.go b/internal/providers/kiro/credential.go index cf5accd5..3e4adb7d 100644 --- a/internal/providers/kiro/credential.go +++ b/internal/providers/kiro/credential.go @@ -155,10 +155,12 @@ func (c Credential) expired() bool { } func (c Credential) needsRefresh() bool { - if c.AuthMethod == "api_key" || strings.TrimSpace(c.RefreshToken) == "" { + if c.isAPIKey() || strings.TrimSpace(c.RefreshToken) == "" { return false } - return strings.TrimSpace(c.AccessToken) == "" || c.expired() + // Some OAuth exports omit expiry. Exchange the refresh token once to + // establish a known lifetime before using the imported access token. + return strings.TrimSpace(c.AccessToken) == "" || c.ExpiresAt <= 0 || c.expired() } func (c Credential) bearer() string { return strings.TrimSpace(c.AccessToken) }