From 0559fa3cfd5722cf1755a7dc3a90573ede3835f6 Mon Sep 17 00:00:00 2001 From: JUNZE <127123086+WizisCool@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:45:08 +0800 Subject: [PATCH] feat(management): expose oauth_provider in credential list Report the OAuth login entry a credential was signed in through, resolved from Attributes then Metadata via oauth_provider and portal and normalized the way sign-in and re-login already resolve it. A console can then group credentials of a multi-entry plugin by entry instead of by provider. The field is v8-only, so the deprecated /v0/management/auth-files response keeps its frozen shape, and it is omitted when a credential carries no entry, leaving built-in providers, single-entry plugins and files written before #7126 unchanged. type and provider stay as they are because routing and quota lookup key on them. Fixes #7307 --- .../api/handlers/management/auth_files.go | 26 +++++ .../auth_files_oauth_provider_test.go | 100 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 internal/api/handlers/management/auth_files_oauth_provider_test.go diff --git a/internal/api/handlers/management/auth_files.go b/internal/api/handlers/management/auth_files.go index 8305345fe9..b8d8d75d49 100644 --- a/internal/api/handlers/management/auth_files.go +++ b/internal/api/handlers/management/auth_files.go @@ -792,6 +792,11 @@ func (h *Handler) buildAuthFileEntryLocked(auth *coreauth.Auth, quota authFileQu entry["request_retry"] = requestRetry } if quota.v8 { + // OAuth login entry the credential was signed in through; multi-entry plugins store it + // next to the provider, and the deprecated v0 route keeps its frozen response shape. + if oauthProvider := authOAuthProvider(auth); oauthProvider != "" { + entry["oauth_provider"] = oauthProvider + } nowUTC := time.Now().UTC() if exp, okExp := auth.AccessTokenExpirationTime(); okExp && !exp.IsZero() && exp.Unix() > 0 { entry["access_token_expires_at"] = exp.UTC().Format(time.RFC3339) @@ -980,6 +985,27 @@ func authProjectID(auth *coreauth.Auth) string { return "" } +func authOAuthProvider(auth *coreauth.Auth) string { + if auth == nil { + return "" + } + for _, key := range []string{"oauth_provider", "portal"} { + if auth.Attributes != nil { + if v := strings.ToLower(strings.TrimSpace(auth.Attributes[key])); v != "" { + return v + } + } + if auth.Metadata != nil { + if raw, ok := auth.Metadata[key].(string); ok { + if v := strings.ToLower(strings.TrimSpace(raw)); v != "" { + return v + } + } + } + } + return "" +} + func extractCodexIDTokenClaims(auth *coreauth.Auth) gin.H { if auth == nil || auth.Metadata == nil { return nil diff --git a/internal/api/handlers/management/auth_files_oauth_provider_test.go b/internal/api/handlers/management/auth_files_oauth_provider_test.go new file mode 100644 index 0000000000..dd3f033d46 --- /dev/null +++ b/internal/api/handlers/management/auth_files_oauth_provider_test.go @@ -0,0 +1,100 @@ +package management + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" +) + +func TestAuthFilesList_ReportsOAuthProvider(t *testing.T) { + manager := coreauth.NewManager(nil, nil, nil) + auths := []*coreauth.Auth{ + {ID: "builtin", FileName: "builtin.json", Provider: "vendor"}, + {ID: "attributes", FileName: "attributes.json", Provider: "vendor", Attributes: map[string]string{"oauth_provider": "vendor-cn"}}, + {ID: "metadata", FileName: "metadata.json", Provider: "vendor", Metadata: map[string]any{"oauth_provider": "vendor-cn"}}, + {ID: "portal", FileName: "portal.json", Provider: "vendor", Metadata: map[string]any{"portal": " Vendor-CN "}}, + {ID: "precedence", FileName: "precedence.json", Provider: "vendor", Attributes: map[string]string{"oauth_provider": "vendor"}, Metadata: map[string]any{"oauth_provider": "vendor-cn", "portal": "ignored"}}, + {ID: "non-string", FileName: "non-string.json", Provider: "vendor", Metadata: map[string]any{"oauth_provider": 42}}, + } + for _, auth := range auths { + if auth.Attributes == nil { + auth.Attributes = map[string]string{} + } + // Runtime-only keeps the entries off the auth dir, which the list route does not need. + auth.Attributes["runtime_only"] = "true" + if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil { + t.Fatalf("failed to register auth %s: %v", auth.ID, errRegister) + } + } + + h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) + + listFiles := func(v8 bool) map[string]map[string]any { + t.Helper() + rec := httptest.NewRecorder() + ctx, _ := gin.CreateTestContext(rec) + ctx.Request = httptest.NewRequest(http.MethodGet, "/v8/management/credentials", nil) + if v8 { + ctx.Set(ConfigV8ContextKey, true) + } + h.ListAuthFiles(ctx) + if rec.Code != http.StatusOK { + t.Fatalf("expected status 200, got %d: %s", rec.Code, rec.Body.String()) + } + var resp struct { + Files []map[string]any `json:"files"` + } + if errUnmarshal := json.Unmarshal(rec.Body.Bytes(), &resp); errUnmarshal != nil { + t.Fatalf("failed to unmarshal files: %v", errUnmarshal) + } + out := make(map[string]map[string]any, len(resp.Files)) + for _, f := range resp.Files { + id, _ := f["id"].(string) + out[id] = f + } + return out + } + + files := listFiles(true) + cases := []struct { + id string + want string + }{ + {id: "builtin"}, + {id: "attributes", want: "vendor-cn"}, + {id: "metadata", want: "vendor-cn"}, + {id: "portal", want: "vendor-cn"}, + {id: "precedence", want: "vendor"}, + {id: "non-string"}, + } + for _, tc := range cases { + file, ok := files[tc.id] + if !ok { + t.Fatalf("expected auth entry %s in list", tc.id) + } + got, hasOAuthProvider := file["oauth_provider"] + if tc.want == "" { + if hasOAuthProvider { + t.Errorf("%s: expected no oauth_provider, got %#v", tc.id, got) + } + } else if got != tc.want { + t.Errorf("%s: expected oauth_provider %q, got %#v", tc.id, tc.want, got) + } + if file["type"] != "vendor" || file["provider"] != "vendor" { + t.Errorf("%s: expected type/provider to stay vendor, got %#v/%#v", tc.id, file["type"], file["provider"]) + } + } + + // The deprecated v0 route keeps its frozen response shape. + for id, file := range listFiles(false) { + if got, ok := file["oauth_provider"]; ok { + t.Errorf("%s: v0 auth files must not expose oauth_provider, got %#v", id, got) + } + } +}