Unified · Split
| @@ -11,12 +11,11 @@ import ( · 10 unmodified lines | |||
| 11 | "time" | 11 | "time" |
| 12 | 12 | ||
| 13 | "github.com/gin-gonic/gin" | 13 | "github.com/gin-gonic/gin" |
| 14 | metaauth "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/meta" | ||
| 15 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" | 14 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 15 | "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" | ||
| 16 | coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" | 16 | coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 17 | "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" | 17 | "github.com/router-for-me/CLIProxyAPI/v7/sdk/proxyutil" |
| 18 | log "github.com/sirupsen/logrus" | 18 | log "github.com/sirupsen/logrus" |
| 19 | "golang.org/x/sync/singleflight" | ||
| 20 | ) | 19 | ) |
| 21 | 20 | ||
| 22 | const defaultAPICallTimeout = 60 * time.Second | 21 | const defaultAPICallTimeout = 60 * time.Second |
| @@ -360,8 +359,6 @@ func (h *Handler) refreshAntigravityOAuthAccessToken(ctx context.Context, auth * · 337 unmodified lines | |||
| 360 | return strings.TrimSpace(tokenResp.AccessToken), nil | 359 | return strings.TrimSpace(tokenResp.AccessToken), nil |
| 361 | } | 360 | } |
| 362 | 361 | ||
| 363 | var metaManagementMintGroup singleflight.Group | ||
| 364 | |||
| 365 | func metaTokenFromAuth(auth *coreauth.Auth) string { | 362 | func metaTokenFromAuth(auth *coreauth.Auth) string { |
| 366 | if auth == nil { | 363 | if auth == nil { |
| 367 | return "" | 364 | return "" |
| @@ -385,6 +382,29 @@ func metaTokenFromAuth(auth *coreauth.Auth) string { · 17 unmodified lines | |||
| 385 | return "" | 382 | return "" |
| 386 | } | 383 | } |
| 387 | 384 | ||
| 385 | // metaManagementPreparer applies the tool's proxy override only to acquisition. | ||
| 386 | // The saved credential retains its configured proxy. | ||
| 387 | type metaManagementPreparer struct { | ||
| 388 | executor *executor.MetaExecutor | ||
| 389 | proxyURL string | ||
| 390 | } | ||
| 391 | |||
| 392 | func (p metaManagementPreparer) ShouldPrepareRequestAuth(auth *coreauth.Auth) bool { | ||
| 393 | return p.executor.ShouldPrepareRequestAuth(auth) | ||
| 394 | } | ||
| 395 | |||
| 396 | func (p metaManagementPreparer) PrepareRequestAuth(ctx context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) { | ||
| 397 | proxyURL := auth.ProxyURL | ||
| 398 | if strings.TrimSpace(p.proxyURL) != "" { | ||
| 399 | auth.ProxyURL = p.proxyURL | ||
| 400 | } | ||
| 401 | updated, err := p.executor.PrepareRequestAuth(ctx, auth) | ||
| 402 | if updated != nil { | ||
| 403 | updated.ProxyURL = proxyURL | ||
| 404 | } | ||
| 405 | return updated, err | ||
| 406 | } | ||
| 407 | |||
| 388 | func (h *Handler) resolveMetaToken(ctx context.Context, auth *coreauth.Auth, requestProxyURL string) (string, error) { | 408 | func (h *Handler) resolveMetaToken(ctx context.Context, auth *coreauth.Auth, requestProxyURL string) (string, error) { |
| 389 | if ctx == nil { | 409 | if ctx == nil { |
| 390 | ctx = context.Background() | 410 | ctx = context.Background() |
| @@ -392,183 +412,25 @@ func (h *Handler) resolveMetaToken(ctx context.Context, auth *coreauth.Auth, req · 1 unmodified line | |||
| 392 | if auth == nil { | 412 | if auth == nil { |
| 393 | return "", nil | 413 | return "", nil |
| 394 | } | 414 | } |
| 395 | if tok := metaTokenFromAuth(auth); tok != "" { | 415 | if token := metaTokenFromAuth(auth); token != "" { |
| 396 | return tok, nil | 416 | return token, nil |
| 397 | } | 417 | } |
| 398 | 418 | var cfg *config.Config | |
| 399 | var dcaToken string | 419 | if h != nil { |
| 400 | if !coreauth.IsConfigAPIKeyAuth(auth) { | 420 | cfg = h.cfg |
| 401 | if auth.Metadata != nil { | ||
| 402 | if d, ok := auth.Metadata["dca_token"].(string); ok && strings.TrimSpace(d) != "" { | ||
| 403 | dcaToken = strings.TrimSpace(d) | ||
| 404 | } else if t, ok := auth.Metadata["access_token"].(string); ok && strings.HasPrefix(strings.TrimSpace(t), "dca:") { | ||
| 405 | dcaToken = strings.TrimSpace(t) | ||
| 406 | } | ||
| 407 | } | ||
| 408 | if dcaToken == "" && auth.Attributes != nil { | ||
| 409 | if d := strings.TrimSpace(auth.Attributes["dca_token"]); d != "" { | ||
| 410 | dcaToken = d | ||
| 411 | } else if t := strings.TrimSpace(auth.Attributes["access_token"]); strings.HasPrefix(t, "dca:") { | ||
| 412 | dcaToken = t | ||
| 413 | } | ||
| 414 | } | ||
| 415 | } | 421 | } |
| 416 | 422 | preparer := metaManagementPreparer{executor: executor.NewMetaExecutor(cfg), proxyURL: requestProxyURL} | |
| 417 | if dcaToken == "" { | 423 | if !preparer.ShouldPrepareRequestAuth(auth) { |
| 418 | return "", nil | 424 | return "", nil |
| 419 | } | 425 | } |
| 420 | 426 | if h == nil || h.authManager == nil || auth.ID == "" { | |
| 421 | flightKey := firstNonEmptyString(&auth.ID, &dcaToken) | 427 | return "", fmt.Errorf("meta token mint requires a registered credential") |
| 422 | |||
| 423 | mintRes, errMint, _ := metaManagementMintGroup.Do(flightKey, func() (any, error) { | ||
| 424 | if h != nil && h.authManager != nil { | ||
| 425 | var latest *coreauth.Auth | ||
| 426 | if auth.ID != "" { | ||
| 427 | if a, ok := h.authManager.GetByID(auth.ID); ok && a != nil { | ||
| 428 | latest = a | ||
| 429 | } | ||
| 430 | } | ||
| 431 | if latest == nil && auth.Index != "" { | ||
| 432 | latest = h.authByIndex(auth.Index) | ||
| 433 | } | ||
| 434 | if latest != nil { | ||
| 435 | if k := metaTokenFromAuth(latest); k != "" { | ||
| 436 | return k, nil | ||
| 437 | } | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | proxyURL := firstNonEmptyString(&requestProxyURL, &auth.ProxyURL) | ||
| 442 | var cfg *config.Config | ||
| 443 | if h != nil { | ||
| 444 | cfg = h.cfg | ||
| 445 | } | ||
| 446 | authSvc := metaauth.NewMetaAuthWithProxyURL(cfg, proxyURL) | ||
| 447 | minted, err := authSvc.MintAPIKey(ctx, dcaToken) | ||
| 448 | if err != nil { | ||
| 449 | return "", fmt.Errorf("meta token mint failed: %w", err) | ||
| 450 | } | ||
| 451 | if minted == nil || minted.APIKey == "" { | ||
| 452 | return "", fmt.Errorf("meta token mint returned empty key") | ||
| 453 | } | ||
| 454 | |||
| 455 | base := auth.Clone() | ||
| 456 | baseURL := "" | ||
| 457 | if auth.Attributes != nil { | ||
| 458 | baseURL = strings.TrimSpace(auth.Attributes["base_url"]) | ||
| 459 | } | ||
| 460 | if baseURL == "" { | ||
| 461 | baseURL = stringValue(auth.Metadata, "base_url") | ||
| 462 | } | ||
| 463 | if baseURL == "" { | ||
| 464 | baseURL = stringValue(auth.Metadata, "api_base_url") | ||
| 465 | } | ||
| 466 | if baseURL == "" { | ||
| 467 | baseURL = metaauth.DefaultAPIBaseURL | ||
| 468 | } | ||
| 469 | if mintedURL := strings.TrimSpace(minted.BaseURL); mintedURL != "" { | ||
| 470 | baseURL = mintedURL | ||
| 471 | } | ||
| 472 | if auth.Metadata == nil { | ||
| 473 | auth.Metadata = make(map[string]any) | ||
| 474 | } | ||
| 475 | auth.Metadata["base_url"] = baseURL | ||
| 476 | auth.Metadata["api_key"] = minted.APIKey | ||
| 477 | auth.Metadata["access_token"] = minted.APIKey | ||
| 478 | auth.Metadata["dca_token"] = dcaToken | ||
| 479 | delete(auth.Metadata, "expired") | ||
| 480 | if minted.UserEmail != "" { | ||
| 481 | auth.Metadata["email"] = minted.UserEmail | ||
| 482 | } | ||
| 483 | if minted.UserFullName != "" { | ||
| 484 | auth.Metadata["name"] = minted.UserFullName | ||
| 485 | } | ||
| 486 | now := time.Now() | ||
| 487 | nowStr := now.Format(time.RFC3339) | ||
| 488 | auth.LastRefreshedAt = now | ||
| 489 | auth.UpdatedAt = now | ||
| 490 | auth.Metadata["last_refresh"] = nowStr | ||
| 491 | |||
| 492 | if auth.Attributes == nil { | ||
| 493 | auth.Attributes = make(map[string]string) | ||
| 494 | } | ||
| 495 | auth.Attributes["base_url"] = baseURL | ||
| 496 | auth.Attributes["api_key"] = minted.APIKey | ||
| 497 | auth.Attributes["access_token"] = minted.APIKey | ||
| 498 | |||
| 499 | storage := &metaauth.MetaTokenStorage{Type: "meta", AuthKind: "oauth"} | ||
| 500 | if existing, ok := auth.Storage.(*metaauth.MetaTokenStorage); ok && existing != nil { | ||
| 501 | copyStorage := *existing | ||
| 502 | storage = ©Storage | ||
| 503 | } | ||
| 504 | storage.APIKey = minted.APIKey | ||
| 505 | storage.AccessToken = minted.APIKey | ||
| 506 | storage.DCAToken = dcaToken | ||
| 507 | storage.Expired = "" | ||
| 508 | storage.BaseURL = baseURL | ||
| 509 | storage.LastRefresh = nowStr | ||
| 510 | storage.Metadata = auth.Metadata | ||
| 511 | if minted.UserEmail != "" { | ||
| 512 | storage.Email = minted.UserEmail | ||
| 513 | } | ||
| 514 | if minted.UserFullName != "" { | ||
| 515 | storage.Name = minted.UserFullName | ||
| 516 | } | ||
| 517 | auth.Storage = storage | ||
| 518 | |||
| 519 | // Use the configured backend; direct file writes bypass remote token stores. | ||
| 520 | if !coreauth.IsConfigAPIKeyAuth(auth) { | ||
| 521 | store := h.tokenStoreWithBaseDir() | ||
| 522 | if store == nil { | ||
| 523 | return "", fmt.Errorf("meta token store unavailable") | ||
| 524 | } | ||
| 525 | if _, errSave := store.Save(ctx, auth); errSave != nil { | ||
| 526 | return "", fmt.Errorf("persist meta token: %w", errSave) | ||
| 527 | } | ||
| 528 | } | ||
| 529 | if h != nil && h.authManager != nil { | ||
| 530 | if _, errUpdate := h.authManager.UpdateRefreshedAuth(coreauth.WithSkipPersist(ctx), base, auth); errUpdate != nil { | ||
| 531 | return "", fmt.Errorf("update meta auth: %w", errUpdate) | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | return minted.APIKey, nil | ||
| 536 | }) | ||
| 537 | if errMint != nil { | ||
| 538 | return "", errMint | ||
| 539 | } | 428 | } |
| 540 | 429 | updated, err := h.authManager.PrepareRequestAuth(ctx, preparer, auth) | |
| 541 | key, _ := mintRes.(string) | 430 | if err != nil { |
| 542 | if h != nil && h.authManager != nil { | 431 | return "", err |
| 543 | var latest *coreauth.Auth | ||
| 544 | if auth.ID != "" { | ||
| 545 | if a, ok := h.authManager.GetByID(auth.ID); ok && a != nil { | ||
| 546 | latest = a | ||
| 547 | } | ||
| 548 | } | ||
| 549 | if latest == nil && auth.Index != "" { | ||
| 550 | latest = h.authByIndex(auth.Index) | ||
| 551 | } | ||
| 552 | if latest != nil { | ||
| 553 | if auth.Metadata == nil { | ||
| 554 | auth.Metadata = make(map[string]any) | ||
| 555 | } | ||
| 556 | for k, v := range latest.Metadata { | ||
| 557 | auth.Metadata[k] = v | ||
| 558 | } | ||
| 559 | if auth.Attributes == nil { | ||
| 560 | auth.Attributes = make(map[string]string) | ||
| 561 | } | ||
| 562 | for k, v := range latest.Attributes { | ||
| 563 | auth.Attributes[k] = v | ||
| 564 | } | ||
| 565 | auth.Storage = latest.Storage | ||
| 566 | auth.LastRefreshedAt = latest.LastRefreshedAt | ||
| 567 | auth.UpdatedAt = latest.UpdatedAt | ||
| 568 | } | ||
| 569 | } | 432 | } |
| 570 | 433 | return metaTokenFromAuth(updated), nil | |
| 571 | return key, nil | ||
| 572 | } | 434 | } |
| 573 | 435 | ||
| 574 | func antigravityTokenNeedsRefresh(metadata map[string]any) bool { | 436 | func antigravityTokenNeedsRefresh(metadata map[string]any) bool { |
| @@ -6,7 +6,6 @@ import ( · 5 unmodified lines | |||
| 6 | "errors" | 6 | "errors" |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "net/http/httptest" | 8 | "net/http/httptest" |
| 9 | "runtime" | ||
| 10 | "strings" | 9 | "strings" |
| 11 | "sync" | 10 | "sync" |
| 12 | "sync/atomic" | 11 | "sync/atomic" |
| @@ -14,6 +13,7 @@ import ( · 1 unmodified line | |||
| 14 | 13 | ||
| 15 | "github.com/gin-gonic/gin" | 14 | "github.com/gin-gonic/gin" |
| 16 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" | 15 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 16 | "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" | ||
| 17 | coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" | 17 | coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 18 | sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" | 18 | sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" |
| 19 | ) | 19 | ) |
| @@ -524,7 +524,15 @@ func TestResolveMetaToken_ConcurrentSingleflight(t *testing.T) { · 504 unmodified lines | |||
| 524 | entryWg.Done() | 524 | entryWg.Done() |
| 525 | <-ready | 525 | <-ready |
| 526 | inFlight.Done() | 526 | inFlight.Done() |
| 527 | token, errToken := h.resolveTokenForAuth(context.Background(), h.authByIndex(auth.Index), "") | 527 | var token string |
| 528 | var errToken error | ||
| 529 | if i%2 == 0 { | ||
| 530 | prepared, err := manager.PrepareRequestAuth(context.Background(), executor.NewMetaExecutor(h.cfg), auth.Clone()) | ||
| 531 | errToken = err | ||
| 532 | token = metaTokenFromAuth(prepared) | ||
| 533 | } else { | ||
| 534 | token, errToken = h.resolveTokenForAuth(context.Background(), auth.Clone(), "") | ||
| 535 | } | ||
| 528 | if errToken != nil { | 536 | if errToken != nil { |
| 529 | t.Errorf("resolveTokenForAuth error: %v", errToken) | 537 | t.Errorf("resolveTokenForAuth error: %v", errToken) |
| 530 | } | 538 | } |
| @@ -539,9 +547,6 @@ func TestResolveMetaToken_ConcurrentSingleflight(t *testing.T) { · 8 unmodified lines | |||
| 539 | 547 | ||
| 540 | <-serverStarted | 548 | <-serverStarted |
| 541 | inFlight.Wait() | 549 | inFlight.Wait() |
| 542 | for i := 0; i < 50; i++ { | ||
| 543 | runtime.Gosched() | ||
| 544 | } | ||
| 545 | close(releaseServer) | 550 | close(releaseServer) |
| 546 | doneWg.Wait() | 551 | doneWg.Wait() |
| 547 | 552 | ||
| @@ -556,3 +561,32 @@ func TestResolveMetaToken_ConcurrentSingleflight(t *testing.T) { · 8 unmodified lines | |||
| 556 | t.Error("live manager did not retain minted key in attributes") | 561 | t.Error("live manager did not retain minted key in attributes") |
| 557 | } | 562 | } |
| 558 | } | 563 | } |
| 564 | |||
| 565 | func TestResolveMetaTokenUsesRequestProxyWithoutSavingOverride(t *testing.T) { | ||
| 566 | proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| 567 | if r.Header.Get("Authorization") != "Bearer dca:proxy-test" { | ||
| 568 | t.Error("mint request did not carry the DCA token") | ||
| 569 | } | ||
| 570 | _, _ = w.Write([]byte(`{"api_key":"LLM|proxied"}`)) | ||
| 571 | })) | ||
| 572 | defer proxy.Close() | ||
| 573 | t.Setenv("META_MINT_URL", "http://meta.invalid/key") | ||
| 574 | store := &memoryAuthStore{} | ||
| 575 | manager := coreauth.NewManager(store, nil, nil) | ||
| 576 | auth, err := manager.Register(coreauth.WithSkipPersist(context.Background()), &coreauth.Auth{ | ||
| 577 | ID: "meta-proxy.json", Provider: "meta", ProxyURL: "http://127.0.0.1:1", | ||
| 578 | Metadata: map[string]any{"dca_token": "dca:proxy-test"}, | ||
| 579 | }) | ||
| 580 | if err != nil { | ||
| 581 | t.Fatal(err) | ||
| 582 | } | ||
| 583 | h := &Handler{cfg: &config.Config{}, authManager: manager, tokenStore: store} | ||
| 584 | token, err := h.resolveMetaToken(context.Background(), auth.Clone(), proxy.URL) | ||
| 585 | if err != nil || token != "LLM|proxied" { | ||
| 586 | t.Fatalf("resolve via request proxy: token=%q, err=%v", token, err) | ||
| 587 | } | ||
| 588 | live, _ := manager.GetByID(auth.ID) | ||
| 589 | if live.ProxyURL != auth.ProxyURL { | ||
| 590 | t.Fatal("request proxy override changed the credential's configured proxy") | ||
| 591 | } | ||
| 592 | } | ||
| @@ -173,43 +173,44 @@ func (ts *MetaTokenStorage) SaveTokenToFile(authFilePath string) error { · 172 unmodified lines | |||
| 173 | if ts.Name != "" { | 173 | if ts.Name != "" { |
| 174 | data["name"] = ts.Name | 174 | data["name"] = ts.Name |
| 175 | } | 175 | } |
| 176 | if raw, errRead := os.ReadFile(authFilePath); errRead == nil { | 176 | // Managed saves supply the current metadata, including deliberate deletions. |
| 177 | var existing map[string]any | 177 | // Only login callers without a metadata snapshot inherit settings from disk. |
| 178 | if errJSON := json.Unmarshal(raw, &existing); errJSON == nil { | 178 | metadata := ts.Metadata |
| 179 | for k, v := range existing { | 179 | if metadata == nil { |
| 180 | if _, isCred := metaCredentialFields[k]; isCred { | 180 | if raw, errRead := os.ReadFile(authFilePath); errRead == nil { |
| 181 | continue | 181 | _ = json.Unmarshal(raw, &metadata) |
| 182 | } | ||
| 183 | if _, exists := data[k]; !exists { | ||
| 184 | data[k] = v | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | 182 | } |
| 188 | } | 183 | } |
| 189 | for k, v := range ts.Metadata { | 184 | for k, v := range metadata { |
| 190 | if _, isCred := metaCredentialFields[k]; isCred { | 185 | if _, isCred := metaCredentialFields[k]; isCred { |
| 191 | continue | 186 | continue |
| 192 | } | 187 | } |
| 193 | if _, exists := data[k]; !exists { | 188 | // Supplied settings take precedence over storage's older snapshot. |
| 189 | if _, exists := data[k]; !exists || ts.Metadata != nil { | ||
| 194 | data[k] = v | 190 | data[k] = v |
| 195 | } | 191 | } |
| 196 | } | 192 | } |
| 197 | 193 | ||
| 198 | file, err := os.Create(authFilePath) | 194 | raw, err := json.MarshalIndent(data, "", " ") |
| 195 | if err != nil { | ||
| 196 | return fmt.Errorf("meta token storage: encode token file: %w", err) | ||
| 197 | } | ||
| 198 | file, err := os.CreateTemp(filepath.Dir(authFilePath), ".meta-token-*") | ||
| 199 | if err != nil { | 199 | if err != nil { |
| 200 | return fmt.Errorf("meta token storage: create token file: %w", err) | 200 | return fmt.Errorf("meta token storage: create token file: %w", err) |
| 201 | } | 201 | } |
| 202 | defer func() { | 202 | defer func() { _ = os.Remove(file.Name()) }() |
| 203 | if errClose := file.Close(); errClose != nil { | 203 | if _, errWrite := file.Write(append(raw, '\n')); errWrite != nil { |
| 204 | log.Errorf("meta token storage: close token file error: %v", errClose) | 204 | _ = file.Close() |
| 205 | } | 205 | return fmt.Errorf("meta token storage: write token file: %w", errWrite) |
| 206 | }() | 206 | } |
| 207 | 207 | if errClose := file.Close(); errClose != nil { | |
| 208 | encoder := json.NewEncoder(file) | 208 | return fmt.Errorf("meta token storage: close token file: %w", errClose) |
| 209 | encoder.SetIndent("", " ") | ||
| 210 | if err = encoder.Encode(data); err != nil { | ||
| 211 | return fmt.Errorf("meta token storage: write token file: %w", err) | ||
| 212 | } | 209 | } |
| 210 | if errRename := os.Rename(file.Name(), authFilePath); errRename != nil { | ||
| 211 | return fmt.Errorf("meta token storage: replace token file: %w", errRename) | ||
| 212 | } | ||
| 213 | |||
| 213 | return nil | 214 | return nil |
| 214 | } | 215 | } |
| 215 | 216 | ||
| @@ -536,7 +537,8 @@ func (a *MetaAuth) CreateTokenStorage(bundle *MetaAuthBundle) *MetaTokenStorage · 320 unmodified lines | |||
| 536 | } | 537 | } |
| 537 | } | 538 | } |
| 538 | 539 | ||
| 539 | // CredentialFileName derives a deterministic, collision-free file name for the credentials. | 540 | // CredentialFileName keeps a readable account name and hashes the original identity |
| 541 | // so distinct emails that sanitize identically cannot overwrite each other. | ||
| 540 | func CredentialFileName(email, sub string) string { | 542 | func CredentialFileName(email, sub string) string { |
| 541 | clean := strings.TrimSpace(email) | 543 | clean := strings.TrimSpace(email) |
| 542 | if clean != "" { | 544 | if clean != "" { |
| @@ -546,7 +548,12 @@ func CredentialFileName(email, sub string) string { · 3 unmodified lines | |||
| 546 | } | 548 | } |
| 547 | return '_' | 549 | return '_' |
| 548 | }, clean) | 550 | }, clean) |
| 549 | return fmt.Sprintf("meta-%s.json", sanitized) | 551 | // Leave room for the prefix, identity hash and extension on common filesystems. |
| 552 | if len(sanitized) > 120 { | ||
| 553 | sanitized = sanitized[:120] | ||
| 554 | } | ||
| 555 | hash := sha256.Sum256([]byte(clean)) | ||
| 556 | return fmt.Sprintf("meta-%s-%s.json", sanitized, hex.EncodeToString(hash[:8])) | ||
| 550 | } | 557 | } |
| 551 | cleanSub := strings.TrimSpace(sub) | 558 | cleanSub := strings.TrimSpace(sub) |
| 552 | if cleanSub != "" { | 559 | if cleanSub != "" { |
| @@ -7,6 +7,7 @@ import ( · 6 unmodified lines | |||
| 7 | "net/http/httptest" | 7 | "net/http/httptest" |
| 8 | "os" | 8 | "os" |
| 9 | "path/filepath" | 9 | "path/filepath" |
| 10 | "strings" | ||
| 10 | "testing" | 11 | "testing" |
| 11 | 12 | ||
| 12 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" | 13 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| @@ -144,7 +145,7 @@ func TestMetaAuth_WaitForAuthorization(t *testing.T) { · 131 unmodified lines | |||
| 144 | } | 145 | } |
| 145 | 146 | ||
| 146 | func TestCredentialFileName(t *testing.T) { | 147 | func TestCredentialFileName(t *testing.T) { |
| 147 | if got := CredentialFileName("user@example.com", ""); got != "meta-user_example.com.json" { | 148 | if got := CredentialFileName("user@example.com", ""); !strings.HasPrefix(got, "meta-user_example.com-") || !strings.HasSuffix(got, ".json") { |
| 148 | t.Errorf("unexpected fileName: %s", got) | 149 | t.Errorf("unexpected fileName: %s", got) |
| 149 | } | 150 | } |
| 150 | if got := CredentialFileName("", "12345"); got == "" || !filepath.IsLocal(got) { | 151 | if got := CredentialFileName("", "12345"); got == "" || !filepath.IsLocal(got) { |
| @@ -298,3 +299,62 @@ func TestCreateTokenStorageUsesMintedBaseURL(t *testing.T) { · 147 unmodified lines | |||
| 298 | }) | 299 | }) |
| 299 | } | 300 | } |
| 300 | } | 301 | } |
| 302 | |||
| 303 | func TestCredentialFileNameSeparatesAccounts(t *testing.T) { | ||
| 304 | first := CredentialFileName("alice+work@example.com", "dca:first") | ||
| 305 | second := CredentialFileName("alice_work@example.com", "dca:second") | ||
| 306 | if first == second { | ||
| 307 | t.Fatal("distinct accounts overwrite the same file") | ||
| 308 | } | ||
| 309 | if first != CredentialFileName("alice+work@example.com", "dca:replacement") { | ||
| 310 | t.Fatal("re-login changed account file") | ||
| 311 | } | ||
| 312 | long := CredentialFileName(strings.Repeat("a", 300)+"@example.com", "") | ||
| 313 | if len(long) > 255 || !filepath.IsLocal(long) { | ||
| 314 | t.Fatal("filename cannot be saved on common filesystems") | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | func TestSaveTokenToFileUsesCurrentSettings(t *testing.T) { | ||
| 319 | path := filepath.Join(t.TempDir(), "meta.json") | ||
| 320 | if err := os.WriteFile(path, []byte(`{"disabled":false,"priority":1,"notes":"removed"}`), 0600); err != nil { | ||
| 321 | t.Fatal(err) | ||
| 322 | } | ||
| 323 | storage := &MetaTokenStorage{AccessToken: "LLM|key", APIKey: "LLM|key", Metadata: map[string]any{"disabled": true, "priority": float64(2)}} | ||
| 324 | if err := storage.SaveTokenToFile(path); err != nil { | ||
| 325 | t.Fatal(err) | ||
| 326 | } | ||
| 327 | raw, err := os.ReadFile(path) | ||
| 328 | if err != nil { | ||
| 329 | t.Fatal(err) | ||
| 330 | } | ||
| 331 | var saved map[string]any | ||
| 332 | if err := json.Unmarshal(raw, &saved); err != nil { | ||
| 333 | t.Fatal(err) | ||
| 334 | } | ||
| 335 | if saved["disabled"] != true || saved["priority"] != float64(2) { | ||
| 336 | t.Fatalf("restored old settings: %s", raw) | ||
| 337 | } | ||
| 338 | if _, exists := saved["notes"]; exists { | ||
| 339 | t.Fatal("restored a deleted setting") | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | func TestSaveTokenToFilePreservesFileOnEncodingFailure(t *testing.T) { | ||
| 344 | path := filepath.Join(t.TempDir(), "meta.json") | ||
| 345 | initial := `{"access_token":"LLM|previous"}` | ||
| 346 | if err := os.WriteFile(path, []byte(initial), 0600); err != nil { | ||
| 347 | t.Fatal(err) | ||
| 348 | } | ||
| 349 | storage := &MetaTokenStorage{AccessToken: "LLM|new", Metadata: map[string]any{"invalid": make(chan int)}} | ||
| 350 | if err := storage.SaveTokenToFile(path); err == nil { | ||
| 351 | t.Fatal("expected encoding failure") | ||
| 352 | } | ||
| 353 | raw, err := os.ReadFile(path) | ||
| 354 | if err != nil { | ||
| 355 | t.Fatal(err) | ||
| 356 | } | ||
| 357 | if string(raw) != initial { | ||
| 358 | t.Fatalf("failed save corrupted existing credential: %q", raw) | ||
| 359 | } | ||
| 360 | } | ||
| @@ -208,46 +208,27 @@ func (e *MetaExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*c · 207 unmodified lines | |||
| 208 | auth.Attributes["api_key"] = minted.APIKey | 208 | auth.Attributes["api_key"] = minted.APIKey |
| 209 | auth.Attributes["access_token"] = minted.APIKey | 209 | auth.Attributes["access_token"] = minted.APIKey |
| 210 | 210 | ||
| 211 | var storage *metaauth.MetaTokenStorage | 211 | // Auth.Clone does not clone Storage. Keep the candidate isolated from live |
| 212 | if ms, ok := auth.Storage.(*metaauth.MetaTokenStorage); ok && ms != nil { | 212 | // login storage; file-loaded records already persist through Metadata. |
| 213 | storage = ms | 213 | if existing, ok := auth.Storage.(*metaauth.MetaTokenStorage); ok && existing != nil { |
| 214 | storage := *existing | ||
| 214 | storage.APIKey = minted.APIKey | 215 | storage.APIKey = minted.APIKey |
| 215 | storage.AccessToken = minted.APIKey | 216 | storage.AccessToken = minted.APIKey |
| 216 | storage.DCAToken = dcaToken | 217 | storage.DCAToken = dcaToken |
| 217 | storage.Expired = "" | 218 | storage.Expired = "" |
| 219 | storage.BaseURL = baseURL | ||
| 220 | storage.LastRefresh = nowStr | ||
| 221 | storage.Metadata = auth.Metadata | ||
| 218 | if minted.UserEmail != "" { | 222 | if minted.UserEmail != "" { |
| 219 | storage.Email = minted.UserEmail | 223 | storage.Email = minted.UserEmail |
| 220 | } | 224 | } |
| 221 | if minted.UserFullName != "" { | 225 | if minted.UserFullName != "" { |
| 222 | storage.Name = minted.UserFullName | 226 | storage.Name = minted.UserFullName |
| 223 | } | 227 | } |
| 224 | storage.LastRefresh = nowStr | 228 | auth.Storage = &storage |
| 225 | } else { | ||
| 226 | storage = &metaauth.MetaTokenStorage{ | ||
| 227 | Type: "meta", | ||
| 228 | AuthKind: "oauth", | ||
| 229 | AccessToken: minted.APIKey, | ||
| 230 | APIKey: minted.APIKey, | ||
| 231 | DCAToken: dcaToken, | ||
| 232 | Email: minted.UserEmail, | ||
| 233 | Name: minted.UserFullName, | ||
| 234 | LastRefresh: nowStr, | ||
| 235 | Metadata: auth.Metadata, | ||
| 236 | } | ||
| 237 | auth.Storage = storage | ||
| 238 | } | ||
| 239 | |||
| 240 | storage.BaseURL = baseURL | ||
| 241 | |||
| 242 | filePath := strings.TrimSpace(auth.Attributes[cliproxyauth.AttributePath]) | ||
| 243 | if filePath == "" { | ||
| 244 | filePath = strings.TrimSpace(auth.FileName) | ||
| 245 | } | ||
| 246 | if filePath != "" { | ||
| 247 | if errSave := storage.SaveTokenToFile(filePath); errSave != nil { | ||
| 248 | log.Warnf("meta executor: failed to persist refreshed token to %s: %v", filePath, errSave) | ||
| 249 | } | ||
| 250 | } | 229 | } |
| 230 | // Only the manager may accept and persist the candidate. | ||
| 231 | auth.LastRefreshedAt = time.Now() | ||
| 251 | 232 | ||
| 252 | return auth, nil | 233 | return auth, nil |
| 253 | } | 234 | } |
| @@ -8,14 +8,15 @@ import ( · 7 unmodified lines | |||
| 8 | "net/http/httptest" | 8 | "net/http/httptest" |
| 9 | "os" | 9 | "os" |
| 10 | "path/filepath" | 10 | "path/filepath" |
| 11 | "runtime" | ||
| 12 | "sync" | 11 | "sync" |
| 13 | "sync/atomic" | 12 | "sync/atomic" |
| 14 | "testing" | 13 | "testing" |
| 15 | "time" | 14 | "time" |
| 16 | 15 | ||
| 16 | metaauth "github.com/router-for-me/CLIProxyAPI/v7/internal/auth/meta" | ||
| 17 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" | 17 | "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 18 | "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" | 18 | "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" |
| 19 | sdkauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth" | ||
| 19 | cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" | 20 | cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 20 | cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" | 21 | cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" |
| 21 | sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" | 22 | sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" |
| @@ -198,7 +199,7 @@ func mapToJSON(m map[string]any) string { · 176 unmodified lines | |||
| 198 | return string(b) | 199 | return string(b) |
| 199 | } | 200 | } |
| 200 | 201 | ||
| 201 | func TestMetaExecutor_Refresh_DCA_MintAndPersist(t *testing.T) { | 202 | func TestMetaExecutor_Refresh_RequiresManagerAcceptance(t *testing.T) { |
| 202 | tempDir := t.TempDir() | 203 | tempDir := t.TempDir() |
| 203 | authFilePath := filepath.Join(tempDir, "meta-test.json") | 204 | authFilePath := filepath.Join(tempDir, "meta-test.json") |
| 204 | initialContent := `{"type":"meta","auth_kind":"oauth","access_token":"dca:initial-dca","dca_token":"dca:initial-dca","expired":"2020-01-01T00:00:00Z","request-retry":3}` | 205 | initialContent := `{"type":"meta","auth_kind":"oauth","access_token":"dca:initial-dca","dca_token":"dca:initial-dca","expired":"2020-01-01T00:00:00Z","request-retry":3}` |
| @@ -223,6 +224,8 @@ func TestMetaExecutor_Refresh_DCA_MintAndPersist(t *testing.T) { · 18 unmodified lines | |||
| 223 | t.Setenv("META_MINT_URL", server.URL+"/key") | 224 | t.Setenv("META_MINT_URL", server.URL+"/key") |
| 224 | 225 | ||
| 225 | auth := &cliproxyauth.Auth{ | 226 | auth := &cliproxyauth.Auth{ |
| 227 | ID: "meta-test.json", | ||
| 228 | FileName: "meta-test.json", | ||
| 226 | Provider: "meta", | 229 | Provider: "meta", |
| 227 | Attributes: map[string]string{ | 230 | Attributes: map[string]string{ |
| 228 | cliproxyauth.AttributePath: authFilePath, | 231 | cliproxyauth.AttributePath: authFilePath, |
| @@ -233,8 +236,17 @@ func TestMetaExecutor_Refresh_DCA_MintAndPersist(t *testing.T) { · 4 unmodified lines | |||
| 233 | }, | 236 | }, |
| 234 | } | 237 | } |
| 235 | 238 | ||
| 239 | store := sdkauth.NewFileTokenStore() | ||
| 240 | store.SetBaseDir(tempDir) | ||
| 241 | manager := cliproxyauth.NewManager(store, nil, nil) | ||
| 242 | auth.Metadata["request-retry"] = float64(3) | ||
| 243 | auth.Storage = &metaauth.MetaTokenStorage{AccessToken: "dca:initial-dca", DCAToken: "dca:initial-dca", Expired: "2020-01-01T00:00:00Z"} | ||
| 244 | auth, errRegister := manager.Register(cliproxyauth.WithSkipPersist(context.Background()), auth) | ||
| 245 | if errRegister != nil { | ||
| 246 | t.Fatal(errRegister) | ||
| 247 | } | ||
| 236 | exec := NewMetaExecutor(&config.Config{}) | 248 | exec := NewMetaExecutor(&config.Config{}) |
| 237 | refreshed, err := exec.Refresh(context.Background(), auth) | 249 | refreshed, err := exec.Refresh(context.Background(), auth.Clone()) |
| 238 | if err != nil { | 250 | if err != nil { |
| 239 | t.Fatalf("exec.Refresh error: %v", err) | 251 | t.Fatalf("exec.Refresh error: %v", err) |
| 240 | } | 252 | } |
| @@ -249,6 +261,20 @@ func TestMetaExecutor_Refresh_DCA_MintAndPersist(t *testing.T) { · 8 unmodified lines | |||
| 249 | t.Errorf("expected expired to be removed from metadata after minting") | 261 | t.Errorf("expected expired to be removed from metadata after minting") |
| 250 | } | 262 | } |
| 251 | 263 | ||
| 264 | unchanged, errRead := os.ReadFile(authFilePath) | ||
| 265 | if errRead != nil { | ||
| 266 | t.Fatal(errRead) | ||
| 267 | } | ||
| 268 | if string(unchanged) != initialContent { | ||
| 269 | t.Fatal("executor wrote candidate before manager acceptance") | ||
| 270 | } | ||
| 271 | live, _ := manager.GetByID(auth.ID) | ||
| 272 | if live.Storage.(*metaauth.MetaTokenStorage).AccessToken != "dca:initial-dca" { | ||
| 273 | t.Fatal("refresh mutated live token storage") | ||
| 274 | } | ||
| 275 | if _, err := manager.UpdateRefreshedAuth(context.Background(), auth, refreshed); err != nil { | ||
| 276 | t.Fatal(err) | ||
| 277 | } | ||
| 252 | // Verify durable file persistence on disk | 278 | // Verify durable file persistence on disk |
| 253 | diskBytes, errRead := os.ReadFile(authFilePath) | 279 | diskBytes, errRead := os.ReadFile(authFilePath) |
| 254 | if errRead != nil { | 280 | if errRead != nil { |
| @@ -265,12 +291,12 @@ func TestMetaExecutor_Refresh_DCA_MintAndPersist(t *testing.T) { · 10 unmodified lines | |||
| 265 | t.Errorf("expected persisted access_token LLM|persisted-minted-key, got %v", diskData["access_token"]) | 291 | t.Errorf("expected persisted access_token LLM|persisted-minted-key, got %v", diskData["access_token"]) |
| 266 | } | 292 | } |
| 267 | // Verify existing properties preserved | 293 | // Verify existing properties preserved |
| 268 | if diskData["request-retry"] != float64(3) { | 294 | if diskData["request_retry"] != float64(3) { |
| 269 | t.Errorf("expected preserved request-retry: 3, got %v", diskData["request-retry"]) | 295 | t.Errorf("expected preserved request-retry: 3, got %v", diskData["request_retry"]) |
| 270 | } | 296 | } |
| 271 | } | 297 | } |
| 272 | 298 | ||
| 273 | func TestMetaExecutor_Refresh_SingleflightAndMultiAccount(t *testing.T) { | 299 | func TestMetaExecutor_PrepareConcurrentAccounts(t *testing.T) { |
| 274 | var count1, count2 int64 | 300 | var count1, count2 int64 |
| 275 | 301 | ||
| 276 | serverStarted := make(chan struct{}) | 302 | serverStarted := make(chan struct{}) |
| @@ -312,10 +338,15 @@ func TestMetaExecutor_Refresh_SingleflightAndMultiAccount(t *testing.T) { · 35 unmodified lines | |||
| 312 | // 10 concurrent refreshes for account 1 | 338 | // 10 concurrent refreshes for account 1 |
| 313 | var wg sync.WaitGroup | 339 | var wg sync.WaitGroup |
| 314 | auth1 := &cliproxyauth.Auth{ | 340 | auth1 := &cliproxyauth.Auth{ |
| 341 | ID: "meta-acct1", | ||
| 315 | Provider: "meta", | 342 | Provider: "meta", |
| 316 | Metadata: map[string]any{"dca_token": "dca:acct1"}, | 343 | Metadata: map[string]any{"dca_token": "dca:acct1"}, |
| 317 | } | 344 | } |
| 318 | 345 | ||
| 346 | manager := cliproxyauth.NewManager(nil, nil, nil) | ||
| 347 | if _, err := manager.Register(context.Background(), auth1); err != nil { | ||
| 348 | t.Fatal(err) | ||
| 349 | } | ||
| 319 | const goroutines = 10 | 350 | const goroutines = 10 |
| 320 | var entryWg sync.WaitGroup | 351 | var entryWg sync.WaitGroup |
| 321 | entryWg.Add(goroutines) | 352 | entryWg.Add(goroutines) |
| @@ -330,7 +361,7 @@ func TestMetaExecutor_Refresh_SingleflightAndMultiAccount(t *testing.T) { · 8 unmodified lines | |||
| 330 | entryWg.Done() | 361 | entryWg.Done() |
| 331 | <-ready | 362 | <-ready |
| 332 | inFlight.Done() | 363 | inFlight.Done() |
| 333 | res, err := exec.Refresh(context.Background(), auth1.Clone()) | 364 | res, err := manager.PrepareRequestAuth(context.Background(), exec, auth1.Clone()) |
| 334 | if err != nil { | 365 | if err != nil { |
| 335 | t.Errorf("Refresh acct1 error: %v", err) | 366 | t.Errorf("Refresh acct1 error: %v", err) |
| 336 | } | 367 | } |
| @@ -348,12 +379,12 @@ func TestMetaExecutor_Refresh_SingleflightAndMultiAccount(t *testing.T) { · 11 unmodified lines | |||
| 348 | <-serverStarted | 379 | <-serverStarted |
| 349 | inFlight.Wait() | 380 | inFlight.Wait() |
| 350 | 381 | ||
| 351 | // Allow pending goroutines to enter singleflight.Do while the server holds the in-flight request. | 382 | // A second account must complete while the first account's mint is blocked. |
| 352 | for i := 0; i < 50; i++ { | 383 | auth2 := &cliproxyauth.Auth{ID: "meta-acct2", Provider: "meta", Metadata: map[string]any{"dca_token": "dca:acct2"}} |
| 353 | runtime.Gosched() | 384 | if _, err := manager.Register(context.Background(), auth2); err != nil { |
| 385 | t.Fatal(err) | ||
| 354 | } | 386 | } |
| 355 | 387 | res2, err2 := manager.PrepareRequestAuth(context.Background(), exec, auth2.Clone()) | |
| 356 | // Release the HTTP server handler to complete the single in-flight mint. | ||
| 357 | close(releaseServer) | 388 | close(releaseServer) |
| 358 | wg.Wait() | 389 | wg.Wait() |
| 359 | 390 | ||
| @@ -361,12 +392,6 @@ func TestMetaExecutor_Refresh_SingleflightAndMultiAccount(t *testing.T) { · 1 unmodified line | |||
| 361 | t.Errorf("expected exactly 1 singleflight mint request for acct1, got %d", totalMint1) | 392 | t.Errorf("expected exactly 1 singleflight mint request for acct1, got %d", totalMint1) |
| 362 | } | 393 | } |
| 363 | 394 | ||
| 364 | // Account 2 refreshes independently | ||
| 365 | auth2 := &cliproxyauth.Auth{ | ||
| 366 | Provider: "meta", | ||
| 367 | Metadata: map[string]any{"dca_token": "dca:acct2"}, | ||
| 368 | } | ||
| 369 | res2, err2 := exec.Refresh(context.Background(), auth2.Clone()) | ||
| 370 | if err2 != nil { | 395 | if err2 != nil { |
| 371 | t.Fatalf("Refresh acct2 error: %v", err2) | 396 | t.Fatalf("Refresh acct2 error: %v", err2) |
| 372 | } | 397 | } |
| @@ -504,17 +529,13 @@ func TestMetaExecutorRefreshUsesMintedBaseURL(t *testing.T) { · 131 unmodified lines | |||
| 504 | if got, _ := metaCreds(updated); got != tc.want { | 529 | if got, _ := metaCreds(updated); got != tc.want { |
| 505 | t.Errorf("request base URL = %q, want %q", got, tc.want) | 530 | t.Errorf("request base URL = %q, want %q", got, tc.want) |
| 506 | } | 531 | } |
| 507 | raw, err := os.ReadFile(path) | 532 | if updated.Metadata["base_url"] != tc.want { |
| 508 | if err != nil { | 533 | t.Errorf("candidate base URL = %v, want %q", updated.Metadata["base_url"], tc.want) |
| 509 | t.Fatal(err) | ||
| 510 | } | ||
| 511 | var saved map[string]any | ||
| 512 | if err = json.Unmarshal(raw, &saved); err != nil { | ||
| 513 | t.Fatal(err) | ||
| 514 | } | 534 | } |
| 515 | if saved["base_url"] != tc.want { | 535 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 516 | t.Errorf("saved base URL = %v, want %q", saved["base_url"], tc.want) | 536 | t.Fatalf("refresh created a file: %v", err) |
| 517 | } | 537 | } |
| 538 | |||
| 518 | }) | 539 | }) |
| 519 | } | 540 | } |
| 520 | } | 541 | } |
| @@ -616,7 +637,7 @@ func TestMetaExecutor_RequestAuthPreparer(t *testing.T) { · 95 unmodified lines | |||
| 616 | } | 637 | } |
| 617 | 638 | ||
| 618 | authDCAOnly := &cliproxyauth.Auth{ | 639 | authDCAOnly := &cliproxyauth.Auth{ |
| 619 | ID: "meta-prep-test", | 640 | ID: "meta-prep-test.json", |
| 620 | Provider: "meta", | 641 | Provider: "meta", |
| 621 | Metadata: map[string]any{ | 642 | Metadata: map[string]any{ |
| 622 | "dca_token": "dca:valid", | 643 | "dca_token": "dca:valid", |
| @@ -630,7 +651,9 @@ func TestMetaExecutor_RequestAuthPreparer(t *testing.T) { · 7 unmodified lines | |||
| 630 | t.Errorf("ShouldPrepareRequestAuth should be true when only dca_token is present") | 651 | t.Errorf("ShouldPrepareRequestAuth should be true when only dca_token is present") |
| 631 | } | 652 | } |
| 632 | 653 | ||
| 633 | manager := cliproxyauth.NewManager(nil, nil, nil) | 654 | store := sdkauth.NewFileTokenStore() |
| 655 | store.SetBaseDir(t.TempDir()) | ||
| 656 | manager := cliproxyauth.NewManager(store, nil, nil) | ||
| 634 | manager.RegisterExecutor(exec) | 657 | manager.RegisterExecutor(exec) |
| 635 | if _, err := manager.Register(context.Background(), authDCAOnly); err != nil { | 658 | if _, err := manager.Register(context.Background(), authDCAOnly); err != nil { |
| 636 | t.Fatal(err) | 659 | t.Fatal(err) |
| @@ -662,6 +685,14 @@ func TestMetaExecutor_RequestAuthPreparer(t *testing.T) { · 25 unmodified lines | |||
| 662 | t.Errorf("expected stored api_key 'LLM|prepared-key', got %q", key) | 685 | t.Errorf("expected stored api_key 'LLM|prepared-key', got %q", key) |
| 663 | } | 686 | } |
| 664 | 687 | ||
| 688 | reloaded, err := store.List(context.Background()) | ||
| 689 | if err != nil || len(reloaded) != 1 { | ||
| 690 | t.Fatalf("reload auth: %v, records=%d", err, len(reloaded)) | ||
| 691 | } | ||
| 692 | if reloaded[0].Metadata["api_key"] != "LLM|prepared-key" { | ||
| 693 | t.Fatal("minted key was not persisted for restart") | ||
| 694 | } | ||
| 695 | |||
| 665 | _, err = manager.Execute(context.Background(), []string{"meta"}, req, cliproxyexecutor.Options{SourceFormat: sdktranslator.FromString("openai")}) | 696 | _, err = manager.Execute(context.Background(), []string{"meta"}, req, cliproxyexecutor.Options{SourceFormat: sdktranslator.FromString("openai")}) |
| 666 | if err != nil { | 697 | if err != nil { |
| 667 | t.Fatalf("second execute failed: %v", err) | 698 | t.Fatalf("second execute failed: %v", err) |
| @@ -670,3 +701,91 @@ func TestMetaExecutor_RequestAuthPreparer(t *testing.T) { · 2 unmodified lines | |||
| 670 | t.Fatalf("mint count after second request = %d, want 1", mints.Load()) | 701 | t.Fatalf("mint count after second request = %d, want 1", mints.Load()) |
| 671 | } | 702 | } |
| 672 | } | 703 | } |
| 704 | |||
| 705 | func TestMetaMintRejectsRemovedOrReloadedCredential(t *testing.T) { | ||
| 706 | for _, prepare := range []bool{false, true} { | ||
| 707 | for _, action := range []string{"remove", "reload"} { | ||
| 708 | name := action + "/refresh" | ||
| 709 | if prepare { | ||
| 710 | name = action + "/prepare" | ||
| 711 | } | ||
| 712 | t.Run(name, func(t *testing.T) { | ||
| 713 | started := make(chan struct{}) | ||
| 714 | release := make(chan struct{}) | ||
| 715 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| 716 | close(started) | ||
| 717 | <-release | ||
| 718 | _, _ = w.Write([]byte(`{"api_key":"LLM|obsolete"}`)) | ||
| 719 | })) | ||
| 720 | defer server.Close() | ||
| 721 | var releaseOnce sync.Once | ||
| 722 | defer releaseOnce.Do(func() { close(release) }) | ||
| 723 | t.Setenv("META_MINT_URL", server.URL) | ||
| 724 | dir := t.TempDir() | ||
| 725 | store := sdkauth.NewFileTokenStore() | ||
| 726 | store.SetBaseDir(dir) | ||
| 727 | manager := cliproxyauth.NewManager(store, nil, nil) | ||
| 728 | storage := &metaauth.MetaTokenStorage{AccessToken: "dca:initial", DCAToken: "dca:initial"} | ||
| 729 | auth, err := manager.Register(context.Background(), &cliproxyauth.Auth{ID: "meta-lifecycle.json", Provider: "meta", Storage: storage, Metadata: map[string]any{"type": "meta", "access_token": "dca:initial", "dca_token": "dca:initial"}}) | ||
| 730 | if err != nil { | ||
| 731 | t.Fatal(err) | ||
| 732 | } | ||
| 733 | exec := NewMetaExecutor(nil) | ||
| 734 | done := make(chan error, 1) | ||
| 735 | go func() { | ||
| 736 | if prepare { | ||
| 737 | _, err := manager.PrepareRequestAuth(context.Background(), exec, auth.Clone()) | ||
| 738 | done <- err | ||
| 739 | return | ||
| 740 | } | ||
| 741 | updated, err := exec.Refresh(context.Background(), auth.Clone()) | ||
| 742 | if err == nil { | ||
| 743 | _, err = manager.UpdateRefreshedAuth(context.Background(), auth, updated) | ||
| 744 | } | ||
| 745 | done <- err | ||
| 746 | }() | ||
| 747 | <-started | ||
| 748 | path := filepath.Join(dir, auth.ID) | ||
| 749 | if action == "remove" { | ||
| 750 | manager.Remove(context.Background(), auth.ID) | ||
| 751 | if err := store.Delete(context.Background(), auth.ID); err != nil { | ||
| 752 | t.Fatal(err) | ||
| 753 | } | ||
| 754 | } else { | ||
| 755 | _, err := manager.Register(context.Background(), &cliproxyauth.Auth{ID: auth.ID, Provider: "meta", Metadata: map[string]any{"type": "meta", "api_key": "LLM|reloaded", "access_token": "LLM|reloaded", "dca_token": "dca:reloaded"}}) | ||
| 756 | if err != nil { | ||
| 757 | t.Fatal(err) | ||
| 758 | } | ||
| 759 | } | ||
| 760 | releaseOnce.Do(func() { close(release) }) | ||
| 761 | err = <-done | ||
| 762 | if (prepare || action == "reload") && err == nil { | ||
| 763 | t.Fatal("obsolete mint was accepted") | ||
| 764 | } | ||
| 765 | if storage.AccessToken != "dca:initial" { | ||
| 766 | t.Fatal("obsolete mint changed shared login storage") | ||
| 767 | } | ||
| 768 | raw, errRead := os.ReadFile(path) | ||
| 769 | if action == "remove" { | ||
| 770 | if !os.IsNotExist(errRead) { | ||
| 771 | t.Fatalf("removed auth was recreated: %s, %v", raw, errRead) | ||
| 772 | } | ||
| 773 | if _, ok := manager.GetByID(auth.ID); ok { | ||
| 774 | t.Fatal("removed auth was reinstalled") | ||
| 775 | } | ||
| 776 | } else { | ||
| 777 | if errRead != nil { | ||
| 778 | t.Fatal(errRead) | ||
| 779 | } | ||
| 780 | var saved map[string]any | ||
| 781 | if err := json.Unmarshal(raw, &saved); err != nil { | ||
| 782 | t.Fatal(err) | ||
| 783 | } | ||
| 784 | if saved["api_key"] != "LLM|reloaded" { | ||
| 785 | t.Fatalf("obsolete mint overwrote reload: %s", raw) | ||
| 786 | } | ||
| 787 | } | ||
| 788 | }) | ||
| 789 | } | ||
| 790 | } | ||
| 791 | } | ||
| @@ -1387,7 +1387,17 @@ func (m *Manager) prepareRequestAuth(ctx context.Context, executor ProviderExecu · 1386 unmodified lines | |||
| 1387 | return auth, nil | 1387 | return auth, nil |
| 1388 | } | 1388 | } |
| 1389 | preparer, ok := executor.(RequestAuthPreparer) | 1389 | preparer, ok := executor.(RequestAuthPreparer) |
| 1390 | if !ok || preparer == nil || !preparer.ShouldPrepareRequestAuth(auth) { | 1390 | if !ok { |
| 1391 | return auth, nil | ||
| 1392 | } | ||
| 1393 | |||
| 1394 | return m.PrepareRequestAuth(ctx, preparer, auth) | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | // PrepareRequestAuth prepares a registered credential using the same serialization | ||
| 1398 | // and lifecycle checks as normal request execution. Management tools use this path too. | ||
| 1399 | func (m *Manager) PrepareRequestAuth(ctx context.Context, preparer RequestAuthPreparer, auth *Auth) (*Auth, error) { | ||
| 1400 | if m == nil || preparer == nil || auth == nil || !preparer.ShouldPrepareRequestAuth(auth) { | ||
| 1391 | return auth, nil | 1401 | return auth, nil |
| 1392 | } | 1402 | } |
| 1393 | 1403 | ||
| @@ -1396,21 +1406,28 @@ func (m *Manager) prepareRequestAuth(ctx context.Context, executor ProviderExecu · 2 unmodified lines | |||
| 1396 | return preparer.PrepareRequestAuth(ctx, auth.Clone()) | 1406 | return preparer.PrepareRequestAuth(ctx, auth.Clone()) |
| 1397 | } | 1407 | } |
| 1398 | 1408 | ||
| 1399 | lockValue, _ := m.requestPrepareLocks.LoadOrStore(id, &requestAuthPrepareLock{}) | 1409 | var prepareMu *sync.Mutex |
| 1400 | lock, ok := lockValue.(*requestAuthPrepareLock) | 1410 | if strings.EqualFold(strings.TrimSpace(auth.Provider), "meta") { |
| 1401 | if !ok || lock == nil { | 1411 | // Meta also mints on 401 recovery. Serialize both paths per credential. |
| 1402 | return preparer.PrepareRequestAuth(ctx, auth.Clone()) | 1412 | lockValue, _ := m.refreshLocks.LoadOrStore(id, &authRefreshLock{}) |
| 1413 | prepareMu = &lockValue.(*authRefreshLock).mu | ||
| 1414 | } else { | ||
| 1415 | lockValue, _ := m.requestPrepareLocks.LoadOrStore(id, &requestAuthPrepareLock{}) | ||
| 1416 | prepareMu = &lockValue.(*requestAuthPrepareLock).mu | ||
| 1403 | } | 1417 | } |
| 1404 | 1418 | prepareMu.Lock() | |
| 1405 | lock.mu.Lock() | 1419 | defer prepareMu.Unlock() |
| 1406 | defer lock.mu.Unlock() | ||
| 1407 | 1420 | ||
| 1408 | target := auth.Clone() | 1421 | target := auth.Clone() |
| 1409 | m.mu.RLock() | 1422 | m.mu.RLock() |
| 1410 | if current := m.auths[id]; current != nil { | 1423 | current := m.auths[id] |
| 1424 | if current != nil { | ||
| 1411 | target = current.Clone() | 1425 | target = current.Clone() |
| 1412 | } | 1426 | } |
| 1413 | m.mu.RUnlock() | 1427 | m.mu.RUnlock() |
| 1428 | if current == nil && strings.EqualFold(strings.TrimSpace(auth.Provider), "meta") { | ||
| 1429 | return nil, fmt.Errorf("prepare meta auth: credential no longer registered") | ||
| 1430 | } | ||
| 1414 | 1431 | ||
| 1415 | if !preparer.ShouldPrepareRequestAuth(target) { | 1432 | if !preparer.ShouldPrepareRequestAuth(target) { |
| 1416 | return target, nil | 1433 | return target, nil |
| @@ -1432,6 +1449,9 @@ func (m *Manager) prepareRequestAuth(ctx context.Context, executor ProviderExecu · 15 unmodified lines | |||
| 1432 | if saved != nil { | 1449 | if saved != nil { |
| 1433 | return saved, nil | 1450 | return saved, nil |
| 1434 | } | 1451 | } |
| 1452 | if strings.EqualFold(strings.TrimSpace(auth.Provider), "meta") { | ||
| 1453 | return nil, fmt.Errorf("prepare meta auth: credential removed during mint") | ||
| 1454 | } | ||
| 1435 | return target, nil | 1455 | return target, nil |
| 1436 | } | 1456 | } |
| 1437 | 1457 | ||
| @@ -224,6 +224,16 @@ func (m *Manager) updateInternal(ctx context.Context, base, auth *Auth, mode upd · 223 unmodified lines | |||
| 224 | cooldownStateChanged = clearCooldownStateForAuth(auth, now) || cooldownStateChanged | 224 | cooldownStateChanged = clearCooldownStateForAuth(auth, now) || cooldownStateChanged |
| 225 | } | 225 | } |
| 226 | auth.EnsureIndex() | 226 | auth.EnsureIndex() |
| 227 | // A minted Meta key must reach the configured store before requests can use it. | ||
| 228 | // Keep the epoch check, save and installation together so a concurrent reload | ||
| 229 | // or removal cannot let an obsolete mint overwrite the credential on disk. | ||
| 230 | persistMetaMint := (mode == updateModePrepare || mode == updateModeRefresh) && strings.EqualFold(strings.TrimSpace(auth.Provider), "meta") | ||
| 231 | if persistMetaMint { | ||
| 232 | if errPersist := m.persist(ctx, auth); errPersist != nil { | ||
| 233 | m.mu.Unlock() | ||
| 234 | return nil, fmt.Errorf("persist meta auth: %w", errPersist) | ||
| 235 | } | ||
| 236 | } | ||
| 227 | authClone := auth.Clone() | 237 | authClone := auth.Clone() |
| 228 | m.auths[auth.ID] = authClone | 238 | m.auths[auth.ID] = authClone |
| 229 | m.mu.Unlock() | 239 | m.mu.Unlock() |
| @@ -234,7 +244,9 @@ func (m *Manager) updateInternal(ctx context.Context, base, auth *Auth, mode upd · 4 unmodified lines | |||
| 234 | m.scheduler.upsertAuth(authClone.Clone()) | 244 | m.scheduler.upsertAuth(authClone.Clone()) |
| 235 | } | 245 | } |
| 236 | m.queueRefreshReschedule(auth.ID) | 246 | m.queueRefreshReschedule(auth.ID) |
| 237 | _ = m.persist(ctx, auth) | 247 | if !persistMetaMint { |
| 248 | _ = m.persist(ctx, auth) | ||
| 249 | } | ||
| 238 | m.hook.OnAuthUpdated(ctx, auth.Clone()) | 250 | m.hook.OnAuthUpdated(ctx, auth.Clone()) |
| 239 | if cooldownStateChanged { | 251 | if cooldownStateChanged { |
| 240 | m.persistCooldownStates(context.Background()) | 252 | m.persistCooldownStates(context.Background()) |
| @@ -32,6 +32,12 @@ func MergeExistingAuthMetadata(target *Auth, existingMap map[string]any) { · 31 unmodified lines | |||
| 32 | if IsAuthTokenPayloadKey(k) { | 32 | if IsAuthTokenPayloadKey(k) { |
| 33 | continue | 33 | continue |
| 34 | } | 34 | } |
| 35 | if strings.EqualFold(strings.TrimSpace(target.Provider), "meta") { | ||
| 36 | switch CanonicalCredentialMetadataKey(k) { | ||
| 37 | case "api_key", "dca_token", "dca_expired", "dca_expires_at": | ||
| 38 | continue | ||
| 39 | } | ||
| 40 | } | ||
| 35 | if _, exists := target.Metadata[k]; !exists { | 41 | if _, exists := target.Metadata[k]; !exists { |
| 36 | target.Metadata[k] = v | 42 | target.Metadata[k] = v |
| 37 | } | 43 | } |
| @@ -404,3 +404,18 @@ func TestMergeRefreshedAuth(t *testing.T) { · 403 unmodified lines | |||
| 404 | } | 404 | } |
| 405 | }) | 405 | }) |
| 406 | } | 406 | } |
| 407 | |||
| 408 | func TestMergeExistingAuthMetadataMetaDoesNotRestoreOldKey(t *testing.T) { | ||
| 409 | // A new device login can succeed while API key minting fails. | ||
| 410 | // Its DCA credential must not inherit the previous login's API key. | ||
| 411 | auth := &Auth{Provider: "meta", Metadata: map[string]any{"access_token": "dca:new", "dca_token": "dca:new"}} | ||
| 412 | MergeExistingAuthMetadata(auth, map[string]any{"api_key": "LLM|old", "dca_expired": "old expiry", "dca_expires_at": 42, "priority": 3}) | ||
| 413 | for _, key := range []string{"api_key", "dca_expired", "dca_expires_at"} { | ||
| 414 | if _, exists := auth.Metadata[key]; exists { | ||
| 415 | t.Fatalf("restored old Meta credential field %s", key) | ||
| 416 | } | ||
| 417 | } | ||
| 418 | if auth.Metadata["priority"] != 3 || auth.Metadata["dca_token"] != "dca:new" { | ||
| 419 | t.Fatalf("incorrect merged metadata: %#v", auth.Metadata) | ||
| 420 | } | ||
| 421 | } | ||