|
| 1 | +package management |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| 14 | + coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" |
| 15 | +) |
| 16 | + |
| 17 | +func TestListAuthFiles_CodexSecretIDTokenReturnsAccountFields(t *testing.T) { |
| 18 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 19 | + gin.SetMode(gin.TestMode) |
| 20 | + |
| 21 | + idToken := makeCodexIDToken(t, "chatgpt-account-secret", "plus") |
| 22 | + secretServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + if got := r.Header.Get("Authorization"); got != "Bearer test-secret-token" { |
| 24 | + t.Fatalf("Authorization = %q, want bearer token", got) |
| 25 | + } |
| 26 | + if r.URL.Path != "/v1/secrets/codex-id-token" { |
| 27 | + t.Fatalf("secret path = %q", r.URL.Path) |
| 28 | + } |
| 29 | + _ = json.NewEncoder(w).Encode(map[string]string{"value": idToken}) |
| 30 | + })) |
| 31 | + t.Cleanup(secretServer.Close) |
| 32 | + t.Setenv("CCP_SECRET_BROKER_URL", secretServer.URL) |
| 33 | + t.Setenv("CCP_SECRET_BROKER_TOKEN", "test-secret-token") |
| 34 | + |
| 35 | + manager := coreauth.NewManager(nil, nil, nil) |
| 36 | + if _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ |
| 37 | + ID: "codex-secret", |
| 38 | + FileName: "codex-secret.json", |
| 39 | + Provider: "codex", |
| 40 | + Attributes: map[string]string{ |
| 41 | + "runtime_only": "true", |
| 42 | + }, |
| 43 | + Metadata: map[string]any{ |
| 44 | + "id_token": "ccp-secret://codex-id-token", |
| 45 | + }, |
| 46 | + }); errRegister != nil { |
| 47 | + t.Fatalf("register auth: %v", errRegister) |
| 48 | + } |
| 49 | + |
| 50 | + entry := listSingleAuthFile(t, manager) |
| 51 | + |
| 52 | + if got := entry["chatgpt_account_id"]; got != "chatgpt-account-secret" { |
| 53 | + t.Fatalf("chatgpt_account_id = %#v", got) |
| 54 | + } |
| 55 | + if got := entry["plan_type"]; got != "plus" { |
| 56 | + t.Fatalf("plan_type = %#v", got) |
| 57 | + } |
| 58 | + |
| 59 | + idTokenClaims, ok := entry["id_token"].(map[string]any) |
| 60 | + if !ok { |
| 61 | + t.Fatalf("id_token claims missing: %#v", entry["id_token"]) |
| 62 | + } |
| 63 | + if got := idTokenClaims["chatgpt_account_id"]; got != "chatgpt-account-secret" { |
| 64 | + t.Fatalf("id_token.chatgpt_account_id = %#v", got) |
| 65 | + } |
| 66 | + if text := strings.TrimSpace(toJSON(t, entry)); strings.Contains(text, idToken) || strings.Contains(text, "ccp-secret://") { |
| 67 | + t.Fatalf("auth file response exposed token material: %s", text) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestListAuthFiles_CodexVaultIDTokenReturnsAccountFields(t *testing.T) { |
| 72 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 73 | + gin.SetMode(gin.TestMode) |
| 74 | + |
| 75 | + idToken := makeCodexIDToken(t, "chatgpt-account-vault", "team") |
| 76 | + secretServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | + if r.URL.Path != "/v1/secrets/vault-token" { |
| 78 | + t.Fatalf("secret path = %q", r.URL.Path) |
| 79 | + } |
| 80 | + _ = json.NewEncoder(w).Encode(map[string]string{"value": idToken}) |
| 81 | + })) |
| 82 | + t.Cleanup(secretServer.Close) |
| 83 | + t.Setenv("CCP_SECRET_BROKER_URL", secretServer.URL) |
| 84 | + t.Setenv("CCP_SECRET_BROKER_TOKEN", "test-secret-token") |
| 85 | + |
| 86 | + manager := coreauth.NewManager(nil, nil, nil) |
| 87 | + if _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ |
| 88 | + ID: "codex-vault", |
| 89 | + FileName: "codex-vault.json", |
| 90 | + Provider: "codex", |
| 91 | + Attributes: map[string]string{ |
| 92 | + "runtime_only": "true", |
| 93 | + }, |
| 94 | + Metadata: map[string]any{ |
| 95 | + "id_token": "vault://vault-token", |
| 96 | + }, |
| 97 | + }); errRegister != nil { |
| 98 | + t.Fatalf("register auth: %v", errRegister) |
| 99 | + } |
| 100 | + |
| 101 | + entry := listSingleAuthFile(t, manager) |
| 102 | + |
| 103 | + if got := entry["chatgpt_account_id"]; got != "chatgpt-account-vault" { |
| 104 | + t.Fatalf("chatgpt_account_id = %#v", got) |
| 105 | + } |
| 106 | + if got := entry["plan_type"]; got != "team" { |
| 107 | + t.Fatalf("plan_type = %#v", got) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestListAuthFiles_CodexFallsBackToMetadataAccountID(t *testing.T) { |
| 112 | + t.Setenv("MANAGEMENT_PASSWORD", "") |
| 113 | + gin.SetMode(gin.TestMode) |
| 114 | + |
| 115 | + manager := coreauth.NewManager(nil, nil, nil) |
| 116 | + if _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ |
| 117 | + ID: "codex-fallback", |
| 118 | + FileName: "codex-fallback.json", |
| 119 | + Provider: "codex", |
| 120 | + Attributes: map[string]string{ |
| 121 | + "runtime_only": "true", |
| 122 | + }, |
| 123 | + Metadata: map[string]any{ |
| 124 | + "id_token": "not-a-jwt", |
| 125 | + "account_id": "chatgpt-account-fallback", |
| 126 | + "plan_type": "plus", |
| 127 | + }, |
| 128 | + }); errRegister != nil { |
| 129 | + t.Fatalf("register auth: %v", errRegister) |
| 130 | + } |
| 131 | + |
| 132 | + entry := listSingleAuthFile(t, manager) |
| 133 | + |
| 134 | + if got := entry["chatgpt_account_id"]; got != "chatgpt-account-fallback" { |
| 135 | + t.Fatalf("chatgpt_account_id = %#v", got) |
| 136 | + } |
| 137 | + if got := entry["plan_type"]; got != "plus" { |
| 138 | + t.Fatalf("plan_type = %#v", got) |
| 139 | + } |
| 140 | + if _, ok := entry["id_token"]; ok { |
| 141 | + t.Fatalf("invalid id_token should not be exposed: %#v", entry["id_token"]) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func listSingleAuthFile(t *testing.T, manager *coreauth.Manager) map[string]any { |
| 146 | + t.Helper() |
| 147 | + |
| 148 | + handler := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager) |
| 149 | + rec := httptest.NewRecorder() |
| 150 | + ginCtx, _ := gin.CreateTestContext(rec) |
| 151 | + ginCtx.Request = httptest.NewRequest(http.MethodGet, "/v0/management/auth-files", nil) |
| 152 | + |
| 153 | + handler.ListAuthFiles(ginCtx) |
| 154 | + |
| 155 | + if rec.Code != http.StatusOK { |
| 156 | + t.Fatalf("list status = %d, body = %s", rec.Code, rec.Body.String()) |
| 157 | + } |
| 158 | + |
| 159 | + var payload struct { |
| 160 | + Files []map[string]any `json:"files"` |
| 161 | + } |
| 162 | + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { |
| 163 | + t.Fatalf("decode response: %v", err) |
| 164 | + } |
| 165 | + if len(payload.Files) != 1 { |
| 166 | + t.Fatalf("files length = %d, body = %s", len(payload.Files), rec.Body.String()) |
| 167 | + } |
| 168 | + return payload.Files[0] |
| 169 | +} |
| 170 | + |
| 171 | +func makeCodexIDToken(t *testing.T, accountID, planType string) string { |
| 172 | + t.Helper() |
| 173 | + |
| 174 | + header := map[string]any{"alg": "none", "typ": "JWT"} |
| 175 | + payload := map[string]any{ |
| 176 | + "https://api.openai.com/auth": map[string]any{ |
| 177 | + "chatgpt_account_id": accountID, |
| 178 | + "chatgpt_plan_type": planType, |
| 179 | + }, |
| 180 | + } |
| 181 | + return encodeJWTPart(t, header) + "." + encodeJWTPart(t, payload) + ".signature" |
| 182 | +} |
| 183 | + |
| 184 | +func encodeJWTPart(t *testing.T, payload any) string { |
| 185 | + t.Helper() |
| 186 | + |
| 187 | + data, err := json.Marshal(payload) |
| 188 | + if err != nil { |
| 189 | + t.Fatalf("marshal JWT payload: %v", err) |
| 190 | + } |
| 191 | + return base64.RawURLEncoding.EncodeToString(data) |
| 192 | +} |
| 193 | + |
| 194 | +func toJSON(t *testing.T, payload any) string { |
| 195 | + t.Helper() |
| 196 | + |
| 197 | + data, err := json.Marshal(payload) |
| 198 | + if err != nil { |
| 199 | + t.Fatalf("marshal payload: %v", err) |
| 200 | + } |
| 201 | + return string(data) |
| 202 | +} |
0 commit comments