From 35ae1ad09290957bc93290df8edf9ab373af4244 Mon Sep 17 00:00:00 2001 From: KnowSky404 Date: Mon, 9 Nov 2026 07:56:37 +0100 Subject: [PATCH] refactor!: remove OAuth and fingerprint upstream support --- .env.example | 11 +- README.md | 15 +- backend/cmd/mock-openai/main.go | 11 + backend/cmd/mock-openai/main_test.go | 8 + backend/cmd/n2api/main.go | 13 - backend/cmd/n2api/main_test.go | 1 - backend/e2e/helpers_test.go | 4 - backend/e2e/protocol_e2e_test.go | 26 +- backend/e2e/protocol_helpers_test.go | 29 +- backend/e2e/resource_boundaries_e2e_test.go | 14 +- backend/e2e/response_affinity_e2e_test.go | 3 - backend/go.mod | 2 - backend/go.sum | 6 - .../internal/admin/configuration_export.go | 13 +- backend/internal/admin/fingerprint.go | 145 - backend/internal/admin/service.go | 37 - backend/internal/admin/service_test.go | 67 - backend/internal/alerting/service_test.go | 44 +- backend/internal/alerting/templates.go | 15 - backend/internal/config/config.go | 34 +- backend/internal/config/config_test.go | 77 +- .../internal/gateway/documentation_test.go | 41 - backend/internal/gateway/fingerprint.go | 51 - .../internal/gateway/fingerprint_transport.go | 259 -- .../gateway/fingerprint_transport_test.go | 298 -- backend/internal/gateway/oauth_responses.go | 421 --- .../internal/gateway/oauth_responses_test.go | 295 -- backend/internal/gateway/proxy.go | 244 +- backend/internal/gateway/proxy_test.go | 472 +-- .../internal/gateway/request_diagnostics.go | 1 - .../gateway/request_diagnostics_test.go | 2 +- backend/internal/gateway/response_writer.go | 56 +- .../transport_network_acceptance_test.go | 335 +- .../internal/gateway/transport_registry.go | 20 +- .../gateway/transport_registry_test.go | 20 +- .../gateway/transport_selection_test.go | 95 +- .../internal/gateway/upstream_transport.go | 51 + backend/internal/httpapi/alerting_test.go | 26 +- .../internal/httpapi/security_middleware.go | 9 +- .../httpapi/security_middleware_test.go | 12 +- backend/internal/httpapi/server.go | 481 +-- backend/internal/httpapi/server_test.go | 1013 +----- backend/internal/httpapi/system_event.go | 8 - backend/internal/provider/api_only_test.go | 101 + backend/internal/provider/catalog.go | 494 --- backend/internal/provider/endpoint_test.go | 6 +- backend/internal/provider/http_client_test.go | 468 +-- .../provider/model_probe_transport.go | 259 -- .../provider/model_probe_transport_test.go | 103 - backend/internal/provider/scheduling.go | 9 + backend/internal/provider/service.go | 1770 +--------- backend/internal/provider/service_test.go | 2870 ++--------------- backend/internal/store/audit_coverage_test.go | 4 - backend/internal/store/fingerprint.go | 139 - backend/internal/store/ops.go | 1 + backend/internal/store/ops_test.go | 53 + backend/internal/store/provider.go | 57 +- backend/internal/store/provider_test.go | 129 +- deploy/compose.release.yaml | 1 - docs/manual.md | 41 +- .../2026-11-09-api-key-only-upstreams.md | 32 + frontend/src/lib/admin-state.svelte.js | 415 +-- frontend/src/routes/+layout.svelte | 2 - .../src/routes/alerting/alert-state.test.mjs | 30 +- frontend/src/routes/fingerprints/+page.svelte | 446 --- .../fingerprints/fingerprint-page.test.mjs | 41 - .../routes/mutation-session-state.test.mjs | 7 - frontend/src/routes/navigation.test.mjs | 11 +- frontend/src/routes/providers/+page.svelte | 428 +-- .../routes/providers/provider-page.test.mjs | 240 +- frontend/src/routes/session-controls.test.mjs | 3 - 71 files changed, 1165 insertions(+), 11780 deletions(-) delete mode 100644 backend/internal/admin/fingerprint.go delete mode 100644 backend/internal/gateway/fingerprint.go delete mode 100644 backend/internal/gateway/fingerprint_transport.go delete mode 100644 backend/internal/gateway/fingerprint_transport_test.go delete mode 100644 backend/internal/gateway/oauth_responses.go delete mode 100644 backend/internal/gateway/oauth_responses_test.go create mode 100644 backend/internal/gateway/upstream_transport.go create mode 100644 backend/internal/provider/api_only_test.go delete mode 100644 backend/internal/provider/catalog.go delete mode 100644 backend/internal/provider/model_probe_transport.go delete mode 100644 backend/internal/provider/model_probe_transport_test.go delete mode 100644 backend/internal/store/fingerprint.go create mode 100644 backend/internal/store/ops_test.go create mode 100644 docs/specs/2026-11-09-api-key-only-upstreams.md delete mode 100644 frontend/src/routes/fingerprints/+page.svelte delete mode 100644 frontend/src/routes/fingerprints/fingerprint-page.test.mjs diff --git a/.env.example b/.env.example index b1d63df6..9b0a270a 100644 --- a/.env.example +++ b/.env.example @@ -139,7 +139,7 @@ N2API_ADMIN_USERNAME=admin N2API_ADMIN_PASSWORD=change-me N2API_ADMIN_PASSWORD_FILE= -# Secret material used for encrypting OAuth tokens at rest. +# Secret material used for encrypting stored credentials at rest. # Use a long random value in real deployments. N2API_ENCRYPTION_SECRET=change-me-to-a-long-random-secret N2API_ENCRYPTION_SECRET_FILE= @@ -151,15 +151,6 @@ N2API_ENCRYPTION_KEY_ID=default N2API_ENCRYPTION_PREVIOUS_KEYS=[] N2API_ENCRYPTION_PREVIOUS_KEYS_FILE= -# OpenAI/Codex OAuth. -# The default flow uses the Codex-compatible OpenAI OAuth client with PKCE. -# Leave these OAuth values blank unless you intentionally need endpoint/client overrides. -OPENAI_OAUTH_CLIENT_ID= -OPENAI_OAUTH_CLIENT_SECRET= -OPENAI_OAUTH_CLIENT_SECRET_FILE= -OPENAI_OAUTH_REDIRECT_URL=http://localhost:1455/auth/callback -OPENAI_OAUTH_AUTH_URL= -OPENAI_OAUTH_TOKEN_URL= OPENAI_API_BASE_URL=https://api.openai.com # Host paths consumed only by the release secret-file Compose overrides. diff --git a/README.md b/README.md index ceaec29c..871e46bd 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,7 @@ N2API is a personal AI API/account gateway for self-hosted use. It provides a -single OpenAI-compatible entry point for Codex/OpenAI OAuth accounts and -API-key-compatible upstreams, with a compact administration interface for +single OpenAI-compatible entry point for API-key-compatible upstreams, with a compact administration interface for accounts, routing, client keys, and request logs. N2API is inspired by sub2api's practical workflow, but it is a new @@ -15,7 +14,7 @@ operations. ## Features -- Codex/OpenAI OAuth and API-key upstream accounts. +- API-key upstream accounts. - OpenAI-compatible `/v1/models`, `/v1/responses`, and `/v1/chat/completions` routes. - Routing pools, model policies, fallback chains, and sticky sessions. @@ -66,17 +65,17 @@ Compose details for diagnosis and owner-controlled fallback. ## Basic Setup -1. Add a Codex/OpenAI OAuth account or API-key upstream on **Provider - accounts**. +1. Add an API-key upstream on **Provider accounts**. 2. Test the account and enable the models it can serve. 3. Create a routing pool and add the provider account to it. 4. Create a client key on **API Keys** and bind it to the routing pool. 5. Use `http://localhost:3000/v1` as the OpenAI-compatible base URL and the client key as the bearer token. -The OAuth flow returns an authorization link. Complete the provider login in -your browser, then paste the resulting callback URL into N2API to finish the -connection. +OAuth upstreams and fingerprint simulation have been removed. Existing OAuth +accounts remain archived in the database and must be replaced with API-key +upstreams in routing pools. Downstream Codex CLI clients remain supported via +API keys. See the [upgrade scope](docs/specs/2026-11-09-api-key-only-upstreams.md). ## Documentation diff --git a/backend/cmd/mock-openai/main.go b/backend/cmd/mock-openai/main.go index ff2d8b3a..d24a3897 100644 --- a/backend/cmd/mock-openai/main.go +++ b/backend/cmd/mock-openai/main.go @@ -161,6 +161,17 @@ func newDiagnosticState() *diagnosticState { } func (h *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // Scenario paths let API-only fixtures choose failures without header profiles. + if tail, ok := strings.CutPrefix(r.URL.Path, "/__scenario/"); ok { + scenario, path, found := strings.Cut(tail, "/") + if !found { + http.NotFound(w, r) + return + } + r = r.Clone(r.Context()) + r.URL.Path = "/" + path + r.Header.Set(scenarioHeader, scenario) + } if h.serveControl(w, r) { return } diff --git a/backend/cmd/mock-openai/main_test.go b/backend/cmd/mock-openai/main_test.go index 7520ebb3..66bb2233 100644 --- a/backend/cmd/mock-openai/main_test.go +++ b/backend/cmd/mock-openai/main_test.go @@ -482,3 +482,11 @@ func (w *flushRecorder) Flush() { w.flushes++ w.ResponseRecorder.Flush() } + +func TestScenarioPath(t *testing.T) { + handler := newMockHandler() + response := performRequest(t, handler, http.MethodPost, "/__scenario/status-503/v1/chat/completions", `{"model":"mock","messages":[]}`, "") + if response.Code != http.StatusServiceUnavailable { + t.Fatalf("scenario status = %d: %s", response.Code, response.Body.String()) + } +} diff --git a/backend/cmd/n2api/main.go b/backend/cmd/n2api/main.go index 96671791..ba4a6d44 100644 --- a/backend/cmd/n2api/main.go +++ b/backend/cmd/n2api/main.go @@ -94,16 +94,12 @@ func selectedGatewayAccount(selected provider.SelectedAccount, err error) (gatew AuthorizationToken: selected.AuthorizationToken, BaseURL: selected.BaseURL, ProxyURL: selected.ProxyURL, - ChatGPTAccountID: selected.ChatGPTAccountID, MaxConcurrentRequests: selected.MaxConcurrentRequests, RoutingPoolID: selected.RoutingPoolID, RoutingPoolName: selected.RoutingPoolName, RoutingPoolFallbackDepth: selected.RoutingPoolFallbackDepth, RoutingPoolFallbackChain: selected.RoutingPoolFallbackChain, RoutingPoolError: selected.RoutingPoolError, - FingerprintUA: selected.FingerprintUA, - FingerprintTLS: selected.FingerprintTLS, - FingerprintHeaders: selected.FingerprintHeaders, } if err != nil { return mapped, err @@ -115,10 +111,6 @@ func (p gatewayAccountProvider) RecordAccountFailure(ctx context.Context, accoun return p.service.RecordAccountFailure(ctx, accountID, statusCode, retryAfter, message) } -func (p gatewayAccountProvider) RefreshAccountAuthorization(ctx context.Context, accountID int64, rejectedAccessToken string, statusCode int, message string) (string, bool, bool, error) { - return p.service.RefreshAccountAuthorization(ctx, accountID, rejectedAccessToken, statusCode, message) -} - func (p gatewayAccountProvider) RecordAccountUsed(ctx context.Context, accountID int64) error { return p.service.RecordAccountUsed(ctx, accountID) } @@ -437,11 +429,6 @@ func runServer() int { responseAffinityRepo := store.NewResponseAffinityRepository(pool, cfg.EncryptionSecret) providerService := provider.NewService(providerRepo, provider.NewHTTPClient(http.DefaultClient), provider.Config{ Provider: "openai", - ClientID: cfg.OpenAIOAuthClientID, - ClientSecret: cfg.OpenAIOAuthSecret, - RedirectURL: cfg.OpenAIOAuthRedirectURL, - AuthURL: cfg.OpenAIOAuthAuthURL, - TokenURL: cfg.OpenAIOAuthTokenURL, APIBaseURL: cfg.OpenAIAPIBaseURL, Secret: cfg.EncryptionSecret, EncryptionKeyring: cfg.EncryptionKeyring, diff --git a/backend/cmd/n2api/main_test.go b/backend/cmd/n2api/main_test.go index f425e2fc..a1430ec9 100644 --- a/backend/cmd/n2api/main_test.go +++ b/backend/cmd/n2api/main_test.go @@ -305,7 +305,6 @@ func TestProviderAccountMetricsPreserveLastSnapshotAfterRefreshFailure(t *testin func TestGatewayAccountProviderReportsAccountFailures(t *testing.T) { var _ gateway.AccountFailureReporter = gatewayAccountProvider{} - var _ gateway.AccountAuthorizationRefresher = gatewayAccountProvider{} var _ gateway.AccountUsageRecorder = gatewayAccountProvider{} var _ gateway.AccountRecoveryRecorder = gatewayAccountProvider{} } diff --git a/backend/e2e/helpers_test.go b/backend/e2e/helpers_test.go index 074d10fc..4d78b441 100644 --- a/backend/e2e/helpers_test.go +++ b/backend/e2e/helpers_test.go @@ -40,7 +40,6 @@ type e2eResources struct { poolName string clientKeyID int64 clientSecret string - fingerprintIDs []int64 accountIDs []int64 poolIDs []int64 clientKeyIDs []int64 @@ -281,9 +280,6 @@ func registerCleanup(t *testing.T, client *http.Client, env e2eEnvironment, reso for _, id := range uniquePositiveIDs(resources.accountID, resources.accountIDs) { _, _ = performJSON(ctx, client, http.MethodDelete, env.baseURL+"/api/admin/provider-accounts/"+int64String(id), nil, nil, nil) } - for _, id := range uniquePositiveIDs(0, resources.fingerprintIDs) { - _, _ = performJSON(ctx, client, http.MethodDelete, env.baseURL+"/api/admin/fingerprint-profiles/"+int64String(id), nil, nil, nil) - } if resources.pricingUpdated { _, _ = performJSON(ctx, client, http.MethodPut, env.baseURL+"/api/admin/usage-pricing", nil, resources.pricingBefore, nil) } diff --git a/backend/e2e/protocol_e2e_test.go b/backend/e2e/protocol_e2e_test.go index 94ba5ebe..4cccb616 100644 --- a/backend/e2e/protocol_e2e_test.go +++ b/backend/e2e/protocol_e2e_test.go @@ -23,7 +23,7 @@ func TestGatewayNormalizesRetryableUpstreamStatusLogs(t *testing.T) { for _, test := range tests { t.Run(test.scenario, func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile(test.scenario, test.scenario) + profileID := test.scenario accountIDs := make([]int64, 0, 5) for index := 0; index < 5; index++ { accountIDs = append(accountIDs, fixture.createAccount(test.scenario+"-account-"+int64String(int64(index)), index, profileID)) @@ -76,7 +76,7 @@ func TestGatewayHandlesUpstreamContentTypeAndUsageVariants(t *testing.T) { for _, test := range tests { t.Run(test.scenario, func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile(test.scenario, test.scenario) + profileID := test.scenario accountID := fixture.createAccount(test.scenario+"-account", 0, profileID) _, keyID, secret := fixture.createPoolAndKey(test.scenario, []int64{accountID}) sessionID := "e2e-" + test.scenario + "-" + fixture.suffix @@ -126,7 +126,7 @@ func TestGatewayHandlesResponsesStreamUsageVariants(t *testing.T) { for _, test := range tests { t.Run(test.scenario, func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile("responses "+test.scenario, test.scenario) + profileID := test.scenario accountID := fixture.createAccount("responses-"+test.scenario+"-account", 0, profileID) _, keyID, secret := fixture.createPoolAndKey("responses-"+test.scenario, []int64{accountID}) sessionID := "e2e-responses-" + test.scenario + "-" + fixture.suffix @@ -155,9 +155,9 @@ func TestGatewayHandlesResponsesStreamUsageVariants(t *testing.T) { func TestGatewayFallsBackWhenUpstreamDisconnectsBeforeHeaders(t *testing.T) { fixture := newProtocolFixture(t) - disconnectProfileID := fixture.createFingerprintProfile("disconnect before headers", "disconnect-before-headers") + disconnectProfileID := "disconnect-before-headers" failedAccountID := fixture.createAccount("disconnect-before-account", 0, disconnectProfileID) - happyAccountID := fixture.createAccount("disconnect-fallback-account", 1, 0) + happyAccountID := fixture.createAccount("disconnect-fallback-account", 1, "") _, keyID, secret := fixture.createPoolAndKey("disconnect-before", []int64{failedAccountID, happyAccountID}) fixture.resetMock() sessionID := "e2e-disconnect-before-" + fixture.suffix @@ -183,7 +183,7 @@ func TestGatewayFallsBackWhenUpstreamDisconnectsBeforeHeaders(t *testing.T) { func TestGatewayFallsBackAfterOnePreStreamServiceUnavailable(t *testing.T) { fixture := newProtocolFixture(t) - retryProfileID := fixture.createFingerprintProfile("service unavailable once", "status-503-once") + retryProfileID := "status-503-once" failedAccountID := fixture.createAccount("status-once-account", 0, retryProfileID) happyAccountID := fixture.createAccount("status-once-fallback-account", 1, retryProfileID) _, keyID, secret := fixture.createPoolAndKey("status-once", []int64{failedAccountID, happyAccountID}) @@ -211,9 +211,9 @@ func TestGatewayFallsBackAfterOnePreStreamServiceUnavailable(t *testing.T) { func TestGatewayDoesNotRetryAfterFirstStreamEvent(t *testing.T) { fixture := newProtocolFixture(t) - disconnectProfileID := fixture.createFingerprintProfile("disconnect after event", "disconnect-after-first-event") + disconnectProfileID := "disconnect-after-first-event" failedAccountID := fixture.createAccount("disconnect-after-account", 0, disconnectProfileID) - happyAccountID := fixture.createAccount("disconnect-unused-account", 1, 0) + happyAccountID := fixture.createAccount("disconnect-unused-account", 1, "") _, keyID, secret := fixture.createPoolAndKey("disconnect-after", []int64{failedAccountID, happyAccountID}) fixture.resetMock() sessionID := "e2e-disconnect-after-" + fixture.suffix @@ -239,7 +239,7 @@ func TestGatewayDoesNotRetryAfterFirstStreamEvent(t *testing.T) { func TestGatewayDoesNotRetryWhenStreamEndsWithoutCompletion(t *testing.T) { fixture := newProtocolFixture(t) - missingProfileID := fixture.createFingerprintProfile("missing completion", "missing-completion") + missingProfileID := "missing-completion" accountID := fixture.createAccount("missing-completion-account", 0, missingProfileID) _, keyID, secret := fixture.createPoolAndKey("missing-completion", []int64{accountID}) fixture.resetMock() @@ -269,8 +269,8 @@ func TestGatewayDoesNotRetryWhenStreamEndsWithoutCompletion(t *testing.T) { func TestGatewayStickySessionUsesSameAccountTwice(t *testing.T) { fixture := newProtocolFixture(t) - firstAccountID := fixture.createAccount("sticky-first-account", 0, 0) - secondAccountID := fixture.createAccount("sticky-second-account", 0, 0) + firstAccountID := fixture.createAccount("sticky-first-account", 0, "") + secondAccountID := fixture.createAccount("sticky-second-account", 0, "") poolID, keyID, secret := fixture.createPoolAndKey("sticky", []int64{firstAccountID, secondAccountID}) sessionID := "e2e-sticky-" + fixture.suffix request := map[string]any{ @@ -308,7 +308,7 @@ func TestGatewayStickySessionUsesSameAccountTwice(t *testing.T) { func TestGatewayEnforcesPerKeyRequestLimitBeforeUpstream(t *testing.T) { fixture := newProtocolFixture(t) - accountID := fixture.createAccount("rate-limit-account", 0, 0) + accountID := fixture.createAccount("rate-limit-account", 0, "") _, keyID, secret := fixture.createPoolAndKey("rate-limit", []int64{accountID}) mustJSON(t, fixture.client, http.MethodPut, fixture.env.baseURL, "/api/admin/keys/"+int64String(keyID)+"/limits", "key_limits", nil, map[string]int{ "requestsPerMinute": 1, @@ -348,7 +348,7 @@ func TestGatewayEnforcesPerKeyRequestLimitBeforeUpstream(t *testing.T) { func TestGatewayPropagatesCancellationWhileUpstreamWaitsForHeaders(t *testing.T) { fixture := newProtocolFixture(t) - timeoutProfileID := fixture.createFingerprintProfile("timeout before headers", "timeout-before-headers") + timeoutProfileID := "timeout-before-headers" accountID := fixture.createAccount("timeout-account", 0, timeoutProfileID) _, keyID, secret := fixture.createPoolAndKey("timeout-cancel", []int64{accountID}) fixture.resetMock() diff --git a/backend/e2e/protocol_helpers_test.go b/backend/e2e/protocol_helpers_test.go index bc9938f9..572846c8 100644 --- a/backend/e2e/protocol_helpers_test.go +++ b/backend/e2e/protocol_helpers_test.go @@ -56,29 +56,7 @@ func newProtocolFixture(t *testing.T) *protocolFixture { } } -func (fixture *protocolFixture) createFingerprintProfile(name, scenario string) int64 { - fixture.t.Helper() - var response struct { - Profile struct { - ID int64 `json:"id"` - } `json:"profile"` - } - mustJSON(fixture.t, fixture.client, http.MethodPost, fixture.env.baseURL, "/api/admin/fingerprint-profiles", "fingerprint_create", nil, map[string]any{ - "name": name + " " + fixture.suffix, - "description": "gateway protocol e2e fixture", - "headers": map[string]string{ - "X-N2API-E2E-Scenario": scenario, - }, - "enabled": true, - }, &response, http.StatusCreated) - if response.Profile.ID <= 0 { - fixture.t.Fatal("stage=fingerprint_create field=id") - } - fixture.resources.fingerprintIDs = append(fixture.resources.fingerprintIDs, response.Profile.ID) - return response.Profile.ID -} - -func (fixture *protocolFixture) createAccount(name string, priority int, fingerprintProfileID int64) int64 { +func (fixture *protocolFixture) createAccount(name string, priority int, scenario string) int64 { fixture.t.Helper() input := map[string]any{ "name": name + " " + fixture.suffix, @@ -89,9 +67,10 @@ func (fixture *protocolFixture) createAccount(name string, priority int, fingerp "loadFactor": 1, "models": []string{e2eModel}, } - if fingerprintProfileID > 0 { - input["fingerprintProfileId"] = fingerprintProfileID + if scenario != "" { + input["baseUrl"] = strings.TrimRight(fixture.env.mockBaseURL, "/") + "/__scenario/" + scenario } + var response struct { Account struct { ID int64 `json:"id"` diff --git a/backend/e2e/resource_boundaries_e2e_test.go b/backend/e2e/resource_boundaries_e2e_test.go index e7c3887d..a3ca824d 100644 --- a/backend/e2e/resource_boundaries_e2e_test.go +++ b/backend/e2e/resource_boundaries_e2e_test.go @@ -32,7 +32,7 @@ func TestGatewayRejectsKnownLengthAndChunkedOversizedBodies(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { fixture := newProtocolFixture(t) - accountID := fixture.createAccount("oversized-request-account", 0, 0) + accountID := fixture.createAccount("oversized-request-account", 0, "") _, keyID, secret := fixture.createPoolAndKey("oversized-request", []int64{accountID}) fixture.resetMock() @@ -56,7 +56,7 @@ func TestGatewayRejectsKnownLengthAndChunkedOversizedBodies(t *testing.T) { func TestGatewayDoesNotReplayAcceptedBodyAboveReplayLimit(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile("large body service unavailable", "status-503-once") + profileID := "status-503-once" firstAccountID := fixture.createAccount("large-body-first", 0, profileID) secondAccountID := fixture.createAccount("large-body-second", 1, profileID) _, keyID, secret := fixture.createPoolAndKey("large-body", []int64{firstAccountID, secondAccountID}) @@ -84,7 +84,7 @@ func TestGatewayDoesNotReplayAcceptedBodyAboveReplayLimit(t *testing.T) { func TestGatewayTimesOutSlowUploadAndReleasesAdmissionSlots(t *testing.T) { fixture := newProtocolFixture(t) - accountID := fixture.createAccount("slow-upload-account", 0, 0) + accountID := fixture.createAccount("slow-upload-account", 0, "") _, keyID, secret := fixture.createPoolAndKey("slow-upload", []int64{accountID}) fixture.resetMock() @@ -131,7 +131,7 @@ func TestGatewayTimesOutSlowUploadAndReleasesAdmissionSlots(t *testing.T) { func TestGatewayBoundsUpstreamHeadersBodiesAndStreams(t *testing.T) { t.Run("response header timeout", func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile("response header timeout", "timeout-before-headers") + profileID := "timeout-before-headers" accountID := fixture.createAccount("response-header-timeout", 0, profileID) _, keyID, secret := fixture.createPoolAndKey("response-header-timeout", []int64{accountID}) fixture.resetMock() @@ -155,7 +155,7 @@ func TestGatewayBoundsUpstreamHeadersBodiesAndStreams(t *testing.T) { t.Run("oversized JSON response", func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile("oversized JSON response", "oversized-json-response") + profileID := "oversized-json-response" accountID := fixture.createAccount("oversized-json-response", 0, profileID) _, keyID, secret := fixture.createPoolAndKey("oversized-json-response", []int64{accountID}) fixture.resetMock() @@ -175,7 +175,7 @@ func TestGatewayBoundsUpstreamHeadersBodiesAndStreams(t *testing.T) { t.Run("stalled SSE", func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile("stalled SSE", "stalled-sse") + profileID := "stalled-sse" accountID := fixture.createAccount("stalled-sse", 0, profileID) _, keyID, secret := fixture.createPoolAndKey("stalled-sse", []int64{accountID}) fixture.resetMock() @@ -195,7 +195,7 @@ func TestGatewayBoundsUpstreamHeadersBodiesAndStreams(t *testing.T) { t.Run("periodic SSE", func(t *testing.T) { fixture := newProtocolFixture(t) - profileID := fixture.createFingerprintProfile("periodic SSE", "periodic-sse") + profileID := "periodic-sse" accountID := fixture.createAccount("periodic-sse", 0, profileID) _, keyID, secret := fixture.createPoolAndKey("periodic-sse", []int64{accountID}) fixture.resetMock() diff --git a/backend/e2e/response_affinity_e2e_test.go b/backend/e2e/response_affinity_e2e_test.go index 7886e33c..74e99e02 100644 --- a/backend/e2e/response_affinity_e2e_test.go +++ b/backend/e2e/response_affinity_e2e_test.go @@ -233,9 +233,6 @@ func responseAffinitySelectedAccount(selected provider.SelectedAccount, err erro RoutingPoolFallbackDepth: selected.RoutingPoolFallbackDepth, RoutingPoolFallbackChain: selected.RoutingPoolFallbackChain, RoutingPoolError: selected.RoutingPoolError, - FingerprintUA: selected.FingerprintUA, - FingerprintTLS: selected.FingerprintTLS, - FingerprintHeaders: selected.FingerprintHeaders, } return mapped, err } diff --git a/backend/go.mod b/backend/go.mod index 75113cb3..c5e80480 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -8,13 +8,11 @@ require ( github.com/prometheus/client_golang v1.25.0 github.com/prometheus/client_model v0.6.3 github.com/prometheus/common v0.73.1 - github.com/refraction-networking/utls v1.8.2 go.yaml.in/yaml/v3 v3.0.6 golang.org/x/crypto v0.58.0 ) require ( - github.com/andybalholm/brotli v1.2.7 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect diff --git a/backend/go.sum b/backend/go.sum index 05305f77..21c1b8cd 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,5 +1,3 @@ -github.com/andybalholm/brotli v1.2.7 h1:UgafRPGMam5ySHfRkhthMqOFA6IFoeF3D7mWIDLjG5A= -github.com/andybalholm/brotli v1.2.7/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= @@ -44,8 +42,6 @@ github.com/prometheus/common v0.73.1 h1:SZWh/j4AvhCdhX8jvYSnxvPxhWlC9QZkjJ+tOrEr github.com/prometheus/common v0.73.1/go.mod h1:I9aA/Ukz9s1olOobh8KtCkU7B1mQPQsB9UvQPsUQ1Dk= github.com/prometheus/procfs v0.22.0 h1:6q9+/JL9IKAPbCmBrv9n5O5Ty3NKnciV5X7YGw0oics= github.com/prometheus/procfs v0.22.0/go.mod h1:CvmFr/GVhIjIvWJZW3tgkODBQMRIf0EyWMQLHCHab58= -github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= -github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/sethvargo/go-retry v0.5.0 h1:JYfFKmVJ8ER06GReN1efzc5cm52BrVsFVzmcHiKJwfY= @@ -55,8 +51,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= -github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= -github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= diff --git a/backend/internal/admin/configuration_export.go b/backend/internal/admin/configuration_export.go index 374a978a..c6e4c8a1 100644 --- a/backend/internal/admin/configuration_export.go +++ b/backend/internal/admin/configuration_export.go @@ -8,12 +8,13 @@ import ( const ConfigurationRedactedValue = "[redacted]" type ConfigurationSnapshot struct { - RoutingPools []ConfigurationRoutingPool `json:"routingPools"` - APIKeyTemplates []ConfigurationAPIKeyTemplate `json:"apiKeyTemplates"` - ProviderAccounts []ConfigurationProviderAccount `json:"providerAccounts"` - ModelSettings ModelSettings `json:"modelSettings"` - UsagePricing UsagePricing `json:"usagePricing"` - GatewaySettings GatewaySettings `json:"gatewaySettings"` + RoutingPools []ConfigurationRoutingPool `json:"routingPools"` + APIKeyTemplates []ConfigurationAPIKeyTemplate `json:"apiKeyTemplates"` + ProviderAccounts []ConfigurationProviderAccount `json:"providerAccounts"` + ModelSettings ModelSettings `json:"modelSettings"` + UsagePricing UsagePricing `json:"usagePricing"` + GatewaySettings GatewaySettings `json:"gatewaySettings"` + // FingerprintProfiles preserves retired profile data for backup compatibility. FingerprintProfiles []ConfigurationFingerprintProfile `json:"fingerprintProfiles"` ErrorPassthroughRules []ConfigurationErrorPassthroughRule `json:"errorPassthroughRules"` AlertActions []ConfigurationAlertAction `json:"alertActions"` diff --git a/backend/internal/admin/fingerprint.go b/backend/internal/admin/fingerprint.go deleted file mode 100644 index 18d3de1a..00000000 --- a/backend/internal/admin/fingerprint.go +++ /dev/null @@ -1,145 +0,0 @@ -package admin - -import ( - "encoding/json" - "net/http" - "strings" - "time" -) - -// FingerprintProfile defines a TLS/UA fingerprint configuration. -type FingerprintProfile struct { - ID int64 `json:"id"` - SystemKey string `json:"systemKey"` - Name string `json:"name"` - Description string `json:"description"` - UserAgent string `json:"userAgent"` - TLSFingerprint string `json:"tlsFingerprint"` - Headers map[string]string `json:"headers"` - Enabled bool `json:"enabled"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` -} - -// FingerprintProfileInput is the write payload. -type FingerprintProfileInput struct { - Name string `json:"name"` - Description string `json:"description"` - UserAgent string `json:"userAgent"` - TLSFingerprint string `json:"tlsFingerprint"` - Headers map[string]string `json:"headers"` - Enabled bool `json:"enabled"` -} - -// Normalize trims and defaults the input. -func (input *FingerprintProfileInput) Normalize() error { - input.Name = strings.TrimSpace(input.Name) - input.Description = strings.TrimSpace(input.Description) - input.UserAgent = strings.TrimSpace(input.UserAgent) - tlsFingerprint := strings.TrimSpace(input.TLSFingerprint) - input.TLSFingerprint = normalizeFingerprintTLS(tlsFingerprint) - if tlsFingerprint != "" && input.TLSFingerprint == "" { - return ErrInvalidInput - } - if input.Headers == nil { - input.Headers = map[string]string{} - return nil - } - headers := make(map[string]string, len(input.Headers)) - for key, value := range input.Headers { - key = strings.TrimSpace(key) - if !validFingerprintHeaderName(key) { - return ErrInvalidInput - } - value = strings.TrimSpace(value) - if !validFingerprintHeaderValue(value) { - return ErrInvalidInput - } - headers[http.CanonicalHeaderKey(key)] = value - } - input.Headers = headers - return nil -} - -func normalizeFingerprintTLS(value string) string { - value = strings.TrimSpace(strings.ToLower(value)) - if value == "" { - return "" - } - value = strings.ReplaceAll(value, "_", "-") - value = strings.ReplaceAll(value, " ", "-") - switch value { - case "chrome", "hellochrome", "hellochrome-auto", "chrome-auto": - return "chrome" - case "firefox", "hellofirefox", "hellofirefox-auto", "firefox-auto": - return "firefox" - case "safari", "hellosafari", "hellosafari-auto", "safari-auto": - return "safari" - case "ios", "helloios", "helloios-auto", "ios-auto": - return "ios" - case "android", "android-okhttp", "helloandroid", "helloandroid-11-okhttp": - return "android" - case "edge", "helloedge", "helloedge-auto", "edge-auto": - return "edge" - case "random", "randomized", "hellorandomized": - return "randomized" - case "randomized-alpn", "hellorandomizedalpn": - return "randomized-alpn" - case "randomized-no-alpn", "hellorandomizednoalpn": - return "randomized-no-alpn" - case "go", "golang", "hello-golang", "hellogolang": - return "golang" - default: - return "" - } -} - -func validFingerprintHeaderName(value string) bool { - if value == "" { - return false - } - for _, r := range value { - if !isHTTPTokenRune(r) { - return false - } - } - return true -} - -func isHTTPTokenRune(r rune) bool { - if r >= '0' && r <= '9' { - return true - } - if r >= 'a' && r <= 'z' { - return true - } - if r >= 'A' && r <= 'Z' { - return true - } - switch r { - case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~': - return true - default: - return false - } -} - -func validFingerprintHeaderValue(value string) bool { - for _, r := range value { - if r == '\t' { - continue - } - if r < 0x20 || r == 0x7f { - return false - } - } - return true -} - -// HeadersJSON returns headers as JSON bytes for storage. -func (f *FingerprintProfile) HeadersJSON() ([]byte, error) { - if f.Headers == nil { - return []byte(`{}`), nil - } - return json.Marshal(f.Headers) -} diff --git a/backend/internal/admin/service.go b/backend/internal/admin/service.go index 22c70277..31bc7a7b 100644 --- a/backend/internal/admin/service.go +++ b/backend/internal/admin/service.go @@ -466,10 +466,6 @@ type Repository interface { GetOpsAccountHealth(ctx context.Context, since time.Time) (OpsAccountHealth, error) ListOpsAccountTests(ctx context.Context, since time.Time, limit int) ([]OpsAccountTest, error) GetOpsCostBreakdown(ctx context.Context, since time.Time) (OpsCostBreakdown, error) - ListFingerprintProfiles(ctx context.Context) ([]FingerprintProfile, error) - CreateFingerprintProfile(ctx context.Context, input FingerprintProfileInput) (FingerprintProfile, error) - UpdateFingerprintProfile(ctx context.Context, id int64, input FingerprintProfileInput) (FingerprintProfile, error) - DeleteFingerprintProfile(ctx context.Context, id int64) error ListErrorPassthroughRules(ctx context.Context) ([]ErrorPassthroughRule, error) CreateErrorPassthroughRule(ctx context.Context, input ErrorPassthroughRuleInput) (ErrorPassthroughRule, error) UpdateErrorPassthroughRule(ctx context.Context, id int64, input ErrorPassthroughRuleInput) (ErrorPassthroughRule, error) @@ -1823,42 +1819,9 @@ func validOpsInterval(interval string) bool { return false } -func (s *Service) ListFingerprintProfiles(ctx context.Context) ([]FingerprintProfile, error) { - return s.repo.ListFingerprintProfiles(ctx) -} -func (s *Service) CreateFingerprintProfile(ctx context.Context, input FingerprintProfileInput) (FingerprintProfile, error) { - if strings.TrimSpace(input.Name) == "" { - return FingerprintProfile{}, ErrInvalidInput - } - if err := input.Normalize(); err != nil { - return FingerprintProfile{}, err - } - ctx = withAuditIntent(ctx, systemevent.ActionFingerprintProfileCreated, "fingerprint_profile", "", input.Name, nil) - return s.repo.CreateFingerprintProfile(ctx, input) -} -func (s *Service) UpdateFingerprintProfile(ctx context.Context, id int64, input FingerprintProfileInput) (FingerprintProfile, error) { - if id <= 0 { - return FingerprintProfile{}, ErrInvalidInput - } - if strings.TrimSpace(input.Name) == "" { - return FingerprintProfile{}, ErrInvalidInput - } - if err := input.Normalize(); err != nil { - return FingerprintProfile{}, err - } - ctx = withAuditIntent(ctx, systemevent.ActionFingerprintProfileUpdated, "fingerprint_profile", auditID(id), input.Name, changedFields("name", "description", "user_agent", "tls_fingerprint", "headers", "enabled")) - return s.repo.UpdateFingerprintProfile(ctx, id, input) -} -func (s *Service) DeleteFingerprintProfile(ctx context.Context, id int64) error { - if id <= 0 { - return ErrInvalidInput - } - ctx = withAuditIntent(ctx, systemevent.ActionFingerprintProfileDeleted, "fingerprint_profile", auditID(id), "", nil) - return s.repo.DeleteFingerprintProfile(ctx, id) -} func (s *Service) ListErrorPassthroughRules(ctx context.Context) ([]ErrorPassthroughRule, error) { return s.repo.ListErrorPassthroughRules(ctx) diff --git a/backend/internal/admin/service_test.go b/backend/internal/admin/service_test.go index 8c3fe9e1..e49ad0ab 100644 --- a/backend/internal/admin/service_test.go +++ b/backend/internal/admin/service_test.go @@ -2148,52 +2148,6 @@ func TestDefaultModelRejectsInvalidStoredSettings(t *testing.T) { } } -func TestFingerprintProfileInputNormalizesValidProfile(t *testing.T) { - repo := newMemoryRepo() - service := NewService(repo, Config{SessionTTL: time.Hour}) - - created, err := service.CreateFingerprintProfile(context.Background(), FingerprintProfileInput{ - Name: " Chrome desktop ", - Description: " browser profile ", - UserAgent: " Mozilla/5.0 ", - TLSFingerprint: "HelloChrome", - Headers: map[string]string{ - " x-client-version ": " desktop ", - }, - Enabled: true, - }) - if err != nil { - t.Fatalf("CreateFingerprintProfile returned error: %v", err) - } - if created.Name != "Chrome desktop" || created.Description != "browser profile" || created.UserAgent != "Mozilla/5.0" || created.TLSFingerprint != "chrome" || created.Headers["X-Client-Version"] != "desktop" { - t.Fatalf("created profile = %+v, want normalized fields", created) - } - if repo.lastFingerprintInput.Name != created.Name || repo.lastFingerprintInput.TLSFingerprint != "chrome" || repo.lastFingerprintInput.Headers["X-Client-Version"] != "desktop" { - t.Fatalf("repo input = %+v, want normalized profile input", repo.lastFingerprintInput) - } -} - -func TestFingerprintProfileInputRejectsInvalidTLSFingerprint(t *testing.T) { - service := NewService(newMemoryRepo(), Config{SessionTTL: time.Hour}) - - if _, err := service.CreateFingerprintProfile(context.Background(), FingerprintProfileInput{Name: "bad", TLSFingerprint: "netscape"}); !errors.Is(err, ErrInvalidInput) { - t.Fatalf("CreateFingerprintProfile error = %v, want ErrInvalidInput", err) - } -} - -func TestFingerprintProfileInputRejectsInvalidHeaders(t *testing.T) { - service := NewService(newMemoryRepo(), Config{SessionTTL: time.Hour}) - - for _, input := range []FingerprintProfileInput{ - {Name: "bad header name", Headers: map[string]string{"Bad Header": "value"}}, - {Name: "bad header value", Headers: map[string]string{"X-Test": "line\r\nbreak"}}, - } { - if _, err := service.UpdateFingerprintProfile(context.Background(), 7, input); !errors.Is(err, ErrInvalidInput) { - t.Fatalf("UpdateFingerprintProfile(%q) error = %v, want ErrInvalidInput", input.Name, err) - } - } -} - func buildModelNames(count int) []string { names := make([]string, 0, count) for i := range count { @@ -2252,8 +2206,6 @@ type memoryRepo struct { lastOpsAccountTestsLimit int opsCostBreakdown OpsCostBreakdown lastOpsCostSince time.Time - lastFingerprintInput FingerprintProfileInput - lastFingerprintUpdateID int64 } type memorySession struct { @@ -2989,25 +2941,6 @@ func (r *memoryRepo) GetOpsCostBreakdown(_ context.Context, since time.Time) (Op return r.opsCostBreakdown, nil } -func (r *memoryRepo) ListFingerprintProfiles(_ context.Context) ([]FingerprintProfile, error) { - return nil, nil -} - -func (r *memoryRepo) CreateFingerprintProfile(_ context.Context, input FingerprintProfileInput) (FingerprintProfile, error) { - r.lastFingerprintInput = input - return FingerprintProfile{ID: 1, Name: input.Name, Description: input.Description, UserAgent: input.UserAgent, TLSFingerprint: input.TLSFingerprint, Headers: input.Headers, Enabled: input.Enabled}, nil -} - -func (r *memoryRepo) UpdateFingerprintProfile(_ context.Context, id int64, input FingerprintProfileInput) (FingerprintProfile, error) { - r.lastFingerprintUpdateID = id - r.lastFingerprintInput = input - return FingerprintProfile{ID: id, Name: input.Name, Description: input.Description, UserAgent: input.UserAgent, TLSFingerprint: input.TLSFingerprint, Headers: input.Headers, Enabled: input.Enabled}, nil -} - -func (r *memoryRepo) DeleteFingerprintProfile(_ context.Context, _ int64) error { - return nil -} - func (r *memoryRepo) ListErrorPassthroughRules(_ context.Context) ([]ErrorPassthroughRule, error) { return nil, nil } diff --git a/backend/internal/alerting/service_test.go b/backend/internal/alerting/service_test.go index fe677e67..8494d091 100644 --- a/backend/internal/alerting/service_test.go +++ b/backend/internal/alerting/service_test.go @@ -575,21 +575,13 @@ func TestServiceCreatesAndValidatesRules(t *testing.T) { func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { templates := RuleTemplates() - if len(templates) != 10 { - t.Fatalf("RuleTemplates count = %d, want 10", len(templates)) + if len(templates) != 9 { + t.Fatalf("RuleTemplates count = %d, want 9", len(templates)) } - if templates[0].Key != OAuthRefreshRepeatedTemplateKey || templates[1].Key != RequestLogRetentionFailedTemplateKey || templates[2].Key != ProviderAutoTestFailedTemplateKey || templates[3].Key != ProviderAccountExpiredTemplateKey || templates[4].Key != ProviderAccountCircuitOpenTemplateKey || templates[5].Key != APIKeyBudget80PercentTemplateKey || templates[6].Key != APIKeyBudget100PercentTemplateKey || templates[7].Key != RoutingPoolExhaustedTemplateKey || templates[8].Key != APIKeyPurgeFailedTemplateKey || templates[9].Key != SystemEventRetentionFailedTemplateKey { + if templates[0].Key != RequestLogRetentionFailedTemplateKey || templates[1].Key != ProviderAutoTestFailedTemplateKey || templates[2].Key != ProviderAccountExpiredTemplateKey || templates[3].Key != ProviderAccountCircuitOpenTemplateKey || templates[4].Key != APIKeyBudget80PercentTemplateKey || templates[5].Key != APIKeyBudget100PercentTemplateKey || templates[6].Key != RoutingPoolExhaustedTemplateKey || templates[7].Key != APIKeyPurgeFailedTemplateKey || templates[8].Key != SystemEventRetentionFailedTemplateKey { t.Fatalf("template order = %+v", templates) } - oauthTemplate := templates[0] - if oauthTemplate.Name != "Repeated OAuth refresh failures" || oauthTemplate.Enabled || - oauthTemplate.Category != systemevent.CategoryOAuth || oauthTemplate.Severity != systemevent.SeverityWarning || - oauthTemplate.EventAction != systemevent.ActionOAuthRefreshAutomaticFailed || oauthTemplate.RecoveryAction != systemevent.ActionOAuthRefreshAutomaticSucceeded || - oauthTemplate.AggregationCount != 3 || oauthTemplate.AggregationWindowSeconds != 900 || oauthTemplate.CooldownSeconds != 3600 || - oauthTemplate.DeduplicationScope != DeduplicationScopeTarget || !oauthTemplate.NotifyRecovery { - t.Fatalf("OAuth template = %+v", oauthTemplate) - } - retentionTemplate := templates[1] + retentionTemplate := templates[0] if retentionTemplate.Name != "Request log retention failures" || retentionTemplate.Enabled || retentionTemplate.Category != systemevent.CategoryScheduler || retentionTemplate.Severity != "" || retentionTemplate.EventAction != systemevent.ActionSchedulerRequestLogRetentionFailed || retentionTemplate.RecoveryAction != systemevent.ActionSchedulerRequestLogRetentionSucceeded || @@ -597,7 +589,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { retentionTemplate.DeduplicationScope != DeduplicationScopeRule || !retentionTemplate.NotifyRecovery { t.Fatalf("retention template = %+v", retentionTemplate) } - autoTestTemplate := templates[2] + autoTestTemplate := templates[1] if autoTestTemplate.Name != "Provider account auto-test failures" || autoTestTemplate.Enabled || autoTestTemplate.Category != systemevent.CategoryScheduler || autoTestTemplate.Severity != "" || autoTestTemplate.EventAction != systemevent.ActionSchedulerProviderAutoTestFailed || autoTestTemplate.RecoveryAction != systemevent.ActionSchedulerProviderAutoTestCompleted || @@ -605,7 +597,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { autoTestTemplate.DeduplicationScope != DeduplicationScopeTarget || !autoTestTemplate.NotifyRecovery { t.Fatalf("auto-test template = %+v", autoTestTemplate) } - expiredTemplate := templates[3] + expiredTemplate := templates[2] if expiredTemplate.Name != "Provider account expiry" || expiredTemplate.Enabled || expiredTemplate.Category != systemevent.CategoryRuntime || expiredTemplate.Severity != systemevent.SeverityWarning || expiredTemplate.EventAction != systemevent.ActionProviderAccountExpired || expiredTemplate.RecoveryAction != systemevent.ActionProviderAccountRecovered || @@ -613,7 +605,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { expiredTemplate.DeduplicationScope != DeduplicationScopeTarget || !expiredTemplate.NotifyRecovery { t.Fatalf("expired template = %+v", expiredTemplate) } - circuitTemplate := templates[4] + circuitTemplate := templates[3] if circuitTemplate.Name != "Provider account circuit open" || circuitTemplate.Enabled || circuitTemplate.Category != systemevent.CategoryRuntime || circuitTemplate.Severity != systemevent.SeverityWarning || circuitTemplate.EventAction != systemevent.ActionProviderAccountCircuitOpened || circuitTemplate.RecoveryAction != systemevent.ActionProviderAccountRecovered || @@ -621,7 +613,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { circuitTemplate.DeduplicationScope != DeduplicationScopeTarget || !circuitTemplate.NotifyRecovery { t.Fatalf("circuit template = %+v", circuitTemplate) } - budget80Template := templates[5] + budget80Template := templates[4] if budget80Template.Name != "API key budget at 80 percent" || budget80Template.Enabled || budget80Template.Category != systemevent.CategoryRuntime || budget80Template.Severity != systemevent.SeverityWarning || budget80Template.EventAction != systemevent.ActionAPIKeyBudgetThreshold80Crossed || budget80Template.RecoveryAction != systemevent.ActionAPIKeyBudgetThreshold80Recovered || @@ -629,7 +621,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { budget80Template.DeduplicationScope != DeduplicationScopeTarget || !budget80Template.NotifyRecovery { t.Fatalf("80 percent budget template = %+v", budget80Template) } - budget100Template := templates[6] + budget100Template := templates[5] if budget100Template.Name != "API key budget exhausted" || budget100Template.Enabled || budget100Template.Category != systemevent.CategoryRuntime || budget100Template.Severity != systemevent.SeverityError || budget100Template.EventAction != systemevent.ActionAPIKeyBudgetThreshold100Crossed || budget100Template.RecoveryAction != systemevent.ActionAPIKeyBudgetThreshold100Recovered || @@ -637,7 +629,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { budget100Template.DeduplicationScope != DeduplicationScopeTarget || !budget100Template.NotifyRecovery { t.Fatalf("100 percent budget template = %+v", budget100Template) } - routingExhaustedTemplate := templates[7] + routingExhaustedTemplate := templates[6] if routingExhaustedTemplate.Name != "API key routing pool exhausted" || routingExhaustedTemplate.Enabled || routingExhaustedTemplate.Category != systemevent.CategoryRuntime || routingExhaustedTemplate.Severity != systemevent.SeverityError || routingExhaustedTemplate.EventAction != systemevent.ActionAPIKeyRoutingPoolExhausted || routingExhaustedTemplate.RecoveryAction != systemevent.ActionAPIKeyRoutingPoolRecovered || @@ -645,7 +637,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { routingExhaustedTemplate.DeduplicationScope != DeduplicationScopeTarget || !routingExhaustedTemplate.NotifyRecovery { t.Fatalf("routing exhaustion template = %+v", routingExhaustedTemplate) } - apiKeyPurgeFailedTemplate := templates[8] + apiKeyPurgeFailedTemplate := templates[7] if apiKeyPurgeFailedTemplate.Name != "API key purge failures" || apiKeyPurgeFailedTemplate.Enabled || apiKeyPurgeFailedTemplate.Category != systemevent.CategoryScheduler || apiKeyPurgeFailedTemplate.Severity != systemevent.SeverityError || apiKeyPurgeFailedTemplate.EventAction != systemevent.ActionSchedulerAPIKeyPurgeFailed || apiKeyPurgeFailedTemplate.RecoveryAction != systemevent.ActionSchedulerAPIKeyPurgeCompleted || @@ -653,7 +645,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { apiKeyPurgeFailedTemplate.DeduplicationScope != DeduplicationScopeTarget || !apiKeyPurgeFailedTemplate.NotifyRecovery { t.Fatalf("API key purge failure template = %+v", apiKeyPurgeFailedTemplate) } - systemEventRetentionFailedTemplate := templates[9] + systemEventRetentionFailedTemplate := templates[8] if systemEventRetentionFailedTemplate.Name != "System event retention failures" || systemEventRetentionFailedTemplate.Enabled || systemEventRetentionFailedTemplate.Category != systemevent.CategoryScheduler || systemEventRetentionFailedTemplate.Severity != "" || systemEventRetentionFailedTemplate.EventAction != systemevent.ActionSchedulerEventRetentionFailed || systemEventRetentionFailedTemplate.RecoveryAction != systemevent.ActionSchedulerEventRetentionCompleted || @@ -664,21 +656,17 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { repo := newMemoryRepository() service := NewService(repo, testKeyring(t)) - rule, created, err := service.InstallRuleTemplate(context.Background(), OAuthRefreshRepeatedTemplateKey, 7) + rule, created, err := service.InstallRuleTemplate(context.Background(), RequestLogRetentionFailedTemplateKey, 7) if err != nil || !created { t.Fatalf("first InstallRuleTemplate = %+v, %v, %v", rule, created, err) } - if rule.TemplateKey != OAuthRefreshRepeatedTemplateKey || rule.ActionID != 7 || rule.Enabled || repo.lastRuleInstall.Rule.TemplateKey != OAuthRefreshRepeatedTemplateKey { + if rule.TemplateKey != RequestLogRetentionFailedTemplateKey || rule.ActionID != 7 || rule.Enabled || repo.lastRuleInstall.Rule.TemplateKey != RequestLogRetentionFailedTemplateKey { t.Fatalf("installed rule = %+v input=%+v", rule, repo.lastRuleInstall) } - again, created, err := service.InstallRuleTemplate(context.Background(), OAuthRefreshRepeatedTemplateKey, 99) + again, created, err := service.InstallRuleTemplate(context.Background(), RequestLogRetentionFailedTemplateKey, 99) if err != nil || created || again.ID != rule.ID || again.ActionID != 7 { t.Fatalf("second InstallRuleTemplate = %+v, %v, %v", again, created, err) } - retentionRule, created, err := service.InstallRuleTemplate(context.Background(), RequestLogRetentionFailedTemplateKey, 11) - if err != nil || !created || retentionRule.TemplateKey != RequestLogRetentionFailedTemplateKey || retentionRule.ActionID != 11 || retentionRule.Enabled { - t.Fatalf("retention InstallRuleTemplate = %+v, %v, %v", retentionRule, created, err) - } autoTestRule, created, err := service.InstallRuleTemplate(context.Background(), ProviderAutoTestFailedTemplateKey, 13) if err != nil || !created || autoTestRule.TemplateKey != ProviderAutoTestFailedTemplateKey || autoTestRule.ActionID != 13 || autoTestRule.Enabled { t.Fatalf("auto-test InstallRuleTemplate = %+v, %v, %v", autoTestRule, created, err) @@ -742,7 +730,7 @@ func TestRuleTemplateCatalogAndIdempotentInstall(t *testing.T) { for _, input := range []struct { key string actionID int64 - }{{"unknown", 7}, {OAuthRefreshRepeatedTemplateKey, 0}} { + }{{"unknown", 7}, {"oauth-refresh-repeated-v1", 7}, {RequestLogRetentionFailedTemplateKey, 0}} { if _, _, err := service.InstallRuleTemplate(context.Background(), input.key, input.actionID); !errors.Is(err, ErrInvalidInput) { t.Fatalf("InstallRuleTemplate(%q, %d) error = %v", input.key, input.actionID, err) } diff --git a/backend/internal/alerting/templates.go b/backend/internal/alerting/templates.go index 9e90bfc9..7eb16bb4 100644 --- a/backend/internal/alerting/templates.go +++ b/backend/internal/alerting/templates.go @@ -3,7 +3,6 @@ package alerting import "github.com/KnowSky404/N2API/backend/internal/systemevent" const ( - OAuthRefreshRepeatedTemplateKey = "oauth-refresh-repeated-v1" RequestLogRetentionFailedTemplateKey = "request-log-retention-failed-v1" ProviderAutoTestFailedTemplateKey = "provider-auto-test-failed-v1" ProviderAccountExpiredTemplateKey = "provider-account-expired-v1" @@ -16,20 +15,6 @@ const ( ) var ruleTemplateCatalog = []RuleTemplate{ - { - Key: OAuthRefreshRepeatedTemplateKey, - Name: "Repeated OAuth refresh failures", - Enabled: false, - Category: systemevent.CategoryOAuth, - Severity: systemevent.SeverityWarning, - EventAction: systemevent.ActionOAuthRefreshAutomaticFailed, - RecoveryAction: systemevent.ActionOAuthRefreshAutomaticSucceeded, - AggregationCount: 3, - AggregationWindowSeconds: 900, - CooldownSeconds: 3600, - DeduplicationScope: DeduplicationScopeTarget, - NotifyRecovery: true, - }, { Key: RequestLogRetentionFailedTemplateKey, Name: "Request log retention failures", diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go index 09fffedf..183f2bf6 100644 --- a/backend/internal/config/config.go +++ b/backend/internal/config/config.go @@ -28,11 +28,6 @@ type Config struct { EncryptionSecret string EncryptionKeyID string EncryptionKeyring *secret.Keyring - OpenAIOAuthClientID string - OpenAIOAuthSecret string - OpenAIOAuthRedirectURL string - OpenAIOAuthAuthURL string - OpenAIOAuthTokenURL string OpenAIAPIBaseURL string AllowHTTPAPIUpstreams bool AllowUnsafeMultiInstance bool @@ -79,11 +74,6 @@ type Config struct { } const ( - defaultOpenAIOAuthClientID = "app_EMoamEEZ73f0CkXaXp7hrann" - defaultOpenAIOAuthRedirect = "http://localhost:1455/auth/callback" - defaultOpenAIOAuthAuthURL = "https://auth.openai.com/oauth/authorize" - defaultOpenAIOAuthTokenURL = "https://auth.openai.com/oauth/token" - defaultProviderAccountAutoTestInterval = 5 * time.Minute minProviderAccountAutoTestInterval = time.Minute defaultRequestLogRetentionInterval = 24 * time.Hour @@ -122,7 +112,6 @@ var secretFileEnvironmentNames = [...]string{ "N2API_ADMIN_PASSWORD", "N2API_ENCRYPTION_SECRET", "N2API_ENCRYPTION_PREVIOUS_KEYS", - "OPENAI_OAUTH_CLIENT_SECRET", "N2API_METRICS_BEARER_TOKEN", } @@ -153,15 +142,10 @@ func Load(lookup func(string) string) (Config, error) { AdminUsername: valueOrDefault(lookup("N2API_ADMIN_USERNAME"), "admin"), AdminPassword: lookup("N2API_ADMIN_PASSWORD"), - DatabaseURL: lookup("DATABASE_URL"), - EncryptionSecret: lookup("N2API_ENCRYPTION_SECRET"), - EncryptionKeyID: valueOrDefault(lookup("N2API_ENCRYPTION_KEY_ID"), secret.DefaultEncryptionKeyID), - OpenAIOAuthClientID: valueOrDefault(lookup("OPENAI_OAUTH_CLIENT_ID"), defaultOpenAIOAuthClientID), - OpenAIOAuthSecret: lookup("OPENAI_OAUTH_CLIENT_SECRET"), - OpenAIOAuthRedirectURL: valueOrDefault(lookup("OPENAI_OAUTH_REDIRECT_URL"), defaultOpenAIOAuthRedirect), - OpenAIOAuthAuthURL: valueOrDefault(lookup("OPENAI_OAUTH_AUTH_URL"), defaultOpenAIOAuthAuthURL), - OpenAIOAuthTokenURL: valueOrDefault(lookup("OPENAI_OAUTH_TOKEN_URL"), defaultOpenAIOAuthTokenURL), - OpenAIAPIBaseURL: valueOrDefault(lookup("OPENAI_API_BASE_URL"), "https://api.openai.com"), + DatabaseURL: lookup("DATABASE_URL"), + EncryptionSecret: lookup("N2API_ENCRYPTION_SECRET"), + EncryptionKeyID: valueOrDefault(lookup("N2API_ENCRYPTION_KEY_ID"), secret.DefaultEncryptionKeyID), + OpenAIAPIBaseURL: valueOrDefault(lookup("OPENAI_API_BASE_URL"), "https://api.openai.com"), } allowHTTPAPIUpstreams, err := parseBool(lookup("N2API_ALLOW_HTTP_API_UPSTREAMS"), "N2API_ALLOW_HTTP_API_UPSTREAMS") if err != nil { @@ -735,16 +719,6 @@ func validateStartupSecurity(cfg *Config, acceptedRisks map[string]struct{}) err return err } cfg.OpenAIAPIBaseURL = openAIAPIBaseURL - openAIOAuthAuthURL, err := validateUpstreamURL("OPENAI_OAUTH_AUTH_URL", cfg.OpenAIOAuthAuthURL, false) - if err != nil { - return err - } - cfg.OpenAIOAuthAuthURL = openAIOAuthAuthURL - openAIOAuthTokenURL, err := validateUpstreamURL("OPENAI_OAUTH_TOKEN_URL", cfg.OpenAIOAuthTokenURL, false) - if err != nil { - return err - } - cfg.OpenAIOAuthTokenURL = openAIOAuthTokenURL return nil } diff --git a/backend/internal/config/config_test.go b/backend/internal/config/config_test.go index 35bd80db..e867d5c1 100644 --- a/backend/internal/config/config_test.go +++ b/backend/internal/config/config_test.go @@ -30,7 +30,6 @@ func TestLoadReadsBoundedSecretFiles(t *testing.T) { "N2API_ADMIN_PASSWORD_FILE": write("admin-password", " admin password \n"), "N2API_ENCRYPTION_SECRET_FILE": write("encryption-secret", "file-encryption-secret-at-least-32-bytes\r\n"), "N2API_ENCRYPTION_PREVIOUS_KEYS_FILE": write("previous-keys", "[]\n\n"), - "OPENAI_OAUTH_CLIENT_SECRET_FILE": write("oauth-secret", "oauth-secret\n"), "N2API_METRICS_BEARER_TOKEN_FILE": write("metrics-token", "metrics-token\n"), "N2API_HOST": "127.0.0.1", "N2API_PUBLIC_URL": "https://n2api.knowsky.uk", @@ -44,7 +43,7 @@ func TestLoadReadsBoundedSecretFiles(t *testing.T) { if cfg.AdminPassword != " admin password " { t.Fatalf("AdminPassword bytes = %q", cfg.AdminPassword) } - if cfg.EncryptionSecret != "file-encryption-secret-at-least-32-bytes" || cfg.OpenAIOAuthSecret != "oauth-secret" || cfg.MetricsBearerToken != "metrics-token" { + if cfg.EncryptionSecret != "file-encryption-secret-at-least-32-bytes" || cfg.MetricsBearerToken != "metrics-token" { t.Fatal("secret file values were not loaded") } if got := values["N2API_ENCRYPTION_PREVIOUS_KEYS_FILE"]; got == "" || cfg.EncryptionKeyring.PreviousKeyCount() != 0 { @@ -147,12 +146,10 @@ func TestLoadRejectsEveryDirectAndFileSecretConflict(t *testing.T) { func TestLoadUsesDefaultsForOptionalServerValues(t *testing.T) { cfg, err := Load(mapLookup(map[string]string{ - "DATABASE_URL": "postgres://n2api:secret@localhost:5432/n2api?sslmode=disable", - "N2API_ENCRYPTION_SECRET": "test-encryption-secret-at-least-32-bytes", - "N2API_ADMIN_USERNAME": "owner", - "N2API_ADMIN_PASSWORD": "test-admin-password", - "OPENAI_OAUTH_CLIENT_ID": "client-id", - "OPENAI_OAUTH_REDIRECT_URL": "http://localhost:3000/oauth/openai/callback", + "DATABASE_URL": "postgres://n2api:secret@localhost:5432/n2api?sslmode=disable", + "N2API_ENCRYPTION_SECRET": "test-encryption-secret-at-least-32-bytes", + "N2API_ADMIN_USERNAME": "owner", + "N2API_ADMIN_PASSWORD": "test-admin-password", })) if err != nil { t.Fatalf("Load returned error: %v", err) @@ -596,60 +593,12 @@ func TestLoadRequestLogExportConfig(t *testing.T) { } } -func TestLoadOpenAIOAuthEndpointConfig(t *testing.T) { - env := map[string]string{ - "DATABASE_URL": "postgres://example", - "N2API_ENCRYPTION_SECRET": "test-encryption-secret-at-least-32-bytes", - "N2API_ADMIN_PASSWORD": "admin-password", - "OPENAI_OAUTH_CLIENT_ID": "client-id", - "OPENAI_OAUTH_CLIENT_SECRET": "client-secret", - "OPENAI_OAUTH_REDIRECT_URL": "http://localhost:3000/oauth/openai/callback", - "OPENAI_OAUTH_AUTH_URL": "https://auth.example.test/authorize", - "OPENAI_OAUTH_TOKEN_URL": "https://auth.example.test/token", - } - cfg, err := Load(mapLookup(env)) - if err != nil { - t.Fatalf("Load returned error: %v", err) - } - if cfg.OpenAIOAuthAuthURL != "https://auth.example.test/authorize" { - t.Fatalf("OpenAIOAuthAuthURL = %q", cfg.OpenAIOAuthAuthURL) - } - if cfg.OpenAIOAuthTokenURL != "https://auth.example.test/token" { - t.Fatalf("OpenAIOAuthTokenURL = %q", cfg.OpenAIOAuthTokenURL) - } -} - -func TestLoadDefaultsOpenAIOAuthForCodexPKCE(t *testing.T) { +func TestLoadOpenAIAPIBaseURLConfig(t *testing.T) { cfg, err := Load(mapLookup(map[string]string{ "DATABASE_URL": "postgres://example", "N2API_ENCRYPTION_SECRET": "test-encryption-secret-at-least-32-bytes", "N2API_ADMIN_PASSWORD": "admin-password", - })) - if err != nil { - t.Fatalf("Load returned error: %v", err) - } - if cfg.OpenAIOAuthClientID == "" { - t.Fatal("OpenAIOAuthClientID was empty") - } - if cfg.OpenAIOAuthSecret != "" { - t.Fatalf("OpenAIOAuthSecret = %q, want empty public PKCE client secret", cfg.OpenAIOAuthSecret) - } - if cfg.OpenAIOAuthRedirectURL != "http://localhost:1455/auth/callback" { - t.Fatalf("OpenAIOAuthRedirectURL = %q, want Codex callback", cfg.OpenAIOAuthRedirectURL) - } - if cfg.OpenAIOAuthAuthURL == "" || cfg.OpenAIOAuthTokenURL == "" || cfg.OpenAIOAuthRedirectURL == "" { - t.Fatalf("OAuth defaults incomplete: auth=%q token=%q redirect=%q", cfg.OpenAIOAuthAuthURL, cfg.OpenAIOAuthTokenURL, cfg.OpenAIOAuthRedirectURL) - } -} - -func TestLoadOpenAIAPIBaseURLConfig(t *testing.T) { - cfg, err := Load(mapLookup(map[string]string{ - "DATABASE_URL": "postgres://example", - "N2API_ENCRYPTION_SECRET": "test-encryption-secret-at-least-32-bytes", - "N2API_ADMIN_PASSWORD": "admin-password", - "OPENAI_API_BASE_URL": "https://api.example.test", - "OPENAI_OAUTH_CLIENT_ID": "client-id", - "OPENAI_OAUTH_REDIRECT_URL": "http://localhost:3000/oauth/openai/callback", + "OPENAI_API_BASE_URL": "https://api.example.test", })) if err != nil { t.Fatalf("Load returned error: %v", err) @@ -1302,8 +1251,6 @@ func TestLoadRejectsUnsafeUpstreamURLs(t *testing.T) { {name: "http api without opt in", overrides: map[string]string{"OPENAI_API_BASE_URL": "http://upstream.internal/v1"}, wantName: "N2API_ALLOW_HTTP_API_UPSTREAMS", secret: "upstream.internal"}, {name: "api userinfo", overrides: map[string]string{"OPENAI_API_BASE_URL": "https://owner:secret@upstream.internal/v1"}, wantName: "OPENAI_API_BASE_URL", secret: "secret"}, {name: "api invalid host", overrides: map[string]string{"OPENAI_API_BASE_URL": "https://upstream,internal/v1"}, wantName: "OPENAI_API_BASE_URL", secret: "upstream,internal"}, - {name: "oauth auth http", overrides: map[string]string{"OPENAI_OAUTH_AUTH_URL": "http://auth.internal/authorize"}, wantName: "OPENAI_OAUTH_AUTH_URL", secret: "auth.internal"}, - {name: "oauth token invalid scheme", overrides: map[string]string{"OPENAI_OAUTH_TOKEN_URL": "ftp://auth.internal/token"}, wantName: "OPENAI_OAUTH_TOKEN_URL", secret: "auth.internal"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1323,9 +1270,7 @@ func TestLoadRejectsUnsafeUpstreamURLs(t *testing.T) { func TestLoadNormalizesUpstreamURLSchemes(t *testing.T) { cfg, err := Load(strictConfigLookup(map[string]string{ - "OPENAI_API_BASE_URL": "HTTPS://api.internal/v1", - "OPENAI_OAUTH_AUTH_URL": "HTTPS://auth.internal/authorize", - "OPENAI_OAUTH_TOKEN_URL": "HTTPS://auth.internal/token", + "OPENAI_API_BASE_URL": "HTTPS://api.internal/v1", })) if err != nil { t.Fatalf("Load returned error: %v", err) @@ -1333,12 +1278,6 @@ func TestLoadNormalizesUpstreamURLSchemes(t *testing.T) { if cfg.OpenAIAPIBaseURL != "https://api.internal/v1" { t.Fatalf("OpenAIAPIBaseURL = %q", cfg.OpenAIAPIBaseURL) } - if cfg.OpenAIOAuthAuthURL != "https://auth.internal/authorize" { - t.Fatalf("OpenAIOAuthAuthURL = %q", cfg.OpenAIOAuthAuthURL) - } - if cfg.OpenAIOAuthTokenURL != "https://auth.internal/token" { - t.Fatalf("OpenAIOAuthTokenURL = %q", cfg.OpenAIOAuthTokenURL) - } } func TestMetricsDefaultsDisabledAndLoopback(t *testing.T) { diff --git a/backend/internal/gateway/documentation_test.go b/backend/internal/gateway/documentation_test.go index 3149bbf3..598d20ff 100644 --- a/backend/internal/gateway/documentation_test.go +++ b/backend/internal/gateway/documentation_test.go @@ -441,27 +441,6 @@ func TestGatewayDocumentationMentionsAPIUpstreamCredentialRotation(t *testing.T) } } -func TestGatewayDocumentationMentionsProviderAccountFingerprintProfileCreation(t *testing.T) { - for _, path := range []string{"../../../docs/manual.md"} { - content, err := os.ReadFile(path) - if err != nil { - t.Fatalf("ReadFile(%q) returned error: %v", path, err) - } - text := string(content) - for _, want := range []string{ - "Fingerprint profile", - "at creation time", - "pending OAuth state", - "callback completion", - "API upstream selections", - } { - if !strings.Contains(text, want) { - t.Fatalf("%s missing %q in provider account fingerprint profile creation documentation", path, want) - } - } - } -} - func TestGatewayDocumentationOmitsGlobalAllowedModelList(t *testing.T) { for _, path := range []string{"../../../docs/manual.md"} { content, err := os.ReadFile(path) @@ -645,7 +624,6 @@ func TestGatewayDocumentationMentionsExplicitProviderAccountRecoveryPaths(t *tes } text := string(content) for _, want := range []string{ - "Reauthorization replaces the saved OAuth credentials but preserves current account health", "Rotating the encrypted API key, base URL, or per-account outbound proxy URL preserves current account health", "Reset is an explicit operator override", "records both the status-reset audit event and a runtime recovery event", @@ -753,25 +731,6 @@ func TestGatewayDocumentationMentionsSelectedProviderAccountTests(t *testing.T) } } -func TestGatewayDocumentationMentionsProviderAccountBulkRefresh(t *testing.T) { - for _, path := range []string{"../../../docs/manual.md"} { - content, err := os.ReadFile(path) - if err != nil { - t.Fatalf("ReadFile(%q) returned error: %v", path, err) - } - text := string(content) - for _, want := range []string{ - "Refresh selected", - "selected provider accounts", - "force credential refresh", - } { - if !strings.Contains(text, want) { - t.Fatalf("%s missing %q in provider account bulk refresh documentation", path, want) - } - } - } -} - func TestGatewayDocumentationMentionsProviderAccountBulkDisconnect(t *testing.T) { for _, path := range []string{"../../../docs/manual.md"} { content, err := os.ReadFile(path) diff --git a/backend/internal/gateway/fingerprint.go b/backend/internal/gateway/fingerprint.go deleted file mode 100644 index 6061fd15..00000000 --- a/backend/internal/gateway/fingerprint.go +++ /dev/null @@ -1,51 +0,0 @@ -package gateway - -import ( - "context" - "strings" -) - -type tlsFingerprintContextKey struct{} - -func contextWithTLSFingerprint(ctx context.Context, fingerprint string) context.Context { - fingerprint = normalizeTLSFingerprintName(fingerprint) - if fingerprint == "" { - return ctx - } - return context.WithValue(ctx, tlsFingerprintContextKey{}, fingerprint) -} - -func tlsFingerprintFromContext(ctx context.Context) string { - value, _ := ctx.Value(tlsFingerprintContextKey{}).(string) - return normalizeTLSFingerprintName(value) -} - -func normalizeTLSFingerprintName(fingerprint string) string { - fingerprint = strings.TrimSpace(strings.ToLower(fingerprint)) - fingerprint = strings.ReplaceAll(fingerprint, "_", "-") - fingerprint = strings.ReplaceAll(fingerprint, " ", "-") - switch fingerprint { - case "chrome", "hellochrome", "hellochrome-auto", "chrome-auto": - return "chrome" - case "firefox", "hellofirefox", "hellofirefox-auto", "firefox-auto": - return "firefox" - case "safari", "hellosafari", "hellosafari-auto", "safari-auto": - return "safari" - case "ios", "helloios", "helloios-auto", "ios-auto": - return "ios" - case "android", "android-okhttp", "helloandroid", "helloandroid-11-okhttp": - return "android" - case "edge", "helloedge", "helloedge-auto", "edge-auto": - return "edge" - case "random", "randomized", "hellorandomized": - return "randomized" - case "randomized-alpn", "hellorandomizedalpn": - return "randomized-alpn" - case "randomized-no-alpn", "hellorandomizednoalpn": - return "randomized-no-alpn" - case "go", "golang", "hello-golang", "hellogolang": - return "golang" - default: - return "" - } -} diff --git a/backend/internal/gateway/fingerprint_transport.go b/backend/internal/gateway/fingerprint_transport.go deleted file mode 100644 index c99cd2fe..00000000 --- a/backend/internal/gateway/fingerprint_transport.go +++ /dev/null @@ -1,259 +0,0 @@ -package gateway - -import ( - "bufio" - "context" - "crypto/tls" - "encoding/base64" - "fmt" - "net" - "net/http" - "net/url" - "strings" - "time" - - utls "github.com/refraction-networking/utls" -) - -type upstreamTimeouts struct { - connect time.Duration - tlsHandshake time.Duration - responseHeader time.Duration -} - -func newUpstreamTransport(timeouts upstreamTimeouts) *http.Transport { - transport := http.DefaultTransport.(*http.Transport).Clone() - transport.DialContext = (&net.Dialer{ - Timeout: timeouts.connect, - KeepAlive: 30 * time.Second, - }).DialContext - transport.TLSHandshakeTimeout = timeouts.tlsHandshake - transport.ResponseHeaderTimeout = timeouts.responseHeader - return transport -} - -type selectedAccountTransport struct { - standard *http.Transport - fingerprint *http.Transport -} - -func newSelectedAccountTransport(proxyURL *url.URL, fingerprint string, timeouts upstreamTimeouts) *selectedAccountTransport { - standard := newUpstreamTransport(timeouts) - if proxyURL != nil { - standard.Proxy = http.ProxyURL(proxyURL) - } - - transport := &selectedAccountTransport{standard: standard} - fingerprint = normalizeTLSFingerprintName(fingerprint) - if fingerprint == "" { - return transport - } - - fingerprintTransport := newUpstreamTransport(timeouts) - fingerprintTransport.ForceAttemptHTTP2 = false - fingerprintTransport.Proxy = nil - dialer := newFingerprintTLSDialer(proxyURL, fingerprint, timeouts) - fingerprintTransport.DialTLSContext = dialer.DialTLSContext - transport.fingerprint = fingerprintTransport - return transport -} - -func (t *selectedAccountTransport) RoundTrip(req *http.Request) (*http.Response, error) { - if t.fingerprint != nil && strings.EqualFold(req.URL.Scheme, "https") { - return t.fingerprint.RoundTrip(req) - } - return t.standard.RoundTrip(req) -} - -func (t *selectedAccountTransport) CloseIdleConnections() { - t.standard.CloseIdleConnections() - if t.fingerprint != nil { - t.fingerprint.CloseIdleConnections() - } -} - -type fingerprintTLSDialer struct { - dialer *net.Dialer - proxyURL *url.URL - fingerprint string - connectTimeout time.Duration - tlsTimeout time.Duration - utlsConfig *utls.Config - proxyTLSConfig *tls.Config -} - -func newFingerprintTLSDialer(proxyURL *url.URL, fingerprint string, timeouts upstreamTimeouts) *fingerprintTLSDialer { - return &fingerprintTLSDialer{ - dialer: &net.Dialer{ - Timeout: timeouts.connect, - KeepAlive: 30 * time.Second, - }, - proxyURL: proxyURL, - fingerprint: normalizeTLSFingerprintName(fingerprint), - connectTimeout: timeouts.connect, - tlsTimeout: timeouts.tlsHandshake, - } -} - -func (d *fingerprintTLSDialer) DialTLSContext(ctx context.Context, network, targetAddr string) (net.Conn, error) { - dialAddr := targetAddr - if d.proxyURL != nil { - dialAddr = canonicalProxyAddress(d.proxyURL) - } - conn, err := d.dialer.DialContext(ctx, network, dialAddr) - if err != nil { - return nil, fmt.Errorf("dial upstream transport: %w", err) - } - rawConn := conn - succeeded := false - defer func() { - if !succeeded { - _ = rawConn.Close() - } - }() - - if d.proxyURL != nil { - tunnelCtx := ctx - cancelTunnel := func() {} - if d.connectTimeout > 0 { - tunnelCtx, cancelTunnel = context.WithTimeout(ctx, d.connectTimeout) - } - closeOnTunnelCancel := context.AfterFunc(tunnelCtx, func() { _ = rawConn.Close() }) - tunneledConn, tunnelErr := d.connectProxyTunnel(tunnelCtx, conn, targetAddr) - tunnelContextErr := tunnelCtx.Err() - closeOnTunnelCancel() - cancelTunnel() - if tunnelErr != nil && tunnelContextErr != nil { - return nil, fmt.Errorf("proxy CONNECT canceled: %w", tunnelContextErr) - } - err = tunnelErr - if err != nil { - return nil, err - } - conn = tunneledConn - } - - host, _, splitErr := net.SplitHostPort(targetAddr) - if splitErr != nil { - host = targetAddr - } - tlsConfig := &utls.Config{} - if d.utlsConfig != nil { - tlsConfig = d.utlsConfig.Clone() - } - tlsConfig.ServerName = host - tlsConfig.NextProtos = []string{"http/1.1"} - uconn := utls.UClient(conn, tlsConfig, clientHelloIDForFingerprint(d.fingerprint)) - handshakeCtx := ctx - cancelHandshake := func() {} - if d.tlsTimeout > 0 { - handshakeCtx, cancelHandshake = context.WithTimeout(ctx, d.tlsTimeout) - } - err = uconn.HandshakeContext(handshakeCtx) - cancelHandshake() - if err != nil { - return nil, fmt.Errorf("upstream TLS handshake failed: %w", err) - } - if negotiated := uconn.ConnectionState().NegotiatedProtocol; negotiated != "" && negotiated != "http/1.1" { - return nil, fmt.Errorf("upstream negotiated unsupported protocol") - } - succeeded = true - return uconn, nil -} - -func (d *fingerprintTLSDialer) connectProxyTunnel(ctx context.Context, conn net.Conn, targetAddr string) (net.Conn, error) { - if strings.EqualFold(d.proxyURL.Scheme, "https") { - proxyTLSConfig := &tls.Config{MinVersion: tls.VersionTLS12} - if d.proxyTLSConfig != nil { - proxyTLSConfig = d.proxyTLSConfig.Clone() - } - proxyTLSConfig.ServerName = d.proxyURL.Hostname() - proxyTLSConfig.NextProtos = []string{"http/1.1"} - tlsConn := tls.Client(conn, proxyTLSConfig) - handshakeCtx := ctx - cancelHandshake := func() {} - if d.tlsTimeout > 0 { - handshakeCtx, cancelHandshake = context.WithTimeout(ctx, d.tlsTimeout) - } - err := tlsConn.HandshakeContext(handshakeCtx) - cancelHandshake() - if err != nil { - return nil, fmt.Errorf("proxy TLS handshake failed") - } - conn = tlsConn - } - - connectReq := &http.Request{ - Method: http.MethodConnect, - URL: &url.URL{Opaque: targetAddr}, - Host: targetAddr, - Header: make(http.Header), - } - if d.proxyURL.User != nil { - username := d.proxyURL.User.Username() - password, _ := d.proxyURL.User.Password() - credentials := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) - connectReq.Header.Set("Proxy-Authorization", "Basic "+credentials) - } - if err := connectReq.Write(conn); err != nil { - return nil, fmt.Errorf("write proxy CONNECT request") - } - reader := bufio.NewReader(conn) - resp, err := http.ReadResponse(reader, connectReq) - if err != nil { - return nil, fmt.Errorf("read proxy CONNECT response") - } - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("proxy CONNECT failed with status %d", resp.StatusCode) - } - if reader.Buffered() > 0 { - return &bufferedConn{Conn: conn, reader: reader}, nil - } - return conn, nil -} - -type bufferedConn struct { - net.Conn - reader *bufio.Reader -} - -func (c *bufferedConn) Read(p []byte) (int, error) { - return c.reader.Read(p) -} - -func canonicalProxyAddress(proxyURL *url.URL) string { - port := proxyURL.Port() - if port == "" { - if strings.EqualFold(proxyURL.Scheme, "https") { - port = "443" - } else { - port = "80" - } - } - return net.JoinHostPort(strings.ToLower(proxyURL.Hostname()), port) -} - -func clientHelloIDForFingerprint(fingerprint string) utls.ClientHelloID { - switch normalizeTLSFingerprintName(fingerprint) { - case "chrome": - return utls.HelloChrome_Auto - case "firefox": - return utls.HelloFirefox_Auto - case "safari": - return utls.HelloSafari_Auto - case "ios": - return utls.HelloIOS_Auto - case "android": - return utls.HelloAndroid_11_OkHttp - case "edge": - return utls.HelloEdge_Auto - case "randomized": - return utls.HelloRandomized - case "randomized-alpn": - return utls.HelloRandomizedALPN - case "randomized-no-alpn": - return utls.HelloRandomizedNoALPN - default: - return utls.HelloGolang - } -} diff --git a/backend/internal/gateway/fingerprint_transport_test.go b/backend/internal/gateway/fingerprint_transport_test.go deleted file mode 100644 index 4b596adb..00000000 --- a/backend/internal/gateway/fingerprint_transport_test.go +++ /dev/null @@ -1,298 +0,0 @@ -package gateway - -import ( - "context" - "crypto/tls" - "encoding/base64" - "io" - "net" - "net/http" - "net/http/httptest" - "net/url" - "reflect" - "strings" - "sync/atomic" - "testing" - "time" - - utls "github.com/refraction-networking/utls" -) - -func TestUpstreamTransportsApplyConfiguredTimeouts(t *testing.T) { - timeouts := upstreamTimeouts{ - connect: 7 * time.Second, - tlsHandshake: 8 * time.Second, - responseHeader: 9 * time.Second, - } - base := newUpstreamTransport(timeouts) - if base.DialContext == nil || base.TLSHandshakeTimeout != timeouts.tlsHandshake || base.ResponseHeaderTimeout != timeouts.responseHeader { - t.Fatalf("base transport timeouts = dial:%v tls:%s header:%s", base.DialContext != nil, base.TLSHandshakeTimeout, base.ResponseHeaderTimeout) - } - - selected := newSelectedAccountTransport(nil, "chrome", timeouts) - if selected.standard.ResponseHeaderTimeout != timeouts.responseHeader || selected.fingerprint == nil || selected.fingerprint.DialTLSContext == nil || selected.fingerprint.ResponseHeaderTimeout != timeouts.responseHeader { - t.Fatalf("selected transport timeouts = standard header:%s fingerprint dial:%v header:%s", selected.standard.ResponseHeaderTimeout, selected.fingerprint != nil && selected.fingerprint.DialTLSContext != nil, selected.fingerprint.ResponseHeaderTimeout) - } -} - -func TestClientHelloIDForFingerprint(t *testing.T) { - tests := []struct { - input string - want utls.ClientHelloID - }{ - {input: "chrome", want: utls.HelloChrome_Auto}, - {input: "Firefox Auto", want: utls.HelloFirefox_Auto}, - {input: "safari", want: utls.HelloSafari_Auto}, - {input: "ios", want: utls.HelloIOS_Auto}, - {input: "android", want: utls.HelloAndroid_11_OkHttp}, - {input: "edge", want: utls.HelloEdge_Auto}, - {input: "randomized", want: utls.HelloRandomized}, - {input: "randomized_alpn", want: utls.HelloRandomizedALPN}, - {input: "randomized-no-alpn", want: utls.HelloRandomizedNoALPN}, - {input: "golang", want: utls.HelloGolang}, - {input: "t13d1516h2_8daaf6152771_e4107deab09e", want: utls.HelloGolang}, - } - - for _, tt := range tests { - t.Run(tt.input, func(t *testing.T) { - if got := clientHelloIDForFingerprint(tt.input); got != tt.want { - t.Fatalf("clientHelloIDForFingerprint(%q) = %+v, want %+v", tt.input, got, tt.want) - } - }) - } -} - -func TestChromeFingerprintBuildsDifferentClientHelloFromGo(t *testing.T) { - chromeHello := buildClientHelloBytes(t, utls.HelloChrome_Auto) - goHello := buildClientHelloBytes(t, utls.HelloGolang) - if len(chromeHello) == 0 || len(goHello) == 0 { - t.Fatal("built ClientHello is empty") - } - if reflect.DeepEqual(chromeHello, goHello) { - t.Fatal("Chrome fingerprint built the Go ClientHello bytes") - } -} - -func TestFingerprintTransportThroughConnectProxy(t *testing.T) { - upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/stream" { - w.Header().Set("Content-Type", "text/event-stream") - _, _ = io.WriteString(w, "event: response.created\ndata: {\"response\":{\"id\":\"resp_test\"}}\n\n") - w.(http.Flusher).Flush() - _, _ = io.WriteString(w, "event: response.completed\ndata: {\"response\":{\"id\":\"resp_test\"}}\n\n") - return - } - _, _ = io.WriteString(w, "ok") - })) - upstream.StartTLS() - t.Cleanup(upstream.Close) - - for _, secureProxy := range []bool{false, true} { - name := "http proxy" - if secureProxy { - name = "https proxy" - } - t.Run(name, func(t *testing.T) { - var connectCount atomic.Int32 - proxyServer := newConnectProxyServer(t, upstream.Listener.Addr().String(), secureProxy, "proxy-user", "proxy-password", &connectCount) - proxyURL, err := url.Parse(proxyServer.URL) - if err != nil { - t.Fatalf("parse proxy URL: %v", err) - } - proxyURL.User = url.UserPassword("proxy-user", "proxy-password") - client, transport := newTestFingerprintClient(proxyURL) - t.Cleanup(transport.CloseIdleConnections) - - for _, path := range []string{"/first", "/stream"} { - resp, err := client.Get(upstream.URL + path) - if err != nil { - t.Fatalf("GET %s through proxy: %v", path, err) - } - body, readErr := io.ReadAll(resp.Body) - closeErr := resp.Body.Close() - if readErr != nil || closeErr != nil { - t.Fatalf("read/close %s response: %v/%v", path, readErr, closeErr) - } - if path == "/stream" && (!strings.Contains(string(body), "response.created") || !strings.Contains(string(body), "response.completed")) { - t.Fatalf("SSE response = %q", body) - } - } - if got := connectCount.Load(); got != 1 { - t.Fatalf("CONNECT count = %d, want 1 reused tunnel", got) - } - }) - } -} - -func TestFingerprintProxyFailuresRespectCancellationAndRedactCredentials(t *testing.T) { - t.Run("CONNECT rejection", func(t *testing.T) { - proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - http.Error(w, "denied", http.StatusProxyAuthRequired) - })) - t.Cleanup(proxyServer.Close) - proxyURL, _ := url.Parse(proxyServer.URL) - proxyURL.User = url.UserPassword("secret-user", "secret-password") - client, transport := newTestFingerprintClient(proxyURL) - t.Cleanup(transport.CloseIdleConnections) - - _, err := client.Get("https://upstream.example.test/v1/responses") - if err == nil { - t.Fatal("CONNECT rejection returned no error") - } - if strings.Contains(err.Error(), "secret-user") || strings.Contains(err.Error(), "secret-password") { - t.Fatalf("CONNECT error leaked proxy credentials: %v", err) - } - }) - - t.Run("upstream TLS failure", func(t *testing.T) { - plainUpstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) - t.Cleanup(plainUpstream.Close) - client, transport := newTestFingerprintClient(nil) - t.Cleanup(transport.CloseIdleConnections) - - _, err := client.Get("https" + strings.TrimPrefix(plainUpstream.URL, "http")) - if err == nil { - t.Fatal("plain upstream accepted a fingerprint TLS request") - } - }) - - t.Run("context cancellation", func(t *testing.T) { - requestStarted := make(chan struct{}) - proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - close(requestStarted) - <-r.Context().Done() - })) - t.Cleanup(proxyServer.Close) - proxyURL, _ := url.Parse(proxyServer.URL) - client, transport := newTestFingerprintClient(proxyURL) - t.Cleanup(transport.CloseIdleConnections) - - ctx, cancel := context.WithCancel(context.Background()) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://upstream.example.test/v1/responses", nil) - if err != nil { - t.Fatalf("create request: %v", err) - } - done := make(chan error, 1) - go func() { - _, requestErr := client.Do(req) - done <- requestErr - }() - select { - case <-requestStarted: - case <-time.After(2 * time.Second): - t.Fatal("proxy did not receive CONNECT request") - } - cancel() - select { - case requestErr := <-done: - if requestErr == nil || !strings.Contains(requestErr.Error(), context.Canceled.Error()) { - t.Fatalf("request error = %v, want context canceled", requestErr) - } - case <-time.After(2 * time.Second): - t.Fatal("canceled CONNECT request did not return") - } - }) - - t.Run("CONNECT timeout", func(t *testing.T) { - proxyServer := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - <-r.Context().Done() - })) - t.Cleanup(proxyServer.Close) - proxyURL, _ := url.Parse(proxyServer.URL) - timeouts := upstreamTimeouts{connect: 50 * time.Millisecond, tlsHandshake: time.Second, responseHeader: time.Second} - client, transport := newTestFingerprintClientWithTimeouts(proxyURL, timeouts) - t.Cleanup(transport.CloseIdleConnections) - - startedAt := time.Now() - _, err := client.Get("https://upstream.example.test/v1/responses") - if err == nil || !strings.Contains(err.Error(), context.DeadlineExceeded.Error()) { - t.Fatalf("request error = %v, want deadline exceeded", err) - } - if elapsed := time.Since(startedAt); elapsed > time.Second { - t.Fatalf("CONNECT timeout returned after %s, want under 1s", elapsed) - } - }) -} - -func buildClientHelloBytes(t *testing.T, helloID utls.ClientHelloID) []byte { - t.Helper() - clientConn, serverConn := net.Pipe() - t.Cleanup(func() { - _ = clientConn.Close() - _ = serverConn.Close() - }) - uconn := utls.UClient(clientConn, &utls.Config{ServerName: "example.test"}, helloID) - if err := uconn.BuildHandshakeState(); err != nil { - t.Fatalf("BuildHandshakeState: %v", err) - } - if err := uconn.MarshalClientHello(); err != nil { - t.Fatalf("MarshalClientHello: %v", err) - } - return append([]byte(nil), uconn.HandshakeState.Hello.Raw...) -} - -func newTestFingerprintClient(proxyURL *url.URL) (*http.Client, *http.Transport) { - timeouts := upstreamTimeouts{connect: 2 * time.Second, tlsHandshake: 2 * time.Second, responseHeader: 2 * time.Second} - return newTestFingerprintClientWithTimeouts(proxyURL, timeouts) -} - -func newTestFingerprintClientWithTimeouts(proxyURL *url.URL, timeouts upstreamTimeouts) (*http.Client, *http.Transport) { - dialer := newFingerprintTLSDialer(proxyURL, "chrome", timeouts) - dialer.utlsConfig = &utls.Config{InsecureSkipVerify: true} - if proxyURL != nil && strings.EqualFold(proxyURL.Scheme, "https") { - dialer.proxyTLSConfig = &tls.Config{InsecureSkipVerify: true, MinVersion: tls.VersionTLS12} - } - transport := newUpstreamTransport(timeouts) - transport.ForceAttemptHTTP2 = false - transport.Proxy = nil - transport.DialTLSContext = dialer.DialTLSContext - return &http.Client{Transport: transport}, transport -} - -func newConnectProxyServer(t *testing.T, allowedTarget string, secure bool, username, password string, connectCount *atomic.Int32) *httptest.Server { - t.Helper() - expectedAuthorization := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)) - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodConnect || r.Host != allowedTarget || r.Header.Get("Proxy-Authorization") != expectedAuthorization { - http.Error(w, "proxy request rejected", http.StatusProxyAuthRequired) - return - } - upstreamConn, err := net.DialTimeout("tcp", allowedTarget, 2*time.Second) - if err != nil { - http.Error(w, "upstream unavailable", http.StatusBadGateway) - return - } - hijacker, ok := w.(http.Hijacker) - if !ok { - _ = upstreamConn.Close() - http.Error(w, "hijacking unavailable", http.StatusInternalServerError) - return - } - clientConn, buffered, err := hijacker.Hijack() - if err != nil { - _ = upstreamConn.Close() - return - } - connectCount.Add(1) - _, _ = buffered.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n") - _ = buffered.Flush() - defer clientConn.Close() - defer upstreamConn.Close() - copyDone := make(chan struct{}) - go func() { - _, _ = io.Copy(upstreamConn, clientConn) - _ = upstreamConn.(*net.TCPConn).CloseWrite() - close(copyDone) - }() - _, _ = io.Copy(clientConn, upstreamConn) - <-copyDone - }) - server := httptest.NewUnstartedServer(handler) - if secure { - server.StartTLS() - } else { - server.Start() - } - t.Cleanup(server.Close) - return server -} diff --git a/backend/internal/gateway/oauth_responses.go b/backend/internal/gateway/oauth_responses.go deleted file mode 100644 index a299747c..00000000 --- a/backend/internal/gateway/oauth_responses.go +++ /dev/null @@ -1,421 +0,0 @@ -package gateway - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "io" - "net/http" - "strings" - "sync" - "time" -) - -var ( - errUpstreamSSETerminalMissing = errors.New("upstream SSE stream has no terminal response") - errUpstreamSSETruncated = errors.New("upstream SSE stream was truncated") - errUpstreamSSEMalformed = errors.New("upstream SSE stream contained malformed data") - errUpstreamSSEError = errors.New("upstream SSE stream reported an error") - errUpstreamResponseFailed = errors.New("upstream response failed") - errUpstreamResponseIncomplete = errors.New("upstream response was incomplete") - errUpstreamResponseCanceled = errors.New("upstream response was canceled") - errUpstreamResponseDeadline = errors.New("upstream response deadline exceeded") -) - -type oauthSSEEnvelope struct { - Type string `json:"type"` - Response json.RawMessage `json:"response"` -} - -type oauthSSEEventResult struct { - terminal bool - outcome string - response []byte - err error -} - -func (p *Proxy) aggregateOAuthResponses(ctx context.Context, body io.ReadCloser, route string) (observation upstreamResponseObservation, responseBody []byte, err error) { - phaseTracker := newResponsePhaseTracker(route) - defer func() { - observation.ResponseTiming = phaseTracker.snapshot() - }() - if body == nil { - return upstreamResponseObservation{Usage: Usage{Source: "missing"}}, nil, errUpstreamResponseRead - } - defer body.Close() - - maxBytes := defaultMaxUpstreamResponseBody - if p != nil && p.maxResponseBody > 0 { - maxBytes = p.maxResponseBody - } - reader := &oauthSSEBoundedReader{ - reader: &oauthSSEIdleReadCloser{body: body, timeout: p.oauthSSEIdleTimeout()}, - max: maxBytes, - } - stopContextClose := func() bool { return true } - if ctx != nil { - stopContextClose = context.AfterFunc(ctx, func() { - _ = body.Close() - }) - } - defer stopContextClose() - - parser := NewSSEParser(reader, SSEParserLimits{ - MaxLineBytes: minPositive(DefaultSSEParserMaxLineBytes, maxBytes), - MaxEventBytes: minPositive(DefaultSSEParserMaxEventBytes, maxBytes), - MaxDataBytes: minPositive(DefaultSSEParserMaxDataBytes, maxBytes), - }) - for { - event, err := parser.Next() - if err != nil { - return upstreamResponseObservation{Usage: Usage{Source: "missing"}}, nil, classifyOAuthSSEReadError(ctx, err) - } - phaseTracker.observeSSEEvent(event.Event, event.Data) - result := parseOAuthSSEEvent(event) - if result.err != nil && !result.terminal { - return upstreamResponseObservation{Usage: Usage{Source: "missing"}, StreamOutcome: result.outcome}, nil, result.err - } - if !result.terminal { - continue - } - observation := upstreamResponseObservation{ - Usage: ParseUsageFromJSON(route, result.response), - ResponseID: responseIDFromJSON(route, result.response), - StreamOutcome: result.outcome, - } - return observation, result.response, result.err - } -} - -func parseOAuthSSEEvent(event SSEEvent) oauthSSEEventResult { - if !event.HasData { - return oauthSSEEventResult{} - } - data := strings.TrimSpace(event.Data) - if data == "" { - return oauthSSEEventResult{outcome: "malformed", err: errUpstreamSSEMalformed} - } - if strings.TrimSpace(event.Event) == "error" { - return oauthSSEEventResult{outcome: "error", err: errUpstreamSSEError} - } - if data == "[DONE]" { - return oauthSSEEventResult{} - } - if !json.Valid([]byte(data)) { - return oauthSSEEventResult{outcome: "malformed", err: errUpstreamSSEMalformed} - } - - var envelope oauthSSEEnvelope - if err := json.Unmarshal([]byte(data), &envelope); err != nil { - return oauthSSEEventResult{outcome: "malformed", err: errUpstreamSSEMalformed} - } - eventType := strings.TrimSpace(envelope.Type) - if eventType == "" { - eventType = strings.TrimSpace(event.Event) - } - switch eventType { - case "error": - return oauthSSEEventResult{outcome: "error", err: errUpstreamSSEError} - case "response.completed", "response.failed", "response.incomplete": - responseBody, err := oauthResponseObject(envelope.Response) - if err != nil { - return oauthSSEEventResult{outcome: "malformed", err: err} - } - result := oauthSSEEventResult{ - terminal: true, - response: responseBody, - outcome: map[string]string{ - "response.completed": "completed", - "response.failed": "failed", - "response.incomplete": "incomplete", - }[eventType], - } - if eventType == "response.failed" { - result.err = errUpstreamResponseFailed - } - if eventType == "response.incomplete" { - result.err = errUpstreamResponseIncomplete - } - return result - } - return oauthSSEEventResult{} -} - -func oauthResponseObject(raw json.RawMessage) ([]byte, error) { - responseBody := bytes.TrimSpace(raw) - if len(responseBody) == 0 || responseBody[0] != '{' || !json.Valid(responseBody) { - return nil, errUpstreamSSEMalformed - } - return append([]byte(nil), responseBody...), nil -} - -func classifyOAuthSSEReadError(ctx context.Context, err error) error { - if ctx != nil { - switch ctx.Err() { - case context.Canceled: - return errUpstreamResponseCanceled - case context.DeadlineExceeded: - return errUpstreamResponseDeadline - } - } - if errors.Is(err, errUpstreamSSEIdleTimeout) { - return errUpstreamSSEIdleTimeout - } - if errors.Is(err, errUpstreamResponseTooLarge) || - errors.Is(err, ErrSSELineTooLarge) || - errors.Is(err, ErrSSEEventTooLarge) || - errors.Is(err, ErrSSEDataTooLarge) { - return errUpstreamResponseTooLarge - } - if errors.Is(err, ErrSSEUnexpectedEOF) { - return errUpstreamSSETruncated - } - if errors.Is(err, io.EOF) { - return errUpstreamSSETerminalMissing - } - return errUpstreamResponseRead -} - -func (p *Proxy) oauthSSEIdleTimeout() time.Duration { - if p == nil || p.sseIdleTimeout <= 0 { - return 0 - } - return p.sseIdleTimeout -} - -func minPositive(left, right int) int { - if right <= 0 { - return left - } - if left <= right { - return left - } - return right -} - -type oauthSSEIdleReadCloser struct { - body io.ReadCloser - timeout time.Duration -} - -func (r *oauthSSEIdleReadCloser) Read(p []byte) (int, error) { - if r == nil || r.body == nil { - return 0, io.EOF - } - if r.timeout <= 0 { - return r.body.Read(p) - } - idleExpired := make(chan struct{}) - idleTimer := time.AfterFunc(r.timeout, func() { - close(idleExpired) - _ = r.body.Close() - }) - n, err := r.body.Read(p) - if !idleTimer.Stop() { - <-idleExpired - return n, errUpstreamSSEIdleTimeout - } - return n, err -} - -type oauthSSEBoundedReader struct { - reader io.Reader - max int - read int -} - -func (r *oauthSSEBoundedReader) Read(p []byte) (int, error) { - if r == nil || r.reader == nil { - return 0, io.EOF - } - if r.max > 0 { - remaining := r.max - r.read - if remaining < 0 { - return 0, errUpstreamResponseTooLarge - } - if len(p) > remaining+1 { - p = p[:remaining+1] - } - } - n, err := r.reader.Read(p) - r.read += n - if r.max > 0 && r.read > r.max { - return n, errUpstreamResponseTooLarge - } - return n, err -} - -func copyOAuthStreamingResponse(ctx context.Context, w http.ResponseWriter, body io.ReadCloser, route string, idleTimeout time.Duration, maxBytes int) (observation upstreamResponseObservation, err error) { - observer := NewSSEUsageObserver(route) - phaseTracker := newResponsePhaseTracker(route) - defer func() { - observation.ResponseTiming = phaseTracker.snapshot() - }() - tracker := newOAuthSSEStreamTracker(maxBytes) - defer tracker.close() - buffer := make([]byte, 32*1024) - writer := flushWriter{ResponseWriter: w} - stopContextClose := func() bool { return true } - if ctx != nil { - stopContextClose = context.AfterFunc(ctx, func() { - _ = body.Close() - }) - } - defer stopContextClose() - readBytes := 0 - for { - idleExpired := make(chan struct{}) - idleTimer := time.AfterFunc(idleTimeout, func() { - close(idleExpired) - _ = body.Close() - }) - n, readErr := body.Read(buffer) - if !idleTimer.Stop() { - <-idleExpired - } - if n > 0 { - if maxBytes > 0 && readBytes > maxBytes-n { - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "upstream_error"}, errUpstreamResponseTooLarge - } - readBytes += n - chunk := buffer[:n] - observer.Observe(chunk) - phaseTracker.observeSSE(chunk) - tracker.write(chunk) - if _, writeErr := writer.Write(chunk); writeErr != nil { - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "client_canceled", DiagnosticError: "client_canceled"}, nil - } - } - select { - case <-idleExpired: - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "upstream_error"}, errUpstreamSSEIdleTimeout - default: - } - if readErr != nil { - if ctx != nil { - switch ctx.Err() { - case context.Canceled: - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "client_canceled"}, errUpstreamResponseCanceled - case context.DeadlineExceeded: - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "server_error"}, errUpstreamResponseDeadline - } - } - if errors.Is(readErr, io.EOF) { - tracker.close() - if err := tracker.resultError(); err != nil { - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: tracker.resultOutcome()}, err - } - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "completed"}, nil - } - return upstreamResponseObservation{Usage: observer.Usage(), ResponseID: observer.ResponseID(), StreamOutcome: "upstream_error"}, errUpstreamResponseRead - } - } -} - -type oauthSSEStreamTracker struct { - reader *io.PipeReader - writer *io.PipeWriter - done chan struct{} - closeOnce sync.Once - err error - outcome string -} - -func newOAuthSSEStreamTracker(maxBytes int) *oauthSSEStreamTracker { - reader, writer := io.Pipe() - tracker := &oauthSSEStreamTracker{ - reader: reader, - writer: writer, - done: make(chan struct{}), - } - if maxBytes <= 0 { - maxBytes = defaultMaxUpstreamResponseBody - } - go func() { - defer close(tracker.done) - defer reader.Close() - bounded := &oauthSSEBoundedReader{reader: reader, max: maxBytes} - parser := NewSSEParser(bounded, SSEParserLimits{ - MaxLineBytes: minPositive(DefaultSSEParserMaxLineBytes, maxBytes), - MaxEventBytes: minPositive(DefaultSSEParserMaxEventBytes, maxBytes), - MaxDataBytes: minPositive(DefaultSSEParserMaxDataBytes, maxBytes), - }) - for { - event, err := parser.Next() - if err != nil { - if errors.Is(err, io.EOF) { - tracker.err = errUpstreamSSETerminalMissing - tracker.outcome = "terminal_missing" - } else { - tracker.err = classifyOAuthSSEReadError(context.Background(), err) - tracker.outcome = oauthSSEOutcomeForError(tracker.err) - } - return - } - result := parseOAuthSSEEvent(event) - if result.err != nil { - tracker.err = result.err - tracker.outcome = result.outcome - return - } - if result.terminal { - tracker.outcome = result.outcome - return - } - } - }() - return tracker -} - -func (t *oauthSSEStreamTracker) write(chunk []byte) { - if t == nil || t.writer == nil || len(chunk) == 0 { - return - } - _, _ = t.writer.Write(chunk) -} - -func (t *oauthSSEStreamTracker) close() { - if t == nil { - return - } - t.closeOnce.Do(func() { - _ = t.writer.Close() - <-t.done - }) -} - -func (t *oauthSSEStreamTracker) resultError() error { - if t == nil { - return errUpstreamSSETerminalMissing - } - return t.err -} - -func (t *oauthSSEStreamTracker) resultOutcome() string { - if t == nil || t.outcome == "" { - return "upstream_error" - } - return t.outcome -} - -func oauthSSEOutcomeForError(err error) string { - switch { - case errors.Is(err, errUpstreamSSETerminalMissing): - return "terminal_missing" - case errors.Is(err, errUpstreamSSETruncated): - return "truncated" - case errors.Is(err, errUpstreamSSEMalformed): - return "malformed" - case errors.Is(err, errUpstreamSSEError): - return "error" - case errors.Is(err, errUpstreamResponseFailed): - return "failed" - case errors.Is(err, errUpstreamResponseIncomplete): - return "incomplete" - case errors.Is(err, errUpstreamResponseTooLarge): - return "too_large" - default: - return "upstream_error" - } -} diff --git a/backend/internal/gateway/oauth_responses_test.go b/backend/internal/gateway/oauth_responses_test.go deleted file mode 100644 index 9c07ae68..00000000 --- a/backend/internal/gateway/oauth_responses_test.go +++ /dev/null @@ -1,295 +0,0 @@ -package gateway - -import ( - "context" - "encoding/json" - "errors" - "io" - "net/http" - "net/http/httptest" - "strconv" - "strings" - "testing" - "time" - - "github.com/KnowSky404/N2API/backend/internal/provider" -) - -const completeOAuthResponseBody = `{"id":"resp_full","object":"response","status":"completed","model":"gpt-5.4-mini","output":[{"id":"msg_1","type":"message","role":"assistant","content":[{"type":"output_text","text":"hello","annotations":[]}]},{"id":"fc_1","type":"function_call","name":"lookup","arguments":"{\"q\":\"x\"}"}],"usage":{"input_tokens":11,"output_tokens":2,"total_tokens":13,"input_tokens_details":{"cached_tokens":3},"output_tokens_details":{"reasoning_tokens":1}},"metadata":{"keep":"yes"}}` - -func TestProxyAggregatesOAuthResponsesForNonStreamingClients(t *testing.T) { - stream := []string{ - ": heartbeat\r\n\r\n", - "event: response.output_text.delta\r\n", - "data: {\"type\":\"response.output_text.delta\",\"delta\":\"hel\"}\r\n\r\n", - "event: response.completed\r\n", - "data: {\"type\":\"response.completed\",\"response\":\r\n", - "data: " + completeOAuthResponseBody + "}\r\n\r\n", - "data: [DONE]\r\n\r\n", - } - for _, test := range []struct { - name string - body string - }{ - {name: "omitted", body: `{"model":"gpt-5.4-mini","input":"hi"}`}, - {name: "false", body: `{"model":"gpt-5.4-mini","input":"hi","stream":false}`}, - } { - t.Run(test.name, func(t *testing.T) { - logger := &fakeRequestLogger{} - var upstreamBody map[string]any - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - requestBody, err := io.ReadAll(r.Body) - if err != nil { - t.Fatalf("ReadAll request body: %v", err) - } - if err := json.Unmarshal(requestBody, &upstreamBody); err != nil { - t.Fatalf("Unmarshal request body: %v", err) - } - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Body: &chunkedOAuthReadCloser{chunks: stream}, - Request: r, - }, nil - })} - proxy := newOAuthResponsesTestProxy(client, logger, Config{}) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(test.body)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - req.Header.Set("Content-Type", "application/json") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body=%s", recorder.Code, recorder.Body.String()) - } - if got := recorder.Header().Get("Content-Type"); got != "application/json" { - t.Fatalf("Content-Type = %q, want application/json", got) - } - if got := recorder.Header().Get("Content-Length"); got != stringLength(completeOAuthResponseBody) { - t.Fatalf("Content-Length = %q, want %d", got, len(completeOAuthResponseBody)) - } - if recorder.Body.String() != completeOAuthResponseBody { - t.Fatalf("response body = %q, want complete response object", recorder.Body.String()) - } - var response map[string]any - if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil { - t.Fatalf("Unmarshal response: %v", err) - } - if response["id"] != "resp_full" || response["status"] != "completed" || response["metadata"].(map[string]any)["keep"] != "yes" { - t.Fatalf("response identity/status/metadata = %+v", response) - } - if upstreamBody["stream"] != true || upstreamBody["store"] != false { - t.Fatalf("normalized upstream body = %+v, want stream=true/store=false", upstreamBody) - } - assertLastLoggedError(t, logger, "") - if len(logger.entries) != 1 { - t.Fatalf("logged entries = %d, want one diagnostic entry", len(logger.entries)) - } - timing := logger.entries[0].ResponseTiming - if timing.HeaderWaitMS == nil || timing.FirstUsefulOutputMS == nil || timing.StreamFinishMS == nil { - t.Fatalf("response timing = %+v, want header wait, first useful output, and finish", timing) - } - }) - } -} - -func TestProxyReturnsOAuthTerminalFailureObjectWithoutCallingItSuccess(t *testing.T) { - for _, test := range []struct { - name string - eventType string - status string - wantError string - }{ - {name: "failed", eventType: "response.failed", status: "failed", wantError: "upstream_response_failed"}, - {name: "incomplete", eventType: "response.incomplete", status: "incomplete", wantError: "upstream_response_incomplete"}, - } { - t.Run(test.name, func(t *testing.T) { - logger := &fakeRequestLogger{} - responseBody := `{"id":"resp_terminal","status":"` + test.status + `","error":{"code":"provider_failed","message":"provider detail"}}` - stream := "event: " + test.eventType + "\ndata: {\"type\":\"" + test.eventType + "\",\"response\":" + responseBody + "}\n\n" - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Body: io.NopCloser(strings.NewReader(stream)), - Request: r, - }, nil - })} - proxy := newOAuthResponsesTestProxy(client, logger, Config{}) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi","stream":false}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK || recorder.Body.String() != responseBody { - t.Fatalf("status/body = %d/%q, want terminal response object with HTTP 200", recorder.Code, recorder.Body.String()) - } - assertLastLoggedError(t, logger, test.wantError) - }) - } -} - -func TestProxyMapsOAuthResponsesStreamFailuresToStableErrors(t *testing.T) { - for _, test := range []struct { - name string - stream string - wantError string - }{ - {name: "error event", stream: "event: error\ndata: {\"error\":{\"message\":\"private upstream detail\"}}\n\n", wantError: "upstream_sse_error"}, - {name: "done without terminal", stream: ": heartbeat\n\ndata: [DONE]\n\n", wantError: "upstream_sse_terminal_missing"}, - {name: "malformed event", stream: "data: {not-json}\n\n", wantError: "upstream_sse_malformed"}, - {name: "early EOF", stream: "data: {\"type\":\"response.completed\"", wantError: "upstream_sse_truncated"}, - } { - t.Run(test.name, func(t *testing.T) { - logger := &fakeRequestLogger{} - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Body: io.NopCloser(strings.NewReader(test.stream)), - Request: r, - }, nil - })} - proxy := newOAuthResponsesTestProxy(client, logger, Config{}) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi"}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadGateway { - t.Fatalf("status = %d, want 502; body=%s", recorder.Code, recorder.Body.String()) - } - assertLastLoggedError(t, logger, test.wantError) - if strings.Contains(recorder.Body.String(), "private upstream detail") { - t.Fatalf("gateway error exposed upstream detail: %s", recorder.Body.String()) - } - }) - } -} - -func TestProxyMapsOAuthResponsesSizeAndIdleFailures(t *testing.T) { - t.Run("size", func(t *testing.T) { - logger := &fakeRequestLogger{} - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Body: io.NopCloser(strings.NewReader("data: {\"type\":\"response.output_text.delta\",\"delta\":\"too-large\"}\n\n")), - Request: r, - }, nil - })} - proxy := newOAuthResponsesTestProxy(client, logger, Config{MaxUpstreamResponseBodyBytes: 16}) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi"}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadGateway { - t.Fatalf("status = %d, want 502; body=%s", recorder.Code, recorder.Body.String()) - } - assertLastLoggedError(t, logger, "upstream_response_too_large") - }) - - t.Run("idle", func(t *testing.T) { - logger := &fakeRequestLogger{} - body := newBlockingReadCloser() - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Body: body, - Request: r, - }, nil - })} - proxy := newOAuthResponsesTestProxy(client, logger, Config{UpstreamSSEIdleTimeout: 10 * time.Millisecond}) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi"}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadGateway || !body.isClosed() { - t.Fatalf("status/body/closed = %d/%q/%v", recorder.Code, recorder.Body.String(), body.isClosed()) - } - assertLastLoggedError(t, logger, "upstream_sse_idle_timeout") - }) -} - -func TestAggregateOAuthResponsesHonorsCancellation(t *testing.T) { - body := newBlockingReadCloser() - ctx, cancel := context.WithCancel(context.Background()) - done := make(chan error, 1) - go func() { - _, _, err := (&Proxy{sseIdleTimeout: time.Second}).aggregateOAuthResponses(ctx, body, "/v1/responses") - done <- err - }() - cancel() - - select { - case err := <-done: - if !errors.Is(err, errUpstreamResponseCanceled) { - t.Fatalf("aggregate error = %v, want cancellation", err) - } - case <-time.After(250 * time.Millisecond): - t.Fatal("canceled aggregation did not stop promptly") - } -} - -func TestProxyOAuthStreamingRequiresTerminalResponse(t *testing.T) { - logger := &fakeRequestLogger{} - stream := "data: {\"type\":\"response.output_text.delta\",\"delta\":\"partial\"}\n\ndata: [DONE]\n\n" - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Body: io.NopCloser(strings.NewReader(stream)), - Request: r, - }, nil - })} - proxy := newOAuthResponsesTestProxy(client, logger, Config{}) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi","stream":true}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := &flushRecordingResponseWriter{} - - proxy.ServeHTTP(recorder, req) - - if recorder.status != http.StatusOK || recorder.body.String() != stream { - t.Fatalf("status/body = %d/%q, want raw upstream stream", recorder.status, recorder.body.String()) - } - assertLastLoggedError(t, logger, "upstream_sse_terminal_missing") -} - -func newOAuthResponsesTestProxy(client *http.Client, logger *fakeRequestLogger, config Config) *Proxy { - config.Logger = logger - config.CodexResponsesBaseURL = "https://chatgpt.example.test/backend-api/codex" - return NewProxyWithClient(&fakeAPIKeyAuthenticator{}, &fakeSelectedAccountProvider{accounts: []SelectedAccount{{ - AccountID: 77, - AccountType: provider.AccountTypeCodexOAuth, - AuthorizationToken: "oauth-access-token", - ChatGPTAccountID: "acct_chatgpt", - }}}, config, client) -} - -func stringLength(value string) string { - return strconv.Itoa(len(value)) -} - -type chunkedOAuthReadCloser struct { - chunks []string - index int -} - -func (r *chunkedOAuthReadCloser) Read(p []byte) (int, error) { - if r.index >= len(r.chunks) { - return 0, io.EOF - } - chunk := r.chunks[r.index] - r.index++ - return copy(p, chunk), nil -} - -func (*chunkedOAuthReadCloser) Close() error { return nil } diff --git a/backend/internal/gateway/proxy.go b/backend/internal/gateway/proxy.go index 10f9dbf8..12a9bc49 100644 --- a/backend/internal/gateway/proxy.go +++ b/backend/internal/gateway/proxy.go @@ -24,9 +24,6 @@ import ( ) const defaultUpstreamBaseURL = "https://api.openai.com" -const defaultCodexResponsesBaseURL = "https://chatgpt.com/backend-api/codex" -const defaultCodexUserAgent = provider.DefaultCodexFingerprintUserAgent -const defaultCodexInstructions = "You are Codex, a coding agent." const defaultMaxAcceptedRequestBody = 4 << 20 const defaultMaxInMemoryReplayBody = 1 << 20 const defaultMaxUpstreamResponseBody = 8 << 20 @@ -56,16 +53,12 @@ type SelectedAccount struct { AuthorizationToken string BaseURL string ProxyURL string - ChatGPTAccountID string MaxConcurrentRequests int RoutingPoolID int64 RoutingPoolName string RoutingPoolFallbackDepth int RoutingPoolFallbackChain string RoutingPoolError string - FingerprintUA string - FingerprintTLS string - FingerprintHeaders map[string]string } type AccountProvider interface { @@ -108,10 +101,6 @@ type AccountFailureReporter interface { RecordAccountFailure(ctx context.Context, accountID int64, statusCode int, retryAfter, message string) error } -type AccountAuthorizationRefresher interface { - RefreshAccountAuthorization(ctx context.Context, accountID int64, rejectedAccessToken string, statusCode int, message string) (accessToken string, retry bool, failureRecorded bool, err error) -} - type AccountUsageRecorder interface { RecordAccountUsed(ctx context.Context, accountID int64) error } @@ -216,9 +205,6 @@ type RequestLog struct { RoutingPoolFallbackDepth int RoutingPoolFallbackChain string RoutingPoolError string - FingerprintUA string - FingerprintTLS string - FingerprintHeaders map[string]string Model string SessionID string Route string @@ -245,7 +231,6 @@ type RequestLog struct { type Config struct { UpstreamBaseURL string - CodexResponsesBaseURL string MaxConcurrentGatewayRequests int MaxConcurrentRequestsPerAccount int MaxConcurrentRequestsPerKey int @@ -277,7 +262,6 @@ type Proxy struct { accounts AccountProvider client *http.Client upstreamBaseURL string - codexBaseURL string staticSettings admin.GatewaySettings settings GatewaySettingsProvider limiter *gatewayConcurrencyLimiter @@ -321,10 +305,6 @@ func NewProxyWithClient(auth APIKeyAuthenticator, accounts AccountProvider, cfg if upstreamBaseURL == "" { upstreamBaseURL = defaultUpstreamBaseURL } - codexBaseURL := strings.TrimRight(strings.TrimSpace(cfg.CodexResponsesBaseURL), "/") - if codexBaseURL == "" { - codexBaseURL = defaultCodexResponsesBaseURL - } staticSettings := admin.GatewaySettings{ MaxConcurrentGatewayRequests: cfg.MaxConcurrentGatewayRequests, MaxConcurrentRequestsPerAccount: cfg.MaxConcurrentRequestsPerAccount, @@ -363,7 +343,6 @@ func NewProxyWithClient(auth APIKeyAuthenticator, accounts AccountProvider, cfg accounts: accounts, client: client, upstreamBaseURL: upstreamBaseURL, - codexBaseURL: codexBaseURL, staticSettings: staticSettings, settings: cfg.SettingsProvider, limiter: newGatewayConcurrencyLimiter(), @@ -616,7 +595,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer close(bodyReadInterruptDone) _ = p.setReadDeadline(recorder, time.Now()) }) - bodyFactory, maxAttempts, model, sessionID, previousResponseID, clientStream, err := p.requestBodyFactoryWithOptions(r) + bodyFactory, maxAttempts, model, sessionID, previousResponseID, _, err := p.requestBodyFactoryWithOptions(r) if !stopBodyReadInterrupt() { <-bodyReadInterruptDone } @@ -780,33 +759,24 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { ) var upstreamResp *http.Response - var upstreamErr error - var lastUpstreamSpan *requestAttemptSpan upstreamRequestID = "" - authorizationRetried := false - authorizationRefreshFailureRecorded := false - upstreamFallbackReason := attemptFallbackReason - for { - upstreamStartedAt := time.Now() - upstreamSpan := diagnostics.beginAttempt(attemptTypeUpstreamHTTP, selected, upstreamFallbackReason) - upstreamFallbackReason = "" - upstreamReq, err := p.newUpstreamRequest(r, selected, bodyFactory()) - if err != nil { - upstreamSpan.setType(attemptTypeUpstreamTransport) - upstreamSpan.finish(http.StatusBadGateway, "upstream_request_error") - releaseAccount() - errorCode = "upstream_request_error" - writeOpenAIError(recorder, http.StatusBadGateway, errorCode, "could not create upstream request") - return - } - client, clientErr := p.clientForSelectedAccount(selected) - if clientErr != nil { - upstreamErr = clientErr - upstreamSpan.setType(attemptTypeUpstreamTransport) - upstreamSpan.finish(http.StatusBadGateway, upstreamRequestErrorCode(clientErr)) - lastUpstreamSpan = upstreamSpan - break - } + upstreamStartedAt := time.Now() + upstreamSpan := diagnostics.beginAttempt(attemptTypeUpstreamHTTP, selected, attemptFallbackReason) + lastUpstreamSpan := upstreamSpan + upstreamReq, err := p.newUpstreamRequest(r, selected, bodyFactory()) + if err != nil { + upstreamSpan.setType(attemptTypeUpstreamTransport) + upstreamSpan.finish(http.StatusBadGateway, "upstream_request_error") + releaseAccount() + errorCode = "upstream_request_error" + writeOpenAIError(recorder, http.StatusBadGateway, errorCode, "could not create upstream request") + return + } + client, upstreamErr := p.clientForSelectedAccount(selected) + if upstreamErr != nil { + upstreamSpan.setType(attemptTypeUpstreamTransport) + upstreamSpan.finish(http.StatusBadGateway, upstreamRequestErrorCode(upstreamErr)) + } else { upstreamResp, upstreamErr = client.Do(upstreamReq) if upstreamErr != nil { upstreamSpan.setType(attemptTypeUpstreamTransport) @@ -815,53 +785,20 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { upstreamErrorCode = "request_canceled" } upstreamSpan.finish(http.StatusBadGateway, upstreamErrorCode) - lastUpstreamSpan = upstreamSpan p.observeUpstreamAttempt(selected.AccountType, upstreamTransportOutcome(r.Context(), upstreamErr)) - break - } - upstreamRequestID = responseUpstreamRequestID(upstreamResp) - upstreamSpan.setUpstreamRequestID(upstreamRequestID) - diagnostics.setHeaderWait(upstreamStartedAt, time.Now()) - lastUpstreamSpan = upstreamSpan - if authorizationRetried || maxAttempts == 1 || !authorizationFailureStatus(upstreamResp.StatusCode) { + } else { + upstreamRequestID = responseUpstreamRequestID(upstreamResp) + upstreamSpan.setUpstreamRequestID(upstreamRequestID) + diagnostics.setHeaderWait(upstreamStartedAt, time.Now()) attemptErrorCode := "" if upstreamResp.StatusCode >= http.StatusBadRequest { attemptErrorCode = upstreamStatusErrorCode(upstreamResp.StatusCode) } upstreamSpan.finish(upstreamResp.StatusCode, attemptErrorCode) p.observeUpstreamAttempt(selected.AccountType, upstreamHTTPOutcome(upstreamResp.StatusCode)) - break } - - message, failureBody := captureFailure(upstreamResp) - if p.shouldPassThroughUpstreamError(r.Context(), upstreamResp.StatusCode, failureBody) { - upstreamSpan.finish(upstreamResp.StatusCode, upstreamStatusErrorCode(upstreamResp.StatusCode)) - p.observeUpstreamAttempt(selected.AccountType, "http_error") - break - } - refreshedToken, retry, failureRecorded, refreshErr := p.refreshAccountAuthorization(r.Context(), selected, upstreamResp.StatusCode, message) - if !retry { - upstreamSpan.finish(upstreamResp.StatusCode, upstreamStatusErrorCode(upstreamResp.StatusCode)) - p.observeUpstreamAttempt(selected.AccountType, "http_error") - break - } - authorizationRetried = true - authRefreshSpan := diagnostics.beginAttempt(attemptTypeAuthRefreshRetry, selected, "") - authRefreshSpan.setUpstreamRequestID(upstreamRequestID) - if refreshErr != nil || strings.TrimSpace(refreshedToken) == "" { - authorizationRefreshFailureRecorded = failureRecorded - upstreamSpan.finish(upstreamResp.StatusCode, upstreamStatusErrorCode(upstreamResp.StatusCode)) - authRefreshSpan.finish(upstreamResp.StatusCode, "auth_refresh_failed") - p.observeUpstreamAttempt(selected.AccountType, "http_error") - break - } - p.observeUpstreamAttempt(selected.AccountType, "refresh_retry") - upstreamSpan.finish(upstreamResp.StatusCode, upstreamStatusErrorCode(upstreamResp.StatusCode)) - authRefreshSpan.finish(upstreamResp.StatusCode, "") - _ = upstreamResp.Body.Close() - selected.AuthorizationToken = refreshedToken - gatewayAttemptCount++ } + if upstreamErr != nil { releaseAccount() if r.Context().Err() != nil { @@ -891,10 +828,9 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer releaseAccount() defer upstreamResp.Body.Close() - streamResponse := shouldStreamUpstreamResponseForClient(r, selected, upstreamResp, clientStream) - aggregateResponse := shouldAggregateOAuthResponses(r, selected, upstreamResp, clientStream) + streamResponse := shouldStreamUpstreamResponse(upstreamResp) metricsStream = streamResponse - observation, writeErr := p.writeUpstreamResponseWithOptions(r.Context(), recorder, upstreamResp, r.URL.Path, streamResponse, aggregateResponse, usesCodexResponsesEndpoint(r, selected)) + observation, writeErr := p.writeUpstreamResponseWithOptions(r.Context(), recorder, upstreamResp, r.URL.Path, streamResponse) observedUsage, err = observation.Usage, writeErr diagnostics.setResponseTiming(observation.ResponseTiming) if streamResponse { @@ -912,9 +848,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { } return } - if !authorizationRefreshFailureRecorded { - p.recordAccountFailure(r.Context(), selected.AccountID, upstreamResp.StatusCode, upstreamResp.Header.Get("Retry-After"), message) - } + p.recordAccountFailure(r.Context(), selected.AccountID, upstreamResp.StatusCode, upstreamResp.Header.Get("Retry-After"), message) failedAccountIDs = appendUniqueInt64(failedAccountIDs, selected.AccountID) if attempt+1 < maxAttempts { releaseAccount() @@ -932,10 +866,9 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer releaseAccount() defer upstreamResp.Body.Close() - streamResponse := shouldStreamUpstreamResponseForClient(r, selected, upstreamResp, clientStream) - aggregateResponse := shouldAggregateOAuthResponses(r, selected, upstreamResp, clientStream) + streamResponse := shouldStreamUpstreamResponse(upstreamResp) metricsStream = streamResponse - observation, writeErr := p.writeUpstreamResponseWithOptions(r.Context(), recorder, upstreamResp, r.URL.Path, streamResponse, aggregateResponse, usesCodexResponsesEndpoint(r, selected)) + observation, writeErr := p.writeUpstreamResponseWithOptions(r.Context(), recorder, upstreamResp, r.URL.Path, streamResponse) observedUsage, err = observation.Usage, writeErr diagnostics.setResponseTiming(observation.ResponseTiming) if streamResponse { @@ -1667,17 +1600,6 @@ func (p *Proxy) recordAccountFailure(ctx context.Context, accountID int64, statu _ = reporter.RecordAccountFailure(ctx, accountID, statusCode, retryAfter, message) } -func (p *Proxy) refreshAccountAuthorization(ctx context.Context, selected SelectedAccount, statusCode int, message string) (string, bool, bool, error) { - if selected.AccountID <= 0 || selected.AccountType != provider.AccountTypeCodexOAuth { - return "", false, false, nil - } - refresher, ok := p.accounts.(AccountAuthorizationRefresher) - if !ok { - return "", false, false, nil - } - return refresher.RefreshAccountAuthorization(ctx, selected.AccountID, selected.AuthorizationToken, statusCode, message) -} - func containsInt64(values []int64, target int64) bool { for _, value := range values { if value == target { @@ -1751,10 +1673,6 @@ func retryableUpstreamStatus(statusCode int) bool { statusCode >= http.StatusInternalServerError } -func authorizationFailureStatus(statusCode int) bool { - return statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden -} - func upstreamStatusErrorCode(statusCode int) string { switch statusCode { case http.StatusUnauthorized: @@ -1873,18 +1791,9 @@ func (r *prefixedReadCloser) Close() error { } func (p *Proxy) newUpstreamRequest(r *http.Request, selected SelectedAccount, body io.ReadCloser) (*http.Request, error) { - useCodexEndpoint := usesCodexResponsesEndpoint(r, selected) - upstreamPath := r.URL.Path upstreamBaseURL := p.upstreamBaseURL - if useCodexEndpoint { - upstreamBaseURL = p.codexBaseURL - upstreamPath = "/responses" - var err error - body, err = normalizeCodexResponsesBody(body) - if err != nil { - return nil, err - } - } else if selectedBaseURL := strings.TrimRight(strings.TrimSpace(selected.BaseURL), "/"); selectedBaseURL != "" { + upstreamPath := r.URL.Path + if selectedBaseURL := strings.TrimRight(strings.TrimSpace(selected.BaseURL), "/"); selectedBaseURL != "" { upstreamBaseURL, upstreamPath = upstreamURLBaseAndPath(selectedBaseURL, upstreamPath) } upstreamURL, err := url.Parse(upstreamBaseURL + upstreamPath) @@ -1899,62 +1808,12 @@ func (p *Proxy) newUpstreamRequest(r *http.Request, selected SelectedAccount, bo copyRequestHeaders(req.Header, r.Header) req.Header.Set("X-Request-ID", gatewayCorrelationID(r)) req.Header.Set("Authorization", "Bearer "+selected.AuthorizationToken) - if useCodexEndpoint { - req.Header.Set("chatgpt-account-id", strings.TrimSpace(selected.ChatGPTAccountID)) - req.Header.Set("Accept", "text/event-stream") - req.Header.Set("OpenAI-Beta", "responses=experimental") - req.Header.Set("originator", provider.DefaultCodexFingerprintOriginator) - req.Header.Set("User-Agent", defaultCodexUserAgent) - req.Header.Set("Version", provider.DefaultCodexFingerprintVersion) - req.Header.Set("Content-Type", "application/json") - } - - // Apply fingerprint profile overrides (User-Agent and custom headers) - if strings.TrimSpace(selected.FingerprintUA) != "" { - req.Header.Set("User-Agent", strings.TrimSpace(selected.FingerprintUA)) - } - for key, value := range selected.FingerprintHeaders { - req.Header.Set(key, value) - } - if strings.TrimSpace(selected.FingerprintTLS) != "" { - req = req.WithContext(contextWithTLSFingerprint(req.Context(), selected.FingerprintTLS)) - } return req, nil } -func usesCodexResponsesEndpoint(r *http.Request, selected SelectedAccount) bool { - return r != nil && - selected.AccountType == provider.AccountTypeCodexOAuth && - r.Method == http.MethodPost && - r.URL.Path == "/v1/responses" && - strings.TrimSpace(selected.ChatGPTAccountID) != "" -} - -func shouldStreamUpstreamResponse(r *http.Request, selected SelectedAccount, resp *http.Response) bool { - if resp == nil { - return false - } - if strings.Contains(strings.ToLower(resp.Header.Get("Content-Type")), "text/event-stream") { - return true - } - return resp.StatusCode >= http.StatusOK && - resp.StatusCode < http.StatusMultipleChoices && - usesCodexResponsesEndpoint(r, selected) -} - -func shouldStreamUpstreamResponseForClient(r *http.Request, selected SelectedAccount, resp *http.Response, clientStream bool) bool { - if usesCodexResponsesEndpoint(r, selected) && !clientStream { - return false - } - return shouldStreamUpstreamResponse(r, selected, resp) -} - -func shouldAggregateOAuthResponses(r *http.Request, selected SelectedAccount, resp *http.Response, clientStream bool) bool { - if clientStream || !usesCodexResponsesEndpoint(r, selected) || resp == nil { - return false - } - return resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices +func shouldStreamUpstreamResponse(resp *http.Response) bool { + return resp != nil && strings.Contains(strings.ToLower(resp.Header.Get("Content-Type")), "text/event-stream") } func upstreamURLBaseAndPath(baseURL, routePath string) (string, string) { @@ -1966,45 +1825,6 @@ func upstreamURLBaseAndPath(baseURL, routePath string) (string, string) { return baseURL, routePath } -func normalizeCodexResponsesBody(body io.ReadCloser) (io.ReadCloser, error) { - if body == nil { - return nil, nil - } - raw, err := io.ReadAll(body) - if closeErr := body.Close(); err == nil && closeErr != nil { - err = closeErr - } - if err != nil { - return nil, err - } - if len(bytes.TrimSpace(raw)) == 0 { - return io.NopCloser(bytes.NewReader(raw)), nil - } - var payload map[string]any - if err := json.Unmarshal(raw, &payload); err != nil { - return io.NopCloser(bytes.NewReader(raw)), nil - } - payload["stream"] = true - payload["store"] = false - if instructions, ok := payload["instructions"].(string); !ok || strings.TrimSpace(instructions) == "" { - payload["instructions"] = defaultCodexInstructions - } - if input, ok := payload["input"].(string); ok { - payload["input"] = []any{ - map[string]any{ - "type": "message", - "role": "user", - "content": input, - }, - } - } - normalized, err := json.Marshal(payload) - if err != nil { - return nil, err - } - return io.NopCloser(bytes.NewReader(normalized)), nil -} - var ( errRequestBodyTooLarge = errors.New("request body exceeds accepted limit") errInvalidJSONBody = errors.New("request body must be valid json") diff --git a/backend/internal/gateway/proxy_test.go b/backend/internal/gateway/proxy_test.go index c5e2275d..e0e8d534 100644 --- a/backend/internal/gateway/proxy_test.go +++ b/backend/internal/gateway/proxy_test.go @@ -2761,9 +2761,8 @@ func TestProxyRoutesAPIUpstreamResponsesCreateWithoutCodexTransform(t *testing.T BaseURL: apiUpstream.URL, }}} proxy := NewProxy(&fakeAPIKeyAuthenticator{}, accounts, Config{ - UpstreamBaseURL: defaultUpstream.URL, - CodexResponsesBaseURL: "http://codex.invalid", - ModelProvider: fakeModelProvider{defaultModel: "gpt-5"}, + UpstreamBaseURL: defaultUpstream.URL, + ModelProvider: fakeModelProvider{defaultModel: "gpt-5"}, }) req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5","input":"hi","stream":false}`)) req.Header.Set("Authorization", "Bearer n2api_client_secret") @@ -2789,201 +2788,6 @@ func TestProxyRoutesAPIUpstreamResponsesCreateWithoutCodexTransform(t *testing.T } } -func TestProxyForwardsOAuthResponsesCreateToCodexEndpoint(t *testing.T) { - var gotPath string - var gotAuthorization string - var gotChatGPTAccountID string - var gotOpenAIBeta string - var gotOriginator string - var gotUserAgent string - var gotVersion string - var gotHost string - var gotBody map[string]any - upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - gotPath = r.URL.Path - gotAuthorization = r.Header.Get("Authorization") - gotChatGPTAccountID = r.Header.Get("chatgpt-account-id") - gotOpenAIBeta = r.Header.Get("OpenAI-Beta") - gotOriginator = r.Header.Get("originator") - gotUserAgent = r.Header.Get("User-Agent") - gotVersion = r.Header.Get("Version") - gotHost = r.Host - body, err := io.ReadAll(r.Body) - if err != nil { - t.Fatalf("ReadAll returned error: %v", err) - } - if err := json.Unmarshal(body, &gotBody); err != nil { - t.Fatalf("Unmarshal body returned error: %v; body=%s", err, string(body)) - } - w.Header().Set("Content-Type", "text/event-stream") - w.WriteHeader(http.StatusOK) - _, _ = w.Write([]byte("data: {\"type\":\"response.output_text.delta\"}\n\n")) - _, _ = w.Write([]byte("event: response.completed\n")) - _, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_oauth\",\"status\":\"completed\",\"output\":[]}}\n\n")) - _, _ = w.Write([]byte("data: [DONE]\n\n")) - })) - defer upstream.Close() - proxy := NewProxy(&fakeAPIKeyAuthenticator{}, &fakeSelectedAccountProvider{accounts: []SelectedAccount{{ - AccountID: 1, - AccountType: provider.AccountTypeCodexOAuth, - AuthorizationToken: "upstream-token", - ChatGPTAccountID: "acct_chatgpt", - }}}, Config{ - UpstreamBaseURL: "https://api.example.test", - CodexResponsesBaseURL: upstream.URL + "/backend-api/codex", - }) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi"}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - req.Header.Set("Content-Type", "application/json") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body=%s", recorder.Code, recorder.Body.String()) - } - if gotPath != "/backend-api/codex/responses" { - t.Fatalf("path = %q, want Codex responses endpoint", gotPath) - } - if gotAuthorization != "Bearer upstream-token" { - t.Fatalf("Authorization = %q", gotAuthorization) - } - if gotChatGPTAccountID != "acct_chatgpt" { - t.Fatalf("chatgpt-account-id = %q", gotChatGPTAccountID) - } - if gotOpenAIBeta != "responses=experimental" { - t.Fatalf("OpenAI-Beta = %q", gotOpenAIBeta) - } - if gotOriginator != provider.DefaultCodexFingerprintOriginator { - t.Fatalf("originator = %q", gotOriginator) - } - if gotUserAgent != provider.DefaultCodexFingerprintUserAgent { - t.Fatalf("User-Agent = %q, want %q", gotUserAgent, provider.DefaultCodexFingerprintUserAgent) - } - if gotVersion != provider.DefaultCodexFingerprintVersion { - t.Fatalf("Version = %q, want %q", gotVersion, provider.DefaultCodexFingerprintVersion) - } - if gotHost != strings.TrimPrefix(upstream.URL, "http://") { - t.Fatalf("Host = %q, want configured upstream host", gotHost) - } - if gotBody["stream"] != true { - t.Fatalf("stream = %#v, want true", gotBody["stream"]) - } - if gotBody["store"] != false { - t.Fatalf("store = %#v, want false", gotBody["store"]) - } - if strings.TrimSpace(gotBody["instructions"].(string)) == "" { - t.Fatalf("instructions = %#v, want non-empty string", gotBody["instructions"]) - } - input, ok := gotBody["input"].([]any) - if !ok || len(input) != 1 { - t.Fatalf("input = %#v, want one message", gotBody["input"]) - } - message, ok := input[0].(map[string]any) - if !ok || message["type"] != "message" || message["role"] != "user" || message["content"] != "hi" { - t.Fatalf("input message = %#v", input[0]) - } -} - -func TestProxyTreatsSuccessfulOAuthResponsesAsSSEWithoutUpstreamSSEHeader(t *testing.T) { - const stream = "event: response.output_text.delta\n" + - "data: {\"type\":\"response.output_text.delta\",\"delta\":\"OK\"}\n\n" + - "event: response.completed\n" + - "data: {\"type\":\"response.completed\",\"response\":{\"model\":\"gpt-5.4-mini\",\"usage\":{\"input_tokens\":11,\"output_tokens\":2,\"total_tokens\":13,\"input_tokens_details\":{\"cached_tokens\":3},\"output_tokens_details\":{\"reasoning_tokens\":1}}}}\n\n" - logger := &fakeRequestLogger{} - pricer := &fakeUsagePricer{estimate: UsageCostEstimate{ - Matched: true, - CostMicrousd: 14, - Snapshot: map[string]any{ - "matched": true, - "model": "gpt-5.4-mini", - }, - }} - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/plain; charset=utf-8"}}, - Body: io.NopCloser(strings.NewReader(stream)), - Request: r, - }, nil - })} - proxy := NewProxyWithClient( - &fakeAPIKeyAuthenticator{}, - &fakeSelectedAccountProvider{accounts: []SelectedAccount{{ - AccountID: 14, - AccountType: provider.AccountTypeCodexOAuth, - AuthorizationToken: "oauth-access-token", - ChatGPTAccountID: "acct_chatgpt", - DisplayName: "free0", - }}}, - Config{ - CodexResponsesBaseURL: "https://chatgpt.example.test/backend-api/codex", - Logger: logger, - UsagePricer: pricer, - }, - client, - ) - req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"gpt-5.4-mini","input":"hi","stream":true}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - req.Header.Set("Content-Type", "application/json") - recorder := &flushRecordingResponseWriter{} - - proxy.ServeHTTP(recorder, req) - - if recorder.status != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.status, recorder.body.String()) - } - if got := recorder.Header().Get("Content-Type"); got != "text/event-stream" { - t.Fatalf("Content-Type = %q, want text/event-stream", got) - } - if recorder.body.String() != stream { - t.Fatalf("stream body = %q, want exact upstream bytes", recorder.body.String()) - } - if recorder.flushes == 0 { - t.Fatal("flushes = 0, want streamed chunks to flush") - } - if len(logger.entries) != 1 { - t.Fatalf("logged entries = %d, want 1", len(logger.entries)) - } - entry := logger.entries[0] - if entry.UsageSource != "stream" || entry.InputTokens != 11 || entry.OutputTokens != 2 || entry.TotalTokens != 13 || entry.CachedInputTokens != 3 || entry.ReasoningTokens != 1 { - t.Fatalf("logged usage = %+v, want parsed Codex stream usage", entry) - } - if pricer.usage != (Usage{Model: "gpt-5.4-mini", InputTokens: 11, OutputTokens: 2, TotalTokens: 13, CachedInputTokens: 3, ReasoningTokens: 1, Source: "stream"}) { - t.Fatalf("priced usage = %+v, want parsed Codex stream usage", pricer.usage) - } - if entry.EstimatedCostMicrousd != 14 || entry.PricingSnapshot["matched"] != true || entry.PricingSnapshot["model"] != "gpt-5.4-mini" { - t.Fatalf("logged pricing = %d/%+v, want matched Codex stream pricing", entry.EstimatedCostMicrousd, entry.PricingSnapshot) - } -} - -func TestShouldStreamUpstreamResponseDoesNotRelabelErrorsOrAPIUpstreams(t *testing.T) { - req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) - oauth := SelectedAccount{ - AccountType: provider.AccountTypeCodexOAuth, - ChatGPTAccountID: "acct_chatgpt", - } - plainSuccess := &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"text/plain; charset=utf-8"}}, - } - plainError := &http.Response{ - StatusCode: http.StatusBadRequest, - Header: http.Header{"Content-Type": []string{"application/json"}}, - } - apiUpstream := SelectedAccount{AccountType: provider.AccountTypeAPIUpstream} - - if !shouldStreamUpstreamResponse(req, oauth, plainSuccess) { - t.Fatal("successful Codex OAuth response should stream") - } - if shouldStreamUpstreamResponse(req, oauth, plainError) { - t.Fatal("Codex OAuth error response should keep its error content type") - } - if shouldStreamUpstreamResponse(req, apiUpstream, plainSuccess) { - t.Fatal("plain API upstream response should not be relabeled as SSE") - } -} - func TestProxyForwardsResponsesRetrieveAndInputItems(t *testing.T) { var gotPaths []string var gotQueries []string @@ -4870,202 +4674,6 @@ func TestProxyRecordsExpiredAccountOnUnauthorizedAndRetriesAnotherAccount(t *tes } } -func TestProxyRefreshesRejectedOAuthTokenAndRetriesSameAccountOnce(t *testing.T) { - transportCalls := 0 - authorizations := []string{} - logger := &fakeRequestLogger{} - tokens := &fakeSelectedAccountProvider{ - accounts: []SelectedAccount{{AccountID: 1, AccountType: provider.AccountTypeCodexOAuth, AuthorizationToken: "old-token"}}, - refreshedAuthorizationToken: "new-token", - refreshAuthorizationRetry: true, - } - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - transportCalls++ - authorizations = append(authorizations, r.Header.Get("Authorization")) - if transportCalls == 1 { - return &http.Response{ - StatusCode: http.StatusUnauthorized, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"error":{"message":"invalid access token"}}`)), - Request: r, - }, nil - } - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), - Request: r, - }, nil - })} - proxy := NewProxyWithClient(&fakeAPIKeyAuthenticator{}, tokens, Config{UpstreamBaseURL: "https://upstream.example.test", Logger: logger}, client) - req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_123", nil) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if !slices.Equal(authorizations, []string{"Bearer old-token", "Bearer new-token"}) { - t.Fatalf("upstream authorizations = %+v", authorizations) - } - if tokens.calls != 1 || len(tokens.authorizationRefreshes) != 1 || len(tokens.failures) != 0 { - t.Fatalf("selection/refresh/failures = %d/%+v/%+v", tokens.calls, tokens.authorizationRefreshes, tokens.failures) - } - refresh := tokens.authorizationRefreshes[0] - if refresh.accountID != 1 || refresh.rejectedAccessToken != "old-token" || refresh.statusCode != http.StatusUnauthorized { - t.Fatalf("authorization refresh = %+v", refresh) - } - if !slices.Equal(tokens.used, []int64{1}) || !slices.Equal(tokens.recovered, []int64{1}) { - t.Fatalf("account attempts/recoveries = %+v/%+v, want [1]/[1]", tokens.used, tokens.recovered) - } - if len(logger.entries) != 1 || len(logger.entries[0].Attempts) != 4 { - t.Fatalf("request diagnostics = %+v, want selection, unauthorized, refresh, success", logger.entries) - } - attempts := logger.entries[0].Attempts - if attempts[0].Type != attemptTypeSelection || attempts[1].Type != attemptTypeUpstreamHTTP || attempts[1].HTTPStatus != http.StatusUnauthorized || attempts[1].Error != "upstream_unauthorized" || attempts[2].Type != attemptTypeAuthRefreshRetry || attempts[2].Error != "" || attempts[3].Type != attemptTypeUpstreamHTTP || attempts[3].HTTPStatus != http.StatusOK { - t.Fatalf("OAuth retry timeline = %+v", attempts) - } -} - -func TestProxyFallsBackAfterRefreshedOAuthTokenIsStillUnauthorized(t *testing.T) { - transportCalls := 0 - tokens := &fakeSelectedAccountProvider{ - accounts: []SelectedAccount{ - {AccountID: 1, AccountType: provider.AccountTypeCodexOAuth, AuthorizationToken: "old-token"}, - {AccountID: 2, AccountType: provider.AccountTypeAPIUpstream, AuthorizationToken: "fallback-token"}, - }, - refreshedAuthorizationToken: "new-token", - refreshAuthorizationRetry: true, - } - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - transportCalls++ - if transportCalls <= 2 { - return &http.Response{ - StatusCode: http.StatusUnauthorized, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"error":{"message":"invalid access token"}}`)), - Request: r, - }, nil - } - if got := r.Header.Get("Authorization"); got != "Bearer fallback-token" { - t.Fatalf("fallback authorization = %q", got) - } - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), - Request: r, - }, nil - })} - proxy := NewProxyWithClient(&fakeAPIKeyAuthenticator{}, tokens, Config{UpstreamBaseURL: "https://upstream.example.test"}, client) - req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_123", nil) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if transportCalls != 3 || len(tokens.authorizationRefreshes) != 1 || len(tokens.failures) != 1 { - t.Fatalf("transport/refresh/failures = %d/%+v/%+v", transportCalls, tokens.authorizationRefreshes, tokens.failures) - } - if len(tokens.exclusions) != 2 || !slices.Equal(tokens.exclusions[1], []int64{1}) { - t.Fatalf("selection exclusions = %+v, want failed OAuth account excluded", tokens.exclusions) - } - if !slices.Equal(tokens.used, []int64{1, 2}) || !slices.Equal(tokens.recovered, []int64{2}) { - t.Fatalf("account attempts/recoveries = %+v/%+v, want [1 2]/[2]", tokens.used, tokens.recovered) - } -} - -func TestProxyRecordsAccountFailureWhenOAuthAuthorizationRefreshFailsBeforeRecording(t *testing.T) { - transportCalls := 0 - tokens := &fakeSelectedAccountProvider{ - accounts: []SelectedAccount{ - {AccountID: 1, AccountType: provider.AccountTypeCodexOAuth, AuthorizationToken: "old-token"}, - {AccountID: 2, AccountType: provider.AccountTypeAPIUpstream, AuthorizationToken: "fallback-token"}, - }, - refreshAuthorizationRetry: true, - refreshAuthorizationErr: errors.New("refresh failed"), - } - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - transportCalls++ - if transportCalls == 1 { - return &http.Response{ - StatusCode: http.StatusUnauthorized, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"error":{"message":"invalid access token"}}`)), - Request: r, - }, nil - } - if got := r.Header.Get("Authorization"); got != "Bearer fallback-token" { - t.Fatalf("fallback authorization = %q", got) - } - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), - Request: r, - }, nil - })} - proxy := NewProxyWithClient(&fakeAPIKeyAuthenticator{}, tokens, Config{UpstreamBaseURL: "https://upstream.example.test"}, client) - req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_123", nil) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if transportCalls != 2 || len(tokens.authorizationRefreshes) != 1 { - t.Fatalf("transport/refresh = %d/%+v, want 2/1", transportCalls, tokens.authorizationRefreshes) - } - if len(tokens.failures) != 1 || tokens.failures[0].accountID != 1 || tokens.failures[0].statusCode != http.StatusUnauthorized { - t.Fatalf("gateway account failures = %+v, want rejected authorization recorded", tokens.failures) - } - if len(tokens.exclusions) != 2 || !slices.Equal(tokens.exclusions[1], []int64{1}) { - t.Fatalf("selection exclusions = %+v, want failed OAuth account excluded", tokens.exclusions) - } -} - -func TestProxyDoesNotDuplicatePersistedOAuthAuthorizationRefreshFailure(t *testing.T) { - tokens := &fakeSelectedAccountProvider{ - accounts: []SelectedAccount{ - {AccountID: 1, AccountType: provider.AccountTypeCodexOAuth, AuthorizationToken: "old-token"}, - }, - refreshAuthorizationRetry: true, - refreshFailureRecorded: true, - refreshAuthorizationErr: errors.New("refresh rejected"), - } - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusUnauthorized, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"error":{"message":"invalid access token"}}`)), - Request: r, - }, nil - })} - proxy := NewProxyWithClient(&fakeAPIKeyAuthenticator{}, tokens, Config{UpstreamBaseURL: "https://upstream.example.test"}, client) - req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_123", nil) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusServiceUnavailable { - t.Fatalf("status = %d body=%s, want 503", recorder.Code, recorder.Body.String()) - } - if len(tokens.authorizationRefreshes) != 1 { - t.Fatalf("authorization refreshes = %+v, want one", tokens.authorizationRefreshes) - } - if len(tokens.failures) != 0 { - t.Fatalf("gateway account failures = %+v, want provider record preserved without duplication", tokens.failures) - } -} - func TestProxyRecordsCircuitOpenOnUpstreamServerErrorAndReturnsFinalError(t *testing.T) { tokens := &fakeSelectedAccountProvider{accounts: []SelectedAccount{{AccountID: 1, AuthorizationToken: "bad-token"}, {AccountID: 1, AuthorizationToken: "same-account-token"}}} client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { @@ -5511,50 +5119,6 @@ func TestProxyDoesNotRetryAfterStreamingBegins(t *testing.T) { } } -func TestProxyAppliesSelectedAccountFingerprintHeaders(t *testing.T) { - var gotUserAgent string - var gotHeader string - var gotTLS string - tokens := &fakeSelectedAccountProvider{accounts: []SelectedAccount{{ - AccountID: 1, - AuthorizationToken: "upstream-token", - FingerprintUA: "N2API-Fingerprint/1.0", - FingerprintTLS: "chrome", - FingerprintHeaders: map[string]string{"X-Fingerprint-Test": "enabled"}, - }}} - client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - gotUserAgent = r.Header.Get("User-Agent") - gotHeader = r.Header.Get("X-Fingerprint-Test") - gotTLS = tlsFingerprintFromContext(r.Context()) - return &http.Response{ - StatusCode: http.StatusOK, - Header: http.Header{"Content-Type": []string{"application/json"}}, - Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), - Request: r, - }, nil - })} - proxy := NewProxyWithClient(&fakeAPIKeyAuthenticator{}, tokens, Config{UpstreamBaseURL: "https://upstream.example.test"}, client) - req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(`{"model":"gpt-5","messages":[]}`)) - req.Header.Set("Authorization", "Bearer n2api_client_secret") - req.Header.Set("Content-Type", "application/json") - recorder := httptest.NewRecorder() - - proxy.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if gotUserAgent != "N2API-Fingerprint/1.0" { - t.Fatalf("User-Agent = %q, want fingerprint override", gotUserAgent) - } - if gotHeader != "enabled" { - t.Fatalf("X-Fingerprint-Test = %q, want fingerprint custom header", gotHeader) - } - if gotTLS != "chrome" { - t.Fatalf("TLS fingerprint = %q, want chrome", gotTLS) - } -} - func TestProxyPassesThroughMatchingRetryableUpstreamError(t *testing.T) { transportCalls := 0 rules := &fakeErrorPassthroughRuleProvider{rules: []admin.ErrorPassthroughRule{{ @@ -5606,3 +5170,35 @@ func TestProxyPassesThroughMatchingRetryableUpstreamError(t *testing.T) { t.Fatalf("account attempts/recoveries = %+v/%+v, want [1]/none", tokens.used, tokens.recovered) } } + +func TestShouldStreamUpstreamResponseUsesContentType(t *testing.T) { + for _, contentType := range []string{"application/json", "text/plain", ""} { + if shouldStreamUpstreamResponse(&http.Response{Header: http.Header{"Content-Type": {contentType}}}) { + t.Fatalf("unexpected SSE for %q", contentType) + } + } + if !shouldStreamUpstreamResponse(&http.Response{Header: http.Header{"Content-Type": {"text/event-stream; charset=utf-8"}}}) { + t.Fatal("SSE should stream") + } +} +func TestAPIUpstreamDoesNotSynthesizeClientIdentity(t *testing.T) { + p := NewProxy(nil, nil, Config{}) + defer p.Close() + incoming := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"test","input":"hello","stream":false}`)) + req, err := p.newUpstreamRequest(incoming, SelectedAccount{AccountType: provider.AccountTypeAPIUpstream, AuthorizationToken: "api-key", BaseURL: "https://upstream.example/v1"}, incoming.Body) + if err != nil { + t.Fatal(err) + } + for _, key := range []string{"Originator", "Version", "Chatgpt-Account-Id", "Openai-Beta", "User-Agent"} { + if got := req.Header.Get(key); got != "" { + t.Errorf("synthesized %s = %q", key, got) + } + } + body, err := io.ReadAll(req.Body) + if err != nil || !strings.Contains(string(body), `"stream":false`) { + t.Fatalf("API body changed: %s (%v)", body, err) + } + if req.URL.String() != "https://upstream.example/v1/responses" || req.Header.Get("Authorization") != "Bearer api-key" { + t.Fatalf("wrong target/auth: %s", req.URL) + } +} diff --git a/backend/internal/gateway/request_diagnostics.go b/backend/internal/gateway/request_diagnostics.go index bf142326..82eae77e 100644 --- a/backend/internal/gateway/request_diagnostics.go +++ b/backend/internal/gateway/request_diagnostics.go @@ -16,7 +16,6 @@ const ( attemptTypeGatewayRejection = "gateway_rejection" attemptTypeUpstreamHTTP = "upstream_http" attemptTypeUpstreamTransport = "upstream_transport" - attemptTypeAuthRefreshRetry = "auth_refresh_retry" ) type requestDiagnostics struct { diff --git a/backend/internal/gateway/request_diagnostics_test.go b/backend/internal/gateway/request_diagnostics_test.go index 3cc8a9b1..bbcb89ab 100644 --- a/backend/internal/gateway/request_diagnostics_test.go +++ b/backend/internal/gateway/request_diagnostics_test.go @@ -15,7 +15,7 @@ import ( func TestRequestDiagnosticsBoundsAttemptTimelineAndPreservesOrder(t *testing.T) { diagnostics := newRequestDiagnostics() - selected := SelectedAccount{AccountID: 42, AccountType: "codex_oauth", DisplayName: "owner"} + selected := SelectedAccount{AccountID: 42, AccountType: "api_upstream", DisplayName: "owner"} for index := 0; index < requestlog.MaxRequestAttemptTimelineEntries+3; index++ { span := diagnostics.beginAttempt("upstream_http", selected, "retryable_status") span.finish(503, "upstream_unavailable") diff --git a/backend/internal/gateway/response_writer.go b/backend/internal/gateway/response_writer.go index f5b4a2b3..7d6f1ace 100644 --- a/backend/internal/gateway/response_writer.go +++ b/backend/internal/gateway/response_writer.go @@ -5,7 +5,6 @@ import ( "errors" "io" "net/http" - "strconv" "time" "github.com/KnowSky404/N2API/backend/internal/requestlog" @@ -27,32 +26,14 @@ type upstreamResponseObservation struct { ResponseTiming requestlog.ResponseTiming } -func (p *Proxy) writeUpstreamResponseWithOptions(ctx context.Context, w http.ResponseWriter, resp *http.Response, route string, stream, aggregateOAuth, validateSSETerminal bool) (upstreamResponseObservation, error) { +func (p *Proxy) writeUpstreamResponseWithOptions(ctx context.Context, w http.ResponseWriter, resp *http.Response, route string, stream bool) (upstreamResponseObservation, error) { if resp == nil || resp.Body == nil { return upstreamResponseObservation{Usage: Usage{Source: "missing"}}, errUpstreamResponseRead } - if aggregateOAuth { - observation, body, err := p.aggregateOAuthResponses(ctx, resp.Body, route) - if err != nil && len(body) == 0 { - return observation, err - } - copyResponseHeaders(w.Header(), resp.Header) - w.Header().Set("Content-Type", "application/json") - w.Header().Del("Content-Length") - w.Header().Set("Content-Length", strconv.Itoa(len(body))) - w.WriteHeader(resp.StatusCode) - if _, writeErr := w.Write(body); writeErr != nil { - return observation, writeErr - } - return observation, err - } if stream { copyResponseHeaders(w.Header(), resp.Header) w.Header().Set("Content-Type", "text/event-stream") w.WriteHeader(resp.StatusCode) - if validateSSETerminal { - return copyOAuthStreamingResponse(ctx, w, resp.Body, route, p.sseIdleTimeout, p.maxResponseBody) - } return copyStreamingResponse(ctx, w, resp.Body, route, p.sseIdleTimeout) } return writeBufferedUpstreamResponse(w, resp, route, p.maxResponseBody) @@ -98,22 +79,6 @@ func upstreamResponseErrorCode(err error) string { return "upstream_generation_missing" case errors.Is(err, errUpstreamSSEIdleTimeout): return "upstream_sse_idle_timeout" - case errors.Is(err, errUpstreamSSETerminalMissing): - return "upstream_sse_terminal_missing" - case errors.Is(err, errUpstreamSSETruncated): - return "upstream_sse_truncated" - case errors.Is(err, errUpstreamSSEMalformed): - return "upstream_sse_malformed" - case errors.Is(err, errUpstreamSSEError): - return "upstream_sse_error" - case errors.Is(err, errUpstreamResponseFailed): - return "upstream_response_failed" - case errors.Is(err, errUpstreamResponseIncomplete): - return "upstream_response_incomplete" - case errors.Is(err, errUpstreamResponseCanceled): - return "upstream_response_canceled" - case errors.Is(err, errUpstreamResponseDeadline): - return "upstream_response_timeout" } return "upstream_response_error" } @@ -128,22 +93,6 @@ func upstreamResponseErrorMessage(err error) string { return "upstream response contained no generated output" case errors.Is(err, errUpstreamSSEIdleTimeout): return "upstream stream timed out" - case errors.Is(err, errUpstreamSSETerminalMissing): - return "upstream SSE stream ended without a terminal response" - case errors.Is(err, errUpstreamSSETruncated): - return "upstream SSE stream ended before an event was complete" - case errors.Is(err, errUpstreamSSEMalformed): - return "upstream SSE stream contained malformed data" - case errors.Is(err, errUpstreamSSEError): - return "upstream SSE stream reported an error" - case errors.Is(err, errUpstreamResponseFailed): - return "upstream response failed" - case errors.Is(err, errUpstreamResponseIncomplete): - return "upstream response was incomplete" - case errors.Is(err, errUpstreamResponseCanceled): - return "upstream response was canceled" - case errors.Is(err, errUpstreamResponseDeadline): - return "upstream response timed out" } return "could not read upstream response" } @@ -202,9 +151,6 @@ func copyStreamingResponse(ctx context.Context, w http.ResponseWriter, body io.R } func responseObservationErrorCode(observation upstreamResponseObservation, err error) string { - if errors.Is(err, errUpstreamResponseCanceled) { - return "client_canceled" - } if err != nil { return upstreamResponseErrorCode(err) } diff --git a/backend/internal/gateway/transport_network_acceptance_test.go b/backend/internal/gateway/transport_network_acceptance_test.go index 5f44b0e9..4639915c 100644 --- a/backend/internal/gateway/transport_network_acceptance_test.go +++ b/backend/internal/gateway/transport_network_acceptance_test.go @@ -11,107 +11,13 @@ import ( "net/http" "net/http/httptest" "net/url" - "reflect" - "slices" "strings" "sync" "sync/atomic" "testing" "time" - - utls "github.com/refraction-networking/utls" ) -func TestTransportNetworkAcceptanceFourQuadrants(t *testing.T) { - type quadrant struct { - name string - fingerprint bool - proxy bool - } - quadrants := []quadrant{ - {name: "direct without fingerprint"}, - {name: "direct with fingerprint", fingerprint: true}, - {name: "proxy without fingerprint", proxy: true}, - {name: "proxy with fingerprint", fingerprint: true, proxy: true}, - } - - shapes := make(map[string]acceptanceClientHelloShape, len(quadrants)) - for _, testCase := range quadrants { - t.Run(testCase.name, func(t *testing.T) { - capture := &acceptanceTLSCapture{} - upstream := newAcceptanceTLSServer(t, capture, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/quadrant" { - http.NotFound(w, r) - return - } - _, _ = io.WriteString(w, "ok") - })) - - var proxyURL *url.URL - var connectCount atomic.Int32 - if testCase.proxy { - proxyServer := newConnectProxyServer(t, upstream.Listener.Addr().String(), false, "matrix-user", "matrix-password", &connectCount) - var err error - proxyURL, err = url.Parse(proxyServer.URL) - if err != nil { - t.Fatalf("parse proxy URL: %v", err) - } - proxyURL.User = url.UserPassword("matrix-user", "matrix-password") - } - - client, closeIdle := newAcceptanceTransportClient(proxyURL, testCase.fingerprint, acceptanceTimeouts()) - t.Cleanup(closeIdle) - resp, err := client.Get(upstream.URL + "/quadrant") - if err != nil { - t.Fatalf("GET quadrant: %v", err) - } - body, readErr := io.ReadAll(resp.Body) - closeErr := resp.Body.Close() - if readErr != nil || closeErr != nil || string(body) != "ok" { - t.Fatalf("response body/read/close = %q/%v/%v", body, readErr, closeErr) - } - if testCase.proxy && connectCount.Load() != 1 { - t.Fatalf("CONNECT count = %d, want 1", connectCount.Load()) - } - - handshake, err := extractAcceptanceClientHello(capture.Bytes()) - if err != nil { - t.Fatalf("extract captured ClientHello: %v", err) - } - shape, err := parseAcceptanceClientHelloShape(handshake) - if err != nil { - t.Fatalf("parse captured ClientHello: %v", err) - } - shapes[testCase.name] = shape - - if testCase.fingerprint { - host, _, splitErr := net.SplitHostPort(upstream.Listener.Addr().String()) - if splitErr != nil { - t.Fatalf("split upstream address: %v", splitErr) - } - expected := buildAcceptanceClientHelloShape(t, utls.HelloChrome_Auto, host) - if !reflect.DeepEqual(shape, expected) { - t.Fatalf("captured fingerprint ClientHello shape = %+v, want Chrome shape %+v", shape, expected) - } - } - }) - } - - directPlain := shapes["direct without fingerprint"] - proxyPlain := shapes["proxy without fingerprint"] - directFingerprint := shapes["direct with fingerprint"] - proxyFingerprint := shapes["proxy with fingerprint"] - if !reflect.DeepEqual(directPlain, proxyPlain) { - t.Fatalf("proxy changed standard ClientHello shape: direct=%+v proxy=%+v", directPlain, proxyPlain) - } - if !reflect.DeepEqual(directFingerprint, proxyFingerprint) { - t.Fatalf("proxy changed fingerprint ClientHello shape: direct=%+v proxy=%+v", directFingerprint, proxyFingerprint) - } - if reflect.DeepEqual(directPlain, directFingerprint) { - t.Fatal("captured Chrome ClientHello shape matches the standard Go ClientHello") - } -} - func TestTransportNetworkAcceptanceStandardProxyAuthAndRedaction(t *testing.T) { const ( username = "standard-secret-user" @@ -129,7 +35,7 @@ func TestTransportNetworkAcceptanceStandardProxyAuthAndRedaction(t *testing.T) { } proxyURL.User = url.UserPassword(username, password) - client, closeIdle := newAcceptanceTransportClient(proxyURL, false, acceptanceTimeouts()) + client, closeIdle := newAcceptanceTransportClient(proxyURL, acceptanceTimeouts()) t.Cleanup(closeIdle) _, requestErr := client.Get("https://upstream.example.test/v1/responses") if requestErr == nil { @@ -155,13 +61,13 @@ func TestTransportNetworkAcceptanceTLSHandshakeStall(t *testing.T) { t.Run("timeout", func(t *testing.T) { address, accepted := newAcceptanceStalledTLSListener(t) timeouts := upstreamTimeouts{connect: time.Second, tlsHandshake: 50 * time.Millisecond, responseHeader: time.Second} - client, closeIdle := newAcceptanceTransportClient(nil, true, timeouts) + client, closeIdle := newAcceptanceTransportClient(nil, timeouts) t.Cleanup(closeIdle) startedAt := time.Now() _, err := client.Get("https://" + address + "/stall") - if err == nil || !errors.Is(err, context.DeadlineExceeded) { - t.Fatalf("TLS stall error = %v, want deadline exceeded", err) + if timeout, ok := err.(net.Error); !ok || !timeout.Timeout() { + t.Fatalf("TLS stall error = %v, want timeout", err) } if elapsed := time.Since(startedAt); elapsed > time.Second { t.Fatalf("TLS stall timeout returned after %s", elapsed) @@ -175,7 +81,7 @@ func TestTransportNetworkAcceptanceTLSHandshakeStall(t *testing.T) { t.Run("cancellation", func(t *testing.T) { address, accepted := newAcceptanceStalledTLSListener(t) - client, closeIdle := newAcceptanceTransportClient(nil, true, upstreamTimeouts{connect: time.Second, tlsHandshake: 5 * time.Second, responseHeader: time.Second}) + client, closeIdle := newAcceptanceTransportClient(nil, upstreamTimeouts{connect: time.Second, tlsHandshake: 5 * time.Second, responseHeader: time.Second}) t.Cleanup(closeIdle) ctx, cancel := context.WithCancel(context.Background()) req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+address+"/stall", nil) @@ -233,7 +139,7 @@ func TestTransportNetworkAcceptanceTunnelSSECancellation(t *testing.T) { t.Fatalf("parse proxy URL: %v", err) } proxyURL.User = url.UserPassword("sse-user", "sse-password") - client, closeIdle := newAcceptanceTransportClient(proxyURL, true, acceptanceTimeouts()) + client, closeIdle := newAcceptanceTransportClient(proxyURL, acceptanceTimeouts()) t.Cleanup(closeIdle) ctx, cancel := context.WithCancel(context.Background()) @@ -290,189 +196,12 @@ func acceptanceTimeouts() upstreamTimeouts { return upstreamTimeouts{connect: 2 * time.Second, tlsHandshake: 2 * time.Second, responseHeader: 2 * time.Second} } -func newAcceptanceTransportClient(proxyURL *url.URL, fingerprint bool, timeouts upstreamTimeouts) (*http.Client, func()) { - if fingerprint { - client, transport := newTestFingerprintClientWithTimeouts(proxyURL, timeouts) - return client, transport.CloseIdleConnections - } - transport := newSelectedAccountTransport(proxyURL, "", timeouts) +func newAcceptanceTransportClient(proxyURL *url.URL, timeouts upstreamTimeouts) (*http.Client, func()) { + transport := newSelectedAccountTransport(proxyURL, timeouts) transport.standard.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} return &http.Client{Transport: transport}, transport.CloseIdleConnections } -type acceptanceTLSCapture struct { - mu sync.Mutex - raw []byte -} - -func (c *acceptanceTLSCapture) Append(data []byte) { - c.mu.Lock() - c.raw = append(c.raw, data...) - c.mu.Unlock() -} - -func (c *acceptanceTLSCapture) Bytes() []byte { - c.mu.Lock() - defer c.mu.Unlock() - return append([]byte(nil), c.raw...) -} - -type acceptanceCaptureListener struct { - net.Listener - capture *acceptanceTLSCapture -} - -func (l *acceptanceCaptureListener) Accept() (net.Conn, error) { - conn, err := l.Listener.Accept() - if err != nil { - return nil, err - } - return &acceptanceCaptureConn{Conn: conn, capture: l.capture}, nil -} - -type acceptanceCaptureConn struct { - net.Conn - capture *acceptanceTLSCapture -} - -func (c *acceptanceCaptureConn) Read(data []byte) (int, error) { - n, err := c.Conn.Read(data) - if n > 0 { - c.capture.Append(data[:n]) - } - return n, err -} - -func newAcceptanceTLSServer(t *testing.T, capture *acceptanceTLSCapture, handler http.Handler) *httptest.Server { - t.Helper() - server := httptest.NewUnstartedServer(handler) - server.Listener = &acceptanceCaptureListener{Listener: server.Listener, capture: capture} - server.StartTLS() - t.Cleanup(server.Close) - return server -} - -type acceptanceClientHelloShape struct { - cipherSuites []uint16 - extensions []uint16 -} - -func extractAcceptanceClientHello(records []byte) ([]byte, error) { - var handshake []byte - for len(records) >= 5 { - recordLength := int(records[3])<<8 | int(records[4]) - if recordLength < 0 || len(records) < 5+recordLength { - return nil, errors.New("truncated TLS record") - } - if records[0] == 22 { - handshake = append(handshake, records[5:5+recordLength]...) - if len(handshake) >= 4 { - handshakeLength := int(handshake[1])<<16 | int(handshake[2])<<8 | int(handshake[3]) - if handshake[0] != 1 { - return nil, errors.New("first TLS handshake is not ClientHello") - } - if len(handshake) >= 4+handshakeLength { - return append([]byte(nil), handshake[:4+handshakeLength]...), nil - } - } - } - records = records[5+recordLength:] - } - return nil, errors.New("ClientHello not found") -} - -func parseAcceptanceClientHelloShape(handshake []byte) (acceptanceClientHelloShape, error) { - if len(handshake) < 4 || handshake[0] != 1 { - return acceptanceClientHelloShape{}, errors.New("invalid ClientHello handshake") - } - bodyLength := int(handshake[1])<<16 | int(handshake[2])<<8 | int(handshake[3]) - if len(handshake) < 4+bodyLength { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello handshake") - } - body := handshake[4 : 4+bodyLength] - position := 2 + 32 - if len(body) < position+1 { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello session") - } - sessionLength := int(body[position]) - position++ - if len(body) < position+sessionLength+2 { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello session data") - } - position += sessionLength - cipherLength := int(body[position])<<8 | int(body[position+1]) - position += 2 - if cipherLength%2 != 0 || len(body) < position+cipherLength+1 { - return acceptanceClientHelloShape{}, errors.New("invalid ClientHello cipher suites") - } - shape := acceptanceClientHelloShape{} - for end := position + cipherLength; position < end; position += 2 { - value := uint16(body[position])<<8 | uint16(body[position+1]) - if !isAcceptanceGREASE(value) { - shape.cipherSuites = append(shape.cipherSuites, value) - } - } - compressionLength := int(body[position]) - position++ - if len(body) < position+compressionLength { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello compression methods") - } - position += compressionLength - if position == len(body) { - return shape, nil - } - if len(body) < position+2 { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello extensions length") - } - extensionLength := int(body[position])<<8 | int(body[position+1]) - position += 2 - if len(body) < position+extensionLength { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello extensions") - } - for end := position + extensionLength; position < end; { - if end-position < 4 { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello extension") - } - extensionType := uint16(body[position])<<8 | uint16(body[position+1]) - valueLength := int(body[position+2])<<8 | int(body[position+3]) - position += 4 - if end-position < valueLength { - return acceptanceClientHelloShape{}, errors.New("truncated ClientHello extension value") - } - if !isAcceptanceGREASE(extensionType) { - shape.extensions = append(shape.extensions, extensionType) - } - position += valueLength - } - slices.Sort(shape.extensions) - return shape, nil -} - -func isAcceptanceGREASE(value uint16) bool { - return value&0x0f0f == 0x0a0a -} - -func buildAcceptanceClientHelloShape(t *testing.T, helloID utls.ClientHelloID, serverName string) acceptanceClientHelloShape { - t.Helper() - clientConn, serverConn := net.Pipe() - t.Cleanup(func() { - _ = clientConn.Close() - _ = serverConn.Close() - }) - uconn := utls.UClient(clientConn, &utls.Config{ServerName: serverName, NextProtos: []string{"http/1.1"}}, helloID) - if err := uconn.BuildHandshakeState(); err != nil { - t.Fatalf("BuildHandshakeState: %v", err) - } - if err := uconn.MarshalClientHello(); err != nil { - t.Fatalf("MarshalClientHello: %v", err) - } - shape, err := parseAcceptanceClientHelloShape(uconn.HandshakeState.Hello.Raw) - if err != nil { - t.Fatalf("parse built ClientHello: %v", err) - } - return shape -} - func newAcceptanceStalledTLSListener(t *testing.T) (string, <-chan struct{}) { t.Helper() listener, err := net.Listen("tcp", "127.0.0.1:0") @@ -499,3 +228,51 @@ func newAcceptanceStalledTLSListener(t *testing.T) (string, <-chan struct{}) { }) return listener.Addr().String(), accepted } + +func newConnectProxyServer(t *testing.T, allowedTarget string, secure bool, username, password string, connectCount *atomic.Int32) *httptest.Server { + t.Helper() + expectedAuthorization := "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)) + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodConnect || r.Host != allowedTarget || r.Header.Get("Proxy-Authorization") != expectedAuthorization { + http.Error(w, "proxy request rejected", http.StatusProxyAuthRequired) + return + } + upstreamConn, err := net.DialTimeout("tcp", allowedTarget, 2*time.Second) + if err != nil { + http.Error(w, "upstream unavailable", http.StatusBadGateway) + return + } + hijacker, ok := w.(http.Hijacker) + if !ok { + _ = upstreamConn.Close() + http.Error(w, "hijacking unavailable", http.StatusInternalServerError) + return + } + clientConn, buffered, err := hijacker.Hijack() + if err != nil { + _ = upstreamConn.Close() + return + } + connectCount.Add(1) + _, _ = buffered.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n") + _ = buffered.Flush() + defer clientConn.Close() + defer upstreamConn.Close() + copyDone := make(chan struct{}) + go func() { + _, _ = io.Copy(upstreamConn, clientConn) + _ = upstreamConn.(*net.TCPConn).CloseWrite() + close(copyDone) + }() + _, _ = io.Copy(clientConn, upstreamConn) + <-copyDone + }) + server := httptest.NewUnstartedServer(handler) + if secure { + server.StartTLS() + } else { + server.Start() + } + t.Cleanup(server.Close) + return server +} diff --git a/backend/internal/gateway/transport_registry.go b/backend/internal/gateway/transport_registry.go index b69e6a9d..c0b67a8d 100644 --- a/backend/internal/gateway/transport_registry.go +++ b/backend/internal/gateway/transport_registry.go @@ -15,11 +15,9 @@ const defaultTransportRegistryCapacity = 64 var errTransportRegistryClosed = errors.New("upstream transport registry is closed") type transportKey struct { - proxyScheme string - proxyAddress string - proxyUserHash [sha256.Size]byte - fingerprint string - protocolPolicy string + proxyScheme string + proxyAddress string + proxyUserHash [sha256.Size]byte } type transportEntry struct { @@ -81,7 +79,7 @@ func (r *transportRegistry) ClientFor(selected SelectedAccount) (*http.Client, e return entry.client, nil } - transport := newSelectedAccountTransport(proxyURL, key.fingerprint, r.timeouts) + transport := newSelectedAccountTransport(proxyURL, r.timeouts) client := &http.Client{ Transport: transport, Timeout: r.template.Timeout, @@ -182,15 +180,7 @@ func (r *transportRegistry) closeEntriesLocked() { } func selectedAccountTransportKey(selected SelectedAccount) (transportKey, *url.URL, error) { - fingerprintValue := strings.TrimSpace(selected.FingerprintTLS) - fingerprint := normalizeTLSFingerprintName(fingerprintValue) - if fingerprintValue != "" && fingerprint == "" { - return transportKey{}, nil, errors.New("unsupported upstream TLS fingerprint configuration") - } - key := transportKey{ - fingerprint: fingerprint, - protocolPolicy: "http1-fingerprint-v1", - } + key := transportKey{} proxyValue := strings.TrimSpace(selected.ProxyURL) if proxyValue == "" { return key, nil, nil diff --git a/backend/internal/gateway/transport_registry_test.go b/backend/internal/gateway/transport_registry_test.go index 0e8dbc5b..af33928e 100644 --- a/backend/internal/gateway/transport_registry_test.go +++ b/backend/internal/gateway/transport_registry_test.go @@ -13,9 +13,8 @@ func TestTransportKeyDoesNotExposeProxyCredentials(t *testing.T) { password = "plaintext-proxy-password" ) selected := SelectedAccount{ - AccountID: 1, - ProxyURL: "http://" + username + ":" + password + "@proxy.example.test:8080", - FingerprintTLS: "chrome", + AccountID: 1, + ProxyURL: "http://" + username + ":" + password + "@proxy.example.test:8080", } key := mustTransportKey(t, selected) @@ -37,14 +36,12 @@ func TestTransportRegistrySeparatesCredentialsForCanonicalProxyAddress(t *testin registry := newTransportRegistry(http.DefaultClient, upstreamTimeouts{}, 4) t.Cleanup(registry.Close) first := SelectedAccount{ - AccountID: 1, - ProxyURL: "http://first-user:first-password@PROXY.example.test", - FingerprintTLS: "chrome", + AccountID: 1, + ProxyURL: "http://first-user:first-password@PROXY.example.test", } second := SelectedAccount{ - AccountID: 2, - ProxyURL: "http://second-user:second-password@proxy.example.test:80", - FingerprintTLS: "chrome", + AccountID: 2, + ProxyURL: "http://second-user:second-password@proxy.example.test:80", } firstKey := mustTransportKey(t, first) @@ -69,9 +66,8 @@ func TestTransportRegistryCredentialRotationReplacesClient(t *testing.T) { registry := newTransportRegistry(http.DefaultClient, upstreamTimeouts{}, 4) t.Cleanup(registry.Close) before := SelectedAccount{ - AccountID: 7, - ProxyURL: "https://proxy-user:old-password@proxy.example.test:8443", - FingerprintTLS: "firefox", + AccountID: 7, + ProxyURL: "https://proxy-user:old-password@proxy.example.test:8443", } after := before after.ProxyURL = "https://proxy-user:new-password@proxy.example.test:8443" diff --git a/backend/internal/gateway/transport_selection_test.go b/backend/internal/gateway/transport_selection_test.go index f306e783..3e252eed 100644 --- a/backend/internal/gateway/transport_selection_test.go +++ b/backend/internal/gateway/transport_selection_test.go @@ -3,88 +3,14 @@ package gateway import ( "errors" "net/http" - "net/http/httptest" "sync" "testing" ) -func TestSelectedAccountTransportMatrix(t *testing.T) { - proxy := NewProxy(nil, nil, Config{}) - t.Cleanup(proxy.Close) - - tests := []struct { - name string - accountID int64 - proxyURL string - fingerprint string - wantFingerprint string - wantProxy bool - }{ - {name: "direct without fingerprint", accountID: 1}, - {name: "direct with fingerprint", accountID: 2, fingerprint: "chrome", wantFingerprint: "chrome"}, - {name: "proxy without fingerprint", accountID: 3, proxyURL: "http://proxy.example.test:8080", wantProxy: true}, - {name: "proxy with fingerprint", accountID: 4, proxyURL: "http://proxy.example.test:8080", fingerprint: "chrome", wantFingerprint: "chrome", wantProxy: true}, - } - - for _, testCase := range tests { - t.Run(testCase.name, func(t *testing.T) { - selected := SelectedAccount{ - AccountID: testCase.accountID, - AuthorizationToken: "upstream-token", - BaseURL: "https://upstream.example.test", - ProxyURL: testCase.proxyURL, - FingerprintTLS: testCase.fingerprint, - } - incoming := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) - upstreamRequest, err := proxy.newUpstreamRequest(incoming, selected, nil) - if err != nil { - t.Fatalf("newUpstreamRequest returned error: %v", err) - } - client, err := proxy.clientForSelectedAccount(selected) - if err != nil { - t.Fatalf("clientForSelectedAccount returned error: %v", err) - } - - if got := tlsFingerprintFromContext(upstreamRequest.Context()); got != testCase.wantFingerprint { - t.Fatalf("request fingerprint = %q, want %q", got, testCase.wantFingerprint) - } - transport, ok := client.Transport.(*selectedAccountTransport) - if !ok { - t.Fatalf("transport type = %T, want *selectedAccountTransport", client.Transport) - } - if got := transport.fingerprint != nil; got != (testCase.wantFingerprint != "") { - t.Fatalf("fingerprint transport configured = %v, want %v", got, testCase.wantFingerprint != "") - } - key := mustTransportKey(t, selected) - if got := key.proxyScheme != ""; got != testCase.wantProxy { - t.Fatalf("account proxy key configured = %v, want %v", got, testCase.wantProxy) - } - if testCase.wantProxy { - if transport.standard.Proxy == nil { - t.Fatal("standard transport has no proxy resolver for configured account proxy") - } - configuredProxy, err := transport.standard.Proxy(upstreamRequest) - if err != nil { - t.Fatalf("resolve standard transport proxy: %v", err) - } - if configuredProxy == nil || configuredProxy.String() != testCase.proxyURL { - t.Fatalf("standard transport proxy = %q, want %q", configuredProxy, testCase.proxyURL) - } - } - if transport.fingerprint == nil { - return - } - if transport.fingerprint.Proxy != nil || transport.fingerprint.DialTLSContext == nil { - t.Fatalf("fingerprint transport proxy/dialer configured = %v/%v, want false/true", transport.fingerprint.Proxy != nil, transport.fingerprint.DialTLSContext != nil) - } - }) - } -} - func TestTransportRegistrySequentialReuse(t *testing.T) { registry := newTransportRegistry(http.DefaultClient, upstreamTimeouts{}, 4) t.Cleanup(registry.Close) - selected := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy.example.test:8080", FingerprintTLS: "chrome"} + selected := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy.example.test:8080"} first, err := registry.ClientFor(selected) if err != nil { @@ -105,7 +31,7 @@ func TestTransportRegistrySequentialReuse(t *testing.T) { func TestTransportRegistryConcurrentDedupe(t *testing.T) { registry := newTransportRegistry(http.DefaultClient, upstreamTimeouts{}, 4) t.Cleanup(registry.Close) - selected := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy.example.test:8080", FingerprintTLS: "chrome"} + selected := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy.example.test:8080"} const callers = 32 clients := make(chan *http.Client, callers) @@ -148,9 +74,9 @@ func TestTransportRegistryConcurrentDedupe(t *testing.T) { func TestTransportRegistryCapacityEvictsLeastRecentlyUsed(t *testing.T) { registry := newTransportRegistry(http.DefaultClient, upstreamTimeouts{}, 2) t.Cleanup(registry.Close) - first := SelectedAccount{AccountID: 1, FingerprintTLS: "chrome"} - second := SelectedAccount{AccountID: 2, FingerprintTLS: "firefox"} - third := SelectedAccount{AccountID: 3, FingerprintTLS: "safari"} + first := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy1.example:8080"} + second := SelectedAccount{AccountID: 2, ProxyURL: "http://proxy2.example:8080"} + third := SelectedAccount{AccountID: 3, ProxyURL: "http://proxy3.example:8080"} firstClient := mustRegistryClient(t, registry, first) secondClient := mustRegistryClient(t, registry, second) @@ -179,8 +105,8 @@ func TestTransportRegistryCapacityEvictsLeastRecentlyUsed(t *testing.T) { func TestTransportRegistryAccountConfigChangeReplacesBinding(t *testing.T) { registry := newTransportRegistry(http.DefaultClient, upstreamTimeouts{}, 4) t.Cleanup(registry.Close) - before := SelectedAccount{AccountID: 7, ProxyURL: "http://proxy-a.example.test:8080", FingerprintTLS: "chrome"} - after := SelectedAccount{AccountID: 7, ProxyURL: "https://proxy-b.example.test:8443", FingerprintTLS: "firefox"} + before := SelectedAccount{AccountID: 7, ProxyURL: "http://proxy-a.example.test:8080"} + after := SelectedAccount{AccountID: 7, ProxyURL: "https://proxy-b.example.test:8443"} beforeClient := mustRegistryClient(t, registry, before) afterClient := mustRegistryClient(t, registry, after) @@ -200,7 +126,7 @@ func TestTransportRegistryAccountConfigChangeReplacesBinding(t *testing.T) { func TestProxyTransportInvalidationPreservesSharedEntryUntilLastAccount(t *testing.T) { proxy := NewProxy(nil, nil, Config{}) t.Cleanup(proxy.Close) - first := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy.example.test:8080", FingerprintTLS: "chrome"} + first := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy.example.test:8080"} second := first second.AccountID = 2 @@ -234,7 +160,7 @@ func TestProxyTransportInvalidationPreservesSharedEntryUntilLastAccount(t *testi func TestProxyTransportInvalidationAllAllowsRecreation(t *testing.T) { proxy := NewProxy(nil, nil, Config{}) t.Cleanup(proxy.Close) - selected := SelectedAccount{AccountID: 1, FingerprintTLS: "chrome"} + selected := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy1.example:8080"} original, err := proxy.clientForSelectedAccount(selected) if err != nil { t.Fatalf("clientForSelectedAccount returned error: %v", err) @@ -255,7 +181,7 @@ func TestProxyTransportInvalidationAllAllowsRecreation(t *testing.T) { func TestProxyTransportShutdownClosesRegistry(t *testing.T) { proxy := NewProxy(nil, nil, Config{}) - selected := SelectedAccount{AccountID: 1, FingerprintTLS: "chrome"} + selected := SelectedAccount{AccountID: 1, ProxyURL: "http://proxy1.example:8080"} if _, err := proxy.clientForSelectedAccount(selected); err != nil { t.Fatalf("clientForSelectedAccount returned error: %v", err) } @@ -275,7 +201,6 @@ func TestTransportRegistryRejectsUnsupportedConfiguration(t *testing.T) { t.Cleanup(registry.Close) tests := []SelectedAccount{ {AccountID: 1, ProxyURL: "socks5://proxy.example.test:1080"}, - {AccountID: 2, FingerprintTLS: "unknown-client-hello"}, } for _, selected := range tests { if _, err := registry.ClientFor(selected); err == nil { diff --git a/backend/internal/gateway/upstream_transport.go b/backend/internal/gateway/upstream_transport.go new file mode 100644 index 00000000..12a590ec --- /dev/null +++ b/backend/internal/gateway/upstream_transport.go @@ -0,0 +1,51 @@ +package gateway + +import ( + "net" + "net/http" + "net/url" + "strings" + "time" +) + +type upstreamTimeouts struct { + connect time.Duration + tlsHandshake time.Duration + responseHeader time.Duration +} + +func newUpstreamTransport(timeouts upstreamTimeouts) *http.Transport { + transport := http.DefaultTransport.(*http.Transport).Clone() + transport.DialContext = (&net.Dialer{ + Timeout: timeouts.connect, + KeepAlive: 30 * time.Second, + }).DialContext + transport.TLSHandshakeTimeout = timeouts.tlsHandshake + transport.ResponseHeaderTimeout = timeouts.responseHeader + return transport +} + +type selectedAccountTransport struct{ standard *http.Transport } + +func newSelectedAccountTransport(proxyURL *url.URL, timeouts upstreamTimeouts) *selectedAccountTransport { + standard := newUpstreamTransport(timeouts) + if proxyURL != nil { + standard.Proxy = http.ProxyURL(proxyURL) + } + return &selectedAccountTransport{standard: standard} +} +func (t *selectedAccountTransport) RoundTrip(req *http.Request) (*http.Response, error) { + return t.standard.RoundTrip(req) +} +func (t *selectedAccountTransport) CloseIdleConnections() { t.standard.CloseIdleConnections() } +func canonicalProxyAddress(proxyURL *url.URL) string { + port := proxyURL.Port() + if port == "" { + if strings.EqualFold(proxyURL.Scheme, "https") { + port = "443" + } else { + port = "80" + } + } + return net.JoinHostPort(strings.ToLower(proxyURL.Hostname()), port) +} diff --git a/backend/internal/httpapi/alerting_test.go b/backend/internal/httpapi/alerting_test.go index a0c2e689..ed35a630 100644 --- a/backend/internal/httpapi/alerting_test.go +++ b/backend/internal/httpapi/alerting_test.go @@ -81,7 +81,7 @@ func TestAlertingAdminRoutesRequireAuthentication(t *testing.T) { httptest.NewRequest(http.MethodPost, "/api/admin/alert-actions/7/test", strings.NewReader(`{"expectedUpdatedAt":"2026-07-21T12:00:00Z"}`)), httptest.NewRequest(http.MethodGet, "/api/admin/alert-rules", nil), httptest.NewRequest(http.MethodGet, "/api/admin/alert-rule-templates", nil), - httptest.NewRequest(http.MethodPost, "/api/admin/alert-rule-templates/oauth-refresh-repeated-v1/install", strings.NewReader(`{"actionId":7}`)), + httptest.NewRequest(http.MethodPost, "/api/admin/alert-rule-templates/request-log-retention-failed-v1/install", strings.NewReader(`{"actionId":7}`)), } { response := httptest.NewRecorder() server.ServeHTTP(response, request) @@ -92,7 +92,7 @@ func TestAlertingAdminRoutesRequireAuthentication(t *testing.T) { } func TestAlertRuleTemplateCatalogAndInstallRoutes(t *testing.T) { - service := &fakeAlertingAdminService{rule: alerting.Rule{ID: 9, TemplateKey: alerting.OAuthRefreshRepeatedTemplateKey, ActionID: 7}} + service := &fakeAlertingAdminService{rule: alerting.Rule{ID: 9, TemplateKey: alerting.RequestLogRetentionFailedTemplateKey, ActionID: 7}} server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), nil, service) response := httptest.NewRecorder() @@ -103,15 +103,15 @@ func TestAlertRuleTemplateCatalogAndInstallRoutes(t *testing.T) { var catalog struct { Templates []alerting.RuleTemplate `json:"templates"` } - if err := json.NewDecoder(response.Body).Decode(&catalog); err != nil || len(catalog.Templates) != 10 || - catalog.Templates[0].Key != alerting.OAuthRefreshRepeatedTemplateKey || catalog.Templates[1].Key != alerting.RequestLogRetentionFailedTemplateKey || - catalog.Templates[2].Key != alerting.ProviderAutoTestFailedTemplateKey || catalog.Templates[3].Key != alerting.ProviderAccountExpiredTemplateKey || - catalog.Templates[4].Key != alerting.ProviderAccountCircuitOpenTemplateKey || catalog.Templates[5].Key != alerting.APIKeyBudget80PercentTemplateKey || - catalog.Templates[6].Key != alerting.APIKeyBudget100PercentTemplateKey || catalog.Templates[7].Key != alerting.RoutingPoolExhaustedTemplateKey || - catalog.Templates[8].Key != alerting.APIKeyPurgeFailedTemplateKey || catalog.Templates[9].Key != alerting.SystemEventRetentionFailedTemplateKey { + if err := json.NewDecoder(response.Body).Decode(&catalog); err != nil || len(catalog.Templates) != 9 || + catalog.Templates[0].Key != alerting.RequestLogRetentionFailedTemplateKey || + catalog.Templates[1].Key != alerting.ProviderAutoTestFailedTemplateKey || catalog.Templates[2].Key != alerting.ProviderAccountExpiredTemplateKey || + catalog.Templates[3].Key != alerting.ProviderAccountCircuitOpenTemplateKey || catalog.Templates[4].Key != alerting.APIKeyBudget80PercentTemplateKey || + catalog.Templates[5].Key != alerting.APIKeyBudget100PercentTemplateKey || catalog.Templates[6].Key != alerting.RoutingPoolExhaustedTemplateKey || + catalog.Templates[7].Key != alerting.APIKeyPurgeFailedTemplateKey || catalog.Templates[8].Key != alerting.SystemEventRetentionFailedTemplateKey { t.Fatalf("catalog=%+v err=%v", catalog, err) } - apiKeyPurgeTemplate := catalog.Templates[8] + apiKeyPurgeTemplate := catalog.Templates[7] if apiKeyPurgeTemplate.Name != "API key purge failures" || apiKeyPurgeTemplate.Enabled || apiKeyPurgeTemplate.Category != systemevent.CategoryScheduler || apiKeyPurgeTemplate.Severity != systemevent.SeverityError || apiKeyPurgeTemplate.EventAction != systemevent.ActionSchedulerAPIKeyPurgeFailed || apiKeyPurgeTemplate.RecoveryAction != systemevent.ActionSchedulerAPIKeyPurgeCompleted || @@ -119,7 +119,7 @@ func TestAlertRuleTemplateCatalogAndInstallRoutes(t *testing.T) { apiKeyPurgeTemplate.DeduplicationScope != alerting.DeduplicationScopeTarget || !apiKeyPurgeTemplate.NotifyRecovery { t.Fatalf("API key purge template=%+v", apiKeyPurgeTemplate) } - systemEventRetentionTemplate := catalog.Templates[9] + systemEventRetentionTemplate := catalog.Templates[8] if systemEventRetentionTemplate.Name != "System event retention failures" || systemEventRetentionTemplate.Enabled || systemEventRetentionTemplate.Category != systemevent.CategoryScheduler || systemEventRetentionTemplate.Severity != "" || systemEventRetentionTemplate.EventAction != systemevent.ActionSchedulerEventRetentionFailed || systemEventRetentionTemplate.RecoveryAction != systemevent.ActionSchedulerEventRetentionCompleted || @@ -137,16 +137,16 @@ func TestAlertRuleTemplateCatalogAndInstallRoutes(t *testing.T) { t.Fatalf("created install status=%d body=%s service=%+v", response.Code, response.Body.String(), service) } - service.rule.TemplateKey = alerting.OAuthRefreshRepeatedTemplateKey + service.rule.TemplateKey = alerting.RequestLogRetentionFailedTemplateKey service.installCreated = false response = httptest.NewRecorder() - server.ServeHTTP(response, authenticatedAlertingRequest(http.MethodPost, "/api/admin/alert-rule-templates/oauth-refresh-repeated-v1/install", `{"actionId":7}`)) + server.ServeHTTP(response, authenticatedAlertingRequest(http.MethodPost, "/api/admin/alert-rule-templates/request-log-retention-failed-v1/install", `{"actionId":7}`)) if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"created":false`) { t.Fatalf("existing install status=%d body=%s", response.Code, response.Body.String()) } response = httptest.NewRecorder() - server.ServeHTTP(response, authenticatedAlertingRequest(http.MethodPost, "/api/admin/alert-rule-templates/oauth-refresh-repeated-v1/install", `{"actionId":7,"extra":true}`)) + server.ServeHTTP(response, authenticatedAlertingRequest(http.MethodPost, "/api/admin/alert-rule-templates/request-log-retention-failed-v1/install", `{"actionId":7,"extra":true}`)) if response.Code != http.StatusBadRequest || !strings.Contains(response.Body.String(), "bad_request") { t.Fatalf("strict install status=%d body=%s", response.Code, response.Body.String()) } diff --git a/backend/internal/httpapi/security_middleware.go b/backend/internal/httpapi/security_middleware.go index a450489a..7331b147 100644 --- a/backend/internal/httpapi/security_middleware.go +++ b/backend/internal/httpapi/security_middleware.go @@ -7,10 +7,9 @@ import ( ) const ( - browserAppContentSecurityPolicy = "default-src 'self'; base-uri 'self'; connect-src 'self'; font-src 'self'; form-action 'self'; frame-ancestors 'none'; img-src 'self' data:; object-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'" - browserDataContentSecurityPolicy = "default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" - browserOAuthContentSecurityPolicy = browserDataContentSecurityPolicy + "; style-src 'unsafe-inline'" - browserPermissionsPolicy = "camera=(), geolocation=(), microphone=(), payment=(), usb=()" + browserAppContentSecurityPolicy = "default-src 'self'; base-uri 'self'; connect-src 'self'; font-src 'self'; form-action 'self'; frame-ancestors 'none'; img-src 'self' data:; object-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'" + browserDataContentSecurityPolicy = "default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'" + browserPermissionsPolicy = "camera=(), geolocation=(), microphone=(), payment=(), usb=()" ) func withBrowserSecurity(next http.Handler) http.Handler { @@ -127,7 +126,7 @@ func sensitiveBrowserResponsePath(path string) bool { func browserContentSecurityPolicy(path string) string { if strings.HasPrefix(path, "/oauth/") { - return browserOAuthContentSecurityPolicy + return browserDataContentSecurityPolicy } if strings.HasPrefix(path, "/api/") || strings.HasPrefix(path, "/v1/") { return browserDataContentSecurityPolicy diff --git a/backend/internal/httpapi/security_middleware_test.go b/backend/internal/httpapi/security_middleware_test.go index 8e141b2f..c6bee8c5 100644 --- a/backend/internal/httpapi/security_middleware_test.go +++ b/backend/internal/httpapi/security_middleware_test.go @@ -11,7 +11,7 @@ import ( "github.com/KnowSky404/N2API/backend/internal/config" ) -func TestBrowserSecurityHeadersCoverStaticAdminOAuthAndHTTPS(t *testing.T) { +func TestBrowserSecurityHeadersCoverStaticAdminAndHTTPS(t *testing.T) { web := fstest.MapFS{ "index.html": {Data: []byte("
N2API
")}, } @@ -26,14 +26,18 @@ func TestBrowserSecurityHeadersCoverStaticAdminOAuthAndHTTPS(t *testing.T) { {name: "static HTTP", target: "http://n2api.example/", wantCSP: browserAppContentSecurityPolicy}, {name: "static HTTPS", target: "https://n2api.example/", wantHSTS: true, wantCSP: browserAppContentSecurityPolicy}, {name: "admin API", target: "http://n2api.example/api/admin/bootstrap", wantNoStore: true, wantCSP: browserDataContentSecurityPolicy}, - {name: "OAuth callback", target: "http://n2api.example/oauth/openai/callback?code=secret", wantNoStore: true, wantCSP: browserOAuthContentSecurityPolicy}, + {name: "retired OAuth callback", target: "http://n2api.example/oauth/openai/callback?code=secret", wantNoStore: true, wantCSP: browserDataContentSecurityPolicy}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { recorder := httptest.NewRecorder() server.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, tt.target, nil)) - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) + wantStatus := http.StatusOK + if strings.HasPrefix(tt.target, "http://n2api.example/oauth/") { + wantStatus = http.StatusNotFound + } + if recorder.Code != wantStatus { + t.Fatalf("status = %d body=%s, want %d", recorder.Code, recorder.Body.String(), wantStatus) } assertBrowserSecurityHeaders(t, recorder.Header(), tt.wantCSP) if got := recorder.Header().Get("Cache-Control"); (got == "no-store") != tt.wantNoStore { diff --git a/backend/internal/httpapi/server.go b/backend/internal/httpapi/server.go index a2bb221e..1a6a00bf 100644 --- a/backend/internal/httpapi/server.go +++ b/backend/internal/httpapi/server.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "html" "io" "io/fs" "net/http" @@ -99,10 +98,6 @@ type AdminService interface { GetOpsAccountHealth(ctx context.Context, since time.Time) (admin.OpsAccountHealth, error) ListOpsAccountTests(ctx context.Context, since time.Time, limit int) ([]admin.OpsAccountTest, error) GetOpsCostBreakdown(ctx context.Context, since time.Time) (admin.OpsCostBreakdown, error) - ListFingerprintProfiles(ctx context.Context) ([]admin.FingerprintProfile, error) - CreateFingerprintProfile(ctx context.Context, input admin.FingerprintProfileInput) (admin.FingerprintProfile, error) - UpdateFingerprintProfile(ctx context.Context, id int64, input admin.FingerprintProfileInput) (admin.FingerprintProfile, error) - DeleteFingerprintProfile(ctx context.Context, id int64) error ListErrorPassthroughRules(ctx context.Context) ([]admin.ErrorPassthroughRule, error) CreateErrorPassthroughRule(ctx context.Context, input admin.ErrorPassthroughRuleInput) (admin.ErrorPassthroughRule, error) UpdateErrorPassthroughRule(ctx context.Context, id int64, input admin.ErrorPassthroughRuleInput) (admin.ErrorPassthroughRule, error) @@ -111,32 +106,24 @@ type AdminService interface { } type ProviderService interface { - Status(ctx context.Context) (provider.Status, error) ListAccounts(ctx context.Context) ([]provider.Account, error) CreateAPIUpstreamAccount(ctx context.Context, input provider.APIUpstreamInput) (provider.Account, error) - StartConnect(ctx context.Context, options provider.ConnectOptions) (provider.ConnectResult, error) - CompleteCallback(ctx context.Context, code, state string) (provider.Account, error) UpdateAccount(ctx context.Context, id int64, update provider.AccountUpdate) (provider.Account, error) ListAccountModels(ctx context.Context, accountID int64) ([]provider.AccountModel, error) ListAccountModelsForAccounts(ctx context.Context, accountIDs []int64) (map[int64][]provider.AccountModel, error) ReplaceAccountModels(ctx context.Context, accountID int64, models []provider.AccountModelInput) ([]provider.AccountModel, error) SyncUpstreamAccountModels(ctx context.Context, accountID int64) ([]provider.AccountModel, provider.AccountModelSyncSummary, error) - SyncOAuthAccountModels(ctx context.Context, accountID int64) ([]provider.AccountModel, provider.AccountModelSyncSummary, error) - RefreshOAuthAccountModels(ctx context.Context, accountID int64) ([]provider.AccountModel, provider.AccountModelSyncSummary, error) - OAuthModelCatalogStatus(ctx context.Context, accountID int64) (provider.OAuthModelCatalogStatus, error) TestAccountModel(ctx context.Context, accountID int64, model string) (provider.AccountModelTestResult, error) PreviewAccountSelection(ctx context.Context, model, sessionID string, excludedAccountIDs ...int64) (provider.SelectionPreview, error) PreviewAccountSelectionInRoutingPool(ctx context.Context, routingPoolID int64, model, sessionID string, excludedAccountIDs ...int64) (provider.SelectionPreview, error) PreviewAccountSelectionForEndpoint(ctx context.Context, model, endpoint, sessionID string, excludedAccountIDs ...int64) (provider.SelectionPreview, error) PreviewAccountSelectionInRoutingPoolForEndpoint(ctx context.Context, routingPoolID int64, model, endpoint, sessionID string, excludedAccountIDs ...int64) (provider.SelectionPreview, error) - RefreshAccount(ctx context.Context, id int64) (provider.Account, error) TestAccount(ctx context.Context, id int64) (provider.Account, error) TestAccounts(ctx context.Context) ([]provider.Account, error) ListAccountTestResults(ctx context.Context, accountID int64, limit int) ([]provider.AccountTestResult, error) PauseAccountScheduling(ctx context.Context, id int64, duration time.Duration) (provider.Account, error) ResetAccountStatus(ctx context.Context, id int64) (provider.Account, error) DisconnectAccount(ctx context.Context, id int64) error - Disconnect(ctx context.Context) error } type ProviderAccountAutoTestStatusSource interface { @@ -1435,77 +1422,6 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro writeJSON(w, http.StatusOK, breakdown) })) - mux.HandleFunc("GET /api/admin/fingerprint-profiles", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - profiles, err := admins.ListFingerprintProfiles(r.Context()) - if err != nil { - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - writeJSON(w, http.StatusOK, map[string][]admin.FingerprintProfile{"profiles": profiles}) - })) - - mux.HandleFunc("POST /api/admin/fingerprint-profiles", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - var req admin.FingerprintProfileInput - if err := decodeJSON(w, r, &req); err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - profile, err := admins.CreateFingerprintProfile(r.Context(), req) - if err != nil { - if errors.Is(err, admin.ErrInvalidInput) { - writeError(w, http.StatusBadRequest, "invalid_input") - return - } - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - writeJSON(w, http.StatusCreated, map[string]admin.FingerprintProfile{"profile": profile}) - })) - - mux.HandleFunc("PATCH /api/admin/fingerprint-profiles/{id}", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - id, err := parsePositivePathID(r, "id") - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - var req admin.FingerprintProfileInput - if err := decodeJSON(w, r, &req); err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - profile, err := admins.UpdateFingerprintProfile(r.Context(), id, req) - if err != nil { - if errors.Is(err, admin.ErrInvalidInput) { - writeError(w, http.StatusBadRequest, "invalid_input") - return - } - if errors.Is(err, admin.ErrNotFound) { - writeError(w, http.StatusNotFound, "not_found") - return - } - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - writeJSON(w, http.StatusOK, map[string]admin.FingerprintProfile{"profile": profile}) - })) - - mux.HandleFunc("DELETE /api/admin/fingerprint-profiles/{id}", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - id, err := parsePositivePathID(r, "id") - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - if err := admins.DeleteFingerprintProfile(r.Context(), id); err != nil { - if errors.Is(err, admin.ErrNotFound) { - writeError(w, http.StatusNotFound, "not_found") - return - } - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - w.WriteHeader(http.StatusNoContent) - })) - mux.HandleFunc("GET /api/admin/error-passthrough-rules", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { rules, err := admins.ListErrorPassthroughRules(r.Context()) if err != nil { @@ -1639,34 +1555,28 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro return } var req struct { - Name string `json:"name"` - BaseURL string `json:"baseUrl"` - APIKey string `json:"apiKey"` - ProxyURL string `json:"proxyUrl"` - Enabled *bool `json:"enabled"` - Priority int `json:"priority"` - LoadFactor int `json:"loadFactor"` - FingerprintProfileID *int64 `json:"fingerprintProfileId"` - Models []string `json:"models"` + Name string `json:"name"` + BaseURL string `json:"baseUrl"` + APIKey string `json:"apiKey"` + ProxyURL string `json:"proxyUrl"` + Enabled *bool `json:"enabled"` + Priority int `json:"priority"` + LoadFactor int `json:"loadFactor"` + Models []string `json:"models"` } if err := decodeJSON(w, r, &req); err != nil { writeError(w, http.StatusBadRequest, "bad_request") return } - fingerprintProfileID := req.FingerprintProfileID - if fingerprintProfileID != nil && *fingerprintProfileID == 0 { - fingerprintProfileID = nil - } account, err := providers.CreateAPIUpstreamAccount(r.Context(), provider.APIUpstreamInput{ - Name: req.Name, - BaseURL: req.BaseURL, - APIKey: req.APIKey, - ProxyURL: req.ProxyURL, - Enabled: req.Enabled, - Priority: req.Priority, - LoadFactor: req.LoadFactor, - FingerprintProfileID: fingerprintProfileID, - Models: req.Models, + Name: req.Name, + BaseURL: req.BaseURL, + APIKey: req.APIKey, + ProxyURL: req.ProxyURL, + Enabled: req.Enabled, + Priority: req.Priority, + LoadFactor: req.LoadFactor, + Models: req.Models, }) if err != nil { writeProviderAccountError(w, err) @@ -1687,10 +1597,6 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro handleBulkTestProviderAccounts(w, r, providers, systemEvents) })) - mux.HandleFunc("POST /api/admin/provider-accounts/bulk-refresh", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleBulkRefreshProviderAccounts(w, r, providers, systemEvents) - })) - mux.HandleFunc("POST /api/admin/provider-accounts/bulk-disconnect", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { handleBulkDisconnectProviderAccounts(w, r, providers, systemEvents) })) @@ -1707,18 +1613,6 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro handleBulkReplaceProviderAccountModels(w, r, providers, systemEvents) })) - mux.HandleFunc("GET /api/admin/provider-accounts/codex-oauth/status", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleProviderStatus(w, r, providers) - })) - - mux.HandleFunc("POST /api/admin/provider-accounts/codex-oauth/connect", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleProviderConnect(w, r, providers) - })) - - mux.HandleFunc("POST /api/admin/provider-accounts/codex-oauth/callback", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleProviderCallback(w, r, providers) - })) - mux.HandleFunc("PATCH /api/admin/provider-accounts/{id}", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { handlePatchProviderAccount(w, r, providers) })) @@ -1743,18 +1637,10 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro handleSyncProviderAccountModels(w, r, providers) })) - mux.HandleFunc("POST /api/admin/provider-accounts/{id}/models/refresh", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleRefreshProviderAccountModels(w, r, providers) - })) - mux.HandleFunc("POST /api/admin/provider-accounts/{id}/model-tests", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { handleTestProviderAccountModel(w, r, providers) })) - mux.HandleFunc("POST /api/admin/provider-accounts/{id}/refresh", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleRefreshProviderAccount(w, r, providers) - })) - mux.HandleFunc("POST /api/admin/provider-accounts/{id}/test", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { handleTestProviderAccount(w, r, providers) })) @@ -1771,30 +1657,6 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro handleResetProviderAccountStatus(w, r, providers) })) - mux.HandleFunc("GET /api/admin/providers/openai", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleProviderStatus(w, r, providers) - })) - - mux.HandleFunc("POST /api/admin/providers/openai/connect", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleProviderConnect(w, r, providers) - })) - - mux.HandleFunc("POST /api/admin/providers/openai/callback", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleProviderCallback(w, r, providers) - })) - - mux.HandleFunc("POST /api/admin/providers/openai/disconnect", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - if err := providers.Disconnect(r.Context()); err != nil { - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - w.WriteHeader(http.StatusNoContent) - })) - mux.HandleFunc("GET /api/admin/providers/openai/accounts", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { handleListProviderAccounts(w, r, admins, providers, accountConcurrencySource) })) @@ -1811,10 +1673,6 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro handleReplaceProviderAccountModels(w, r, providers) })) - mux.HandleFunc("POST /api/admin/providers/openai/accounts/{id}/refresh", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { - handleRefreshProviderAccount(w, r, providers) - })) - mux.HandleFunc("POST /api/admin/providers/openai/accounts/{id}/test", requireAdmin(func(w http.ResponseWriter, r *http.Request, _ admin.Admin) { handleTestProviderAccount(w, r, providers) })) @@ -1857,10 +1715,6 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro w.WriteHeader(http.StatusNoContent) })) - mux.HandleFunc("GET /oauth/openai/callback", func(w http.ResponseWriter, r *http.Request) { - writeManualOAuthCallbackPage(w, r) - }) - if gateway != nil { mux.Handle("/v1/", gateway) } @@ -1873,6 +1727,10 @@ func NewServer(cfg config.Config, health HealthChecker, admins AdminService, pro writeError(w, http.StatusNotFound, "not_found") }) + mux.HandleFunc("/oauth/", func(w http.ResponseWriter, r *http.Request) { + writeError(w, http.StatusNotFound, "not_found") + }) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if serveWeb(w, r, webFS) { return @@ -1935,12 +1793,6 @@ func providerAccountErrorResponse(err error) (int, string) { if errors.Is(err, provider.ErrNotConnected) || errors.Is(err, admin.ErrNotFound) { return http.StatusNotFound, "not_found" } - if errors.Is(err, provider.ErrOAuthModelCatalogBusy) { - return http.StatusTooManyRequests, "catalog_refresh_busy" - } - if errors.Is(err, provider.ErrOAuthModelCatalogStale) { - return http.StatusConflict, "catalog_refresh_superseded" - } return http.StatusInternalServerError, "internal_error" } @@ -2268,79 +2120,6 @@ func parseExcludedAccountIDs(raw string) ([]int64, error) { return ids, nil } -func handleProviderStatus(w http.ResponseWriter, r *http.Request, providers ProviderService) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - status, err := providers.Status(r.Context()) - if err != nil { - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - writeJSON(w, http.StatusOK, status) -} - -func handleProviderConnect(w http.ResponseWriter, r *http.Request, providers ProviderService) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - options, err := decodeConnectOptions(w, r) - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - result, err := providers.StartConnect(r.Context(), options) - if err != nil { - if errors.Is(err, provider.ErrNotConfigured) { - writeError(w, http.StatusConflict, "provider_not_configured") - return - } - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - writeJSON(w, http.StatusOK, map[string]string{"authorizationUrl": result.AuthorizationURL}) -} - -func handleProviderCallback(w http.ResponseWriter, r *http.Request, providers ProviderService) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - code, state, err := decodeCallbackURL(w, r) - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - account, err := providers.CompleteCallback(r.Context(), code, state) - if err != nil { - if errors.Is(err, provider.ErrInvalidState) { - writeError(w, http.StatusBadRequest, "invalid_oauth_callback") - return - } - if errors.Is(err, provider.ErrNotConfigured) { - writeError(w, http.StatusConflict, "provider_not_configured") - return - } - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - models, summary, err := providers.SyncOAuthAccountModels(r.Context(), account.ID) - if err != nil { - writeJSON(w, http.StatusBadGateway, map[string]any{ - "error": "oauth_model_sync_failed", - "account": account, - }) - return - } - writeJSON(w, http.StatusOK, map[string]any{ - "account": account, - "models": models, - "synced": summary, - }) -} - func handlePatchProviderAccount(w http.ResponseWriter, r *http.Request, providers ProviderService) { if providers == nil { writeError(w, http.StatusServiceUnavailable, "service_unavailable") @@ -2361,7 +2140,6 @@ func handlePatchProviderAccount(w http.ResponseWriter, r *http.Request, provider BaseURL *string `json:"baseUrl"` APIKey *string `json:"apiKey"` ProxyURL *string `json:"proxyUrl"` - FingerprintProfileID *int64 `json:"fingerprintProfileId"` } body, err := readJSONBody(w, r) if err != nil { @@ -2372,27 +2150,20 @@ func handlePatchProviderAccount(w http.ResponseWriter, r *http.Request, provider writeError(w, http.StatusBadRequest, "bad_request") return } - fingerprintProfileIDSet, err := jsonFieldPresent(body, "fingerprintProfileId") - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - if req.Enabled == nil && req.Priority == nil && req.LoadFactor == nil && req.MaxConcurrentRequests == nil && req.Name == nil && req.BaseURL == nil && req.APIKey == nil && req.ProxyURL == nil && !fingerprintProfileIDSet { + if req.Enabled == nil && req.Priority == nil && req.LoadFactor == nil && req.MaxConcurrentRequests == nil && req.Name == nil && req.BaseURL == nil && req.APIKey == nil && req.ProxyURL == nil { writeError(w, http.StatusBadRequest, "invalid_input") return } account, err := providers.UpdateAccount(r.Context(), id, provider.AccountUpdate{ - Enabled: req.Enabled, - Priority: req.Priority, - LoadFactor: req.LoadFactor, - MaxConcurrentRequests: req.MaxConcurrentRequests, - Name: req.Name, - APIUpstreamBaseURL: req.BaseURL, - APIUpstreamAPIKey: req.APIKey, - ProxyURL: req.ProxyURL, - FingerprintProfileIDSet: fingerprintProfileIDSet, - FingerprintProfileID: req.FingerprintProfileID, + Enabled: req.Enabled, + Priority: req.Priority, + LoadFactor: req.LoadFactor, + MaxConcurrentRequests: req.MaxConcurrentRequests, + Name: req.Name, + APIUpstreamBaseURL: req.BaseURL, + APIUpstreamAPIKey: req.APIKey, + ProxyURL: req.ProxyURL, }) if err != nil { writeProviderAccountError(w, err) @@ -2513,43 +2284,6 @@ func parseBulkProviderAccountIDs(w http.ResponseWriter, ids []int64) ([]int64, b return accountIDs, true } -func handleBulkRefreshProviderAccounts(w http.ResponseWriter, r *http.Request, providers ProviderService, recorder SystemEventRecorder) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - var req struct { - AccountIDs []int64 `json:"accountIds"` - } - if err := decodeJSON(w, r, &req); err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - accountIDs, ok := parseBulkProviderAccountIDs(w, req.AccountIDs) - if !ok { - return - } - - accounts := make([]provider.Account, 0, len(accountIDs)) - audit := newProviderBatchAudit(systemevent.ActionProviderAccountBatchRefreshed, accountIDs) - started := time.Now() - targetContext := audit.targetContext(r.Context()) - for _, id := range accountIDs { - account, err := providers.RefreshAccount(targetContext, id) - if err != nil { - statusCode, errorCode := providerAccountErrorResponse(err) - audit.failed(id, errorCode) - audit.record(r.Context(), recorder, statusCode, time.Since(started)) - writeProviderAccountError(w, err) - return - } - audit.succeeded(id) - accounts = append(accounts, account) - } - audit.record(r.Context(), recorder, http.StatusOK, time.Since(started)) - writeJSON(w, http.StatusOK, map[string][]provider.Account{"accounts": accounts}) -} - func handleBulkDisconnectProviderAccounts(w http.ResponseWriter, r *http.Request, providers ProviderService, recorder SystemEventRecorder) { if providers == nil { writeError(w, http.StatusServiceUnavailable, "service_unavailable") @@ -2745,9 +2479,6 @@ func handleListProviderAccountModels(w http.ResponseWriter, r *http.Request, pro return } response := map[string]any{"models": models} - if status, statusErr := providers.OAuthModelCatalogStatus(r.Context(), id); statusErr == nil { - response["catalog"] = status - } writeJSON(w, http.StatusOK, response) } @@ -2797,29 +2528,6 @@ func handleSyncProviderAccountModels(w http.ResponseWriter, r *http.Request, pro writeJSON(w, http.StatusOK, map[string]any{"models": models, "synced": summary}) } -func handleRefreshProviderAccountModels(w http.ResponseWriter, r *http.Request, providers ProviderService) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - id, err := parsePositivePathID(r, "id") - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - - models, summary, err := providers.RefreshOAuthAccountModels(r.Context(), id) - if err != nil { - writeProviderAccountError(w, err) - return - } - response := map[string]any{"models": models, "synced": summary} - if status, statusErr := providers.OAuthModelCatalogStatus(r.Context(), id); statusErr == nil { - response["catalog"] = status - } - writeJSON(w, http.StatusOK, response) -} - func handleTestProviderAccountModel(w http.ResponseWriter, r *http.Request, providers ProviderService) { if providers == nil { writeError(w, http.StatusServiceUnavailable, "service_unavailable") @@ -2845,33 +2553,6 @@ func handleTestProviderAccountModel(w http.ResponseWriter, r *http.Request, prov writeJSON(w, http.StatusOK, map[string]provider.AccountModelTestResult{"result": result}) } -func handleRefreshProviderAccount(w http.ResponseWriter, r *http.Request, providers ProviderService) { - if providers == nil { - writeError(w, http.StatusServiceUnavailable, "service_unavailable") - return - } - id, err := parsePositivePathID(r, "id") - if err != nil { - writeError(w, http.StatusBadRequest, "bad_request") - return - } - - account, err := providers.RefreshAccount(r.Context(), id) - if err != nil { - if errors.Is(err, provider.ErrInvalidInput) { - writeError(w, http.StatusBadRequest, "invalid_input") - return - } - if errors.Is(err, provider.ErrNotConnected) { - writeError(w, http.StatusNotFound, "not_found") - return - } - writeError(w, http.StatusInternalServerError, "internal_error") - return - } - writeJSON(w, http.StatusOK, map[string]provider.Account{"account": account}) -} - func handleTestProviderAccount(w http.ResponseWriter, r *http.Request, providers ProviderService) { if providers == nil { writeError(w, http.StatusServiceUnavailable, "service_unavailable") @@ -2977,41 +2658,6 @@ func handleResetProviderAccountStatus(w http.ResponseWriter, r *http.Request, pr writeJSON(w, http.StatusOK, map[string]provider.Account{"account": account}) } -func writeManualOAuthCallbackPage(w http.ResponseWriter, r *http.Request) { - callbackURL := absoluteRequestURL(r) - escapedURL := html.EscapeString(callbackURL) - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.WriteHeader(http.StatusOK) - _, _ = io.WriteString(w, ` - - - - - N2API OAuth Callback - - - -
-

OAuth callback received

-

Copy this callback URL into the N2API admin provider form to complete the account connection.

- `+escapedURL+` -
- -`) -} - -func absoluteRequestURL(r *http.Request) string { - info := requestInfoForRequest(r) - if info.Host == "" { - return r.URL.RequestURI() - } - return info.Scheme + "://" + info.Host + r.URL.RequestURI() -} - func modelRoutingStatus(ctx context.Context, admins AdminService, providers ProviderService) (admin.ModelRoutingStatus, error) { defaultModel, err := admins.DefaultModel(ctx) if err != nil { @@ -3183,66 +2829,6 @@ func normalizedProviderAccountLoadFactor(value int) int { return value } -func decodeCallbackURL(w http.ResponseWriter, r *http.Request) (string, string, error) { - var req struct { - CallbackURL string `json:"callbackUrl"` - } - if err := decodeJSON(w, r, &req); err != nil { - return "", "", err - } - parsed, err := url.Parse(strings.TrimSpace(req.CallbackURL)) - if err != nil { - return "", "", err - } - code := strings.TrimSpace(parsed.Query().Get("code")) - state := strings.TrimSpace(parsed.Query().Get("state")) - if code == "" || state == "" { - return "", "", provider.ErrInvalidState - } - return code, state, nil -} - -func decodeConnectOptions(w http.ResponseWriter, r *http.Request) (provider.ConnectOptions, error) { - options := provider.ConnectOptions{ - RedirectAfter: "/", - Fingerprint: provider.Fingerprint{ - UserAgent: strings.TrimSpace(r.UserAgent()), - IP: requestInfoForRequest(r).ClientIP, - }, - } - if r.Body == nil { - return options, nil - } - if r.ContentLength == 0 { - return options, nil - } - var req struct { - RedirectAfter string `json:"redirectAfter"` - Name string `json:"name"` - Priority int `json:"priority"` - Enabled *bool `json:"enabled"` - TargetAccountID int64 `json:"targetAccountId"` - FingerprintProfileID *int64 `json:"fingerprintProfileId"` - Fingerprint string `json:"fingerprint"` - } - if err := decodeJSON(w, r, &req); err != nil { - return provider.ConnectOptions{}, err - } - if strings.TrimSpace(req.RedirectAfter) != "" { - options.RedirectAfter = strings.TrimSpace(req.RedirectAfter) - } - options.Name = strings.TrimSpace(req.Name) - options.Priority = req.Priority - options.Enabled = req.Enabled - options.TargetAccountID = req.TargetAccountID - options.FingerprintProfileID = req.FingerprintProfileID - if options.FingerprintProfileID != nil && *options.FingerprintProfileID == 0 { - options.FingerprintProfileID = nil - } - options.Fingerprint.Value = strings.TrimSpace(req.Fingerprint) - return options, nil -} - func parseServerOptions(options ...any) (http.Handler, fs.FS, ProviderAccountAutoTestStatusSource, buildinfo.Info) { var gateway http.Handler var webFS fs.FS @@ -3456,15 +3042,6 @@ func decodeJSONBytes(body []byte, value any) error { return nil } -func jsonFieldPresent(body []byte, field string) (bool, error) { - var raw map[string]json.RawMessage - if err := json.Unmarshal(body, &raw); err != nil { - return false, err - } - _, ok := raw[field] - return ok, nil -} - func writeJSON(w http.ResponseWriter, status int, value any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) diff --git a/backend/internal/httpapi/server_test.go b/backend/internal/httpapi/server_test.go index ea2fdd4a..bdebe96e 100644 --- a/backend/internal/httpapi/server_test.go +++ b/backend/internal/httpapi/server_test.go @@ -7,7 +7,6 @@ import ( "maps" "net/http" "net/http/httptest" - "net/netip" "reflect" "slices" "strings" @@ -195,9 +194,6 @@ type fakeAdminService struct { opsAccountTestsLimit int opsCostBreakdown admin.OpsCostBreakdown opsCostSince time.Time - fingerprintInput admin.FingerprintProfileInput - fingerprintID int64 - fingerprintErr error errorRuleInput admin.ErrorPassthroughRuleInput errorRuleID int64 errorRuleErr error @@ -227,9 +223,6 @@ type fakeSecretReveal struct { } type fakeProviderService struct { - status provider.Status - connect provider.ConnectResult - connectOptions provider.ConnectOptions createdAPIUpstream provider.APIUpstreamInput accounts []provider.Account accountModels map[int64][]provider.AccountModel @@ -255,15 +248,8 @@ type fakeProviderService struct { accountModelsErr error accountTestResultsErr error replaceModelsErr error - refreshErr error resetStatusErr error disconnectErr error - callbackErr error - callbackCode string - callbackState string - disconnected bool - refreshedAccountID int64 - refreshedAccountIDs []int64 testedAccountID int64 testedAccountIDs []int64 testResultsAccountID int64 @@ -277,13 +263,9 @@ type fakeProviderService struct { disconnectedAccountID int64 disconnectedAccountIDs []int64 - syncModelsResult []provider.AccountModel - syncModelsSummary provider.AccountModelSyncSummary - syncModelsErr error - oauthSyncAccountID int64 - oauthRefreshAccountID int64 - catalogStatus provider.OAuthModelCatalogStatus - catalogStatusErr error + syncModelsResult []provider.AccountModel + syncModelsSummary provider.AccountModelSyncSummary + syncModelsErr error } type fakeGatewayHandler struct { called bool @@ -845,29 +827,10 @@ func (s *fakeAdminService) DefaultModel(ctx context.Context) (string, error) { func newFakeProviderService() *fakeProviderService { return &fakeProviderService{ - status: provider.Status{ - Provider: "openai", - Configured: true, - Connected: true, - DisplayName: "Codex Account", - }, - connect: provider.ConnectResult{AuthorizationURL: "https://auth.example.test/authorize?state=oauth_state"}, accountModels: map[int64][]provider.AccountModel{}, } } -func (s *fakeProviderService) Status(_ context.Context) (provider.Status, error) { - return s.status, nil -} - -func (s *fakeProviderService) StartConnect(_ context.Context, options provider.ConnectOptions) (provider.ConnectResult, error) { - if !s.status.Configured { - return provider.ConnectResult{}, provider.ErrNotConfigured - } - s.connectOptions = options - return s.connect, nil -} - func (s *fakeProviderService) ListAccounts(_ context.Context) ([]provider.Account, error) { return s.accounts, nil } @@ -886,16 +849,15 @@ func (s *fakeProviderService) CreateAPIUpstreamAccount(_ context.Context, input loadFactor = 1 } account := provider.Account{ - ID: int64(len(s.accounts) + 1), - Provider: "openai", - AccountType: provider.AccountTypeAPIUpstream, - Name: strings.TrimSpace(input.Name), - DisplayName: strings.TrimSpace(input.Name), - Enabled: enabled, - Priority: input.Priority, - LoadFactor: loadFactor, - Status: provider.AccountStatusActive, - FingerprintProfileID: input.FingerprintProfileID, + ID: int64(len(s.accounts) + 1), + Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Name: strings.TrimSpace(input.Name), + DisplayName: strings.TrimSpace(input.Name), + Enabled: enabled, + Priority: input.Priority, + LoadFactor: loadFactor, + Status: provider.AccountStatusActive, Credential: provider.AccountCredential{ CredentialType: provider.CredentialTypeAPIKey, BaseURL: strings.TrimRight(strings.TrimSpace(input.BaseURL), "/"), @@ -1029,15 +991,6 @@ func (s *fakeProviderService) PreviewAccountSelectionInRoutingPoolForEndpoint(_ return s.selectionPreview, nil } -func (s *fakeProviderService) CompleteCallback(_ context.Context, code, state string) (provider.Account, error) { - s.callbackCode = code - s.callbackState = state - if s.callbackErr != nil { - return provider.Account{}, s.callbackErr - } - return provider.Account{ID: 7, Provider: "openai", DisplayName: "Codex Account"}, nil -} - func (s *fakeProviderService) UpdateAccount(_ context.Context, id int64, update provider.AccountUpdate) (provider.Account, error) { s.lastAccountUpdate = update s.accountUpdateIDs = append(s.accountUpdateIDs, id) @@ -1045,7 +998,7 @@ func (s *fakeProviderService) UpdateAccount(_ context.Context, id int64, update if s.updateErr != nil { return provider.Account{}, s.updateErr } - if update.Enabled == nil && update.Priority == nil && update.LoadFactor == nil && update.MaxConcurrentRequests == nil && !update.ClearStatus && update.Name == nil && update.APIUpstreamBaseURL == nil && update.APIUpstreamAPIKey == nil && !update.FingerprintProfileIDSet { + if update.Enabled == nil && update.Priority == nil && update.LoadFactor == nil && update.MaxConcurrentRequests == nil && !update.ClearStatus && update.Name == nil && update.APIUpstreamBaseURL == nil && update.APIUpstreamAPIKey == nil { return provider.Account{}, provider.ErrInvalidInput } if update.Priority != nil && *update.Priority < 0 { @@ -1081,9 +1034,6 @@ func (s *fakeProviderService) UpdateAccount(_ context.Context, id int64, update if update.APIUpstreamAPIKey != nil { account.Credential.EncryptedAPIKey = "updated-encrypted-api-key" } - if update.FingerprintProfileIDSet { - account.FingerprintProfileID = update.FingerprintProfileID - } if update.ClearStatus || update.APIUpstreamBaseURL != nil || update.APIUpstreamAPIKey != nil { account.Status = provider.AccountStatusActive account.StatusReason = "" @@ -1113,25 +1063,6 @@ func (s *fakeProviderService) ResetAccountStatus(_ context.Context, id int64) (p return account, nil } -func (s *fakeProviderService) RefreshAccount(_ context.Context, id int64) (provider.Account, error) { - if s.refreshErr != nil { - return provider.Account{}, s.refreshErr - } - for i, account := range s.accounts { - if account.ID == id { - now := time.Now() - account.LastRefreshAt = &now - account.Status = provider.AccountStatusActive - account.StatusReason = "" - s.accounts[i] = account - s.refreshedAccountID = id - s.refreshedAccountIDs = append(s.refreshedAccountIDs, id) - return account, nil - } - } - return provider.Account{}, provider.ErrNotConnected -} - func (s *fakeProviderService) TestAccount(_ context.Context, id int64) (provider.Account, error) { for i, account := range s.accounts { if account.ID == id { @@ -1199,11 +1130,6 @@ func (s *fakeProviderService) DisconnectAccount(_ context.Context, id int64) err return provider.ErrNotConnected } -func (s *fakeProviderService) Disconnect(_ context.Context) error { - s.disconnected = true - return nil -} - func (s *fakeProviderService) SyncUpstreamAccountModels(_ context.Context, accountID int64) ([]provider.AccountModel, provider.AccountModelSyncSummary, error) { if s.syncModelsErr != nil { return nil, provider.AccountModelSyncSummary{}, s.syncModelsErr @@ -1211,29 +1137,6 @@ func (s *fakeProviderService) SyncUpstreamAccountModels(_ context.Context, accou return append([]provider.AccountModel(nil), s.syncModelsResult...), s.syncModelsSummary, nil } -func (s *fakeProviderService) SyncOAuthAccountModels(_ context.Context, accountID int64) ([]provider.AccountModel, provider.AccountModelSyncSummary, error) { - s.oauthSyncAccountID = accountID - if s.syncModelsErr != nil { - return nil, provider.AccountModelSyncSummary{}, s.syncModelsErr - } - return append([]provider.AccountModel(nil), s.syncModelsResult...), s.syncModelsSummary, nil -} - -func (s *fakeProviderService) RefreshOAuthAccountModels(_ context.Context, accountID int64) ([]provider.AccountModel, provider.AccountModelSyncSummary, error) { - s.oauthRefreshAccountID = accountID - if s.syncModelsErr != nil { - return nil, provider.AccountModelSyncSummary{}, s.syncModelsErr - } - return append([]provider.AccountModel(nil), s.syncModelsResult...), s.syncModelsSummary, nil -} - -func (s *fakeProviderService) OAuthModelCatalogStatus(_ context.Context, _ int64) (provider.OAuthModelCatalogStatus, error) { - if s.catalogStatusErr != nil { - return provider.OAuthModelCatalogStatus{}, s.catalogStatusErr - } - return s.catalogStatus, nil -} - func TestLivezReturnsOK(t *testing.T) { server := NewServer(config.Config{}, staticHealth{err: nil}, nil, nil) recorder := httptest.NewRecorder() @@ -3218,200 +3121,6 @@ func TestUpdateAPIKeyRoutingPoolEndpoint(t *testing.T) { } } -func TestProviderStatusRequiresSessionAndReturnsStatus(t *testing.T) { - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/admin/providers/openai", nil)) - - if recorder.Code != http.StatusUnauthorized { - t.Fatalf("status = %d, want 401", recorder.Code) - } - - req := httptest.NewRequest(http.MethodGet, "/api/admin/providers/openai", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder = httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d, want 200", recorder.Code) - } - var body provider.Status - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if !body.Configured || !body.Connected || body.DisplayName != "Codex Account" { - t.Fatalf("provider status = %+v", body) - } -} - -func TestUnifiedProviderAccountCodexOAuthStatusReturnsStatus(t *testing.T) { - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) - req := httptest.NewRequest(http.MethodGet, "/api/admin/provider-accounts/codex-oauth/status", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - var body provider.Status - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if !body.Configured || !body.Connected || body.DisplayName != "Codex Account" { - t.Fatalf("provider status = %+v", body) - } -} - -func TestProviderConnectReturnsAuthorizationURL(t *testing.T) { - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/connect", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d, want 200", recorder.Code) - } - var body struct { - AuthorizationURL string `json:"authorizationUrl"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body.AuthorizationURL == "" { - t.Fatal("authorizationUrl is empty") - } -} - -func TestProviderConnectAcceptsAccountOptionsAndFingerprint(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{TrustedProxyCIDRs: []netip.Prefix{ - netip.MustParsePrefix("192.0.2.0/24"), - netip.MustParsePrefix("198.51.100.0/24"), - }}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/connect", strings.NewReader(`{"name":"Work Codex","priority":7,"enabled":false,"targetAccountId":42,"fingerprint":"browser-fp","fingerprintProfileId":9}`)) - req.Header.Set("User-Agent", "Mozilla/5.0") - req.Header.Set("X-Forwarded-For", "203.0.113.10, 198.51.100.2") - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String()) - } - if providers.connectOptions.RedirectAfter != "/" || - providers.connectOptions.Name != "Work Codex" || - providers.connectOptions.Priority != 7 || - providers.connectOptions.Enabled == nil || - *providers.connectOptions.Enabled || - providers.connectOptions.TargetAccountID != 42 || - providers.connectOptions.FingerprintProfileID == nil || - *providers.connectOptions.FingerprintProfileID != 9 { - t.Fatalf("connectOptions = %+v", providers.connectOptions) - } - if providers.connectOptions.Fingerprint.Value != "browser-fp" || - providers.connectOptions.Fingerprint.UserAgent != "Mozilla/5.0" || - providers.connectOptions.Fingerprint.IP != "203.0.113.10" { - t.Fatalf("fingerprint = %+v", providers.connectOptions.Fingerprint) - } -} - -func TestUnifiedProviderAccountCodexOAuthConnectDelegatesToProviderConnect(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/codex-oauth/connect", strings.NewReader(`{"name":"Work Codex","priority":7,"enabled":false,"targetAccountId":42,"fingerprint":"browser-fp","fingerprintProfileId":9}`)) - req.Header.Set("User-Agent", "Mozilla/5.0") - req.Header.Set("X-Forwarded-For", "203.0.113.10, 198.51.100.2") - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - var body struct { - AuthorizationURL string `json:"authorizationUrl"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body.AuthorizationURL == "" { - t.Fatal("authorizationUrl is empty") - } - if providers.connectOptions.Name != "Work Codex" || - providers.connectOptions.Priority != 7 || - providers.connectOptions.Enabled == nil || - *providers.connectOptions.Enabled || - providers.connectOptions.TargetAccountID != 42 || - providers.connectOptions.FingerprintProfileID == nil || - *providers.connectOptions.FingerprintProfileID != 9 { - t.Fatalf("connectOptions = %+v", providers.connectOptions) - } -} -func TestAdminProviderConnectTreatsZeroFingerprintProfileAsDefaultSentinel(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/connect", strings.NewReader("{\"name\":\"Zero FP\",\"fingerprintProfileId\":0}")) - req.Header.Set("User-Agent", "Mozilla/5.0") - req.Header.Set("X-Forwarded-For", "203.0.113.10, 198.51.100.2") - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String()) - } - if providers.connectOptions.FingerprintProfileID != nil { - t.Fatalf("fingerprintProfileId = %v, want nil (resolved to default sentinel)", *providers.connectOptions.FingerprintProfileID) - } -} -func TestProviderConnectReturnsConflictWhenUnconfigured(t *testing.T) { - providers := newFakeProviderService() - providers.status.Configured = false - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/connect", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusConflict { - t.Fatalf("status = %d, want 409", recorder.Code) - } - var body map[string]string - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body["error"] != "provider_not_configured" { - t.Fatalf("error = %q, want provider_not_configured", body["error"]) - } -} - -func TestProviderDisconnectReturnsNoContent(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/disconnect", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusNoContent { - t.Fatalf("status = %d, want 204", recorder.Code) - } - if !providers.disconnected { - t.Fatal("provider service was not disconnected") - } -} - func TestAdminProviderAccountsRequireSession(t *testing.T) { server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) req := httptest.NewRequest(http.MethodGet, "/api/admin/providers/openai/accounts", nil) @@ -3435,7 +3144,6 @@ func TestAdminProviderAccountsEndpointsRequireSession(t *testing.T) { {name: "list", method: http.MethodGet, path: "/api/admin/provider-accounts"}, {name: "create api upstream", method: http.MethodPost, path: "/api/admin/provider-accounts/api-upstream", body: `{"name":"Upstream","baseUrl":"https://upstream.example.test","apiKey":"secret"}`}, {name: "test all", method: http.MethodPost, path: "/api/admin/provider-accounts/test"}, - {name: "connect codex oauth", method: http.MethodPost, path: "/api/admin/provider-accounts/codex-oauth/connect", body: `{"name":"Work Codex"}`}, {name: "patch", method: http.MethodPatch, path: "/api/admin/provider-accounts/7", body: `{"enabled":true}`}, {name: "disconnect", method: http.MethodPost, path: "/api/admin/provider-accounts/7/disconnect"}, {name: "test", method: http.MethodPost, path: "/api/admin/provider-accounts/7/test"}, @@ -3470,7 +3178,6 @@ func TestAdminProviderAccountsEndpointsRequireProviderService(t *testing.T) { {name: "list", method: http.MethodGet, path: "/api/admin/provider-accounts"}, {name: "create api upstream", method: http.MethodPost, path: "/api/admin/provider-accounts/api-upstream", body: `{"name":"Upstream","baseUrl":"https://upstream.example.test","apiKey":"secret"}`}, {name: "test all", method: http.MethodPost, path: "/api/admin/provider-accounts/test"}, - {name: "connect codex oauth", method: http.MethodPost, path: "/api/admin/provider-accounts/codex-oauth/connect", body: `{"name":"Work Codex"}`}, {name: "patch", method: http.MethodPatch, path: "/api/admin/provider-accounts/7", body: `{"enabled":true}`}, {name: "disconnect", method: http.MethodPost, path: "/api/admin/provider-accounts/7/disconnect"}, {name: "test", method: http.MethodPost, path: "/api/admin/provider-accounts/7/test"}, @@ -3505,7 +3212,7 @@ func TestAdminProviderAccountsEndpointsRequireProviderService(t *testing.T) { func TestCreateAPIUpstreamAccount(t *testing.T) { providers := newFakeProviderService() server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/api-upstream", strings.NewReader(`{"name":" Upstream ","baseUrl":"https://upstream.example.test/v1/","apiKey":" secret ","proxyUrl":" http://proxy-user:proxy-pass@proxy.example.test:8080 ","enabled":true,"priority":8,"fingerprintProfileId":9,"models":[" gpt-5 ","gpt-4.1"]}`)) + req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/api-upstream", strings.NewReader(`{"name":" Upstream ","baseUrl":"https://upstream.example.test/v1/","apiKey":" secret ","proxyUrl":" http://proxy-user:proxy-pass@proxy.example.test:8080 ","enabled":true,"priority":8,"models":[" gpt-5 ","gpt-4.1"]}`)) req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) recorder := httptest.NewRecorder() @@ -3523,9 +3230,6 @@ func TestCreateAPIUpstreamAccount(t *testing.T) { if providers.createdAPIUpstream.Enabled == nil || !*providers.createdAPIUpstream.Enabled || providers.createdAPIUpstream.Priority != 8 || len(providers.createdAPIUpstream.Models) != 2 { t.Fatalf("created input scheduling/models = %+v", providers.createdAPIUpstream) } - if providers.createdAPIUpstream.FingerprintProfileID == nil || *providers.createdAPIUpstream.FingerprintProfileID != 9 { - t.Fatalf("created input fingerprint profile = %+v, want 9", providers.createdAPIUpstream.FingerprintProfileID) - } var body struct { Account provider.Account `json:"account"` } @@ -3615,7 +3319,6 @@ func TestAdminProviderAccountMutationsRequireSession(t *testing.T) { body string }{ {name: "patch", method: http.MethodPatch, path: "/api/admin/providers/openai/accounts/7", body: `{"enabled":true}`}, - {name: "refresh", method: http.MethodPost, path: "/api/admin/providers/openai/accounts/7/refresh"}, {name: "disconnect", method: http.MethodPost, path: "/api/admin/providers/openai/accounts/7/disconnect"}, } { t.Run(tc.name, func(t *testing.T) { @@ -3654,8 +3357,8 @@ func TestAdminProviderAccountsIncludeConcurrencyState(t *testing.T) { admins.gatewaySettings.MaxConcurrentRequestsPerAccount = 5 providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Busy", Enabled: true, Priority: 10, MaxConcurrentRequests: 3}, - {ID: 8, Provider: "openai", DisplayName: "Inherited", Enabled: true, Priority: 10}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Busy", Enabled: true, Priority: 10, MaxConcurrentRequests: 3}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Inherited", Enabled: true, Priority: 10}, } gateway := &fakeGatewayHandler{accountConcurrency: map[int64]int{7: 2, 8: 1}} server := NewServer(config.Config{}, staticHealth{}, admins, providers, gateway) @@ -3719,39 +3422,11 @@ func TestAdminCanUpdateUnifiedProviderAccount(t *testing.T) { } } -func TestAdminCanClearProviderAccountFingerprintProfile(t *testing.T) { - profileID := int64(42) - providers := newFakeProviderService() - providers.accounts = []provider.Account{{ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1, FingerprintProfileID: &profileID}} - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPatch, "/api/admin/provider-accounts/7", strings.NewReader(`{"fingerprintProfileId":null}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String()) - } - var body struct { - Account provider.Account `json:"account"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body.Account.FingerprintProfileID != nil { - t.Fatalf("fingerprintProfileId = %v, want cleared nil", *body.Account.FingerprintProfileID) - } - if !providers.lastAccountUpdate.FingerprintProfileIDSet || providers.lastAccountUpdate.FingerprintProfileID != nil { - t.Fatalf("fingerprint update = set %v value %+v, want explicit clear", providers.lastAccountUpdate.FingerprintProfileIDSet, providers.lastAccountUpdate.FingerprintProfileID) - } -} - func TestAdminCanBulkDisableUnifiedProviderAccounts(t *testing.T) { providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true, Priority: 20, LoadFactor: 1}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true, Priority: 20, LoadFactor: 1}, } server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-update", strings.NewReader(`{"accountIds":[7,8,7],"enabled":false}`)) @@ -3785,8 +3460,8 @@ func TestAdminCanBulkDisableUnifiedProviderAccounts(t *testing.T) { func TestAdminCanBulkUpdateUnifiedProviderAccountScheduling(t *testing.T) { providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true, Priority: 20, LoadFactor: 1}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true, Priority: 20, LoadFactor: 1}, } server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-update", strings.NewReader(`{"accountIds":[7,8,7],"priority":2,"loadFactor":5,"maxConcurrentRequests":3}`)) @@ -3857,8 +3532,8 @@ func TestAdminBulkProviderAccountUpdateValidatesInput(t *testing.T) { func TestAdminCanTestSelectedUnifiedProviderAccounts(t *testing.T) { providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1, Status: provider.AccountStatusCircuitOpen, LastError: "paused"}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true, Priority: 20, LoadFactor: 1, Status: provider.AccountStatusRateLimited, LastError: "rate limited"}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true, Priority: 10, LoadFactor: 1, Status: provider.AccountStatusCircuitOpen, LastError: "paused"}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true, Priority: 20, LoadFactor: 1, Status: provider.AccountStatusRateLimited, LastError: "rate limited"}, } server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-test", strings.NewReader(`{"accountIds":[7,8,7]}`)) @@ -3917,8 +3592,8 @@ func TestAdminBulkProviderAccountTestValidatesInput(t *testing.T) { func TestAdminCanBulkPauseUnifiedProviderAccountScheduling(t *testing.T) { providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Status: provider.AccountStatusActive}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true, Status: provider.AccountStatusActive}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true, Status: provider.AccountStatusActive}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true, Status: provider.AccountStatusActive}, } server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-pause", strings.NewReader(`{"accountIds":[7,8,7],"durationSeconds":600}`)) @@ -3982,8 +3657,8 @@ func TestAdminCanBulkResetUnifiedProviderAccountStatus(t *testing.T) { future := time.Now().Add(time.Hour) providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &future, CircuitOpenUntil: &future, FailureCount: 2}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true, Status: provider.AccountStatusCircuitOpen, RateLimitedUntil: &future, CircuitOpenUntil: &future, FailureCount: 3}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &future, CircuitOpenUntil: &future, FailureCount: 2}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true, Status: provider.AccountStatusCircuitOpen, RateLimitedUntil: &future, CircuitOpenUntil: &future, FailureCount: 3}, } server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-reset-status", strings.NewReader(`{"accountIds":[7,8,7]}`)) @@ -4039,74 +3714,11 @@ func TestAdminBulkProviderAccountResetStatusValidatesInput(t *testing.T) { } } -func TestAdminCanBulkRefreshUnifiedProviderAccounts(t *testing.T) { - providers := newFakeProviderService() - providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Status: provider.AccountStatusCircuitOpen}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true, Status: provider.AccountStatusExpired}, - } - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-refresh", strings.NewReader(`{"accountIds":[7,8,7]}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if !reflect.DeepEqual(providers.refreshedAccountIDs, []int64{7, 8}) { - t.Fatalf("refresh ids = %+v, want [7 8]", providers.refreshedAccountIDs) - } - var body struct { - Accounts []provider.Account `json:"accounts"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if len(body.Accounts) != 2 || body.Accounts[0].ID != 7 || body.Accounts[1].ID != 8 { - t.Fatalf("accounts = %+v, want entries for accounts 7 and 8", body.Accounts) - } - if body.Accounts[0].Status != provider.AccountStatusActive || body.Accounts[0].LastRefreshAt == nil || body.Accounts[1].LastRefreshAt == nil { - t.Fatalf("accounts = %+v, want refreshed active accounts", body.Accounts) - } -} - -func TestAdminBulkProviderAccountRefreshValidatesInput(t *testing.T) { - cases := []struct { - name string - body string - }{ - {name: "empty ids", body: `{"accountIds":[]}`}, - {name: "bad id", body: `{"accountIds":[0]}`}, - {name: "too many ids", body: `{"accountIds":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101]}`}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - providers := newFakeProviderService() - providers.accounts = []provider.Account{{ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true}} - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-refresh", strings.NewReader(tc.body)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadRequest { - t.Fatalf("status = %d body=%s, want 400", recorder.Code, recorder.Body.String()) - } - if len(providers.refreshedAccountIDs) != 0 { - t.Fatalf("refresh ids = %+v, want no refreshes", providers.refreshedAccountIDs) - } - }) - } -} - func TestAdminCanBulkDisconnectUnifiedProviderAccounts(t *testing.T) { providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true}, } server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/bulk-disconnect", strings.NewReader(`{"accountIds":[7,8,7]}`)) @@ -4159,8 +3771,8 @@ func TestAdminBulkProviderAccountDisconnectValidatesInput(t *testing.T) { func TestAdminCanBulkReplaceUnifiedProviderAccountModels(t *testing.T) { providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true}, - {ID: 8, Provider: "openai", DisplayName: "Account B", Enabled: true}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account B", Enabled: true}, } providers.accountModels[7] = []provider.AccountModel{{AccountID: 7, Provider: "openai", Model: "old-model", Enabled: true}} providers.accountModels[8] = []provider.AccountModel{{AccountID: 8, Provider: "openai", Model: "old-model", Enabled: true}} @@ -4435,77 +4047,23 @@ func TestAdminUpdateProviderAccountMapsErrors(t *testing.T) { } } -func TestAdminCanRefreshProviderAccount(t *testing.T) { +func TestAdminCanTestUnifiedProviderAccount(t *testing.T) { + future := time.Now().Add(time.Hour) providers := newFakeProviderService() - providers.accounts = []provider.Account{{ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Priority: 10, Status: provider.AccountStatusCircuitOpen}} + providers.accounts = []provider.Account{{ + ID: 7, + Provider: "openai", + DisplayName: "Account A", + Enabled: true, + Priority: 10, + Status: provider.AccountStatusCircuitOpen, + StatusReason: "previous failure", + LastError: "previous failure", + CircuitOpenUntil: &future, + FailureCount: 2, + }} server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/accounts/7/refresh", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String()) - } - if providers.refreshedAccountID != 7 { - t.Fatalf("refreshedAccountID = %d, want 7", providers.refreshedAccountID) - } - var body struct { - Account provider.Account `json:"account"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body.Account.ID != 7 || body.Account.Status != provider.AccountStatusActive || body.Account.LastRefreshAt == nil { - t.Fatalf("account = %+v, want refreshed active account 7", body.Account) - } -} - -func TestAdminCanRefreshUnifiedProviderAccount(t *testing.T) { - providers := newFakeProviderService() - providers.accounts = []provider.Account{{ID: 7, Provider: "openai", DisplayName: "Account A", Enabled: true, Priority: 10, Status: provider.AccountStatusCircuitOpen}} - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/7/refresh", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s", recorder.Code, recorder.Body.String()) - } - if providers.refreshedAccountID != 7 { - t.Fatalf("refreshedAccountID = %d, want 7", providers.refreshedAccountID) - } - var body struct { - Account provider.Account `json:"account"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body.Account.ID != 7 || body.Account.Status != provider.AccountStatusActive || body.Account.LastRefreshAt == nil { - t.Fatalf("account = %+v, want refreshed active account 7", body.Account) - } -} - -func TestAdminCanTestUnifiedProviderAccount(t *testing.T) { - future := time.Now().Add(time.Hour) - providers := newFakeProviderService() - providers.accounts = []provider.Account{{ - ID: 7, - Provider: "openai", - DisplayName: "Account A", - Enabled: true, - Priority: 10, - Status: provider.AccountStatusCircuitOpen, - StatusReason: "previous failure", - LastError: "previous failure", - CircuitOpenUntil: &future, - FailureCount: 2, - }} - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/7/test", nil) + req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/7/test", nil) req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) recorder := httptest.NewRecorder() @@ -4777,7 +4335,7 @@ func TestAdminCanDeleteUnifiedProviderAccount(t *testing.T) { func TestAdminCanDisconnectUnifiedProviderAccountAction(t *testing.T) { providers := newFakeProviderService() - providers.accounts = []provider.Account{{ID: 7, Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, DisplayName: "Account A", Enabled: true, Priority: 10}} + providers.accounts = []provider.Account{{ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Account A", Enabled: true, Priority: 10}} server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/7/disconnect", nil) req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) @@ -5016,86 +4574,6 @@ func TestSyncProviderAccountModelsReturnsModelsAndSummary(t *testing.T) { } } -func TestRefreshOAuthProviderAccountModelsReturnsCatalogStatus(t *testing.T) { - providers := newFakeProviderService() - now := time.Now().UTC() - cacheHit := false - providers.syncModelsResult = []provider.AccountModel{ - {ID: 1, AccountID: 7, Provider: "openai", Model: "gpt-5.6-sol", Enabled: true, Source: provider.AccountModelSourceOAuthCatalog}, - } - providers.syncModelsSummary = provider.AccountModelSyncSummary{Total: 1, New: 1} - providers.catalogStatus = provider.OAuthModelCatalogStatus{ - LastAttemptAt: &now, - LastSuccessAt: &now, - Source: "upstream", - CacheHit: &cacheHit, - } - - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/7/models/refresh", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if providers.oauthRefreshAccountID != 7 { - t.Fatalf("refresh account ID = %d, want 7", providers.oauthRefreshAccountID) - } - var body struct { - Models []provider.AccountModel `json:"models"` - Synced provider.AccountModelSyncSummary `json:"synced"` - Catalog provider.OAuthModelCatalogStatus `json:"catalog"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if len(body.Models) != 1 || body.Models[0].Model != "gpt-5.6-sol" || body.Synced.New != 1 { - t.Fatalf("body = %+v, want refreshed model and summary", body) - } - if body.Catalog.Source != "upstream" || body.Catalog.CacheHit == nil || *body.Catalog.CacheHit { - t.Fatalf("catalog = %+v, want upstream cache miss status", body.Catalog) - } -} - -func TestRefreshOAuthProviderAccountModelsMapsErrors(t *testing.T) { - for _, tc := range []struct { - name string - err error - want int - code string - }{ - {name: "invalid input", err: provider.ErrInvalidInput, want: http.StatusBadRequest, code: "invalid_input"}, - {name: "not found", err: provider.ErrNotConnected, want: http.StatusNotFound, code: "not_found"}, - {name: "busy", err: provider.ErrOAuthModelCatalogBusy, want: http.StatusTooManyRequests, code: "catalog_refresh_busy"}, - {name: "superseded", err: provider.ErrOAuthModelCatalogStale, want: http.StatusConflict, code: "catalog_refresh_superseded"}, - } { - t.Run(tc.name, func(t *testing.T) { - providers := newFakeProviderService() - providers.syncModelsErr = tc.err - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/7/models/refresh", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != tc.want { - t.Fatalf("status = %d, want %d", recorder.Code, tc.want) - } - var body map[string]string - if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil { - t.Fatalf("decode body: %v", err) - } - if body["error"] != tc.code { - t.Fatalf("error = %q, want %q", body["error"], tc.code) - } - }) - } -} - func TestSyncProviderAccountModelsMapsProviderErrors(t *testing.T) { for _, tc := range []struct { name string @@ -5278,177 +4756,6 @@ func TestAdminDeleteUnifiedProviderAccountMapsErrors(t *testing.T) { } } -func TestProviderCallbackDoesNotConsumeManualCallback(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "http://localhost:3000/oauth/openai/callback?code=abc&state=state", nil)) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d, want 200", recorder.Code) - } - if providers.callbackCode != "" || providers.callbackState != "" { - t.Fatalf("callback was called with code %q state %q", providers.callbackCode, providers.callbackState) - } - body := recorder.Body.String() - if !strings.Contains(body, "code=abc") || !strings.Contains(body, "state=state") { - t.Fatalf("body did not include callback values: %s", body) - } - if got := recorder.Header().Get("Content-Type"); !strings.HasPrefix(got, "text/html") { - t.Fatalf("Content-Type = %q, want text/html", got) - } -} - -func TestManualOAuthCallbackUsesTrustedRequestInfo(t *testing.T) { - tests := []struct { - name string - cfg config.Config - target string - remoteAddr string - host string - headers http.Header - want string - reject string - }{ - { - name: "untrusted forwarded metadata ignored", - cfg: config.Config{PublicURL: "http://canonical.example:3000"}, - target: "/oauth/openai/callback?code=abc&state=state", - remoteAddr: "192.0.2.10:1234", - host: "direct.example:3000", - headers: http.Header{ - "X-Forwarded-Proto": {"https"}, - "X-Forwarded-Host": {"attacker.example"}, - }, - want: "http://canonical.example:3000/oauth/openai/callback?code=abc", - reject: "attacker.example", - }, - { - name: "trusted forwarded metadata accepted", - cfg: config.Config{TrustedProxyCIDRs: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")}}, - target: "/oauth/openai/callback?code=abc&state=state", - remoteAddr: "10.0.0.2:1234", - host: "internal.example:3000", - headers: http.Header{ - "X-Forwarded-Proto": {"https"}, - "X-Forwarded-Host": {"public.example"}, - }, - want: "https://public.example/oauth/openai/callback?code=abc", - }, - { - name: "absolute form cannot bypass direct host", - cfg: config.Config{PublicURL: "https://canonical.example"}, - target: "http://attacker.example/oauth/openai/callback?code=abc&state=state", - remoteAddr: "192.0.2.10:1234", - host: "direct.example", - want: "https://canonical.example/oauth/openai/callback?code=abc", - reject: "attacker.example", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - server := NewServer(tt.cfg, staticHealth{}, newFakeAdminService(), newFakeProviderService()) - req := httptest.NewRequest(http.MethodGet, tt.target, nil) - req.RemoteAddr = tt.remoteAddr - req.Host = tt.host - req.Header = tt.headers - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - body := recorder.Body.String() - if recorder.Code != http.StatusOK || !strings.Contains(body, tt.want) { - t.Fatalf("status = %d body = %s, want URL containing %q", recorder.Code, body, tt.want) - } - if tt.reject != "" && strings.Contains(body, tt.reject) { - t.Fatalf("body contains rejected host %q: %s", tt.reject, body) - } - }) - } -} - -func TestProviderManualCallbackCompletesFromCallbackURL(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/callback", strings.NewReader(`{"callbackUrl":"http://localhost:3000/oauth/openai/callback?code=abc&state=oauth_state"}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if providers.callbackCode != "abc" || providers.callbackState != "oauth_state" { - t.Fatalf("callback args = code %q state %q, want parsed callback URL values", providers.callbackCode, providers.callbackState) - } -} - -func TestUnifiedProviderAccountCodexOAuthCallbackCompletesFromCallbackURL(t *testing.T) { - providers := newFakeProviderService() - providers.syncModelsResult = []provider.AccountModel{{AccountID: 7, Model: "gpt-5.6-sol", Enabled: true, Source: provider.AccountModelSourceOAuthCatalog}} - providers.syncModelsSummary = provider.AccountModelSyncSummary{Total: 1, New: 1} - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/codex-oauth/callback", strings.NewReader(`{"callbackUrl":"http://localhost:3000/oauth/openai/callback?code=abc&state=oauth_state"}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if providers.callbackCode != "abc" || providers.callbackState != "oauth_state" { - t.Fatalf("callback args = code %q state %q, want parsed callback URL values", providers.callbackCode, providers.callbackState) - } - if providers.oauthSyncAccountID != 7 { - t.Fatalf("OAuth model sync account ID = %d, want 7", providers.oauthSyncAccountID) - } - if !strings.Contains(recorder.Body.String(), `"source":"oauth_catalog"`) { - t.Fatalf("body = %s, want synced OAuth model", recorder.Body.String()) - } -} - -func TestUnifiedProviderAccountCodexOAuthCallbackReportsModelSyncFailure(t *testing.T) { - providers := newFakeProviderService() - providers.syncModelsErr = errors.New("catalog unavailable") - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/provider-accounts/codex-oauth/callback", strings.NewReader(`{"callbackUrl":"http://localhost:3000/oauth/openai/callback?code=abc&state=oauth_state"}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadGateway { - t.Fatalf("status = %d body=%s, want 502", recorder.Code, recorder.Body.String()) - } - if !strings.Contains(recorder.Body.String(), `"error":"oauth_model_sync_failed"`) { - t.Fatalf("body = %s, want explicit OAuth model sync error", recorder.Body.String()) - } - if providers.oauthSyncAccountID != 7 { - t.Fatalf("OAuth model sync account ID = %d, want 7", providers.oauthSyncAccountID) - } -} - -func TestProviderManualCallbackRejectsMissingCallbackValues(t *testing.T) { - providers := newFakeProviderService() - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), providers) - req := httptest.NewRequest(http.MethodPost, "/api/admin/providers/openai/callback", strings.NewReader(`{"callbackUrl":"http://localhost:3000/oauth/openai/callback?code=abc"}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadRequest { - t.Fatalf("status = %d, want 400", recorder.Code) - } - if providers.callbackCode != "" || providers.callbackState != "" { - t.Fatalf("callback was called with code %q state %q", providers.callbackCode, providers.callbackState) - } -} - func TestListRequestLogsRequiresSessionAndReturnsLogs(t *testing.T) { server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) recorder := httptest.NewRecorder() @@ -5699,106 +5006,6 @@ func TestErrorPassthroughRulesRouteIsRegisteredImmediately(t *testing.T) { } } -func TestFingerprintProfilesCRUDRequiresSessionAndMapsInputs(t *testing.T) { - server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/admin/fingerprint-profiles", nil)) - - if recorder.Code != http.StatusUnauthorized { - t.Fatalf("status = %d, want 401", recorder.Code) - } - - admins := newFakeAdminService() - server = NewServer(config.Config{}, staticHealth{}, admins, newFakeProviderService()) - req := httptest.NewRequest(http.MethodGet, "/api/admin/fingerprint-profiles", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder = httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("list status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - var listBody struct { - Profiles []admin.FingerprintProfile `json:"profiles"` - } - if err := json.Unmarshal(recorder.Body.Bytes(), &listBody); err != nil { - t.Fatalf("decode list body: %v", err) - } - if len(listBody.Profiles) != 1 || listBody.Profiles[0].TLSFingerprint != "chrome" || listBody.Profiles[0].Headers["X-Test"] != "1" { - t.Fatalf("profiles = %+v, want seeded fingerprint profile", listBody.Profiles) - } - - payload := `{"name":"Firefox","description":"desktop","userAgent":"Mozilla/5.0","tlsFingerprint":"firefox","headers":{"X-FP":"yes"},"enabled":true}` - req = httptest.NewRequest(http.MethodPost, "/api/admin/fingerprint-profiles", strings.NewReader(payload)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder = httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusCreated { - t.Fatalf("create status = %d body=%s, want 201", recorder.Code, recorder.Body.String()) - } - if admins.fingerprintInput.Name != "Firefox" || admins.fingerprintInput.TLSFingerprint != "firefox" || admins.fingerprintInput.Headers["X-FP"] != "yes" { - t.Fatalf("fingerprint create input = %+v", admins.fingerprintInput) - } - - req = httptest.NewRequest(http.MethodPatch, "/api/admin/fingerprint-profiles/8", strings.NewReader(payload)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder = httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusOK { - t.Fatalf("update status = %d body=%s, want 200", recorder.Code, recorder.Body.String()) - } - if admins.fingerprintID != 8 || admins.fingerprintInput.Name != "Firefox" { - t.Fatalf("fingerprint update id/input = %d/%+v, want 8/Firefox", admins.fingerprintID, admins.fingerprintInput) - } - - req = httptest.NewRequest(http.MethodDelete, "/api/admin/fingerprint-profiles/8", nil) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder = httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusNoContent { - t.Fatalf("delete status = %d body=%s, want 204", recorder.Code, recorder.Body.String()) - } - if admins.fingerprintID != 8 { - t.Fatalf("fingerprint delete id = %d, want 8", admins.fingerprintID) - } -} - -func TestFingerprintProfilesCRUDMapsValidationAndNotFoundErrors(t *testing.T) { - admins := newFakeAdminService() - admins.fingerprintErr = admin.ErrInvalidInput - server := NewServer(config.Config{}, staticHealth{}, admins, newFakeProviderService()) - req := httptest.NewRequest(http.MethodPost, "/api/admin/fingerprint-profiles", strings.NewReader(`{"name":""}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder := httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusBadRequest { - t.Fatalf("invalid create status = %d body=%s, want 400", recorder.Code, recorder.Body.String()) - } - - admins = newFakeAdminService() - admins.fingerprintErr = admin.ErrNotFound - server = NewServer(config.Config{}, staticHealth{}, admins, newFakeProviderService()) - req = httptest.NewRequest(http.MethodPatch, "/api/admin/fingerprint-profiles/404", strings.NewReader(`{"name":"Missing"}`)) - req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) - recorder = httptest.NewRecorder() - - server.ServeHTTP(recorder, req) - - if recorder.Code != http.StatusNotFound { - t.Fatalf("not found update status = %d body=%s, want 404", recorder.Code, recorder.Body.String()) - } -} - func TestErrorPassthroughRulesCRUDMapsInputsAndErrors(t *testing.T) { admins := newFakeAdminService() server := NewServer(config.Config{}, staticHealth{}, admins, newFakeProviderService()) @@ -6562,8 +5769,8 @@ func TestModelRoutingReturnsStatus(t *testing.T) { admins.modelSettings = admin.ModelSettings{DefaultModel: "gpt-5"} providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", Enabled: true}, - {ID: 8, Provider: "openai", Enabled: false}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: true}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: false}, } admins.routingPools[0].Accounts = []admin.RoutingPoolAccount{{AccountID: 7, Priority: 0}} admins.routingPools[0].AccountIDs = []int64{7} @@ -6620,11 +5827,11 @@ func TestModelRoutingStatusEnabledCountUsesSchedulableAccountRules(t *testing.T) past := now.Add(-time.Hour) providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", Enabled: true, Status: provider.AccountStatusExpired}, - {ID: 8, Provider: "openai", Enabled: true, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &future}, - {ID: 9, Provider: "openai", Enabled: true, Status: provider.AccountStatusCircuitOpen, CircuitOpenUntil: &future}, - {ID: 10, Provider: "openai", Enabled: true, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &past}, - {ID: 11, Provider: "openai", Enabled: true, Status: provider.AccountStatusCircuitOpen, CircuitOpenUntil: &past}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: true, Status: provider.AccountStatusExpired}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: true, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &future}, + {ID: 9, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: true, Status: provider.AccountStatusCircuitOpen, CircuitOpenUntil: &future}, + {ID: 10, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: true, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &past}, + {ID: 11, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, Enabled: true, Status: provider.AccountStatusCircuitOpen, CircuitOpenUntil: &past}, } for _, account := range providers.accounts { providers.accountModels[account.ID] = []provider.AccountModel{ @@ -6660,10 +5867,10 @@ func TestModelRoutingStatusIncludesUnschedulableAccountReasons(t *testing.T) { future := now.Add(time.Hour) providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Disabled", Enabled: false, Status: provider.AccountStatusActive}, - {ID: 8, Provider: "openai", DisplayName: "Expired", Enabled: true, Status: provider.AccountStatusExpired}, - {ID: 9, Provider: "openai", DisplayName: "Limited", Enabled: true, Status: provider.AccountStatusRateLimited, StatusReason: "quota window", LastError: "upstream quota", LastErrorAt: &now, RateLimitedUntil: &future}, - {ID: 10, Provider: "openai", DisplayName: "Model off", Enabled: true, Status: provider.AccountStatusActive}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Disabled", Enabled: false, Status: provider.AccountStatusActive}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Expired", Enabled: true, Status: provider.AccountStatusExpired}, + {ID: 9, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Limited", Enabled: true, Status: provider.AccountStatusRateLimited, StatusReason: "quota window", LastError: "upstream quota", LastErrorAt: &now, RateLimitedUntil: &future}, + {ID: 10, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Model off", Enabled: true, Status: provider.AccountStatusActive}, } for _, account := range providers.accounts { providers.accountModels[account.ID] = []provider.AccountModel{ @@ -6739,11 +5946,11 @@ func TestModelRoutingStatusIncludesSchedulableAccountOrder(t *testing.T) { future := now.Add(time.Hour) providers := newFakeProviderService() providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, DisplayName: "Preferred", Enabled: true, Priority: 1, Status: provider.AccountStatusActive, LastUsedAt: &recent}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Preferred", Enabled: true, Priority: 1, Status: provider.AccountStatusActive, LastUsedAt: &recent}, {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Older same priority", Enabled: true, Priority: 1, Status: provider.AccountStatusActive, LastUsedAt: &older}, - {ID: 9, Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, DisplayName: "Fallback", Enabled: true, Priority: 5, Status: provider.AccountStatusActive}, - {ID: 10, Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, DisplayName: "Rate limited", Enabled: true, Priority: 0, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &future}, - {ID: 11, Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, DisplayName: "Model disabled", Enabled: true, Priority: 0, Status: provider.AccountStatusActive}, + {ID: 9, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Fallback", Enabled: true, Priority: 5, Status: provider.AccountStatusActive}, + {ID: 10, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Rate limited", Enabled: true, Priority: 0, Status: provider.AccountStatusRateLimited, RateLimitedUntil: &future}, + {ID: 11, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Model disabled", Enabled: true, Priority: 0, Status: provider.AccountStatusActive}, } for _, account := range providers.accounts { providers.accountModels[account.ID] = []provider.AccountModel{ @@ -6824,7 +6031,7 @@ func TestModelRoutingPreviewReturnsSessionAwareSelection(t *testing.T) { StickyBoundAccountID: 8, Candidates: []provider.SelectionCandidate{ {ID: 8, DisplayName: "Sticky", AccountType: provider.AccountTypeAPIUpstream, Priority: 1, ScheduleRank: 1, ScheduleReason: stickyReason, Selected: true, StickyBound: true}, - {ID: 7, DisplayName: "Fallback", AccountType: provider.AccountTypeCodexOAuth, Priority: 1, ScheduleRank: 2, ScheduleReason: fallbackReason}, + {ID: 7, DisplayName: "Fallback", AccountType: provider.AccountTypeAPIUpstream, Priority: 1, ScheduleRank: 2, ScheduleReason: fallbackReason}, }, } server := NewServer(config.Config{}, staticHealth{}, admins, providers) @@ -6962,12 +6169,12 @@ func TestModelRoutingPreviewIncludesConcurrencyState(t *testing.T) { SelectedAccountID: 7, Candidates: []provider.SelectionCandidate{ {ID: 7, DisplayName: "Busy", AccountType: provider.AccountTypeAPIUpstream, Priority: 1, ScheduleRank: 1, Selected: true}, - {ID: 8, DisplayName: "Inherited", AccountType: provider.AccountTypeCodexOAuth, Priority: 2, ScheduleRank: 2}, + {ID: 8, DisplayName: "Inherited", AccountType: provider.AccountTypeAPIUpstream, Priority: 2, ScheduleRank: 2}, }, } providers.accounts = []provider.Account{ - {ID: 7, Provider: "openai", DisplayName: "Busy", MaxConcurrentRequests: 2}, - {ID: 8, Provider: "openai", DisplayName: "Inherited"}, + {ID: 7, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Busy", MaxConcurrentRequests: 2}, + {ID: 8, Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, DisplayName: "Inherited"}, } gateway := &fakeGatewayHandler{accountConcurrency: map[int64]int{7: 2, 8: 1}} server := NewServer(config.Config{}, staticHealth{}, admins, providers, gateway) @@ -7023,7 +6230,7 @@ func TestModelRoutingPreviewPassesExcludedAccountIDs(t *testing.T) { SelectedAccountID: 9, Candidates: []provider.SelectionCandidate{ {ID: 9, DisplayName: "Fallback", AccountType: provider.AccountTypeAPIUpstream, Priority: 1, ScheduleRank: 1, Selected: true}, - {ID: 7, DisplayName: "Excluded", AccountType: provider.AccountTypeCodexOAuth, Priority: 1, Schedulable: false, UnschedulableReason: "account excluded"}, + {ID: 7, DisplayName: "Excluded", AccountType: provider.AccountTypeAPIUpstream, Priority: 1, Schedulable: false, UnschedulableReason: "account excluded"}, }, } server := NewServer(config.Config{}, staticHealth{}, admins, providers) @@ -7306,41 +6513,6 @@ func (s *fakeAdminService) GetOpsCostBreakdown(_ context.Context, since time.Tim return s.opsCostBreakdown, nil } -func (s *fakeAdminService) ListFingerprintProfiles(_ context.Context) ([]admin.FingerprintProfile, error) { - return []admin.FingerprintProfile{{ - ID: 8, - SystemKey: "codex_cli_default", - Name: "Chrome", - Description: "browser preset", - UserAgent: "Mozilla/5.0", - TLSFingerprint: "chrome", - Headers: map[string]string{"X-Test": "1"}, - Enabled: true, - }}, nil -} - -func (s *fakeAdminService) CreateFingerprintProfile(_ context.Context, input admin.FingerprintProfileInput) (admin.FingerprintProfile, error) { - s.fingerprintInput = input - if s.fingerprintErr != nil { - return admin.FingerprintProfile{}, s.fingerprintErr - } - return admin.FingerprintProfile{ID: 9, Name: input.Name, Description: input.Description, UserAgent: input.UserAgent, TLSFingerprint: input.TLSFingerprint, Headers: input.Headers, Enabled: input.Enabled}, nil -} - -func (s *fakeAdminService) UpdateFingerprintProfile(_ context.Context, id int64, input admin.FingerprintProfileInput) (admin.FingerprintProfile, error) { - s.fingerprintID = id - s.fingerprintInput = input - if s.fingerprintErr != nil { - return admin.FingerprintProfile{}, s.fingerprintErr - } - return admin.FingerprintProfile{ID: id, Name: input.Name, Description: input.Description, UserAgent: input.UserAgent, TLSFingerprint: input.TLSFingerprint, Headers: input.Headers, Enabled: input.Enabled}, nil -} - -func (s *fakeAdminService) DeleteFingerprintProfile(_ context.Context, id int64) error { - s.fingerprintID = id - return s.fingerprintErr -} - func (s *fakeAdminService) ListErrorPassthroughRules(_ context.Context) ([]admin.ErrorPassthroughRule, error) { return []admin.ErrorPassthroughRule{{ID: 1, Pattern: "insufficient_quota", MatchType: "error_code", Enabled: true}}, nil } @@ -7591,3 +6763,56 @@ func TestAdminSyncOfficialUsagePricingMapsInvalidInputTo400(t *testing.T) { t.Errorf("error = %q, want invalid_input", v) } } + +func TestRetiredOAuthAndFingerprintRoutesReturnNotFound(t *testing.T) { + server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService(), fstest.MapFS{ + "index.html": {Data: []byte("
Admin UI
")}, + "200.html": {Data: []byte("
SPA fallback
")}, + }) + for _, route := range []struct{ method, path string }{ + {http.MethodGet, "/api/admin/provider-accounts/codex-oauth/status"}, + {http.MethodPost, "/api/admin/provider-accounts/codex-oauth/connect"}, + {http.MethodPost, "/api/admin/provider-accounts/codex-oauth/callback"}, + {http.MethodPost, "/api/admin/providers/openai/connect"}, + {http.MethodPost, "/api/admin/providers/openai/callback"}, + {http.MethodPost, "/api/admin/provider-accounts/7/refresh"}, + {http.MethodPost, "/api/admin/provider-accounts/bulk-refresh"}, + {http.MethodPost, "/api/admin/provider-accounts/7/models/refresh"}, + {http.MethodGet, "/oauth/openai/callback?code=retired-secret"}, + {http.MethodGet, "/api/admin/fingerprint-profiles"}, + {http.MethodPost, "/api/admin/fingerprint-profiles"}, + {http.MethodPatch, "/api/admin/fingerprint-profiles/1"}, + {http.MethodDelete, "/api/admin/fingerprint-profiles/1"}, + } { + t.Run(route.method+" "+route.path, func(t *testing.T) { + req := httptest.NewRequest(route.method, route.path, nil) + req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) + recorder := httptest.NewRecorder() + server.ServeHTTP(recorder, req) + if recorder.Code != http.StatusNotFound { + t.Fatalf("status = %d body=%s, want 404", recorder.Code, recorder.Body.String()) + } + if strings.Contains(recorder.Body.String(), "retired-secret") { + t.Fatal("retired callback reflected authorization code") + } + }) + } +} + +func TestProviderAccountRejectsRetiredFingerprintBinding(t *testing.T) { + server := NewServer(config.Config{}, staticHealth{}, newFakeAdminService(), newFakeProviderService()) + for _, tc := range []struct{ method, path, body string }{ + {http.MethodPost, "/api/admin/provider-accounts/api-upstream", `{"name":"Upstream","baseUrl":"https://api.example.test/v1","apiKey":"secret","fingerprintProfileId":9}`}, + {http.MethodPatch, "/api/admin/provider-accounts/7", `{"enabled":true,"fingerprintProfileId":9}`}, + } { + t.Run(tc.method, func(t *testing.T) { + req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body)) + req.AddCookie(&http.Cookie{Name: "n2api_admin_session", Value: "valid-session"}) + recorder := httptest.NewRecorder() + server.ServeHTTP(recorder, req) + if recorder.Code != http.StatusBadRequest { + t.Fatalf("status = %d body=%s, want 400", recorder.Code, recorder.Body.String()) + } + }) + } +} diff --git a/backend/internal/httpapi/system_event.go b/backend/internal/httpapi/system_event.go index c26422a2..c5db491c 100644 --- a/backend/internal/httpapi/system_event.go +++ b/backend/internal/httpapi/system_event.go @@ -214,9 +214,6 @@ func adminFailureEventForRequest(r *http.Request) (systemevent.Action, systemeve "POST /api/admin/usage-pricing/sync-official": {systemevent.ActionUsagePricingSynced, systemevent.CategoryAudit, "usage_pricing"}, "POST /api/admin/usage-pricing/remove-shutdown": {systemevent.ActionUsagePricingShutdownRemoved, systemevent.CategoryAudit, "usage_pricing"}, "POST /api/admin/usage-pricing/ignore-upcoming": {systemevent.ActionUsagePricingUpcomingIgnored, systemevent.CategoryAudit, "usage_pricing"}, - "POST /api/admin/fingerprint-profiles": {systemevent.ActionFingerprintProfileCreated, systemevent.CategoryAudit, "fingerprint_profile"}, - "PATCH /api/admin/fingerprint-profiles/{id}": {systemevent.ActionFingerprintProfileUpdated, systemevent.CategoryAudit, "fingerprint_profile"}, - "DELETE /api/admin/fingerprint-profiles/{id}": {systemevent.ActionFingerprintProfileDeleted, systemevent.CategoryAudit, "fingerprint_profile"}, "POST /api/admin/error-passthrough-rules": {systemevent.ActionErrorPassthroughRuleCreated, systemevent.CategoryAudit, "error_passthrough_rule"}, "PATCH /api/admin/error-passthrough-rules/{id}": {systemevent.ActionErrorPassthroughRuleUpdated, systemevent.CategoryAudit, "error_passthrough_rule"}, "DELETE /api/admin/error-passthrough-rules/{id}": {systemevent.ActionErrorPassthroughRuleDeleted, systemevent.CategoryAudit, "error_passthrough_rule"}, @@ -230,8 +227,6 @@ func adminFailureEventForRequest(r *http.Request) (systemevent.Action, systemeve "DELETE /api/admin/alert-rules/{id}": {systemevent.ActionAlertRuleDeleted, systemevent.CategoryAudit, "alert_rule"}, "PUT /api/admin/model-settings": {systemevent.ActionModelSettingsUpdated, systemevent.CategoryAudit, "model_settings"}, "POST /api/admin/provider-accounts/api-upstream": {systemevent.ActionProviderAccountCreated, systemevent.CategoryAudit, "provider_account"}, - "POST /api/admin/provider-accounts/codex-oauth/connect": {systemevent.ActionOAuthConnectFailed, systemevent.CategoryOAuth, "oauth_connection"}, - "POST /api/admin/provider-accounts/codex-oauth/callback": {systemevent.ActionOAuthCallbackFailed, systemevent.CategoryOAuth, "oauth_connection"}, "PATCH /api/admin/provider-accounts/{id}": {systemevent.ActionProviderAccountUpdated, systemevent.CategoryAudit, "provider_account"}, "DELETE /api/admin/provider-accounts/{id}": {systemevent.ActionProviderAccountDisconnected, systemevent.CategoryAudit, "provider_account"}, "POST /api/admin/provider-accounts/{id}/disconnect": {systemevent.ActionProviderAccountDisconnected, systemevent.CategoryAudit, "provider_account"}, @@ -241,9 +236,6 @@ func adminFailureEventForRequest(r *http.Request) (systemevent.Action, systemeve "POST /api/admin/provider-accounts/{id}/test": {systemevent.ActionProviderAccountTested, systemevent.CategoryAudit, "provider_account"}, "POST /api/admin/provider-accounts/{id}/pause": {systemevent.ActionProviderAccountPaused, systemevent.CategoryAudit, "provider_account"}, "POST /api/admin/provider-accounts/{id}/reset-status": {systemevent.ActionProviderAccountStatusReset, systemevent.CategoryAudit, "provider_account"}, - "POST /api/admin/providers/openai/connect": {systemevent.ActionOAuthConnectFailed, systemevent.CategoryOAuth, "oauth_connection"}, - "POST /api/admin/providers/openai/callback": {systemevent.ActionOAuthCallbackFailed, systemevent.CategoryOAuth, "oauth_connection"}, - "POST /api/admin/providers/openai/disconnect": {systemevent.ActionProviderAccountDisconnectAll, systemevent.CategoryAudit, "provider_account_collection"}, "PATCH /api/admin/providers/openai/accounts/{id}": {systemevent.ActionProviderAccountUpdated, systemevent.CategoryAudit, "provider_account"}, "PUT /api/admin/providers/openai/accounts/{id}/models": {systemevent.ActionProviderAccountModelsReplaced, systemevent.CategoryAudit, "provider_account"}, "POST /api/admin/providers/openai/accounts/{id}/test": {systemevent.ActionProviderAccountTested, systemevent.CategoryAudit, "provider_account"}, diff --git a/backend/internal/provider/api_only_test.go b/backend/internal/provider/api_only_test.go new file mode 100644 index 00000000..e6543bef --- /dev/null +++ b/backend/internal/provider/api_only_test.go @@ -0,0 +1,101 @@ +package provider + +import ( + "context" + "errors" + "reflect" + "testing" + "time" +) + +func TestAPIOnlyProviderRejectsStoredOAuthAccounts(t *testing.T) { + repo := newMemoryRepo() + oauth := testAccount(t, 1, true, 100, "legacy-token") + oauth.AccountType = AccountTypeCodexOAuth + api := testAccount(t, 2, true, 1, "api-key") + repo.accounts = []Account{oauth, api} + service := newConfiguredService(repo, nil) + accounts, err := service.ListAccounts(context.Background()) + if err != nil || len(accounts) != 1 || accounts[0].ID != api.ID { + t.Fatalf("API accounts = %+v, %v", accounts, err) + } + if AccountSchedulable(oauth, time.Now()) { + t.Fatal("legacy OAuth account remains schedulable") + } + if _, err := service.selectedAccount(context.Background(), oauth); !errors.Is(err, ErrNotConnected) { + t.Fatalf("OAuth selection error = %v", err) + } + selected, err := service.SelectAccountForModel(context.Background(), "") + if err != nil || selected.AccountID != api.ID || selected.AuthorizationToken != "api-key" { + t.Fatalf("selected = %+v, %v", selected, err) + } + if len(repo.accounts) != 2 { + t.Fatal("selection deleted historical accounts") + } +} + +func TestAPIOnlyManagementPreservesStoredOAuthAccounts(t *testing.T) { + ctx := context.Background() + enabled, priority, name, proxy := false, 2, "renamed", "http://proxy.example:8080" + tests := []struct { + name string + run func(*Service) error + }{ + {"update enabled", func(s *Service) error { + _, err := s.UpdateAccount(ctx, 1, AccountUpdate{Enabled: &enabled}) + return err + }}, + {"update name", func(s *Service) error { _, err := s.UpdateAccount(ctx, 1, AccountUpdate{Name: &name}); return err }}, + {"update priority", func(s *Service) error { + _, err := s.UpdateAccount(ctx, 1, AccountUpdate{Priority: &priority}) + return err + }}, + {"update proxy", func(s *Service) error { _, err := s.UpdateAccount(ctx, 1, AccountUpdate{ProxyURL: &proxy}); return err }}, + {"reset", func(s *Service) error { _, err := s.ResetAccountStatus(ctx, 1); return err }}, + {"pause", func(s *Service) error { _, err := s.PauseAccountScheduling(ctx, 1, time.Minute); return err }}, + {"list models", func(s *Service) error { _, err := s.ListAccountModels(ctx, 1); return err }}, + {"list models batch", func(s *Service) error { _, err := s.ListAccountModelsForAccounts(ctx, []int64{1}); return err }}, + {"replace models", func(s *Service) error { + _, err := s.ReplaceAccountModels(ctx, 1, []AccountModelInput{{Model: "replacement", Enabled: true}}) + return err + }}, + {"test history", func(s *Service) error { _, err := s.ListAccountTestResults(ctx, 1, 20); return err }}, + {"disconnect", func(s *Service) error { return s.DisconnectAccount(ctx, 1) }}, + {"test account", func(s *Service) error { _, err := s.TestAccount(ctx, 1); return err }}, + {"test model", func(s *Service) error { _, err := s.TestAccountModel(ctx, 1, "legacy"); return err }}, + {"sync models", func(s *Service) error { _, _, err := s.SyncUpstreamAccountModels(ctx, 1); return err }}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + repo := newMemoryRepo() + oauth := testAccount(t, 1, true, 100, "legacy-token") + oauth.AccountType = AccountTypeCodexOAuth + repo.accounts = []Account{oauth} + models := []AccountModel{{AccountID: 1, Provider: "openai", Model: "legacy", Enabled: true}} + repo.accountModels[1] = append([]AccountModel(nil), models...) + if err := test.run(newConfiguredService(repo, nil)); !errors.Is(err, ErrNotConnected) { + t.Fatalf("error = %v, want ErrNotConnected", err) + } + if !reflect.DeepEqual(repo.accounts, []Account{oauth}) || !reflect.DeepEqual(repo.accountModels[1], models) || len(repo.intents) != 0 || len(repo.accountTestResults) != 0 { + t.Fatal("rejected operation changed archived account, credentials, models, or history") + } + }) + } +} + +func TestAPIOnlyPreviewOmitsStoredOAuthAccounts(t *testing.T) { + repo := newMemoryRepo() + oauth := testAccount(t, 1, true, 100, "legacy") + oauth.AccountType = AccountTypeCodexOAuth + api := testAccount(t, 2, true, 1, "key") + repo.accounts = []Account{oauth, api} + repo.routingPools[1] = RoutingPool{ID: 1, Name: "primary", Enabled: true} + repo.routingPoolAccounts[1] = []RoutingPoolAccount{{AccountID: 1}, {AccountID: 2}} + service := newConfiguredService(repo, nil) + for _, poolID := range []int64{0, 1} { + preview, err := service.PreviewAccountSelectionInRoutingPool(context.Background(), poolID, "", "session") + if err != nil || len(preview.Candidates) != 1 || preview.Candidates[0].ID != api.ID { + t.Fatalf("pool %d preview = %+v, %v", poolID, preview, err) + } + } +} diff --git a/backend/internal/provider/catalog.go b/backend/internal/provider/catalog.go deleted file mode 100644 index 392382be..00000000 --- a/backend/internal/provider/catalog.go +++ /dev/null @@ -1,494 +0,0 @@ -package provider - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "net/url" - "strings" - "time" - - "github.com/KnowSky404/N2API/backend/internal/systemevent" -) - -// The Codex catalog has not yet hidden every model that the official Codex -// documentation has announced as deprecated for ChatGPT sign-in. -var codexOAuthDeprecatedModels = map[string]struct{}{ - "gpt-5.2": {}, - "gpt-5.3-codex": {}, -} - -type oauthModelCatalogCacheKey struct { - accountID int64 - generation uint64 - baseURL string - clientVersion string - planType string -} - -type oauthModelCatalogCacheEntry struct { - models []AccountModelInput - expiresAt time.Time -} - -type oauthModelCatalogFlight struct { - done chan struct{} - fetch oauthModelCatalogFetch - err error -} - -type oauthModelCatalogFetch struct { - models []AccountModelInput - attemptAt time.Time - source string - cacheHit bool - upstreamFetchMS *int64 - generation uint64 -} - -// SyncOAuthAccountModels fetches the account-specific Codex model catalog and -// persists only current picker-visible API models. -func (s *Service) SyncOAuthAccountModels(ctx context.Context, accountID int64) ([]AccountModel, AccountModelSyncSummary, error) { - return s.syncOAuthAccountModels(ctx, accountID, false) -} - -// RefreshOAuthAccountModels bypasses a completed catalog cache entry while -// retaining in-flight coalescing for the same account/configuration generation. -func (s *Service) RefreshOAuthAccountModels(ctx context.Context, accountID int64) ([]AccountModel, AccountModelSyncSummary, error) { - return s.syncOAuthAccountModels(ctx, accountID, true) -} - -func (s *Service) syncOAuthAccountModels(ctx context.Context, accountID int64, forceRefresh bool) ([]AccountModel, AccountModelSyncSummary, error) { - if accountID <= 0 { - return nil, AccountModelSyncSummary{}, ErrInvalidInput - } - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, accountID) - if err != nil { - return nil, AccountModelSyncSummary{}, err - } - accountType := strings.TrimSpace(account.AccountType) - if accountType == "" { - accountType = AccountTypeCodexOAuth - } - if accountType != AccountTypeCodexOAuth { - return nil, AccountModelSyncSummary{}, ErrInvalidInput - } - selected, err := s.selectedAccountForModelSync(ctx, account) - if err != nil { - return nil, AccountModelSyncSummary{}, err - } - catalog, err := s.oauthModelCatalogWithOptions(ctx, account, selected, forceRefresh) - if err != nil { - s.recordOAuthModelCatalogFailure(accountID, catalog, err, -1) - return nil, AccountModelSyncSummary{}, err - } - if !s.oauthModelCatalogGenerationCurrent(accountID, catalog.generation) { - return nil, AccountModelSyncSummary{}, ErrOAuthModelCatalogStale - } - syncCtx := withProviderEventIntent(ctx, systemevent.EventIntent{ - Category: systemevent.CategoryAudit, - Severity: systemevent.SeverityInfo, - Action: systemevent.ActionProviderAccountModelsSynced, - Outcome: systemevent.OutcomeSuccess, - Target: providerAccountTarget(accountID, accountDisplayName(account)), - Metadata: map[string]any{ - "oauth_catalog_model_count": len(catalog.models), - "oauth_catalog_source": catalog.source, - "oauth_catalog_cache_hit": catalog.cacheHit, - }, - }) - applyStarted := time.Now() - models, summary, err := s.repo.SyncOAuthAccountModels(syncCtx, s.cfg.Provider, accountID, catalog.models, time.Now().UTC()) - localApplyMS := time.Since(applyStarted).Milliseconds() - if err != nil { - s.recordOAuthModelCatalogFailure(accountID, catalog, err, localApplyMS) - return models, summary, err - } - if !s.oauthModelCatalogGenerationCurrent(accountID, catalog.generation) { - return models, summary, ErrOAuthModelCatalogStale - } - s.recordOAuthModelCatalogSuccess(accountID, catalog, localApplyMS) - return models, summary, nil -} - -func (s *Service) oauthModelCatalog(ctx context.Context, account Account, selected SelectedAccount) ([]AccountModelInput, error) { - catalog, err := s.oauthModelCatalogWithOptions(ctx, account, selected, false) - if err != nil { - s.recordOAuthModelCatalogFailure(account.ID, catalog, err, -1) - return nil, err - } - s.recordOAuthModelCatalogSuccess(account.ID, catalog, -1) - return cloneAccountModelInputs(catalog.models), nil -} - -func (s *Service) oauthModelCatalogWithOptions(ctx context.Context, account Account, selected SelectedAccount, forceRefresh bool) (oauthModelCatalogFetch, error) { - key := s.oauthModelCatalogCacheKey(account, selected) - - for { - now := s.oauthModelCatalogNow() - s.oauthModelCatalogMu.Lock() - if s.oauthModelCatalogCache == nil { - s.oauthModelCatalogCache = make(map[oauthModelCatalogCacheKey]oauthModelCatalogCacheEntry) - } - if s.oauthModelCatalogFlights == nil { - s.oauthModelCatalogFlights = make(map[oauthModelCatalogCacheKey]*oauthModelCatalogFlight) - } - if !forceRefresh { - if cached, ok := s.oauthModelCatalogCache[key]; ok && now.Before(cached.expiresAt) { - fetch := oauthModelCatalogFetch{ - models: cloneAccountModelInputs(cached.models), - attemptAt: now.UTC(), - source: "cache", - cacheHit: true, - generation: key.generation, - } - s.oauthModelCatalogMu.Unlock() - return fetch, nil - } - } - if flight, ok := s.oauthModelCatalogFlights[key]; ok { - s.oauthModelCatalogMu.Unlock() - select { - case <-ctx.Done(): - return oauthModelCatalogFetch{generation: key.generation, attemptAt: now.UTC()}, ctx.Err() - case <-flight.done: - if (errors.Is(flight.err, context.Canceled) || errors.Is(flight.err, context.DeadlineExceeded)) && ctx.Err() == nil { - continue - } - fetch := flight.fetch - fetch.models = cloneAccountModelInputs(fetch.models) - return fetch, flight.err - } - } - if len(s.oauthModelCatalogFlights) >= maxOAuthModelCatalogFlights { - s.oauthModelCatalogMu.Unlock() - return oauthModelCatalogFetch{ - generation: key.generation, - attemptAt: now.UTC(), - source: "admission", - }, ErrOAuthModelCatalogBusy - } - - flight := &oauthModelCatalogFlight{done: make(chan struct{})} - s.oauthModelCatalogFlights[key] = flight - s.oauthModelCatalogMu.Unlock() - - startedAt := time.Now() - models, err := s.fetchOAuthModelCatalog(ctx, selected) - fetch := oauthModelCatalogFetch{ - models: cloneAccountModelInputs(models), - attemptAt: now.UTC(), - source: "upstream", - cacheHit: false, - generation: key.generation, - } - fetchDuration := time.Since(startedAt).Milliseconds() - fetch.upstreamFetchMS = &fetchDuration - - s.oauthModelCatalogMu.Lock() - if err == nil && s.oauthModelCatalogGenerations[key.accountID] == key.generation { - s.storeOAuthModelCatalogCacheLocked(key, oauthModelCatalogCacheEntry{ - models: cloneAccountModelInputs(models), - expiresAt: s.oauthModelCatalogNow().Add(s.cfg.OAuthModelCatalogCacheTTL), - }) - } - flight.fetch = fetch - flight.err = err - delete(s.oauthModelCatalogFlights, key) - close(flight.done) - s.oauthModelCatalogMu.Unlock() - return fetch, err - } -} - -func (s *Service) storeOAuthModelCatalogCacheLocked(key oauthModelCatalogCacheKey, entry oauthModelCatalogCacheEntry) { - if _, exists := s.oauthModelCatalogCache[key]; !exists && len(s.oauthModelCatalogCache) >= maxOAuthModelCatalogCacheEntries { - var oldestKey oauthModelCatalogCacheKey - var oldestExpiry time.Time - for candidateKey, candidate := range s.oauthModelCatalogCache { - if oldestExpiry.IsZero() || candidate.expiresAt.Before(oldestExpiry) { - oldestKey = candidateKey - oldestExpiry = candidate.expiresAt - } - } - delete(s.oauthModelCatalogCache, oldestKey) - } - s.oauthModelCatalogCache[key] = entry -} - -func (s *Service) oauthModelCatalogCacheKey(account Account, selected SelectedAccount) oauthModelCatalogCacheKey { - baseURL := strings.TrimRight(strings.TrimSpace(s.cfg.CodexResponsesBaseURL), "/") - planType := strings.ToLower(strings.TrimSpace(account.Metadata["plan_type"])) - s.oauthModelCatalogMu.Lock() - if s.oauthModelCatalogGenerations == nil { - s.oauthModelCatalogGenerations = make(map[int64]uint64) - } - if _, exists := s.oauthModelCatalogGenerations[account.ID]; !exists { - s.oauthModelCatalogGenerations[account.ID] = 0 - } - generation := s.oauthModelCatalogGenerations[account.ID] - s.oauthModelCatalogMu.Unlock() - return oauthModelCatalogCacheKey{ - accountID: account.ID, - generation: generation, - baseURL: baseURL, - clientVersion: codexCatalogClientVersion(selected), - planType: planType, - } -} - -func (s *Service) OAuthModelCatalogStatus(_ context.Context, accountID int64) (OAuthModelCatalogStatus, error) { - if accountID <= 0 { - return OAuthModelCatalogStatus{}, ErrInvalidInput - } - s.oauthModelCatalogMu.Lock() - defer s.oauthModelCatalogMu.Unlock() - return cloneOAuthModelCatalogStatus(s.oauthModelCatalogStatus[accountID]), nil -} - -func (s *Service) oauthModelCatalogGenerationCurrent(accountID int64, generation uint64) bool { - s.oauthModelCatalogMu.Lock() - defer s.oauthModelCatalogMu.Unlock() - return s.oauthModelCatalogGenerations[accountID] == generation -} - -func cloneOAuthModelCatalogStatus(status OAuthModelCatalogStatus) OAuthModelCatalogStatus { - status.LastAttemptAt = cloneTimePointer(status.LastAttemptAt) - status.LastSuccessAt = cloneTimePointer(status.LastSuccessAt) - status.LastFailureAt = cloneTimePointer(status.LastFailureAt) - status.CacheHit = cloneBoolPointer(status.CacheHit) - status.UpstreamFetchMS = cloneInt64Pointer(status.UpstreamFetchMS) - status.LocalApplyMS = cloneInt64Pointer(status.LocalApplyMS) - return status -} - -func cloneTimePointer(value *time.Time) *time.Time { - if value == nil { - return nil - } - clone := *value - return &clone -} - -func cloneBoolPointer(value *bool) *bool { - if value == nil { - return nil - } - clone := *value - return &clone -} - -func cloneInt64Pointer(value *int64) *int64 { - if value == nil { - return nil - } - clone := *value - return &clone -} - -func (s *Service) recordOAuthModelCatalogSuccess(accountID int64, catalog oauthModelCatalogFetch, localApplyMS int64) { - if accountID <= 0 { - return - } - now := s.oauthModelCatalogNow().UTC() - status := OAuthModelCatalogStatus{ - LastAttemptAt: cloneTimePointer(&catalog.attemptAt), - LastSuccessAt: &now, - LastError: "", - Source: catalog.source, - CacheHit: boolPointer(catalog.cacheHit), - UpstreamFetchMS: cloneInt64Pointer(catalog.upstreamFetchMS), - } - if localApplyMS >= 0 { - status.LocalApplyMS = &localApplyMS - } - s.oauthModelCatalogMu.Lock() - if s.oauthModelCatalogGenerations[accountID] != catalog.generation { - s.oauthModelCatalogMu.Unlock() - return - } - if s.oauthModelCatalogStatus == nil { - s.oauthModelCatalogStatus = make(map[int64]OAuthModelCatalogStatus) - } - s.oauthModelCatalogStatus[accountID] = mergeOAuthModelCatalogStatus(s.oauthModelCatalogStatus[accountID], status) - s.oauthModelCatalogMu.Unlock() -} - -func (s *Service) recordOAuthModelCatalogFailure(accountID int64, catalog oauthModelCatalogFetch, err error, localApplyMS int64) { - if accountID <= 0 { - return - } - now := s.oauthModelCatalogNow().UTC() - attemptAt := catalog.attemptAt - if attemptAt.IsZero() { - attemptAt = now - } - status := OAuthModelCatalogStatus{ - LastAttemptAt: cloneTimePointer(&attemptAt), - LastFailureAt: &now, - LastError: sanitizeCatalogError(err), - Source: catalog.source, - CacheHit: boolPointer(catalog.cacheHit), - UpstreamFetchMS: cloneInt64Pointer(catalog.upstreamFetchMS), - } - if localApplyMS >= 0 { - status.LocalApplyMS = &localApplyMS - } - s.oauthModelCatalogMu.Lock() - if s.oauthModelCatalogGenerations[accountID] != catalog.generation { - s.oauthModelCatalogMu.Unlock() - return - } - if s.oauthModelCatalogStatus == nil { - s.oauthModelCatalogStatus = make(map[int64]OAuthModelCatalogStatus) - } - s.oauthModelCatalogStatus[accountID] = mergeOAuthModelCatalogStatus(s.oauthModelCatalogStatus[accountID], status) - s.oauthModelCatalogMu.Unlock() -} - -func mergeOAuthModelCatalogStatus(previous, current OAuthModelCatalogStatus) OAuthModelCatalogStatus { - if current.LastSuccessAt == nil { - current.LastSuccessAt = previous.LastSuccessAt - } - if current.LastFailureAt == nil { - current.LastFailureAt = previous.LastFailureAt - } - return current -} - -func boolPointer(value bool) *bool { - return &value -} - -func sanitizeCatalogError(err error) string { - if err == nil { - return "" - } - return sanitizeDiagnosticMessage(err.Error()) -} - -func cloneAccountModelInputs(models []AccountModelInput) []AccountModelInput { - if models == nil { - return nil - } - return append([]AccountModelInput(nil), models...) -} - -func (s *Service) fetchOAuthModelCatalog(ctx context.Context, selected SelectedAccount) ([]AccountModelInput, error) { - targetURL, err := codexModelsURL(s.cfg.CodexResponsesBaseURL, codexCatalogClientVersion(selected)) - if err != nil { - return nil, err - } - req, err := http.NewRequestWithContext(ctx, http.MethodGet, targetURL, nil) - if err != nil { - return nil, err - } - req.Header.Set("Authorization", "Bearer "+selected.AuthorizationToken) - req.Header.Set("Accept", "application/json") - req.Header.Set("ChatGPT-Account-ID", strings.TrimSpace(selected.ChatGPTAccountID)) - req.Header.Set("originator", DefaultCodexFingerprintOriginator) - req.Header.Set("Version", DefaultCodexFingerprintVersion) - req.Header.Set("User-Agent", DefaultCodexFingerprintUserAgent) - for key, value := range selected.FingerprintHeaders { - req.Header.Set(key, value) - } - if strings.TrimSpace(selected.FingerprintUA) != "" { - req.Header.Set("User-Agent", strings.TrimSpace(selected.FingerprintUA)) - } - - client := s.httpClient.clientForProxy(selected.ProxyURL) - if strings.TrimSpace(selected.FingerprintTLS) != "" { - cloned := *client - transport := newModelProbeTLSFingerprintTransport(client.Transport, selected.ProxyURL).(*modelProbeTLSFingerprintTransport) - if s.httpClient.modelProbeTLSConfig != nil { - transport.tlsConfig = s.httpClient.modelProbeTLSConfig.Clone() - } - if s.httpClient.modelProbeProxyTLSConfig != nil { - transport.proxyTLSConfig = s.httpClient.modelProbeProxyTLSConfig.Clone() - } - cloned.Transport = transport - client = &cloned - req = req.WithContext(contextWithModelProbeTLSFingerprint(req.Context(), selected.FingerprintTLS)) - } - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - if resp.StatusCode < 200 || resp.StatusCode > 299 { - return nil, fmt.Errorf("codex model catalog returned status %d", resp.StatusCode) - } - - body, err := io.ReadAll(io.LimitReader(resp.Body, 10<<20)) - if err != nil { - return nil, err - } - var parsed struct { - Models []struct { - Slug string `json:"slug"` - Visibility string `json:"visibility"` - SupportedInAPI bool `json:"supported_in_api"` - Upgrade json.RawMessage `json:"upgrade"` - } `json:"models"` - } - if err := json.Unmarshal(body, &parsed); err != nil { - return nil, fmt.Errorf("decode codex model catalog: %w", err) - } - if parsed.Models == nil { - return nil, errors.New("codex model catalog missing models array") - } - - models := make([]AccountModelInput, 0, len(parsed.Models)) - for _, model := range parsed.Models { - slug := strings.TrimSpace(model.Slug) - _, deprecated := codexOAuthDeprecatedModels[slug] - hasUpgrade := len(model.Upgrade) > 0 && string(model.Upgrade) != "null" - if slug == "" || model.Visibility != "list" || !model.SupportedInAPI || hasUpgrade || deprecated { - continue - } - models = append(models, AccountModelInput{Model: slug, Enabled: true}) - } - if len(models) == 0 { - return nil, errors.New("no current models found in codex model catalog") - } - normalized, err := normalizeAccountModelInputs(models) - if err != nil { - return nil, err - } - return normalized, nil -} - -func codexCatalogClientVersion(selected SelectedAccount) string { - for key, value := range selected.FingerprintHeaders { - if strings.EqualFold(strings.TrimSpace(key), "Version") && strings.TrimSpace(value) != "" { - return strings.TrimSpace(value) - } - } - return DefaultCodexFingerprintVersion -} - -func codexModelsURL(baseURL, clientVersion string) (string, error) { - trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/") - if trimmed == "" { - trimmed = "https://chatgpt.com/backend-api/codex" - } - parsed, err := url.Parse(trimmed + "/models") - if err != nil { - return "", err - } - query := parsed.Query() - query.Set("client_version", strings.TrimSpace(clientVersion)) - parsed.RawQuery = query.Encode() - return parsed.String(), nil -} - -func upstreamModelsURL(baseURL string) string { - trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/") - if strings.HasSuffix(trimmed, "/v1") { - return trimmed + "/models" - } - return trimmed + "/v1/models" -} diff --git a/backend/internal/provider/endpoint_test.go b/backend/internal/provider/endpoint_test.go index 82fb342b..419e3390 100644 --- a/backend/internal/provider/endpoint_test.go +++ b/backend/internal/provider/endpoint_test.go @@ -26,7 +26,7 @@ func TestEndpointAwareSelectionSkipsExplicitlyUnsupportedModelEndpoint(t *testin Enabled: true, Metadata: EndpointCapabilityMetadata(EndpointResponses, EndpointCapabilitySupported), }} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelAndEndpoint(context.Background(), "gpt-5", EndpointResponses) if err != nil { @@ -82,7 +82,7 @@ func TestEndpointAwareSelectionReturnsEndpointUnavailableWhenAllModelsRejectEndp Metadata: EndpointCapabilityMetadata(EndpointChatCompletions, EndpointCapabilityUnsupported), }} } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) _, err := service.SelectAccountForModelAndEndpoint(context.Background(), "gpt-5", EndpointChatCompletions) if !errors.Is(err, ErrEndpointUnavailable) { @@ -114,7 +114,7 @@ func TestEndpointAwareSelectionUsesSupportedFallbackPool(t *testing.T) { Enabled: true, Metadata: EndpointCapabilityMetadata(EndpointResponses, EndpointCapabilitySupported), }} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChainAndEndpoint(context.Background(), 1, "gpt-5", EndpointResponses) if err != nil { diff --git a/backend/internal/provider/http_client_test.go b/backend/internal/provider/http_client_test.go index e5f83784..0e850fb2 100644 --- a/backend/internal/provider/http_client_test.go +++ b/backend/internal/provider/http_client_test.go @@ -1,431 +1,13 @@ package provider import ( - "bufio" "context" - "crypto/tls" - "encoding/json" "io" - "net" "net/http" - "net/http/httptest" - "net/url" "strings" - "sync" "testing" - "time" - - utls "github.com/refraction-networking/utls" ) -func TestHTTPClientExchangeCodePostsAuthorizationCodeGrant(t *testing.T) { - var gotGrantType string - var gotCode string - var gotClientID string - var gotClientSecret string - var gotRedirectURI string - var gotCodeVerifier string - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - if err := r.ParseForm(); err != nil { - t.Fatalf("ParseForm returned error: %v", err) - } - gotGrantType = r.Form.Get("grant_type") - gotCode = r.Form.Get("code") - gotClientID = r.Form.Get("client_id") - gotClientSecret = r.Form.Get("client_secret") - gotRedirectURI = r.Form.Get("redirect_uri") - gotCodeVerifier = r.Form.Get("code_verifier") - return jsonResponse(http.StatusOK, map[string]any{ - "access_token": "access-token", - "refresh_token": "refresh-token", - "id_token": "id-token", - "expires_in": 3600, - "subject": "acct_1", - "display_name": "Codex Account", - "account_id": "acct_chatgpt", - "email": "owner@example.com", - "plan_type": "plus", - }) - })}) - - token, err := client.ExchangeCode(context.Background(), Config{ - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - TokenURL: "https://auth.example.test/token", - CodeVerifier: "pkce-verifier", - }, "auth-code") - if err != nil { - t.Fatalf("ExchangeCode returned error: %v", err) - } - if token.AccessToken != "access-token" || token.RefreshToken != "refresh-token" || token.IDToken != "id-token" || token.Email != "owner@example.com" || token.AccountID != "acct_chatgpt" || token.PlanType != "plus" { - t.Fatalf("token = %+v", token) - } - if gotGrantType != "authorization_code" || gotCode != "auth-code" || gotClientID != "client-id" || gotClientSecret != "client-secret" || gotRedirectURI != "http://localhost/oauth/openai/callback" || gotCodeVerifier != "pkce-verifier" { - t.Fatalf("posted form = grant_type:%q code:%q client_id:%q client_secret:%q redirect_uri:%q code_verifier:%q", gotGrantType, gotCode, gotClientID, gotClientSecret, gotRedirectURI, gotCodeVerifier) - } -} - -func TestHTTPClientRefreshTokenPostsRefreshGrant(t *testing.T) { - var gotGrantType string - var gotRefreshToken string - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - if err := r.ParseForm(); err != nil { - t.Fatalf("ParseForm returned error: %v", err) - } - gotGrantType = r.Form.Get("grant_type") - gotRefreshToken = r.Form.Get("refresh_token") - return jsonResponse(http.StatusOK, map[string]any{ - "access_token": "new-access", - "refresh_token": "new-refresh", - "expires_in": 3600, - }) - })}) - - token, err := client.RefreshToken(context.Background(), Config{ - ClientID: "client-id", - ClientSecret: "client-secret", - TokenURL: "https://auth.example.test/token", - }, "old-refresh") - if err != nil { - t.Fatalf("RefreshToken returned error: %v", err) - } - if token.AccessToken != "new-access" || token.RefreshToken != "new-refresh" { - t.Fatalf("token = %+v", token) - } - if gotGrantType != "refresh_token" || gotRefreshToken != "old-refresh" { - t.Fatalf("posted form = grant_type:%q refresh_token:%q", gotGrantType, gotRefreshToken) - } -} - -func TestHTTPClientTokenErrorDoesNotIncludeBodySecret(t *testing.T) { - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusUnauthorized, - Body: io.NopCloser(strings.NewReader("secret-token-value")), - Header: make(http.Header), - Request: r, - }, nil - })}) - - _, err := client.RefreshToken(context.Background(), Config{ - ClientID: "client-id", - ClientSecret: "client-secret", - TokenURL: "https://auth.example.test/token", - }, "old-refresh") - if err == nil { - t.Fatal("RefreshToken returned nil error, want token endpoint error") - } - if strings.Contains(err.Error(), "secret-token-value") { - t.Fatalf("error leaked response body: %v", err) - } -} - -func TestHTTPClientProbeUsesCodexResponsesForChatGPTAccounts(t *testing.T) { - var gotPath string - var gotAuthorization string - var gotChatGPTAccountID string - var gotOpenAIBeta string - var gotOriginator string - var gotUserAgent string - var gotVersion string - var gotBody string - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - gotPath = r.URL.Path - gotAuthorization = r.Header.Get("Authorization") - gotChatGPTAccountID = r.Header.Get("chatgpt-account-id") - gotOpenAIBeta = r.Header.Get("OpenAI-Beta") - gotOriginator = r.Header.Get("originator") - gotUserAgent = r.Header.Get("User-Agent") - gotVersion = r.Header.Get("Version") - raw, _ := io.ReadAll(r.Body) - gotBody = string(raw) - return jsonResponse(http.StatusTooManyRequests, map[string]any{ - "error": map[string]any{"message": "usage limit reached"}, - }) - })}) - - result, err := client.ProbeAccountStatus(context.Background(), Config{ - APIBaseURL: "https://api.example.test", - CodexResponsesBaseURL: "https://chatgpt.example.test/backend-api/codex", - ProbeChatGPTAccountID: "acct_chatgpt", - }, "access-token") - if err != nil { - t.Fatalf("ProbeAccountStatus returned error: %v", err) - } - if result.statusCode != http.StatusTooManyRequests || result.message != "usage limit reached" { - t.Fatalf("probe result = %+v", result) - } - if gotPath != "/backend-api/codex/responses" { - t.Fatalf("path = %q, want Codex responses endpoint", gotPath) - } - if gotAuthorization != "Bearer access-token" || gotChatGPTAccountID != "acct_chatgpt" || gotOpenAIBeta != "responses=experimental" || gotOriginator != DefaultCodexFingerprintOriginator || gotUserAgent != DefaultCodexFingerprintUserAgent || gotVersion != DefaultCodexFingerprintVersion { - t.Fatalf("headers auth=%q account=%q beta=%q originator=%q user-agent=%q version=%q", gotAuthorization, gotChatGPTAccountID, gotOpenAIBeta, gotOriginator, gotUserAgent, gotVersion) - } - var payload struct { - Model string `json:"model"` - Instructions string `json:"instructions"` - Input []struct { - Type string `json:"type"` - Role string `json:"role"` - Content string `json:"content"` - } `json:"input"` - Stream bool `json:"stream"` - Store bool `json:"store"` - } - if err := json.Unmarshal([]byte(gotBody), &payload); err != nil { - t.Fatalf("decode probe body: %v", err) - } - if payload.Model != "gpt-5.4-mini" || payload.Instructions != "You are Codex, a coding agent." || !payload.Stream || payload.Store { - t.Fatalf("probe payload = %+v", payload) - } - if len(payload.Input) != 1 || payload.Input[0].Type != "message" || payload.Input[0].Role != "user" || payload.Input[0].Content != "n2api account status probe" { - t.Fatalf("probe input = %+v", payload.Input) - } -} - -func TestHTTPClientProbeDoesNotReadSuccessfulCodexStream(t *testing.T) { - body := &failOnReadBody{t: t} - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: body, - Header: make(http.Header), - Request: r, - }, nil - })}) - - result, err := client.ProbeAccountStatus(context.Background(), Config{ - CodexResponsesBaseURL: "https://chatgpt.example.test/backend-api/codex", - ProbeChatGPTAccountID: "acct_chatgpt", - }, "access-token") - if err != nil { - t.Fatalf("ProbeAccountStatus returned error: %v", err) - } - if result.statusCode != http.StatusOK { - t.Fatalf("statusCode = %d, want %d", result.statusCode, http.StatusOK) - } - if !body.closed { - t.Fatal("successful probe response body was not closed") - } -} - -func TestHTTPClientProbeAccountModelRequiresCodexCompletedEvent(t *testing.T) { - tests := []struct { - name string - stream string - wantCode string - wantError string - }{ - { - name: "completed", - stream: "event: response.created\ndata: {\"type\":\"response.created\"}\n\nevent: response.completed\ndata: {\"type\":\"response.completed\",\"response\":{}}\n\n", - }, - { - name: "failed", - stream: "event: response.failed\ndata: {\"type\":\"response.failed\",\"response\":{\"error\":{\"message\":\"model failed\"}}}\n\n", - wantCode: "upstream_error", - wantError: "model failed", - }, - { - name: "incomplete", - stream: "event: response.incomplete\ndata: {\"type\":\"response.incomplete\",\"response\":{}}\n\n", - wantCode: "invalid_response", - wantError: "upstream response was incomplete", - }, - { - name: "missing completion", - stream: "event: response.output_text.delta\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"OK\"}\n\n", - wantCode: "invalid_response", - wantError: "upstream stream ended before response.completed", - }, - { - name: "malformed terminal", - stream: "event: response.completed\ndata: not-json\n\n", - wantCode: "invalid_response", - wantError: "upstream returned malformed terminal event", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - var gotPath string - var gotModel string - var gotAccountID string - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - gotPath = r.URL.Path - gotAccountID = r.Header.Get("chatgpt-account-id") - var payload struct { - Model string `json:"model"` - Stream bool `json:"stream"` - } - if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { - t.Fatalf("decode model probe request: %v", err) - } - gotModel = payload.Model - if !payload.Stream { - t.Fatal("Codex model probe stream = false, want true") - } - return &http.Response{ - StatusCode: http.StatusOK, - Body: io.NopCloser(strings.NewReader(test.stream)), - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Request: r, - }, nil - })}) - - result := client.ProbeAccountModel(context.Background(), Config{ - CodexResponsesBaseURL: "https://chatgpt.example.test/backend-api/codex", - }, SelectedAccount{ - AccountType: AccountTypeCodexOAuth, - AuthorizationToken: "oauth-token", - ChatGPTAccountID: "acct-chatgpt", - }, "gpt-test") - - if result.statusCode != http.StatusOK || result.errorCode != test.wantCode || result.message != test.wantError { - t.Fatalf("ProbeAccountModel result = %+v, want code=%q error=%q", result, test.wantCode, test.wantError) - } - if gotPath != "/backend-api/codex/responses" || gotModel != "gpt-test" || gotAccountID != "acct-chatgpt" { - t.Fatalf("request path=%q model=%q account=%q", gotPath, gotModel, gotAccountID) - } - }) - } -} - -func TestHTTPClientProbeAccountModelClassifiesCodexStreamDeadline(t *testing.T) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) - defer cancel() - client := NewHTTPClient(&http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: http.StatusOK, - Body: &contextDeadlineBody{ctx: r.Context()}, - Header: http.Header{"Content-Type": []string{"text/event-stream"}}, - Request: r, - }, nil - })}) - - result := client.ProbeAccountModel(ctx, Config{ - CodexResponsesBaseURL: "https://chatgpt.example.test/backend-api/codex", - }, SelectedAccount{ - AccountType: AccountTypeCodexOAuth, - AuthorizationToken: "oauth-token", - ChatGPTAccountID: "acct-chatgpt", - }, "gpt-test") - - if result.statusCode != http.StatusOK || result.errorCode != "timeout" || result.message != "model test timed out" { - t.Fatalf("ProbeAccountModel result = %+v, want timeout", result) - } -} - -func TestHTTPClientProbeAccountModelUsesTLSFingerprintThroughProxy(t *testing.T) { - for _, tc := range []struct { - name string - proxyTLS bool - }{ - {name: "HTTP proxy"}, - {name: "HTTPS proxy", proxyTLS: true}, - } { - t.Run(tc.name, func(t *testing.T) { - upstreamReached := make(chan struct{}, 1) - upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - upstreamReached <- struct{}{} - w.Header().Set("Content-Type", "application/json") - _, _ = io.WriteString(w, `{"choices":[]}`) - })) - defer upstream.Close() - - var mu sync.Mutex - var gotMethod string - var gotTarget string - var gotProxyAuthorization string - proxy := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - mu.Lock() - gotMethod = r.Method - gotTarget = r.Host - gotProxyAuthorization = r.Header.Get("Proxy-Authorization") - mu.Unlock() - if r.Method != http.MethodConnect { - http.Error(w, "CONNECT required", http.StatusMethodNotAllowed) - return - } - upstreamConn, err := net.Dial("tcp", r.Host) - if err != nil { - http.Error(w, "upstream unavailable", http.StatusBadGateway) - return - } - hijacker, ok := w.(http.Hijacker) - if !ok { - upstreamConn.Close() - http.Error(w, "hijacking unavailable", http.StatusInternalServerError) - return - } - clientConn, buffered, err := hijacker.Hijack() - if err != nil { - upstreamConn.Close() - return - } - defer clientConn.Close() - defer upstreamConn.Close() - if _, err := buffered.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n"); err != nil { - return - } - if err := buffered.Flush(); err != nil { - return - } - copyDone := make(chan struct{}, 1) - go func() { - _, _ = io.Copy(upstreamConn, buffered) - copyDone <- struct{}{} - }() - _, _ = io.Copy(clientConn, bufio.NewReader(upstreamConn)) - <-copyDone - })) - if tc.proxyTLS { - proxy.StartTLS() - } else { - proxy.Start() - } - defer proxy.Close() - - proxyURL, err := url.Parse(proxy.URL) - if err != nil { - t.Fatalf("Parse proxy URL returned error: %v", err) - } - proxyURL.User = url.UserPassword("proxy-user", "proxy-pass") - client := NewHTTPClient(&http.Client{}) - client.modelProbeTLSConfig = &utls.Config{InsecureSkipVerify: true} - if tc.proxyTLS { - client.modelProbeProxyTLSConfig = &tls.Config{InsecureSkipVerify: true} - } - - result := client.ProbeAccountModel(context.Background(), Config{}, SelectedAccount{ - AccountType: AccountTypeAPIUpstream, - AuthorizationToken: "api-secret", - BaseURL: upstream.URL + "/v1", - ProxyURL: proxyURL.String(), - FingerprintTLS: "chrome", - }, "gpt-test") - - if result.statusCode != http.StatusOK || result.errorCode != "" { - t.Fatalf("ProbeAccountModel result = %+v, want success", result) - } - select { - case <-upstreamReached: - case <-time.After(time.Second): - t.Fatal("TLS upstream was not reached through proxy tunnel") - } - mu.Lock() - defer mu.Unlock() - if gotMethod != http.MethodConnect || gotTarget != strings.TrimPrefix(upstream.URL, "https://") { - t.Fatalf("proxy request method=%q target=%q, want CONNECT %q", gotMethod, gotTarget, strings.TrimPrefix(upstream.URL, "https://")) - } - if gotProxyAuthorization != "Basic cHJveHktdXNlcjpwcm94eS1wYXNz" { - t.Fatalf("Proxy-Authorization = %q, want Basic credentials", gotProxyAuthorization) - } - }) - } -} - func TestHTTPClientProbeAccountModelValidatesAPIUpstreamJSON(t *testing.T) { tests := []struct { name string @@ -451,6 +33,11 @@ func TestHTTPClientProbeAccountModelValidatesAPIUpstreamJSON(t *testing.T) { gotAuthorization = r.Header.Get("Authorization") gotUserAgent = r.Header.Get("User-Agent") gotHeader = r.Header.Get("X-Fingerprint") + for _, name := range []string{"Originator", "Version", "ChatGPT-Account-ID", "OpenAI-Beta"} { + if value := r.Header.Get(name); value != "" { + t.Fatalf("unexpected identity header %s=%q", name, value) + } + } return &http.Response{ StatusCode: test.statusCode, Body: io.NopCloser(strings.NewReader(test.body)), @@ -463,14 +50,12 @@ func TestHTTPClientProbeAccountModelValidatesAPIUpstreamJSON(t *testing.T) { AccountType: AccountTypeAPIUpstream, AuthorizationToken: "api-secret", BaseURL: "https://upstream.example.test/v1", - FingerprintUA: "profile-agent", - FingerprintHeaders: map[string]string{"X-Fingerprint": "profile"}, }, "gpt-test") if result.statusCode != test.statusCode || result.errorCode != test.wantCode { t.Fatalf("ProbeAccountModel result = %+v, want status=%d code=%q", result, test.statusCode, test.wantCode) } - if gotPath != "/v1/chat/completions" || gotAuthorization != "Bearer api-secret" || gotUserAgent != "profile-agent" || gotHeader != "profile" { + if gotPath != "/v1/chat/completions" || gotAuthorization != "Bearer api-secret" || strings.Contains(gotUserAgent, "codex") || gotHeader != "" { t.Fatalf("request path=%q auth=%q ua=%q fingerprint=%q", gotPath, gotAuthorization, gotUserAgent, gotHeader) } }) @@ -482,44 +67,3 @@ type roundTripFunc func(*http.Request) (*http.Response, error) func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } - -type contextDeadlineBody struct { - ctx context.Context -} - -func (b *contextDeadlineBody) Read([]byte) (int, error) { - <-b.ctx.Done() - return 0, b.ctx.Err() -} - -func (b *contextDeadlineBody) Close() error { - return nil -} - -type failOnReadBody struct { - t *testing.T - closed bool -} - -func (b *failOnReadBody) Read(_ []byte) (int, error) { - b.t.Fatal("successful codex probe stream body should not be read") - return 0, io.EOF -} - -func (b *failOnReadBody) Close() error { - b.closed = true - return nil -} - -func jsonResponse(status int, value any) (*http.Response, error) { - body := new(strings.Builder) - if err := json.NewEncoder(body).Encode(value); err != nil { - return nil, err - } - return &http.Response{ - StatusCode: status, - Body: io.NopCloser(strings.NewReader(body.String())), - Header: http.Header{"Content-Type": []string{"application/json"}}, - Request: &http.Request{URL: &url.URL{Scheme: "https", Host: "auth.example.test"}}, - }, nil -} diff --git a/backend/internal/provider/model_probe_transport.go b/backend/internal/provider/model_probe_transport.go deleted file mode 100644 index 595aa493..00000000 --- a/backend/internal/provider/model_probe_transport.go +++ /dev/null @@ -1,259 +0,0 @@ -package provider - -import ( - "bufio" - "context" - "crypto/tls" - "encoding/base64" - "errors" - "fmt" - "io" - "net" - "net/http" - "net/url" - "strings" - "sync" - "time" - - utls "github.com/refraction-networking/utls" -) - -type modelProbeTLSFingerprintContextKey struct{} - -func contextWithModelProbeTLSFingerprint(ctx context.Context, fingerprint string) context.Context { - fingerprint = normalizeModelProbeTLSFingerprint(fingerprint) - if fingerprint == "" { - return ctx - } - return context.WithValue(ctx, modelProbeTLSFingerprintContextKey{}, fingerprint) -} - -type modelProbeTLSFingerprintTransport struct { - base http.RoundTripper - dialer *net.Dialer - proxyURL *url.URL - tlsConfig *utls.Config - proxyTLSConfig *tls.Config -} - -func newModelProbeTLSFingerprintTransport(base http.RoundTripper, proxyURL string) http.RoundTripper { - if base == nil { - base = http.DefaultTransport - } - transport := &modelProbeTLSFingerprintTransport{ - base: base, - dialer: &net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - }, - } - if parsed, err := url.Parse(strings.TrimSpace(proxyURL)); err == nil && parsed.IsAbs() && parsed.Host != "" { - transport.proxyURL = parsed - } - return transport -} - -func (t *modelProbeTLSFingerprintTransport) RoundTrip(req *http.Request) (*http.Response, error) { - fingerprint, _ := req.Context().Value(modelProbeTLSFingerprintContextKey{}).(string) - fingerprint = normalizeModelProbeTLSFingerprint(fingerprint) - if fingerprint == "" || !strings.EqualFold(req.URL.Scheme, "https") { - return t.base.RoundTrip(req) - } - host := req.URL.Hostname() - if host == "" { - return nil, fmt.Errorf("missing upstream hostname") - } - addr := req.URL.Host - if _, _, err := net.SplitHostPort(addr); err != nil { - addr = net.JoinHostPort(addr, "443") - } - conn, err := t.dialTarget(req.Context(), addr) - if err != nil { - return nil, err - } - lifecycle := newModelProbeConnLifecycle(req.Context(), conn) - if t.proxyURL != nil { - conn, err = t.connectProxyTunnel(req.Context(), conn, addr) - if err != nil { - _ = lifecycle.Close() - return nil, err - } - } - tlsConfig := &utls.Config{} - if t.tlsConfig != nil { - tlsConfig = t.tlsConfig.Clone() - } - tlsConfig.ServerName = host - tlsConfig.NextProtos = []string{"http/1.1"} - uconn := utls.UClient(conn, tlsConfig, modelProbeClientHelloID(fingerprint)) - if err := uconn.HandshakeContext(req.Context()); err != nil { - _ = lifecycle.Close() - return nil, err - } - if negotiated := uconn.ConnectionState().NegotiatedProtocol; negotiated != "" && negotiated != "http/1.1" { - _ = lifecycle.Close() - return nil, fmt.Errorf("unsupported negotiated protocol %q for fingerprint transport", negotiated) - } - if err := req.Write(uconn); err != nil { - _ = lifecycle.Close() - return nil, err - } - resp, err := http.ReadResponse(bufio.NewReader(uconn), req) - if err != nil { - _ = lifecycle.Close() - return nil, err - } - resp.Body = &modelProbeCloseWithConn{ - ReadCloser: resp.Body, - lifecycle: lifecycle, - watcherDone: lifecycle.watcherDone, - } - return resp, nil -} - -func (t *modelProbeTLSFingerprintTransport) dialTarget(ctx context.Context, targetAddr string) (net.Conn, error) { - addr := targetAddr - if t.proxyURL != nil { - addr = t.proxyURL.Host - if _, _, err := net.SplitHostPort(addr); err != nil { - port := "80" - if strings.EqualFold(t.proxyURL.Scheme, "https") { - port = "443" - } - addr = net.JoinHostPort(t.proxyURL.Hostname(), port) - } - } - return t.dialer.DialContext(ctx, "tcp", addr) -} - -func (t *modelProbeTLSFingerprintTransport) connectProxyTunnel(ctx context.Context, conn net.Conn, targetAddr string) (net.Conn, error) { - if strings.EqualFold(t.proxyURL.Scheme, "https") { - proxyTLSConfig := &tls.Config{MinVersion: tls.VersionTLS12} - if t.proxyTLSConfig != nil { - proxyTLSConfig = t.proxyTLSConfig.Clone() - } - proxyTLSConfig.ServerName = t.proxyURL.Hostname() - proxyTLSConfig.NextProtos = []string{"http/1.1"} - tlsConn := tls.Client(conn, proxyTLSConfig) - if err := tlsConn.HandshakeContext(ctx); err != nil { - return nil, fmt.Errorf("TLS handshake with proxy: %w", err) - } - conn = tlsConn - } else if !strings.EqualFold(t.proxyURL.Scheme, "http") { - return nil, fmt.Errorf("unsupported proxy scheme %q", t.proxyURL.Scheme) - } - - connectReq := &http.Request{ - Method: http.MethodConnect, - URL: &url.URL{Opaque: targetAddr}, - Host: targetAddr, - Header: make(http.Header), - } - if t.proxyURL.User != nil { - username := t.proxyURL.User.Username() - password, _ := t.proxyURL.User.Password() - credentials := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) - connectReq.Header.Set("Proxy-Authorization", "Basic "+credentials) - } - if err := connectReq.Write(conn); err != nil { - return nil, fmt.Errorf("write proxy CONNECT request: %w", err) - } - reader := bufio.NewReader(conn) - resp, err := http.ReadResponse(reader, connectReq) - if err != nil { - return nil, fmt.Errorf("read proxy CONNECT response: %w", err) - } - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("proxy CONNECT returned %s", resp.Status) - } - if reader.Buffered() == 0 { - return conn, nil - } - return &modelProbeBufferedConn{Conn: conn, reader: reader}, nil -} - -type modelProbeBufferedConn struct { - net.Conn - reader *bufio.Reader -} - -func (c *modelProbeBufferedConn) Read(p []byte) (int, error) { - return c.reader.Read(p) -} - -type modelProbeConnLifecycle struct { - conn net.Conn - stop chan struct{} - watcherDone chan struct{} - once sync.Once - closeErr error -} - -func newModelProbeConnLifecycle(ctx context.Context, conn net.Conn) *modelProbeConnLifecycle { - lifecycle := &modelProbeConnLifecycle{ - conn: conn, - stop: make(chan struct{}), - watcherDone: make(chan struct{}), - } - go func() { - defer close(lifecycle.watcherDone) - select { - case <-ctx.Done(): - _ = lifecycle.Close() - case <-lifecycle.stop: - } - }() - return lifecycle -} - -func (l *modelProbeConnLifecycle) Close() error { - l.once.Do(func() { - close(l.stop) - l.closeErr = l.conn.Close() - }) - return l.closeErr -} - -type modelProbeCloseWithConn struct { - io.ReadCloser - lifecycle *modelProbeConnLifecycle - watcherDone <-chan struct{} -} - -func (b *modelProbeCloseWithConn) Close() error { - connErr := b.lifecycle.Close() - bodyErr := b.ReadCloser.Close() - if bodyErr != nil && !errors.Is(bodyErr, net.ErrClosed) { - return bodyErr - } - return connErr -} - -func normalizeModelProbeTLSFingerprint(fingerprint string) string { - return strings.ToLower(strings.TrimSpace(fingerprint)) -} - -func modelProbeClientHelloID(fingerprint string) utls.ClientHelloID { - switch normalizeModelProbeTLSFingerprint(fingerprint) { - case "chrome": - return utls.HelloChrome_Auto - case "firefox": - return utls.HelloFirefox_Auto - case "safari": - return utls.HelloSafari_Auto - case "ios": - return utls.HelloIOS_Auto - case "android": - return utls.HelloAndroid_11_OkHttp - case "edge": - return utls.HelloEdge_Auto - case "randomized": - return utls.HelloRandomized - case "randomized-alpn": - return utls.HelloRandomizedALPN - case "randomized-no-alpn": - return utls.HelloRandomizedNoALPN - default: - return utls.HelloGolang - } -} diff --git a/backend/internal/provider/model_probe_transport_test.go b/backend/internal/provider/model_probe_transport_test.go deleted file mode 100644 index db8f6980..00000000 --- a/backend/internal/provider/model_probe_transport_test.go +++ /dev/null @@ -1,103 +0,0 @@ -package provider - -import ( - "context" - "crypto/x509" - "io" - "net/http" - "net/http/httptest" - "testing" - "time" - - utls "github.com/refraction-networking/utls" -) - -func TestModelProbeTLSFingerprintTransportCancelsBlockedResponseBody(t *testing.T) { - headersWritten := make(chan struct{}) - releaseHandler := make(chan struct{}) - upstream := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/event-stream") - w.WriteHeader(http.StatusOK) - w.(http.Flusher).Flush() - close(headersWritten) - select { - case <-r.Context().Done(): - case <-releaseHandler: - } - })) - defer func() { - close(releaseHandler) - upstream.Close() - }() - - pool := x509.NewCertPool() - pool.AddCert(upstream.Certificate()) - transport := newModelProbeTLSFingerprintTransport(http.DefaultTransport, "").(*modelProbeTLSFingerprintTransport) - transport.tlsConfig = &utls.Config{RootCAs: pool} - client := &http.Client{Transport: transport} - - ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) - defer cancel() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, upstream.URL, nil) - if err != nil { - t.Fatalf("NewRequestWithContext returned error: %v", err) - } - req = req.WithContext(contextWithModelProbeTLSFingerprint(req.Context(), "chrome")) - resp, err := client.Do(req) - if err != nil { - t.Fatalf("Do returned error before response headers: %v", err) - } - defer resp.Body.Close() - <-headersWritten - - body, ok := resp.Body.(*modelProbeCloseWithConn) - if !ok { - t.Fatalf("response body type = %T, want *modelProbeCloseWithConn", resp.Body) - } - readDone := make(chan error, 1) - go func() { - _, readErr := io.ReadAll(resp.Body) - readDone <- readErr - }() - - select { - case readErr := <-readDone: - if readErr == nil { - t.Fatal("blocked response body read returned nil error after context deadline") - } - case <-time.After(time.Second): - t.Fatal("blocked response body read did not exit after context deadline") - } - select { - case <-body.watcherDone: - case <-time.After(time.Second): - t.Fatal("context watcher did not exit after closing the connection") - } -} - -func TestModelProbeTLSFingerprintTransportUsesBaseWithoutFingerprint(t *testing.T) { - baseCalled := false - base := roundTripFunc(func(req *http.Request) (*http.Response, error) { - baseCalled = true - return &http.Response{ - StatusCode: http.StatusNoContent, - Body: http.NoBody, - Header: make(http.Header), - Request: req, - }, nil - }) - transport := newModelProbeTLSFingerprintTransport(base, "http://proxy.example.test:8080") - req, err := http.NewRequest(http.MethodGet, "https://upstream.example.test", nil) - if err != nil { - t.Fatalf("NewRequest returned error: %v", err) - } - - resp, err := transport.RoundTrip(req) - if err != nil { - t.Fatalf("RoundTrip returned error: %v", err) - } - defer resp.Body.Close() - if !baseCalled || resp.StatusCode != http.StatusNoContent { - t.Fatalf("base called=%v status=%d, want base response", baseCalled, resp.StatusCode) - } -} diff --git a/backend/internal/provider/scheduling.go b/backend/internal/provider/scheduling.go index b83e8d61..462b6746 100644 --- a/backend/internal/provider/scheduling.go +++ b/backend/internal/provider/scheduling.go @@ -299,6 +299,9 @@ func (s *Service) responseAffinityTopology(ctx context.Context, primaryPoolID in return nil, chainLabel, err } for _, account := range accounts { + if account.AccountType != AccountTypeAPIUpstream { + continue + } topology = append(topology, responseAffinityCandidate{account: account, pool: pool, depth: depth}) } } @@ -558,6 +561,9 @@ func (s *Service) unschedulableSelectionCandidatesForEndpoint(ctx context.Contex candidates := make([]SelectionCandidate, 0, len(accounts)) for _, account := range accounts { + if account.AccountType != AccountTypeAPIUpstream { + continue + } if _, ok := selectedIDs[account.ID]; ok { continue } @@ -589,6 +595,9 @@ func (s *Service) unschedulableSelectionCandidatesInRoutingPoolForEndpoint(ctx c candidates := make([]SelectionCandidate, 0, len(accounts)) for _, account := range accounts { + if account.AccountType != AccountTypeAPIUpstream { + continue + } if _, ok := selectedIDs[account.ID]; ok { continue } diff --git a/backend/internal/provider/service.go b/backend/internal/provider/service.go index 558a2e26..83599f06 100644 --- a/backend/internal/provider/service.go +++ b/backend/internal/provider/service.go @@ -1,15 +1,8 @@ package provider import ( - "bufio" "bytes" "context" - "crypto/rand" - "crypto/sha256" - "crypto/subtle" - "crypto/tls" - "encoding/base64" - "encoding/hex" "encoding/json" "errors" "fmt" @@ -23,27 +16,12 @@ import ( "github.com/KnowSky404/N2API/backend/internal/secret" "github.com/KnowSky404/N2API/backend/internal/systemevent" - utls "github.com/refraction-networking/utls" ) const ( - defaultStateTTL = 10 * time.Minute - defaultRefreshWindow = 2 * time.Minute - defaultCircuitOpen = 5 * time.Minute - defaultManualPause = 5 * time.Minute - maxManualPause = 24 * time.Hour - defaultOAuthModelCatalogCacheTTL = 6 * time.Hour - maxOAuthModelCatalogCacheEntries = 32 - maxOAuthModelCatalogFlights = 8 - - refreshFailureCircuitThreshold = 3 - - defaultOpenAIOAuthClientID = "app_EMoamEEZ73f0CkXaXp7hrann" - defaultOpenAIOAuthRedirectURL = "http://localhost:1455/auth/callback" - defaultOpenAIOAuthAuthURL = "https://auth.openai.com/oauth/authorize" - defaultOpenAIOAuthTokenURL = "https://auth.openai.com/oauth/token" - defaultOpenAIOAuthScopes = "openid profile email offline_access" - defaultOpenAIOAuthRefreshScopes = "openid profile email" + defaultCircuitOpen = 5 * time.Minute + defaultManualPause = 5 * time.Minute + maxManualPause = 24 * time.Hour ) func withProviderEventIntent(ctx context.Context, intent systemevent.EventIntent) context.Context { @@ -76,45 +54,6 @@ func providerAuditIntent(action systemevent.Action, id int64, name string) syste } } -func oauthRefreshIntent(account Account, trigger RefreshTrigger, succeeded bool, circuitOpenUntil *time.Time) systemevent.EventIntent { - action := systemevent.ActionOAuthRefreshAutomaticFailed - severity := systemevent.SeverityWarning - outcome := systemevent.OutcomeFailure - if trigger == RefreshTriggerManual { - action = systemevent.ActionOAuthRefreshManualFailed - } else if !succeeded && trigger == RefreshTriggerModelTest { - action = systemevent.ActionOAuthRefreshDiagnosticFailed - } - if succeeded { - severity = systemevent.SeverityInfo - outcome = systemevent.OutcomeSuccess - if trigger == RefreshTriggerManual { - action = systemevent.ActionOAuthRefreshManualSucceeded - } else { - action = systemevent.ActionOAuthRefreshAutomaticSucceeded - } - } - metadata := map[string]any{ - "trigger": string(trigger), - "failure_count": account.FailureCount, - } - if account.AccessTokenExpiresAt != nil { - metadata["expiry_at"] = account.AccessTokenExpiresAt.UTC() - } - if circuitOpenUntil != nil { - metadata["circuit_open_until"] = circuitOpenUntil.UTC() - } - return systemevent.EventIntent{ - Category: systemevent.CategoryOAuth, - Severity: severity, - Action: action, - Outcome: outcome, - Target: providerAccountTarget(account.ID, accountDisplayName(account)), - ErrorCode: map[bool]string{true: "", false: "oauth_refresh_failed"}[succeeded], - Metadata: metadata, - } -} - func runtimeAccountIntent(action systemevent.Action, id int64, status string) systemevent.EventIntent { severity := systemevent.SeverityWarning outcome := systemevent.OutcomeFailure @@ -132,39 +71,11 @@ func runtimeAccountIntent(action systemevent.Action, id int64, status string) sy } } -type RefreshTrigger string - -const ( - RefreshTriggerManual RefreshTrigger = "manual" - RefreshTriggerGatewayRequest RefreshTrigger = "gateway_request" - RefreshTriggerAccountTest RefreshTrigger = "account_test" - RefreshTriggerModelTest RefreshTrigger = "model_test" - RefreshTriggerModelSync RefreshTrigger = "model_sync" - RefreshTriggerStatusProbe RefreshTrigger = "status_probe" -) - const ( AccountTypeCodexOAuth = "codex_oauth" AccountTypeAPIUpstream = "api_upstream" ) -const ( - DefaultCodexFingerprintSystemKey = "codex_cli_default" - DefaultCodexFingerprintName = "Default Codex CLI" - DefaultCodexFingerprintDescription = "System-managed Codex TUI outbound identity for OAuth accounts." - DefaultCodexFingerprintUserAgent = "codex-tui/0.135.0 (Mac OS 26.5.0; arm64) iTerm.app/3.6.10 (codex-tui; 0.135.0)" - DefaultCodexFingerprintOriginator = "codex-tui" - DefaultCodexFingerprintVersion = "0.135.0" - DefaultCodexFingerprintTLS = "" -) - -func DefaultCodexFingerprintHeaders() map[string]string { - return map[string]string{ - "Originator": DefaultCodexFingerprintOriginator, - "Version": DefaultCodexFingerprintVersion, - } -} - const ( RoutingPoolErrorDisabled = "routing_pool_disabled" RoutingPoolErrorUnavailable = "routing_pool_unavailable" @@ -221,31 +132,18 @@ var ( ErrRoutingPoolCycle = errors.New("routing pool fallback cycle") ErrRoutingPoolExhausted = errors.New("routing pool fallback chain exhausted") ErrEndpointUnavailable = errors.New("provider endpoint unavailable") - ErrOAuthModelCatalogBusy = errors.New("oauth model catalog refresh busy") - ErrOAuthModelCatalogStale = errors.New("oauth model catalog refresh superseded") ) type Config struct { - Provider string - ClientID string - ClientSecret string - RedirectURL string - AuthURL string - TokenURL string - APIBaseURL string - CodexResponsesBaseURL string - ProxyURL string - ProbeChatGPTAccountID string - Secret string - EncryptionKeyring *secret.Keyring - StateTTL time.Duration - RefreshWindow time.Duration - OAuthModelCatalogCacheTTL time.Duration - CodeVerifier string - AllowHTTPAPIUpstreams bool - AccountTestLogger AccountTestRequestLogger - RequestLogObserver RequestLogWriteObserver - Metrics MetricsObserver + Provider string + APIBaseURL string + ProxyURL string + Secret string + EncryptionKeyring *secret.Keyring + AllowHTTPAPIUpstreams bool + AccountTestLogger AccountTestRequestLogger + RequestLogObserver RequestLogWriteObserver + Metrics MetricsObserver } type MetricsObserver interface { @@ -261,26 +159,6 @@ type Status struct { LastRefreshAt *time.Time `json:"lastRefreshAt"` } -type ConnectResult struct { - AuthorizationURL string -} - -type Fingerprint struct { - Value string - UserAgent string - IP string -} - -type ConnectOptions struct { - RedirectAfter string - Name string - Priority int - Enabled *bool - TargetAccountID int64 - FingerprintProfileID *int64 - Fingerprint Fingerprint -} - type OAuthState struct { Provider string StateHash string @@ -332,13 +210,13 @@ type Account struct { Metadata map[string]string `json:"metadata"` Status string `json:"status"` StatusReason string `json:"statusReason"` - FingerprintHash string `json:"fingerprintHash"` + FingerprintHash string `json:"-"` UserAgentHash string `json:"userAgentHash"` IPHash string `json:"ipHash"` FailureCount int `json:"failureCount"` CircuitOpenUntil *time.Time `json:"circuitOpenUntil"` RateLimitedUntil *time.Time `json:"rateLimitedUntil"` - FingerprintProfileID *int64 `json:"fingerprintProfileId"` + FingerprintProfileID *int64 `json:"-"` LastRefreshError string `json:"lastRefreshError"` LastRefreshErrorAt *time.Time `json:"lastRefreshErrorAt"` CreatedAt time.Time `json:"createdAt"` @@ -455,20 +333,6 @@ type AccountModelSyncSummary struct { SkippedManual int `json:"skippedManual"` } -// OAuthModelCatalogStatus reports the latest account-scoped catalog lifecycle -// state. Nil timestamps and durations mean that the corresponding observation -// has not been recorded for the current credential/configuration generation. -type OAuthModelCatalogStatus struct { - LastAttemptAt *time.Time `json:"lastAttemptAt,omitempty"` - LastSuccessAt *time.Time `json:"lastSuccessAt,omitempty"` - LastFailureAt *time.Time `json:"lastFailureAt,omitempty"` - LastError string `json:"lastError,omitempty"` - Source string `json:"source,omitempty"` - CacheHit *bool `json:"cacheHit,omitempty"` - UpstreamFetchMS *int64 `json:"upstreamFetchMs,omitempty"` - LocalApplyMS *int64 `json:"localApplyMs,omitempty"` -} - type APIUpstreamInput struct { Name string `json:"name"` BaseURL string `json:"baseUrl"` @@ -477,7 +341,7 @@ type APIUpstreamInput struct { Enabled *bool `json:"enabled"` Priority int `json:"priority"` LoadFactor int `json:"loadFactor"` - FingerprintProfileID *int64 `json:"fingerprintProfileId"` + FingerprintProfileID *int64 `json:"-"` Models []string `json:"models"` } @@ -510,16 +374,12 @@ type SelectedAccount struct { AuthorizationToken string BaseURL string ProxyURL string - ChatGPTAccountID string MaxConcurrentRequests int RoutingPoolID int64 RoutingPoolName string RoutingPoolFallbackDepth int RoutingPoolFallbackChain string RoutingPoolError string - FingerprintUA string - FingerprintTLS string - FingerprintHeaders map[string]string } type SelectionPreview struct { @@ -533,9 +393,6 @@ type SelectionPreview struct { RoutingPoolFallbackDepth int `json:"routingPoolFallbackDepth,omitempty"` RoutingPoolFallbackChain string `json:"routingPoolFallbackChain,omitempty"` RoutingPoolError string - FingerprintUA string - FingerprintTLS string `json:"fingerprintTLS,omitempty"` - FingerprintHeaders map[string]string `json:"fingerprintHeaders,omitempty"` Candidates []SelectionCandidate `json:"candidates"` } @@ -559,57 +416,15 @@ type SelectionCandidate struct { EndpointCapability string `json:"endpointCapability,omitempty"` } -type TokenResponse struct { - AccessToken string - RefreshToken string - IDToken string - ExpiresIn int - Subject string - DisplayName string - AccountID string - Email string - PlanType string - ClientID string -} - -type idTokenClaims struct { - Subject string `json:"sub"` - Email string `json:"email"` - OpenAIAuth *openAIAuthClaims `json:"https://api.openai.com/auth"` -} - -type openAIAuthClaims struct { - ChatGPTAccountID string `json:"chatgpt_account_id"` - ChatGPTUserID string `json:"chatgpt_user_id"` - ChatGPTPlanType string `json:"chatgpt_plan_type"` - UserID string `json:"user_id"` - POID string `json:"poid"` - Organizations []organizationClaim `json:"organizations"` -} - -type organizationClaim struct { - ID string `json:"id"` - Role string `json:"role"` - Title string `json:"title"` - IsDefault bool `json:"is_default"` -} - type Repository interface { ListAccounts(ctx context.Context, provider string) ([]Account, error) HasEnabledAccounts(ctx context.Context, provider string) (bool, error) - FindAccount(ctx context.Context, provider string) (Account, error) FindAccountByID(ctx context.Context, provider string, id int64) (Account, error) - FindAccountByIdentity(ctx context.Context, provider string, identities AccountIdentities) (Account, error) SaveAccount(ctx context.Context, account Account) (Account, error) - UpdateOAuthCredential(ctx context.Context, provider string, accountID int64, credential AccountCredential) error UpdateAccount(ctx context.Context, provider string, id int64, update AccountUpdate) (Account, error) DeleteAccount(ctx context.Context, provider string, id int64) error - DeleteAccounts(ctx context.Context, provider string) error - EnsureDefaultCodexFingerprintProfile(ctx context.Context) (int64, error) - FindFingerprintProfileByID(ctx context.Context, id int64) (FingerprintProfileData, error) MarkAccountUsed(ctx context.Context, provider string, id int64, usedAt time.Time) error MarkAccountError(ctx context.Context, provider string, id int64, message string, at time.Time) error - RecordRefreshFailure(ctx context.Context, provider string, id int64, message string, at time.Time, openUntil *time.Time) error RecordAccountStatus(ctx context.Context, provider string, id int64, status, reason string, at time.Time, rateLimitedUntil, circuitOpenUntil *time.Time) error RecordAccountTestResult(ctx context.Context, provider string, id int64, status, message string, at time.Time) error ListAccountTestResults(ctx context.Context, provider string, accountID int64, limit int) ([]AccountTestResult, error) @@ -618,7 +433,6 @@ type Repository interface { RecordAccountModelTestResult(ctx context.Context, provider string, result AccountModelTestResult) error ReplaceAccountModels(ctx context.Context, provider string, accountID int64, models []AccountModelInput) ([]AccountModel, error) SyncAccountModels(ctx context.Context, provider string, accountID int64, models []AccountModelInput, seenAt time.Time) ([]AccountModel, AccountModelSyncSummary, error) - SyncOAuthAccountModels(ctx context.Context, provider string, accountID int64, models []AccountModelInput, seenAt time.Time) ([]AccountModel, AccountModelSyncSummary, error) ListExposedModelsForRoutingPools(ctx context.Context, provider string, poolIDs []int64) ([]ExposedModel, error) ListEligibleAccountsForModel(ctx context.Context, provider string, model string, excludedAccountIDs []int64, now time.Time) ([]Account, error) FindRoutingPool(ctx context.Context, poolID int64) (RoutingPool, error) @@ -629,8 +443,6 @@ type Repository interface { UpsertSessionBinding(ctx context.Context, provider string, model string, sessionID string, accountID int64) error FindSessionBindingInRoutingPool(ctx context.Context, provider string, routingPoolID int64, model string, sessionID string) (SessionBinding, error) UpsertSessionBindingInRoutingPool(ctx context.Context, provider string, routingPoolID int64, model string, sessionID string, accountID int64) error - CreateState(ctx context.Context, state OAuthState) error - ClaimState(ctx context.Context, provider, stateHash string, now time.Time) (OAuthState, error) } // EndpointAwareRepository is optional so older repository fakes and external @@ -642,10 +454,6 @@ type EndpointAwareRepository interface { ListAccountsForRoutingPoolAndEndpoint(ctx context.Context, provider string, poolID int64, model, endpoint string, excludedAccountIDs []int64, now time.Time) ([]Account, error) } -type oauthRefreshFailureEventRecorder interface { - RecordOAuthRefreshFailureEvent(ctx context.Context, provider string, accountID int64) error -} - type AccountIdentities struct { ChatGPTAccountID string ChatGPTUserID string @@ -653,11 +461,6 @@ type AccountIdentities struct { AccessTokenSHA256 string } -type OAuthClient interface { - ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) - RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) -} - type accountStatusProber interface { ProbeAccountStatus(ctx context.Context, cfg Config, accessToken string) (probeResult, error) } @@ -679,9 +482,7 @@ type modelProbeResult struct { } type HTTPClient struct { - client *http.Client - modelProbeTLSConfig *utls.Config - modelProbeProxyTLSConfig *tls.Config + client *http.Client } type AccountTransportInvalidator interface { @@ -690,26 +491,17 @@ type AccountTransportInvalidator interface { } type Service struct { - repo Repository - client OAuthClient - prober accountStatusProber - modelProber accountModelProber - accountTestRequestLogger AccountTestRequestLogger - requestLogWriteObserver RequestLogWriteObserver - metrics MetricsObserver - cfg Config - encryptionKeyring *secret.Keyring - refreshMu sync.Mutex - refreshLocks map[int64]*sync.Mutex - oauthModelCatalogMu sync.Mutex - oauthModelCatalogCache map[oauthModelCatalogCacheKey]oauthModelCatalogCacheEntry - oauthModelCatalogFlights map[oauthModelCatalogCacheKey]*oauthModelCatalogFlight - oauthModelCatalogStatus map[int64]OAuthModelCatalogStatus - oauthModelCatalogGenerations map[int64]uint64 - oauthModelCatalogNow func() time.Time - httpClient *HTTPClient - transportInvalidatorMu sync.RWMutex - transportInvalidator AccountTransportInvalidator + repo Repository + prober accountStatusProber + modelProber accountModelProber + accountTestRequestLogger AccountTestRequestLogger + requestLogWriteObserver RequestLogWriteObserver + metrics MetricsObserver + cfg Config + encryptionKeyring *secret.Keyring + httpClient *HTTPClient + transportInvalidatorMu sync.RWMutex + transportInvalidator AccountTransportInvalidator } type RequestLogWriteObserver interface { @@ -723,83 +515,25 @@ func NewHTTPClient(client *http.Client) *HTTPClient { return &HTTPClient{client: client} } -func (c *HTTPClient) ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) { - values := url.Values{} - values.Set("grant_type", "authorization_code") - values.Set("code", code) - values.Set("client_id", cfg.ClientID) - if strings.TrimSpace(cfg.ClientSecret) != "" { - values.Set("client_secret", cfg.ClientSecret) - } - values.Set("redirect_uri", cfg.RedirectURL) - if strings.TrimSpace(cfg.CodeVerifier) != "" { - values.Set("code_verifier", cfg.CodeVerifier) - } - return c.postToken(ctx, cfg.TokenURL, values, cfg.ProxyURL) -} - -func (c *HTTPClient) RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) { - values := url.Values{} - values.Set("grant_type", "refresh_token") - values.Set("refresh_token", refreshToken) - values.Set("client_id", cfg.ClientID) - values.Set("scope", defaultOpenAIOAuthRefreshScopes) - if strings.TrimSpace(cfg.ClientSecret) != "" { - values.Set("client_secret", cfg.ClientSecret) - } - return c.postToken(ctx, cfg.TokenURL, values, cfg.ProxyURL) -} - -func NewService(repo Repository, client OAuthClient, cfg Config) *Service { +func NewService(repo Repository, client any, cfg Config) *Service { if cfg.Provider == "" { cfg.Provider = "openai" } - if cfg.ClientID == "" { - cfg.ClientID = defaultOpenAIOAuthClientID - } - if cfg.RedirectURL == "" { - cfg.RedirectURL = defaultOpenAIOAuthRedirectURL - } - if cfg.AuthURL == "" { - cfg.AuthURL = defaultOpenAIOAuthAuthURL - } - if cfg.TokenURL == "" { - cfg.TokenURL = defaultOpenAIOAuthTokenURL - } if cfg.APIBaseURL == "" { cfg.APIBaseURL = "https://api.openai.com" } - if cfg.CodexResponsesBaseURL == "" { - cfg.CodexResponsesBaseURL = "https://chatgpt.com/backend-api/codex" - } - if cfg.StateTTL <= 0 { - cfg.StateTTL = defaultStateTTL - } - if cfg.RefreshWindow <= 0 { - cfg.RefreshWindow = defaultRefreshWindow - } - if cfg.OAuthModelCatalogCacheTTL <= 0 { - cfg.OAuthModelCatalogCacheTTL = defaultOAuthModelCatalogCacheTTL - } prober, _ := client.(accountStatusProber) httpClient := NewHTTPClient(nil) return &Service{ - repo: repo, - client: client, - prober: prober, - modelProber: httpClient, - accountTestRequestLogger: cfg.AccountTestLogger, - requestLogWriteObserver: cfg.RequestLogObserver, - metrics: cfg.Metrics, - cfg: cfg, - encryptionKeyring: cfg.EncryptionKeyring, - httpClient: httpClient, - refreshLocks: make(map[int64]*sync.Mutex), - oauthModelCatalogCache: make(map[oauthModelCatalogCacheKey]oauthModelCatalogCacheEntry), - oauthModelCatalogFlights: make(map[oauthModelCatalogCacheKey]*oauthModelCatalogFlight), - oauthModelCatalogStatus: make(map[int64]OAuthModelCatalogStatus), - oauthModelCatalogGenerations: make(map[int64]uint64), - oauthModelCatalogNow: time.Now, + repo: repo, + prober: prober, + modelProber: httpClient, + accountTestRequestLogger: cfg.AccountTestLogger, + requestLogWriteObserver: cfg.RequestLogObserver, + metrics: cfg.Metrics, + cfg: cfg, + encryptionKeyring: cfg.EncryptionKeyring, + httpClient: httpClient, } } @@ -818,102 +552,21 @@ func (s *Service) invalidateAccountTransport(accountID int64) { } } -func (s *Service) invalidateAllAccountTransports() { - s.transportInvalidatorMu.RLock() - invalidator := s.transportInvalidator - s.transportInvalidatorMu.RUnlock() - if invalidator != nil { - invalidator.InvalidateAllAccountTransports() - } -} - -func (s *Service) invalidateOAuthModelCatalog(accountID int64) { - if s == nil || accountID <= 0 { - return - } - s.oauthModelCatalogMu.Lock() - if s.oauthModelCatalogGenerations == nil { - s.oauthModelCatalogGenerations = make(map[int64]uint64) - } - s.oauthModelCatalogGenerations[accountID]++ - for key := range s.oauthModelCatalogCache { - if key.accountID == accountID { - delete(s.oauthModelCatalogCache, key) - } - } - if s.oauthModelCatalogStatus != nil { - delete(s.oauthModelCatalogStatus, accountID) - } - s.oauthModelCatalogMu.Unlock() -} - -func (s *Service) invalidateAllOAuthModelCatalogs() { - if s == nil { - return - } - s.oauthModelCatalogMu.Lock() - if s.oauthModelCatalogGenerations == nil { - s.oauthModelCatalogGenerations = make(map[int64]uint64) - } - accountIDs := make(map[int64]struct{}, len(s.oauthModelCatalogGenerations)+len(s.oauthModelCatalogCache)+len(s.oauthModelCatalogFlights)) - for accountID := range s.oauthModelCatalogGenerations { - accountIDs[accountID] = struct{}{} - } - for key := range s.oauthModelCatalogCache { - accountIDs[key.accountID] = struct{}{} - } - for key := range s.oauthModelCatalogFlights { - accountIDs[key.accountID] = struct{}{} - } - for accountID := range accountIDs { - s.oauthModelCatalogGenerations[accountID]++ - } - s.oauthModelCatalogCache = make(map[oauthModelCatalogCacheKey]oauthModelCatalogCacheEntry) - s.oauthModelCatalogStatus = make(map[int64]OAuthModelCatalogStatus) - s.oauthModelCatalogMu.Unlock() -} - func (c *HTTPClient) ProbeAccountStatus(ctx context.Context, cfg Config, accessToken string) (probeResult, error) { - chatGPTAccountID := strings.TrimSpace(cfg.ProbeChatGPTAccountID) - if chatGPTAccountID != "" { - codexBaseURL := strings.TrimRight(strings.TrimSpace(cfg.CodexResponsesBaseURL), "/") - if codexBaseURL == "" { - codexBaseURL = "https://chatgpt.com/backend-api/codex" - } - return c.probeURL(ctx, codexBaseURL+"/responses", accessToken, cfg.ProxyURL, func(req *http.Request) { - req.Header.Set("chatgpt-account-id", chatGPTAccountID) - req.Header.Set("Accept", "text/event-stream") - req.Header.Set("OpenAI-Beta", "responses=experimental") - req.Header.Set("originator", DefaultCodexFingerprintOriginator) - req.Header.Set("User-Agent", DefaultCodexFingerprintUserAgent) - req.Header.Set("Version", DefaultCodexFingerprintVersion) - req.Header.Set("Content-Type", "application/json") - }) - } - apiBaseURL := strings.TrimRight(strings.TrimSpace(cfg.APIBaseURL), "/") if apiBaseURL == "" { apiBaseURL = "https://api.openai.com" } - return c.probeURL(ctx, apiBaseURL+"/v1/models", accessToken, cfg.ProxyURL, nil) + return c.probeURL(ctx, upstreamModelsURL(apiBaseURL), accessToken, cfg.ProxyURL) } -func (c *HTTPClient) probeURL(ctx context.Context, targetURL, accessToken, proxyURL string, decorate func(*http.Request)) (probeResult, error) { - var body io.Reader - method := http.MethodGet - if strings.HasSuffix(targetURL, "/responses") { - method = http.MethodPost - body = strings.NewReader(`{"model":"gpt-5.4-mini","instructions":"You are Codex, a coding agent.","input":[{"type":"message","role":"user","content":"n2api account status probe"}],"stream":true,"store":false}`) - } - req, err := http.NewRequestWithContext(ctx, method, targetURL, body) +func (c *HTTPClient) probeURL(ctx context.Context, targetURL, accessToken, proxyURL string) (probeResult, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, targetURL, nil) if err != nil { return probeResult{}, err } req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Accept", "application/json") - if decorate != nil { - decorate(req) - } resp, err := c.clientForProxy(proxyURL).Do(req) if err != nil { @@ -934,34 +587,8 @@ func (c *HTTPClient) ProbeAccountModel(ctx context.Context, cfg Config, selected model = strings.TrimSpace(model) var targetURL string var payload any - if selected.AccountType == AccountTypeCodexOAuth { - baseURL := strings.TrimRight(strings.TrimSpace(cfg.CodexResponsesBaseURL), "/") - if baseURL == "" { - baseURL = "https://chatgpt.com/backend-api/codex" - } - targetURL = baseURL + "/responses" - payload = map[string]any{ - "model": model, - "instructions": "You are Codex, a coding agent.", - "input": []map[string]string{{ - "type": "message", - "role": "user", - "content": "Reply with OK only.", - }}, - "stream": true, - "store": false, - } - } else { - targetURL = upstreamChatCompletionsURL(selected.BaseURL) - payload = map[string]any{ - "model": model, - "messages": []map[string]string{{ - "role": "user", - "content": "Reply with OK only.", - }}, - "stream": false, - } - } + targetURL = upstreamChatCompletionsURL(selected.BaseURL) + payload = map[string]any{"model": model, "messages": []map[string]string{{"role": "user", "content": "Reply with OK only."}}, "stream": false} body, err := json.Marshal(payload) if err != nil { @@ -971,43 +598,10 @@ func (c *HTTPClient) ProbeAccountModel(ctx context.Context, cfg Config, selected if err != nil { return modelProbeResult{errorCode: "network_error", message: "could not create model probe request"} } - for key, value := range selected.FingerprintHeaders { - if strings.EqualFold(key, "Authorization") || strings.EqualFold(key, "Host") { - continue - } - req.Header.Set(key, value) - } - if strings.TrimSpace(selected.FingerprintUA) != "" { - req.Header.Set("User-Agent", strings.TrimSpace(selected.FingerprintUA)) - } req.Header.Set("Authorization", "Bearer "+selected.AuthorizationToken) req.Header.Set("Content-Type", "application/json") - if selected.AccountType == AccountTypeCodexOAuth { - req.Header.Set("Accept", "text/event-stream") - req.Header.Set("chatgpt-account-id", strings.TrimSpace(selected.ChatGPTAccountID)) - req.Header.Set("OpenAI-Beta", "responses=experimental") - req.Header.Set("originator", DefaultCodexFingerprintOriginator) - req.Header.Set("Version", DefaultCodexFingerprintVersion) - if strings.TrimSpace(selected.FingerprintUA) == "" { - req.Header.Set("User-Agent", DefaultCodexFingerprintUserAgent) - } - } else { - req.Header.Set("Accept", "application/json") - } + req.Header.Set("Accept", "application/json") probeClient := c.clientForProxy(selected.ProxyURL) - if strings.TrimSpace(selected.FingerprintTLS) != "" { - cloned := *probeClient - transport := newModelProbeTLSFingerprintTransport(probeClient.Transport, selected.ProxyURL).(*modelProbeTLSFingerprintTransport) - if c.modelProbeTLSConfig != nil { - transport.tlsConfig = c.modelProbeTLSConfig.Clone() - } - if c.modelProbeProxyTLSConfig != nil { - transport.proxyTLSConfig = c.modelProbeProxyTLSConfig.Clone() - } - cloned.Transport = transport - probeClient = &cloned - req = req.WithContext(contextWithModelProbeTLSFingerprint(req.Context(), selected.FingerprintTLS)) - } resp, err := probeClient.Do(req) if err != nil { @@ -1026,14 +620,6 @@ func (c *HTTPClient) ProbeAccountModel(ctx context.Context, cfg Config, selected } } - if selected.AccountType == AccountTypeCodexOAuth { - result := consumeResponsesSSE(resp.Body) - if result.errorCode != "" && errors.Is(ctx.Err(), context.DeadlineExceeded) { - return modelProbeResult{statusCode: resp.StatusCode, errorCode: "timeout", message: "model test timed out"} - } - result.statusCode = resp.StatusCode - return result - } raw, tooLarge, err := readBoundedBody(resp.Body, maxModelProbeBody) if err != nil { if errors.Is(ctx.Err(), context.DeadlineExceeded) { @@ -1058,98 +644,6 @@ func upstreamChatCompletionsURL(baseURL string) string { return trimmed + "/v1/chat/completions" } -func consumeResponsesSSE(body io.Reader) modelProbeResult { - limited := &io.LimitedReader{R: body, N: maxModelProbeBody + 1} - scanner := bufio.NewScanner(limited) - scanner.Buffer(make([]byte, 64<<10), maxModelProbeBody) - eventType := "" - data := bytes.Buffer{} - processEvent := func() (modelProbeResult, bool) { - event := strings.TrimSpace(eventType) - raw := bytes.TrimSpace(data.Bytes()) - if event == "" && len(raw) == 0 { - return modelProbeResult{}, false - } - var payload struct { - Type string `json:"type"` - Message string `json:"message"` - Error *struct { - Message string `json:"message"` - } `json:"error"` - Response *struct { - Error *struct { - Message string `json:"message"` - } `json:"error"` - } `json:"response"` - } - if len(raw) == 0 || json.Unmarshal(raw, &payload) != nil { - switch event { - case "response.completed", "response.failed", "response.incomplete", "error": - return modelProbeResult{errorCode: "invalid_response", message: "upstream returned malformed terminal event"}, true - default: - return modelProbeResult{}, false - } - } - kind := strings.TrimSpace(payload.Type) - if kind == "" { - kind = event - } - message := strings.TrimSpace(payload.Message) - if payload.Error != nil && strings.TrimSpace(payload.Error.Message) != "" { - message = payload.Error.Message - } - if payload.Response != nil && payload.Response.Error != nil && strings.TrimSpace(payload.Response.Error.Message) != "" { - message = payload.Response.Error.Message - } - message = sanitizeDiagnosticMessage(message) - switch kind { - case "response.completed": - return modelProbeResult{}, true - case "response.failed", "error": - if message == "" { - message = "upstream response failed" - } - return modelProbeResult{errorCode: "upstream_error", message: message}, true - case "response.incomplete": - if message == "" { - message = "upstream response was incomplete" - } - return modelProbeResult{errorCode: "invalid_response", message: message}, true - default: - return modelProbeResult{}, false - } - } - - for scanner.Scan() { - line := scanner.Text() - if line == "" { - if result, terminal := processEvent(); terminal { - return result - } - eventType = "" - data.Reset() - continue - } - if strings.HasPrefix(line, "event:") { - eventType = strings.TrimSpace(strings.TrimPrefix(line, "event:")) - continue - } - if strings.HasPrefix(line, "data:") { - if data.Len() > 0 { - data.WriteByte('\n') - } - data.WriteString(strings.TrimSpace(strings.TrimPrefix(line, "data:"))) - } - } - if result, terminal := processEvent(); terminal { - return result - } - if scanner.Err() != nil || limited.N <= 0 { - return modelProbeResult{errorCode: "invalid_response", message: "upstream response exceeded the size limit"} - } - return modelProbeResult{errorCode: "invalid_response", message: "upstream stream ended before response.completed"} -} - func readBoundedBody(body io.Reader, limit int64) ([]byte, bool, error) { raw, err := io.ReadAll(io.LimitReader(body, limit+1)) if err != nil { @@ -1218,9 +712,6 @@ func redactModelProbeSecrets(message string, selected SelectedAccount) string { secrets = append(secrets, password) } } - for _, value := range selected.FingerprintHeaders { - secrets = append(secrets, strings.TrimSpace(value)) - } for _, value := range secrets { if len(value) >= 4 { message = strings.ReplaceAll(message, value, "[redacted]") @@ -1248,52 +739,6 @@ func readErrorMessage(body io.Reader, statusCode int) string { return strings.TrimSpace(string(raw)) } -func (c *HTTPClient) postToken(ctx context.Context, tokenURL string, values url.Values, proxyURL string) (TokenResponse, error) { - req, err := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(values.Encode())) - if err != nil { - return TokenResponse{}, fmt.Errorf("create oauth token request: %w", err) - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Header.Set("Accept", "application/json") - - resp, err := c.clientForProxy(proxyURL).Do(req) - if err != nil { - return TokenResponse{}, fmt.Errorf("send oauth token request: %w", err) - } - defer resp.Body.Close() - - if resp.StatusCode < 200 || resp.StatusCode > 299 { - _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 4096)) - return TokenResponse{}, fmt.Errorf("oauth token endpoint returned status %d", resp.StatusCode) - } - - var payload struct { - AccessToken string `json:"access_token"` - RefreshToken string `json:"refresh_token"` - IDToken string `json:"id_token"` - ExpiresIn int `json:"expires_in"` - Subject string `json:"subject"` - DisplayName string `json:"display_name"` - AccountID string `json:"account_id"` - Email string `json:"email"` - PlanType string `json:"plan_type"` - } - if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&payload); err != nil { - return TokenResponse{}, fmt.Errorf("decode oauth token response: %w", err) - } - return TokenResponse{ - AccessToken: payload.AccessToken, - RefreshToken: payload.RefreshToken, - IDToken: payload.IDToken, - ExpiresIn: payload.ExpiresIn, - Subject: payload.Subject, - DisplayName: payload.DisplayName, - AccountID: payload.AccountID, - Email: payload.Email, - PlanType: payload.PlanType, - }, nil -} - func (c *HTTPClient) clientForProxy(proxyURL string) *http.Client { proxyURL = strings.TrimSpace(proxyURL) if proxyURL == "" { @@ -1315,11 +760,7 @@ func (c *HTTPClient) clientForProxy(proxyURL string) *http.Client { } func (s *Service) Configured() bool { - return strings.TrimSpace(s.cfg.ClientID) != "" && - strings.TrimSpace(s.cfg.RedirectURL) != "" && - strings.TrimSpace(s.cfg.AuthURL) != "" && - strings.TrimSpace(s.cfg.TokenURL) != "" && - (s.encryptionKeyring != nil || strings.TrimSpace(s.cfg.Secret) != "") + return s.encryptionKeyring != nil || strings.TrimSpace(s.cfg.Secret) != "" } func (s *Service) encryptString(kind secret.SecretKind, value string) (string, error) { @@ -1350,165 +791,51 @@ func (s *Service) Status(ctx context.Context) (Status, error) { Configured: s.Configured(), } - account, err := s.repo.FindAccount(ctx, s.cfg.Provider) + accounts, err := s.ListAccounts(ctx) if err != nil { - if errors.Is(err, ErrNotConnected) { - return status, nil - } return Status{}, err } - - status.Connected = true - status.DisplayName = account.DisplayName - status.AccessTokenExpiresAt = account.AccessTokenExpiresAt - status.LastRefreshAt = account.LastRefreshAt - return status, nil -} - -func (s *Service) StartConnect(ctx context.Context, options ConnectOptions) (ConnectResult, error) { - if !s.Configured() { - return ConnectResult{}, ErrNotConfigured - } - if strings.TrimSpace(options.RedirectAfter) == "" { - options.RedirectAfter = "/" - } - if options.FingerprintProfileID != nil { - if *options.FingerprintProfileID <= 0 { - return ConnectResult{}, ErrInvalidInput - } - if _, err := s.repo.FindFingerprintProfileByID(ctx, *options.FingerprintProfileID); err != nil { - return ConnectResult{}, ErrInvalidInput - } - } else { - defaultID, err := s.repo.EnsureDefaultCodexFingerprintProfile(ctx) - if err != nil { - return ConnectResult{}, fmt.Errorf("ensure default fingerprint profile: %w", err) - } - options.FingerprintProfileID = &defaultID - } - - state, err := secret.GenerateToken("oauth_state") - if err != nil { - return ConnectResult{}, fmt.Errorf("generate oauth state: %w", err) - } - codeVerifier, err := generateCodeVerifier() - if err != nil { - return ConnectResult{}, fmt.Errorf("generate code verifier: %w", err) - } - codeChallenge := codeChallengeS256(codeVerifier) - encryptedCodeVerifier, err := s.encryptString(secret.SecretKindOAuthCodeVerifier, codeVerifier) - if err != nil { - return ConnectResult{}, fmt.Errorf("encrypt code verifier: %w", err) - } - - connectCtx := withProviderEventIntent(ctx, systemevent.EventIntent{ - Category: systemevent.CategoryOAuth, - Severity: systemevent.SeverityInfo, - Action: systemevent.ActionOAuthConnectStarted, - Outcome: systemevent.OutcomeSuccess, - Target: systemevent.Target{Type: "oauth_connection", ID: s.cfg.Provider, Name: s.cfg.Provider}, - }) - if err := s.repo.CreateState(connectCtx, OAuthState{ - Provider: s.cfg.Provider, - StateHash: secret.HashAPIKey(state), - RedirectAfter: options.RedirectAfter, - ExpiresAt: time.Now().Add(s.cfg.StateTTL), - CodeVerifier: codeVerifier, - EncryptedCodeVerifier: encryptedCodeVerifier, - CodeVerifierHash: secret.HashAPIKey(codeVerifier), - ClientID: s.cfg.ClientID, - TargetAccountID: options.TargetAccountID, - PendingAccountName: strings.TrimSpace(options.Name), - PendingPriority: options.Priority, - PendingEnabled: options.Enabled, - PendingFingerprintProfileID: options.FingerprintProfileID, - FingerprintHash: hashOptional(options.Fingerprint.Value), - UserAgentHash: hashOptional(options.Fingerprint.UserAgent), - IPHash: hashOptional(options.Fingerprint.IP), - }); err != nil { - return ConnectResult{}, err - } - - authURL, err := url.Parse(s.cfg.AuthURL) - if err != nil { - return ConnectResult{}, fmt.Errorf("parse oauth authorization url: %w", err) + if len(accounts) > 0 { + status.Connected = true + status.DisplayName = accountDisplayName(accounts[0]) } - query := authURL.Query() - query.Set("response_type", "code") - query.Set("client_id", s.cfg.ClientID) - query.Set("redirect_uri", s.cfg.RedirectURL) - query.Set("scope", defaultOpenAIOAuthScopes) - query.Set("state", state) - query.Set("code_challenge", codeChallenge) - query.Set("code_challenge_method", "S256") - query.Set("id_token_add_organizations", "true") - query.Set("codex_cli_simplified_flow", "true") - authURL.RawQuery = query.Encode() - return ConnectResult{AuthorizationURL: authURL.String()}, nil + return status, nil } -func (s *Service) CompleteCallback(ctx context.Context, code, state string) (Account, error) { - code = strings.TrimSpace(code) - state = strings.TrimSpace(state) - if code == "" || state == "" { - return Account{}, ErrInvalidState - } - if !s.Configured() { - return Account{}, ErrNotConfigured - } - - stateHash := secret.HashAPIKey(state) - claimed, err := s.repo.ClaimState(ctx, s.cfg.Provider, stateHash, time.Now()) +func (s *Service) ListAccounts(ctx context.Context) ([]Account, error) { + accounts, err := s.repo.ListAccounts(ctx, s.cfg.Provider) if err != nil { - if errors.Is(err, ErrInvalidState) { - return Account{}, ErrInvalidState - } - return Account{}, err + return nil, err } - codeVerifier := strings.TrimSpace(claimed.CodeVerifier) - if codeVerifier == "" { - codeVerifier, err = s.decryptString(secret.SecretKindOAuthCodeVerifier, claimed.EncryptedCodeVerifier) - if err != nil { - return Account{}, ErrInvalidState + filtered := make([]Account, 0, len(accounts)) + for _, account := range accounts { + if account.AccountType == AccountTypeAPIUpstream { + filtered = append(filtered, s.withProxySummary(account)) } } - if strings.TrimSpace(claimed.CodeVerifierHash) != "" && secret.HashAPIKey(codeVerifier) != claimed.CodeVerifierHash { - return Account{}, ErrInvalidState - } + return filtered, nil +} - exchangeCfg := s.cfg - exchangeCfg.CodeVerifier = codeVerifier - if strings.TrimSpace(claimed.ClientID) != "" { - exchangeCfg.ClientID = claimed.ClientID +func (s *Service) findAPIAccount(ctx context.Context, id int64) (Account, error) { + if id <= 0 { + return Account{}, ErrInvalidInput } - tokens, err := s.client.ExchangeCode(ctx, exchangeCfg, code) + account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, id) if err != nil { return Account{}, err } - account, err := s.storeCallbackTokenResponse(ctx, tokens, claimed) - if err != nil { - return Account{}, err + if account.AccountType != AccountTypeAPIUpstream { + return Account{}, ErrNotConnected } return account, nil } -func (s *Service) ListAccounts(ctx context.Context) ([]Account, error) { - accounts, err := s.repo.ListAccounts(ctx, s.cfg.Provider) - if err != nil { - return nil, err - } - for i := range accounts { - accounts[i] = s.withProxySummary(accounts[i]) - } - return accounts, nil -} - func (s *Service) UpdateAccount(ctx context.Context, id int64, update AccountUpdate) (Account, error) { if id <= 0 { return Account{}, ErrInvalidInput } - if update.Enabled == nil && update.Priority == nil && update.LoadFactor == nil && update.MaxConcurrentRequests == nil && !update.ClearStatus && update.Name == nil && update.APIUpstreamBaseURL == nil && update.APIUpstreamAPIKey == nil && update.ProxyURL == nil && !update.FingerprintProfileIDSet { + if update.Enabled == nil && update.Priority == nil && update.LoadFactor == nil && update.MaxConcurrentRequests == nil && !update.ClearStatus && update.Name == nil && update.APIUpstreamBaseURL == nil && update.APIUpstreamAPIKey == nil && update.ProxyURL == nil { return Account{}, ErrInvalidInput } if update.Priority != nil && *update.Priority < 0 { @@ -1527,25 +854,11 @@ func (s *Service) UpdateAccount(ctx context.Context, id int64, update AccountUpd } update.Name = &name } - if update.FingerprintProfileIDSet && update.FingerprintProfileID != nil { - if *update.FingerprintProfileID <= 0 { - return Account{}, ErrInvalidInput - } - if _, err := s.repo.FindFingerprintProfileByID(ctx, *update.FingerprintProfileID); err != nil { - return Account{}, ErrInvalidInput - } - } baseURLChanged := update.APIUpstreamBaseURL != nil apiKeyChanged := update.APIUpstreamAPIKey != nil proxyChanged := update.ProxyURL != nil - if baseURLChanged || apiKeyChanged { - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, id) - if err != nil { - return Account{}, err - } - if account.AccountType != AccountTypeAPIUpstream { - return Account{}, ErrInvalidInput - } + if _, err := s.findAPIAccount(ctx, id); err != nil { + return Account{}, err } if update.APIUpstreamBaseURL != nil { baseURL := normalizeOpenAICompatibleBaseURL(*update.APIUpstreamBaseURL) @@ -1611,9 +924,6 @@ func (s *Service) UpdateAccount(ctx context.Context, id int64, update AccountUpd if proxyChanged { changed = append(changed, "proxy_url") } - if update.FingerprintProfileIDSet { - changed = append(changed, "fingerprint_profile_id") - } intent.Metadata = map[string]any{ "changed_fields": changed, "api_key_changed": apiKeyChanged, @@ -1622,10 +932,6 @@ func (s *Service) UpdateAccount(ctx context.Context, id int64, update AccountUpd if parent, ok := systemevent.IntentFromContext(ctx); ok && parent.Action == systemevent.ActionProviderAccountStatusReset { intent = parent } - // Advance the catalog generation before mutating the account so an - // in-flight request cannot apply a response fetched with the old - // credential, endpoint, proxy, or account configuration. - s.invalidateOAuthModelCatalog(id) account, err := s.repo.UpdateAccount(withProviderEventIntent(ctx, intent), s.cfg.Provider, id, update) if err != nil { return Account{}, err @@ -1662,14 +968,6 @@ func (s *Service) CreateAPIUpstreamAccount(ctx context.Context, input APIUpstrea if err != nil { return Account{}, err } - if input.FingerprintProfileID != nil { - if *input.FingerprintProfileID <= 0 { - return Account{}, ErrInvalidInput - } - if _, err := s.repo.FindFingerprintProfileByID(ctx, *input.FingerprintProfileID); err != nil { - return Account{}, ErrInvalidInput - } - } encryptedProxyURL := "" if proxyURL != "" { encryptedProxyURL, err = s.encryptString(secret.SecretKindProviderProxyURL, proxyURL) @@ -1683,15 +981,15 @@ func (s *Service) CreateAPIUpstreamAccount(ctx context.Context, input APIUpstrea } createCtx := withProviderEventIntent(ctx, providerAuditIntent(systemevent.ActionProviderAccountCreated, 0, name)) account, err := s.repo.SaveAccount(createCtx, Account{ - Provider: s.cfg.Provider, - AccountType: AccountTypeAPIUpstream, - Name: name, - DisplayName: name, - Enabled: enabled, - Priority: input.Priority, - LoadFactor: normalizedLoadFactor(input.LoadFactor), - Status: AccountStatusActive, - FingerprintProfileID: input.FingerprintProfileID, + Provider: s.cfg.Provider, + AccountType: AccountTypeAPIUpstream, + Name: name, + DisplayName: name, + Enabled: enabled, + Priority: input.Priority, + LoadFactor: normalizedLoadFactor(input.LoadFactor), + Status: AccountStatusActive, + Credential: AccountCredential{ CredentialType: CredentialTypeAPIKey, EncryptedAPIKey: encryptedAPIKey, @@ -1730,11 +1028,10 @@ func normalizeOpenAICompatibleBaseURL(value string) string { } func (s *Service) DisconnectAccount(ctx context.Context, id int64) error { - if id <= 0 { - return ErrInvalidInput + if _, err := s.findAPIAccount(ctx, id); err != nil { + return err } ctx = withProviderEventIntent(ctx, providerAuditIntent(systemevent.ActionProviderAccountDisconnected, id, "")) - s.invalidateOAuthModelCatalog(id) if err := s.repo.DeleteAccount(ctx, s.cfg.Provider, id); err != nil { return err } @@ -1742,22 +1039,6 @@ func (s *Service) DisconnectAccount(ctx context.Context, id int64) error { return nil } -func (s *Service) Disconnect(ctx context.Context) error { - ctx = withProviderEventIntent(ctx, systemevent.EventIntent{ - Category: systemevent.CategoryAudit, - Severity: systemevent.SeverityInfo, - Action: systemevent.ActionProviderAccountDisconnectAll, - Outcome: systemevent.OutcomeSuccess, - Target: systemevent.Target{Type: "provider", ID: s.cfg.Provider, Name: s.cfg.Provider}, - }) - s.invalidateAllOAuthModelCatalogs() - if err := s.repo.DeleteAccounts(ctx, s.cfg.Provider); err != nil { - return err - } - s.invalidateAllAccountTransports() - return nil -} - func normalizeAccountModelInputs(inputs []AccountModelInput) ([]AccountModelInput, error) { models := make([]AccountModelInput, 0, len(inputs)) seen := map[string]bool{} @@ -1790,8 +1071,8 @@ func normalizeAccountModelInputs(inputs []AccountModelInput) ([]AccountModelInpu } func (s *Service) ListAccountModels(ctx context.Context, accountID int64) ([]AccountModel, error) { - if accountID <= 0 { - return nil, ErrInvalidInput + if _, err := s.findAPIAccount(ctx, accountID); err != nil { + return nil, err } return s.repo.ListAccountModels(ctx, s.cfg.Provider, accountID) } @@ -1809,6 +1090,23 @@ func (s *Service) ListAccountModelsForAccounts(ctx context.Context, accountIDs [ seen[accountID] = struct{}{} unique = append(unique, accountID) } + if len(unique) > 0 { + accounts, err := s.repo.ListAccounts(ctx, s.cfg.Provider) + if err != nil { + return nil, err + } + allowed := make(map[int64]bool, len(accounts)) + for _, account := range accounts { + if account.AccountType == AccountTypeAPIUpstream { + allowed[account.ID] = true + } + } + for _, id := range unique { + if !allowed[id] { + return nil, ErrNotConnected + } + } + } return s.repo.ListAccountModelsForAccounts(ctx, s.cfg.Provider, unique) } @@ -1817,7 +1115,7 @@ func (s *Service) TestAccountModel(ctx context.Context, accountID int64, model s if accountID <= 0 || model == "" || len(model) > maxModelNameLen { return AccountModelTestResult{}, ErrInvalidInput } - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, accountID) + account, err := s.findAPIAccount(ctx, accountID) if err != nil { return AccountModelTestResult{}, err } @@ -1836,7 +1134,7 @@ func (s *Service) TestAccountModel(ctx context.Context, accountID int64, model s return AccountModelTestResult{}, ErrNotConnected } - selected, err := s.selectedAccountForDiagnostic(ctx, account) + selected, err := s.selectedAccount(ctx, account) if err != nil { return AccountModelTestResult{}, err } @@ -1887,8 +1185,8 @@ func (s *Service) TestAccountModel(ctx context.Context, accountID int64, model s } func (s *Service) ReplaceAccountModels(ctx context.Context, accountID int64, models []AccountModelInput) ([]AccountModel, error) { - if accountID <= 0 { - return nil, ErrInvalidInput + if _, err := s.findAPIAccount(ctx, accountID); err != nil { + return nil, err } normalized, err := normalizeAccountModelInputs(models) if err != nil { @@ -1911,14 +1209,11 @@ func (s *Service) SyncUpstreamAccountModels(ctx context.Context, accountID int64 if accountID <= 0 { return nil, AccountModelSyncSummary{}, ErrInvalidInput } - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, accountID) + account, err := s.findAPIAccount(ctx, accountID) if err != nil { return nil, AccountModelSyncSummary{}, err } - if account.AccountType != AccountTypeAPIUpstream { - return nil, AccountModelSyncSummary{}, ErrInvalidInput - } - selected, err := s.selectedAccountForModelSync(ctx, account) + selected, err := s.selectedAccount(ctx, account) if err != nil { return nil, AccountModelSyncSummary{}, err } @@ -2045,65 +1340,17 @@ func isEndpointPermissionError(message string) bool { (strings.Contains(lower, "insufficient permissions") && strings.Contains(lower, "scope")) } -func (s *Service) RefreshAccount(ctx context.Context, id int64) (Account, error) { - if id <= 0 { - return Account{}, ErrInvalidInput - } - refreshOutcome := "failure" - defer func() { s.observeProviderRefresh("manual", refreshOutcome) }() - unlock := s.lockAccountRefresh(id) - defer unlock() - - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, id) - if err != nil { - return Account{}, err - } - if strings.TrimSpace(account.AccountType) == AccountTypeAPIUpstream { - refreshOutcome = "skipped" - return Account{}, ErrInvalidInput - } - refreshToken, err := s.decryptString(secret.SecretKindOAuthRefreshToken, account.EncryptedRefreshToken) - if err != nil { - return Account{}, err - } - refreshCfg := s.cfg - refreshCfg.ProxyURL, err = s.accountProxyURL(account) - if err != nil { - return Account{}, err - } - tokens, err := s.client.RefreshToken(ctx, refreshCfg, refreshToken) - if err != nil { - now := time.Now() - var openUntil *time.Time - if account.FailureCount+1 >= refreshFailureCircuitThreshold { - until := now.Add(defaultCircuitOpen) - openUntil = &until - } - failureCtx := withProviderEventIntent(ctx, oauthRefreshIntent(account, RefreshTriggerManual, false, openUntil)) - if markErr := s.repo.RecordRefreshFailure(failureCtx, s.cfg.Provider, account.ID, err.Error(), now, openUntil); markErr != nil { - return Account{}, markErr - } - return Account{}, err - } - refreshed, err := s.storeTokenResponse(ctx, tokens, &account, RefreshTriggerManual) - if err != nil { - return Account{}, err - } - refreshOutcome = "success" - return s.probeLatestAccountStatus(ctx, refreshed, tokens.AccessToken) -} - func (s *Service) TestAccount(ctx context.Context, id int64) (Account, error) { if id <= 0 { return Account{}, ErrInvalidInput } - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, id) + account, err := s.findAPIAccount(ctx, id) if err != nil { return Account{}, err } - selected, err := s.selectedAccountForAccountTest(ctx, account) + selected, err := s.selectedAccount(ctx, account) if err != nil { now := time.Now() if markErr := s.recordAccountTestResult(ctx, account, AccountTestStatusFailed, err.Error(), now); markErr != nil { @@ -2119,12 +1366,7 @@ func (s *Service) TestAccount(ctx context.Context, id int64) (Account, error) { } cfg := s.cfg - if selected.AccountType == AccountTypeAPIUpstream { - cfg.APIBaseURL = selected.BaseURL - cfg.ProbeChatGPTAccountID = "" - } else { - cfg.ProbeChatGPTAccountID = strings.TrimSpace(selected.ChatGPTAccountID) - } + cfg.APIBaseURL = selected.BaseURL cfg.ProxyURL = selected.ProxyURL startedAt := time.Now() @@ -2227,35 +1469,6 @@ func (s *Service) logAccountTestRequest(ctx context.Context, selected SelectedAc } } -func accountStatusTestRoute(cfg Config) string { - if strings.TrimSpace(cfg.ProbeChatGPTAccountID) != "" { - baseURL := strings.TrimRight(strings.TrimSpace(cfg.CodexResponsesBaseURL), "/") - if baseURL == "" { - baseURL = "https://chatgpt.com/backend-api/codex" - } - return requestRoutePath(baseURL+"/responses", "/responses") - } - baseURL := strings.TrimRight(strings.TrimSpace(cfg.APIBaseURL), "/") - if baseURL == "" { - baseURL = "https://api.openai.com" - } - return requestRoutePath(baseURL+"/v1/models", "/v1/models") -} - -func accountStatusTestMethod(cfg Config) string { - if strings.TrimSpace(cfg.ProbeChatGPTAccountID) != "" { - return http.MethodPost - } - return http.MethodGet -} - -func accountStatusTestModel(cfg Config) string { - if strings.TrimSpace(cfg.ProbeChatGPTAccountID) != "" { - return "gpt-5.4-mini" - } - return "" -} - func accountStatusTestError(result probeResult, probeErr error) string { if probeErr != nil { if errors.Is(probeErr, context.DeadlineExceeded) { @@ -2276,17 +1489,6 @@ func accountStatusTestError(result probeResult, probeErr error) string { } } -func accountModelTestRoute(cfg Config, selected SelectedAccount) string { - if selected.AccountType == AccountTypeCodexOAuth { - baseURL := strings.TrimRight(strings.TrimSpace(cfg.CodexResponsesBaseURL), "/") - if baseURL == "" { - baseURL = "https://chatgpt.com/backend-api/codex" - } - return requestRoutePath(baseURL+"/responses", "/responses") - } - return requestRoutePath(upstreamChatCompletionsURL(selected.BaseURL), "/v1/chat/completions") -} - func requestRoutePath(rawURL, fallback string) string { target, err := url.Parse(rawURL) if err != nil || target.Path == "" { @@ -2327,7 +1529,7 @@ func (s *Service) ListAccountTestResults(ctx context.Context, id int64, limit in if id <= 0 { return nil, ErrInvalidInput } - if _, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, id); err != nil { + if _, err := s.findAPIAccount(ctx, id); err != nil { return nil, err } if limit <= 0 { @@ -2349,7 +1551,7 @@ func (s *Service) PauseAccountScheduling(ctx context.Context, id int64, duration if duration > maxManualPause { return Account{}, ErrInvalidInput } - if _, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, id); err != nil { + if _, err := s.findAPIAccount(ctx, id); err != nil { return Account{}, err } now := time.Now() @@ -2358,226 +1560,24 @@ func (s *Service) PauseAccountScheduling(ctx context.Context, id int64, duration if err := s.repo.RecordAccountStatus(pauseCtx, s.cfg.Provider, id, AccountStatusCircuitOpen, "manually paused", now, nil, &until); err != nil { return Account{}, err } - return s.repo.FindAccountByID(ctx, s.cfg.Provider, id) -} - -func (s *Service) probeLatestAccountStatus(ctx context.Context, account Account, accessToken string) (Account, error) { - if s.prober == nil || strings.TrimSpace(accessToken) == "" { - return account, nil - } - cfg := s.cfg - cfg.ProbeChatGPTAccountID = strings.TrimSpace(account.Metadata["chatgpt_account_id"]) - proxyURL, err := s.accountProxyURL(account) - if err != nil { - return account, nil - } - cfg.ProxyURL = proxyURL - result, err := s.prober.ProbeAccountStatus(ctx, cfg, accessToken) - if err != nil { - if markErr := s.recordAccountTestResult(ctx, account, AccountTestStatusFailed, err.Error(), time.Now()); markErr != nil { - return Account{}, markErr - } - return s.repo.FindAccountByID(ctx, s.cfg.Provider, account.ID) - } - if !isSuccessfulAccountStatus(result.statusCode) { - message := strings.TrimSpace(result.message) - if message == "" { - message = http.StatusText(result.statusCode) - } - if markErr := s.recordAccountTestResult(ctx, account, AccountTestStatusFailed, message, time.Now()); markErr != nil { - return Account{}, markErr - } - if err := s.RecordAccountFailure(ctx, account.ID, result.statusCode, result.retryAfter, result.message); err != nil { - return Account{}, err - } - return s.repo.FindAccountByID(ctx, s.cfg.Provider, account.ID) - } - if markErr := s.recordAccountTestResult(ctx, account, AccountTestStatusPassed, "", time.Now()); markErr != nil { - return Account{}, markErr - } - if err := s.RecordAccountRecovered(ctx, account.ID); err != nil { - return Account{}, err - } - return s.repo.FindAccountByID(ctx, s.cfg.Provider, account.ID) + return s.findAPIAccount(ctx, id) } func isSuccessfulAccountStatus(statusCode int) bool { return statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices } -func (s *Service) AccessToken(ctx context.Context) (string, error) { - selected, err := s.SelectAccountForModel(ctx, "") - if err != nil { - return "", err - } - return selected.AuthorizationToken, nil -} - -func (s *Service) AccessTokenForAccount(ctx context.Context, account Account) (string, error) { - return s.accessTokenForAccount(ctx, account, RefreshTriggerGatewayRequest, true) -} - -func (s *Service) RefreshAccountAuthorization(ctx context.Context, accountID int64, rejectedAccessToken string, statusCode int, message string) (accessToken string, retry bool, failureRecorded bool, err error) { - if accountID <= 0 || rejectedAccessToken == "" { - return "", false, false, ErrInvalidInput - } - refreshOutcome := "skipped" - defer func() { s.observeProviderRefresh("rejected_token", refreshOutcome) }() - if statusCode != http.StatusUnauthorized && statusCode != http.StatusForbidden { - return "", false, false, nil - } - if statusCode == http.StatusForbidden && isEndpointPermissionError(message) { - return "", false, false, nil - } - - unlock := s.lockAccountRefresh(accountID) - defer unlock() - - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, accountID) - if err != nil { - refreshOutcome = "failure" - return "", false, false, err - } - account = normalizeAccountCredentialFields(account) - accountType := strings.TrimSpace(account.AccountType) - if accountType == "" { - accountType = AccountTypeCodexOAuth - } - if accountType != AccountTypeCodexOAuth { - return "", false, false, nil - } - - currentAccessToken, err := s.decryptString(secret.SecretKindOAuthAccessToken, account.EncryptedAccessToken) - if err != nil { - refreshOutcome = "failure" - return "", true, false, err - } - if subtle.ConstantTimeCompare([]byte(currentAccessToken), []byte(rejectedAccessToken)) != 1 { - return currentAccessToken, true, false, nil - } - - accessToken, failureRecorded, err = s.refreshAccessTokenLocked(ctx, account, RefreshTriggerGatewayRequest, true) - if err != nil { - refreshOutcome = "failure" - } else { - refreshOutcome = "success" - } - return accessToken, true, failureRecorded, err -} - -func (s *Service) accessTokenForAccount(ctx context.Context, account Account, trigger RefreshTrigger, recordRefreshFailure bool) (string, error) { - account = normalizeAccountCredentialFields(account) - if account.AccessTokenExpiresAt == nil || account.AccessTokenExpiresAt.After(time.Now().Add(s.cfg.RefreshWindow)) { - return s.decryptString(secret.SecretKindOAuthAccessToken, account.EncryptedAccessToken) - } - refreshOutcome := "failure" - defer func() { s.observeProviderRefresh("automatic", refreshOutcome) }() - - if account.ID > 0 { - unlock := s.lockAccountRefresh(account.ID) - defer unlock() - - latest, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, account.ID) - if err != nil { - return "", err - } - latest = normalizeAccountCredentialFields(latest) - if latest.AccessTokenExpiresAt == nil || latest.AccessTokenExpiresAt.After(time.Now().Add(s.cfg.RefreshWindow)) { - refreshOutcome = "skipped" - return s.decryptString(secret.SecretKindOAuthAccessToken, latest.EncryptedAccessToken) - } - account = latest - } - - accessToken, _, err := s.refreshAccessTokenLocked(ctx, account, trigger, recordRefreshFailure) - if err == nil { - refreshOutcome = "success" - } - return accessToken, err -} - -func (s *Service) observeProviderRefresh(mode, outcome string) { - if s != nil && s.metrics != nil { - s.metrics.ObserveProviderRefresh(mode, outcome) - } -} - -func (s *Service) refreshAccessTokenLocked(ctx context.Context, account Account, trigger RefreshTrigger, recordRefreshFailure bool) (string, bool, error) { - account = normalizeAccountCredentialFields(account) - refreshToken, err := s.decryptString(secret.SecretKindOAuthRefreshToken, account.EncryptedRefreshToken) - if err != nil { - return "", false, err - } - refreshCfg := s.cfg - refreshCfg.ProxyURL, err = s.accountProxyURL(account) - if err != nil { - return "", false, err - } - tokens, err := s.client.RefreshToken(ctx, refreshCfg, refreshToken) - if err != nil { - if recordRefreshFailure && account.ID > 0 { - now := time.Now() - var openUntil *time.Time - if account.FailureCount+1 >= refreshFailureCircuitThreshold { - until := now.Add(defaultCircuitOpen) - openUntil = &until - } - failureCtx := withProviderEventIntent(ctx, oauthRefreshIntent(account, trigger, false, openUntil)) - if markErr := s.repo.RecordRefreshFailure(failureCtx, s.cfg.Provider, account.ID, err.Error(), now, openUntil); markErr != nil { - return "", false, markErr - } - return "", true, err - } else if account.ID > 0 { - failureCtx := withProviderEventIntent(ctx, oauthRefreshIntent(account, trigger, false, nil)) - if recorder, ok := s.repo.(oauthRefreshFailureEventRecorder); ok { - if recordErr := recorder.RecordOAuthRefreshFailureEvent(failureCtx, s.cfg.Provider, account.ID); recordErr != nil { - return "", false, recordErr - } - } - } - return "", false, err - } - var refreshed Account - if recordRefreshFailure { - refreshed, err = s.storeTokenResponse(ctx, tokens, &account, trigger) - } else { - refreshed, err = s.storeTokenResponseForDiagnostic(ctx, tokens, &account, trigger) - } - if err != nil { - return "", false, err - } - accessToken, err := s.decryptString(secret.SecretKindOAuthAccessToken, refreshed.EncryptedAccessToken) - return accessToken, false, err -} - func (s *Service) selectedAccount(ctx context.Context, account Account) (SelectedAccount, error) { - return s.selectedAccountWithRefreshFailureRecording(ctx, account, RefreshTriggerGatewayRequest, true) -} - -func (s *Service) selectedAccountForAccountTest(ctx context.Context, account Account) (SelectedAccount, error) { - return s.selectedAccountWithRefreshFailureRecording(ctx, account, RefreshTriggerAccountTest, true) -} - -func (s *Service) selectedAccountForModelSync(ctx context.Context, account Account) (SelectedAccount, error) { - return s.selectedAccountWithRefreshFailureRecording(ctx, account, RefreshTriggerModelSync, true) -} - -func (s *Service) selectedAccountForDiagnostic(ctx context.Context, account Account) (SelectedAccount, error) { - return s.selectedAccountWithRefreshFailureRecording(ctx, account, RefreshTriggerModelTest, false) -} - -func (s *Service) selectedAccountWithRefreshFailureRecording(ctx context.Context, account Account, trigger RefreshTrigger, recordRefreshFailure bool) (SelectedAccount, error) { account = normalizeAccountCredentialFields(account) accountType := strings.TrimSpace(account.AccountType) - if accountType == "" { - accountType = AccountTypeCodexOAuth + if accountType != AccountTypeAPIUpstream { + return SelectedAccount{}, ErrNotConnected } selected := SelectedAccount{ AccountID: account.ID, Provider: valueOrDefault(strings.TrimSpace(account.Provider), s.cfg.Provider), AccountType: accountType, DisplayName: accountDisplayName(account), - ChatGPTAccountID: strings.TrimSpace(account.Metadata["chatgpt_account_id"]), MaxConcurrentRequests: account.MaxConcurrentRequests, } if strings.TrimSpace(account.Credential.EncryptedProxyURL) != "" { @@ -2588,13 +1588,6 @@ func (s *Service) selectedAccountWithRefreshFailureRecording(ctx context.Context selected.ProxyURL = strings.TrimSpace(proxyURL) } switch accountType { - case AccountTypeCodexOAuth: - token, err := s.accessTokenForAccount(ctx, account, trigger, recordRefreshFailure) - if err != nil { - return SelectedAccount{}, err - } - selected.AuthorizationToken = token - selected.BaseURL = strings.TrimRight(strings.TrimSpace(s.cfg.APIBaseURL), "/") case AccountTypeAPIUpstream: token, err := s.decryptString(secret.SecretKindProviderAPIKey, account.Credential.EncryptedAPIKey) if err != nil { @@ -2614,21 +1607,6 @@ func (s *Service) selectedAccountWithRefreshFailureRecording(ctx context.Context return SelectedAccount{}, fmt.Errorf("unsupported account type %q", accountType) } - if account.FingerprintProfileID != nil && *account.FingerprintProfileID > 0 { - fp, fpErr := s.repo.FindFingerprintProfileByID(ctx, *account.FingerprintProfileID) - if fpErr == nil { - if strings.TrimSpace(fp.UserAgent) != "" { - selected.FingerprintUA = strings.TrimSpace(fp.UserAgent) - } - if strings.TrimSpace(fp.TLSFingerprint) != "" { - selected.FingerprintTLS = strings.TrimSpace(fp.TLSFingerprint) - } - if len(fp.Headers) > 0 { - selected.FingerprintHeaders = fp.Headers - } - } - } - return selected, nil } @@ -2649,18 +1627,6 @@ func (s *Service) withProxySummary(account Account) Account { return account } -func (s *Service) accountProxyURL(account Account) (string, error) { - account = normalizeAccountCredentialFields(account) - if strings.TrimSpace(account.Credential.EncryptedProxyURL) == "" { - return "", nil - } - proxyURL, err := s.decryptString(secret.SecretKindProviderProxyURL, account.Credential.EncryptedProxyURL) - if err != nil { - return "", fmt.Errorf("decrypt account proxy: %w", err) - } - return strings.TrimSpace(proxyURL), nil -} - func normalizeProxyURL(value string) (string, error) { proxyURL := strings.TrimSpace(value) if proxyURL == "" { @@ -2938,6 +1904,13 @@ func (s *Service) listAccountsForRoutingPoolAndEndpoint(ctx context.Context, rou } func (s *Service) filterAccountsByEndpoint(ctx context.Context, model, endpoint string, accounts []Account) ([]Account, error) { + filteredAccounts := make([]Account, 0, len(accounts)) + for _, account := range accounts { + if account.AccountType == AccountTypeAPIUpstream { + filteredAccounts = append(filteredAccounts, account) + } + } + accounts = filteredAccounts if strings.TrimSpace(model) == "" || strings.TrimSpace(endpoint) == "" || len(accounts) == 0 { return accounts, nil } @@ -2975,197 +1948,10 @@ func normalizedExcludedAccountIDs(ids []int64) []int64 { return excluded } -func (s *Service) lockAccountRefresh(accountID int64) func() { - s.refreshMu.Lock() - lock := s.refreshLocks[accountID] - if lock == nil { - lock = &sync.Mutex{} - s.refreshLocks[accountID] = lock - } - s.refreshMu.Unlock() - - lock.Lock() - return lock.Unlock -} - -func (s *Service) storeTokenResponse(ctx context.Context, tokens TokenResponse, previous *Account, trigger RefreshTrigger) (Account, error) { - return s.storeTokenResponseWithMode(ctx, tokens, previous, trigger, previous != nil) -} - -func (s *Service) storeTokenResponseForDiagnostic(ctx context.Context, tokens TokenResponse, previous *Account, trigger RefreshTrigger) (Account, error) { - return s.storeTokenResponseWithMode(ctx, tokens, previous, trigger, true) -} - -func (s *Service) storeTokenResponseWithMode(ctx context.Context, tokens TokenResponse, previous *Account, trigger RefreshTrigger, credentialOnly bool) (Account, error) { - if strings.TrimSpace(tokens.AccessToken) == "" { - return Account{}, errors.New("oauth token response missing access token") - } - tokens = enrichTokenResponseFromIDToken(tokens) - - refreshToken := tokens.RefreshToken - subject := tokens.Subject - displayName := tokens.DisplayName - if strings.TrimSpace(subject) == "" { - subject = tokens.AccountID - } - if strings.TrimSpace(displayName) == "" { - displayName = valueOrDefault(tokens.Email, tokens.AccountID) - } - if previous != nil { - subject = previous.Subject - displayName = valueOrDefault(displayName, previous.DisplayName) - if refreshToken == "" { - var err error - refreshToken, err = s.decryptString(secret.SecretKindOAuthRefreshToken, previous.EncryptedRefreshToken) - if err != nil { - return Account{}, err - } - } - } - if strings.TrimSpace(subject) == "" { - generatedSubject, err := secret.GenerateToken("local_acct") - if err != nil { - return Account{}, fmt.Errorf("generate local account subject: %w", err) - } - subject = generatedSubject - } - if strings.TrimSpace(refreshToken) == "" { - return Account{}, errors.New("oauth token response missing refresh token") - } - - encryptedAccessToken, err := s.encryptString(secret.SecretKindOAuthAccessToken, tokens.AccessToken) - if err != nil { - return Account{}, fmt.Errorf("encrypt access token: %w", err) - } - encryptedRefreshToken, err := s.encryptString(secret.SecretKindOAuthRefreshToken, refreshToken) - if err != nil { - return Account{}, fmt.Errorf("encrypt refresh token: %w", err) - } - var encryptedIDToken string - if strings.TrimSpace(tokens.IDToken) != "" { - encryptedIDToken, err = s.encryptString(secret.SecretKindOAuthIDToken, tokens.IDToken) - if err != nil { - return Account{}, fmt.Errorf("encrypt id token: %w", err) - } - } else if previous != nil { - encryptedIDToken = previous.EncryptedIDToken - } - - var expiresAt *time.Time - if tokens.ExpiresIn > 0 { - expiry := time.Now().Add(time.Duration(tokens.ExpiresIn) * time.Second) - expiresAt = &expiry - } - var lastRefreshAt *time.Time - if previous != nil { - now := time.Now() - lastRefreshAt = &now - } - - account := Account{ - ID: previousID(previous), - Provider: s.cfg.Provider, - AccountType: AccountTypeCodexOAuth, - Subject: subject, - DisplayName: displayName, - Credential: AccountCredential{ - CredentialType: CredentialTypeOAuthToken, - EncryptedAccessToken: encryptedAccessToken, - EncryptedRefreshToken: encryptedRefreshToken, - EncryptedIDToken: encryptedIDToken, - AccessTokenExpiresAt: expiresAt, - LastRefreshAt: lastRefreshAt, - Metadata: tokenMetadata(tokens, previous), - }, - EncryptedAccessToken: encryptedAccessToken, - EncryptedRefreshToken: encryptedRefreshToken, - EncryptedIDToken: encryptedIDToken, - AccessTokenExpiresAt: expiresAt, - LastRefreshAt: lastRefreshAt, - Enabled: previousEnabled(previous), - Priority: previousPriority(previous), - Name: previousName(previous), - Status: AccountStatusActive, - FingerprintHash: previousFingerprintHash(previous), - UserAgentHash: previousUserAgentHash(previous), - IPHash: previousIPHash(previous), - FailureCount: 0, - CircuitOpenUntil: nil, - LastRefreshError: "", - LastRefreshErrorAt: nil, - Metadata: tokenMetadata(tokens, previous), - } - if credentialOnly { - if previous == nil || previous.ID <= 0 { - return Account{}, ErrInvalidInput - } - s.invalidateOAuthModelCatalog(previous.ID) - refreshCtx := withProviderEventIntent(ctx, oauthRefreshIntent(account, trigger, true, nil)) - if err := s.repo.UpdateOAuthCredential(refreshCtx, s.cfg.Provider, previous.ID, account.Credential); err != nil { - return Account{}, err - } - return s.repo.FindAccountByID(ctx, s.cfg.Provider, previous.ID) - } - if account.ID > 0 { - s.invalidateOAuthModelCatalog(account.ID) - } - refreshCtx := withProviderEventIntent(ctx, oauthRefreshIntent(account, trigger, true, nil)) - saved, err := s.repo.SaveAccount(refreshCtx, account) - if err != nil { - return Account{}, err - } - if saved.ID != account.ID { - s.invalidateOAuthModelCatalog(saved.ID) - } - return saved, nil -} - -func (s *Service) storeCallbackTokenResponse(ctx context.Context, tokens TokenResponse, state OAuthState) (Account, error) { - tokens = enrichTokenResponseFromIDToken(tokens) - identities := identitiesFromTokenResponse(tokens) - - var previous *Account - if state.TargetAccountID > 0 { - account, err := s.repo.FindAccountByID(ctx, s.cfg.Provider, state.TargetAccountID) - if err != nil { - return Account{}, err - } - previous = &account - } else { - account, err := s.repo.FindAccountByIdentity(ctx, s.cfg.Provider, identities) - if err == nil { - previous = &account - } else if !errors.Is(err, ErrNotConnected) { - return Account{}, err - } - } - - account, err := s.accountFromTokenResponse(tokens, previous) - if err != nil { - return Account{}, err - } - applyOAuthStateToAccount(&account, state, previous) - if account.ID > 0 { - s.invalidateOAuthModelCatalog(account.ID) - } - callbackCtx := withProviderEventIntent(ctx, systemevent.EventIntent{ - Category: systemevent.CategoryOAuth, - Severity: systemevent.SeverityInfo, - Action: systemevent.ActionOAuthCallbackCompleted, - Outcome: systemevent.OutcomeSuccess, - Target: providerAccountTarget(account.ID, accountDisplayName(account)), - }) - saved, err := s.repo.SaveAccount(callbackCtx, account) - if err != nil { - return Account{}, err - } - if saved.ID != account.ID { - s.invalidateOAuthModelCatalog(saved.ID) - } - return saved, nil -} - func AccountSchedulable(account Account, now time.Time) bool { + if account.AccountType != AccountTypeAPIUpstream { + return false + } if !account.Enabled { return false } @@ -3194,6 +1980,9 @@ func AccountSchedulable(account Account, now time.Time) bool { } func accountUnschedulableReason(account Account, now time.Time) string { + if account.AccountType != AccountTypeAPIUpstream { + return "unsupported account type" + } if !account.Enabled { return "account disabled" } @@ -3241,231 +2030,6 @@ func retryAfterTime(value string, now time.Time, fallback time.Duration) time.Ti return now.Add(fallback) } -func identitiesFromTokenResponse(tokens TokenResponse) AccountIdentities { - tokens = enrichTokenResponseFromIDToken(tokens) - return AccountIdentities{ - ChatGPTAccountID: strings.TrimSpace(tokens.AccountID), - Email: strings.ToLower(strings.TrimSpace(tokens.Email)), - } -} - -func applyOAuthStateToAccount(account *Account, state OAuthState, previous *Account) { - if previous != nil { - account.Name = previous.Name - account.Priority = previous.Priority - account.Enabled = previous.Enabled - account.FingerprintHash = previous.FingerprintHash - account.UserAgentHash = previous.UserAgentHash - account.IPHash = previous.IPHash - account.FingerprintProfileID = previous.FingerprintProfileID - } - if strings.TrimSpace(state.PendingAccountName) != "" && (previous == nil || state.TargetAccountID > 0) { - account.Name = strings.TrimSpace(state.PendingAccountName) - } - if state.PendingPriority > 0 && (previous == nil || state.TargetAccountID > 0) { - account.Priority = state.PendingPriority - } - if state.PendingEnabled != nil && (previous == nil || state.TargetAccountID > 0) { - account.Enabled = *state.PendingEnabled - } - if state.PendingFingerprintProfileID != nil && (previous == nil || state.TargetAccountID > 0) { - account.FingerprintProfileID = state.PendingFingerprintProfileID - } - if previous == nil && account.Priority == 0 { - account.Priority = 100 - } - if previous == nil && account.Name == "" { - account.Name = firstNonEmpty(account.DisplayName, account.Subject) - } - if state.FingerprintHash != "" { - account.FingerprintHash = state.FingerprintHash - } - if state.UserAgentHash != "" { - account.UserAgentHash = state.UserAgentHash - } - if state.IPHash != "" { - account.IPHash = state.IPHash - } - account.Status = AccountStatusActive - account.StatusReason = "" - account.FailureCount = 0 - account.CircuitOpenUntil = nil - account.LastRefreshError = "" - account.LastRefreshErrorAt = nil -} - -func (s *Service) accountFromTokenResponse(tokens TokenResponse, previous *Account) (Account, error) { - if strings.TrimSpace(tokens.AccessToken) == "" { - return Account{}, errors.New("oauth token response missing access token") - } - tokens = enrichTokenResponseFromIDToken(tokens) - - refreshToken := tokens.RefreshToken - subject := tokens.Subject - displayName := tokens.DisplayName - if strings.TrimSpace(subject) == "" { - subject = firstNonEmpty(tokens.AccountID, tokens.Email) - } - if strings.TrimSpace(displayName) == "" { - displayName = firstNonEmpty(tokens.Email, tokens.AccountID, subject) - } - if previous != nil { - subject = previous.Subject - displayName = valueOrDefault(displayName, previous.DisplayName) - if refreshToken == "" { - var err error - refreshToken, err = s.decryptString(secret.SecretKindOAuthRefreshToken, previous.EncryptedRefreshToken) - if err != nil { - return Account{}, err - } - } - } - if strings.TrimSpace(subject) == "" { - generatedSubject, err := secret.GenerateToken("local_acct") - if err != nil { - return Account{}, fmt.Errorf("generate local account subject: %w", err) - } - subject = generatedSubject - } - if strings.TrimSpace(refreshToken) == "" { - return Account{}, errors.New("oauth token response missing refresh token") - } - - encryptedAccessToken, err := s.encryptString(secret.SecretKindOAuthAccessToken, tokens.AccessToken) - if err != nil { - return Account{}, fmt.Errorf("encrypt access token: %w", err) - } - encryptedRefreshToken, err := s.encryptString(secret.SecretKindOAuthRefreshToken, refreshToken) - if err != nil { - return Account{}, fmt.Errorf("encrypt refresh token: %w", err) - } - var encryptedIDToken string - if strings.TrimSpace(tokens.IDToken) != "" { - encryptedIDToken, err = s.encryptString(secret.SecretKindOAuthIDToken, tokens.IDToken) - if err != nil { - return Account{}, fmt.Errorf("encrypt id token: %w", err) - } - } else if previous != nil { - encryptedIDToken = previous.EncryptedIDToken - } - - var expiresAt *time.Time - if tokens.ExpiresIn > 0 { - expiry := time.Now().Add(time.Duration(tokens.ExpiresIn) * time.Second) - expiresAt = &expiry - } - var lastRefreshAt *time.Time - if previous != nil { - now := time.Now() - lastRefreshAt = &now - } - - return Account{ - ID: previousID(previous), - Provider: s.cfg.Provider, - AccountType: AccountTypeCodexOAuth, - Subject: subject, - Name: previousName(previous), - DisplayName: displayName, - Credential: AccountCredential{ - CredentialType: CredentialTypeOAuthToken, - EncryptedAccessToken: encryptedAccessToken, - EncryptedRefreshToken: encryptedRefreshToken, - EncryptedIDToken: encryptedIDToken, - AccessTokenExpiresAt: expiresAt, - LastRefreshAt: lastRefreshAt, - Metadata: tokenMetadata(tokens, previous), - }, - EncryptedAccessToken: encryptedAccessToken, - EncryptedRefreshToken: encryptedRefreshToken, - EncryptedIDToken: encryptedIDToken, - AccessTokenExpiresAt: expiresAt, - LastRefreshAt: lastRefreshAt, - Enabled: previousEnabled(previous), - Priority: previousPriority(previous), - Status: AccountStatusActive, - Metadata: tokenMetadata(tokens, previous), - }, nil -} - -func generateCodeVerifier() (string, error) { - raw := make([]byte, 64) - if _, err := rand.Read(raw); err != nil { - return "", err - } - return hex.EncodeToString(raw), nil -} - -func codeChallengeS256(verifier string) string { - sum := sha256.Sum256([]byte(verifier)) - return base64.RawURLEncoding.EncodeToString(sum[:]) -} - -func tokenMetadata(tokens TokenResponse, previous *Account) map[string]string { - metadata := map[string]string{} - if previous != nil { - for key, value := range previous.Metadata { - if strings.TrimSpace(value) != "" { - metadata[key] = value - } - } - } - setMetadata(metadata, "account_id", tokens.AccountID) - setMetadata(metadata, "chatgpt_account_id", tokens.AccountID) - setMetadata(metadata, "email", tokens.Email) - setMetadata(metadata, "plan_type", tokens.PlanType) - setMetadata(metadata, "client_id", tokens.ClientID) - setMetadata(metadata, "access_token_sha256", sha256Hex(tokens.AccessToken)) - return metadata -} - -func setMetadata(metadata map[string]string, key, value string) { - if strings.TrimSpace(value) != "" { - metadata[key] = strings.TrimSpace(value) - } -} - -func enrichTokenResponseFromIDToken(tokens TokenResponse) TokenResponse { - claims, err := decodeIDTokenClaims(tokens.IDToken) - if err != nil { - return tokens - } - if strings.TrimSpace(tokens.Subject) == "" { - tokens.Subject = claims.Subject - } - if strings.TrimSpace(tokens.Email) == "" { - tokens.Email = claims.Email - } - if claims.OpenAIAuth != nil { - if strings.TrimSpace(tokens.AccountID) == "" { - tokens.AccountID = claims.OpenAIAuth.ChatGPTAccountID - } - if strings.TrimSpace(tokens.PlanType) == "" { - tokens.PlanType = claims.OpenAIAuth.ChatGPTPlanType - } - } - return tokens -} - -func decodeIDTokenClaims(idToken string) (idTokenClaims, error) { - parts := strings.Split(idToken, ".") - if len(parts) != 3 { - return idTokenClaims{}, errors.New("invalid id token format") - } - payload, err := base64.RawURLEncoding.DecodeString(parts[1]) - if err != nil { - payload, err = base64.URLEncoding.DecodeString(parts[1]) - if err != nil { - return idTokenClaims{}, err - } - } - var claims idTokenClaims - if err := json.Unmarshal(payload, &claims); err != nil { - return idTokenClaims{}, err - } - return claims, nil -} - func valueOrDefault(value, fallback string) string { if strings.TrimSpace(value) == "" { return fallback @@ -3480,83 +2044,19 @@ func normalizedLoadFactor(value int) int { return value } -func firstNonEmpty(values ...string) string { - for _, value := range values { - if strings.TrimSpace(value) != "" { - return strings.TrimSpace(value) - } - } - return "" -} - -func hashOptional(value string) string { - if strings.TrimSpace(value) == "" { - return "" - } - return secret.HashAPIKey(strings.TrimSpace(value)) -} - -func sha256Hex(value string) string { - value = strings.TrimSpace(value) - if value == "" { - return "" - } - sum := sha256.Sum256([]byte(value)) - return hex.EncodeToString(sum[:]) -} - -func previousID(previous *Account) int64 { - if previous == nil { - return 0 - } - return previous.ID -} - -func previousEnabled(previous *Account) bool { - if previous == nil { - return true - } - return previous.Enabled -} - -func previousPriority(previous *Account) int { - if previous == nil { - return 100 - } - return previous.Priority -} - -func previousName(previous *Account) string { - if previous == nil { - return "" - } - return previous.Name -} - -func previousFingerprintHash(previous *Account) string { - if previous == nil { - return "" - } - return previous.FingerprintHash +func accountStatusTestRoute(cfg Config) string { + return requestRoutePath(upstreamModelsURL(cfg.APIBaseURL), "/v1/models") } - -func previousUserAgentHash(previous *Account) string { - if previous == nil { - return "" - } - return previous.UserAgentHash +func accountStatusTestMethod(cfg Config) string { return http.MethodGet } +func accountStatusTestModel(cfg Config) string { return "" } +func accountModelTestRoute(cfg Config, selected SelectedAccount) string { + return requestRoutePath(upstreamChatCompletionsURL(selected.BaseURL), "/v1/chat/completions") } -func previousIPHash(previous *Account) string { - if previous == nil { - return "" +func upstreamModelsURL(baseURL string) string { + trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/") + if strings.HasSuffix(trimmed, "/v1") { + return trimmed + "/models" } - return previous.IPHash -} - -// FingerprintProfileData is the subset of a fingerprint profile needed for outbound requests. -type FingerprintProfileData struct { - UserAgent string - TLSFingerprint string - Headers map[string]string + return trimmed + "/v1/models" } diff --git a/backend/internal/provider/service_test.go b/backend/internal/provider/service_test.go index a591eace..ed8cf0f0 100644 --- a/backend/internal/provider/service_test.go +++ b/backend/internal/provider/service_test.go @@ -2,19 +2,13 @@ package provider import ( "context" - "encoding/base64" - "encoding/json" "errors" - "fmt" "net/http" "net/http/httptest" - "net/url" "reflect" "sort" "strconv" "strings" - "sync" - "sync/atomic" "testing" "time" @@ -22,24 +16,11 @@ import ( "github.com/KnowSky404/N2API/backend/internal/systemevent" ) -type captureProviderMetrics struct { - refreshes [][2]string -} - -func (m *captureProviderMetrics) ObserveProviderRefresh(mode, outcome string) { - m.refreshes = append(m.refreshes, [2]string{mode, outcome}) -} - func TestStatusReportsConfigurationAndConnection(t *testing.T) { repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", - Secret: "encryption-secret", + service := NewService(repo, fakeProbeClient{}, Config{ + Provider: "openai", + Secret: "encryption-secret", }) status, err := service.Status(context.Background()) @@ -50,25 +31,19 @@ func TestStatusReportsConfigurationAndConnection(t *testing.T) { t.Fatalf("status = %+v, want configured disconnected provider", status) } - expiresAt := time.Now().Add(time.Hour).UTC() - if _, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_1", - DisplayName: "Codex Account", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "access-token"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token"), - AccessTokenExpiresAt: &expiresAt, - LastRefreshAt: nil, - }); err != nil { - t.Fatalf("SaveAccount returned error: %v", err) + account := testAccount(t, 1, true, 1, "api-key") + account.DisplayName = "API Account" + account.Name = "API Account" + if _, err := repo.SaveAccount(context.Background(), account); err != nil { + t.Fatal(err) } status, err = service.Status(context.Background()) if err != nil { t.Fatalf("Status returned error: %v", err) } - if !status.Connected || status.DisplayName != "Codex Account" { - t.Fatalf("status = %+v, want connected Codex Account", status) + if !status.Connected || status.DisplayName != "API Account" { + t.Fatalf("status = %+v, want connected API Account", status) } } @@ -78,1021 +53,27 @@ func TestServiceEncryptionKeyringWritesCurrentAndReadsLegacyPreviousKey(t *testi secret.EncryptionKey{ID: "current-202607", Secret: "current-encryption-secret"}, []secret.EncryptionKey{{ID: "previous-legacy", Secret: "legacy-encryption-secret"}}, ) - if err != nil { - t.Fatalf("NewKeyring returned error: %v", err) - } - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{EncryptionKeyring: keyring}) - - encrypted, err := service.encryptString(secret.SecretKindOAuthAccessToken, "current-provider-token") - if err != nil { - t.Fatalf("encryptString returned error: %v", err) - } - if !strings.HasPrefix(encrypted, "n2api:v1:current-202607:oauth-access-token:") { - t.Fatalf("encryptString = %q, want current key envelope", encrypted) - } - if _, err := service.decryptString(secret.SecretKindOAuthRefreshToken, encrypted); err == nil { - t.Fatal("decryptString accepted an access-token envelope as a refresh token") - } - decrypted, err := service.decryptString(secret.SecretKindOAuthRefreshToken, legacyCiphertext) - if err != nil { - t.Fatalf("decryptString returned error for legacy previous key: %v", err) - } - if decrypted != "legacy-oauth-refresh-token" { - t.Fatalf("decryptString = %q, want legacy-oauth-refresh-token", decrypted) - } -} - -func TestStartConnectStoresHashedStateAndBuildsAuthorizationURL(t *testing.T) { - repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) - - result, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - parsed, err := url.Parse(result.AuthorizationURL) - if err != nil { - t.Fatalf("authorization URL did not parse: %v", err) - } - if parsed.Query().Get("client_id") != "client-id" { - t.Fatalf("client_id = %q", parsed.Query().Get("client_id")) - } - if parsed.Query().Get("redirect_uri") != "http://localhost/oauth/openai/callback" { - t.Fatalf("redirect_uri = %q", parsed.Query().Get("redirect_uri")) - } - state := parsed.Query().Get("state") - if state == "" { - t.Fatal("state was empty") - } - if strings.Contains(repo.states[0].StateHash, state) { - t.Fatal("state repository stored cleartext state") - } -} - -func TestStartConnectUsesBuiltInCodexPKCEConfig(t *testing.T) { - repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - Secret: "encryption-secret", - }) - - result, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - parsed, err := url.Parse(result.AuthorizationURL) - if err != nil { - t.Fatalf("authorization URL did not parse: %v", err) - } - query := parsed.Query() - if query.Get("client_id") == "" { - t.Fatal("client_id was empty") - } - if query.Get("redirect_uri") != "http://localhost:1455/auth/callback" { - t.Fatalf("redirect_uri = %q, want Codex callback", query.Get("redirect_uri")) - } - if query.Get("code_challenge") == "" { - t.Fatal("code_challenge was empty") - } - if query.Get("code_challenge_method") != "S256" { - t.Fatalf("code_challenge_method = %q, want S256", query.Get("code_challenge_method")) - } - if query.Get("response_type") != "code" { - t.Fatalf("response_type = %q, want code", query.Get("response_type")) - } - if repo.states[0].CodeVerifierHash == "" { - t.Fatal("state did not record code verifier hash") - } - if repo.states[0].CodeVerifier == "" { - t.Fatal("memory repo did not retain verifier for token exchange") - } - if repo.states[0].EncryptedCodeVerifier == "" { - t.Fatal("state did not record encrypted code verifier") - } - if strings.Contains(repo.states[0].CodeVerifierHash, repo.states[0].CodeVerifier) { - t.Fatal("state stored cleartext code verifier in hash field") - } -} - -func TestStartConnectStoresPendingAccountOptionsAndFingerprintHashes(t *testing.T) { - repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - profileID := int64(9) - repo.fingerprintProfiles[profileID] = FingerprintProfileData{UserAgent: "Mozilla/5.0"} - - result, err := service.StartConnect(context.Background(), ConnectOptions{ - RedirectAfter: "/", - Name: "Work Codex", - Priority: 25, - Enabled: boolPtr(false), - FingerprintProfileID: &profileID, - Fingerprint: Fingerprint{ - Value: "browser-fingerprint", - UserAgent: "Mozilla/5.0", - IP: "203.0.113.10", - }, - }) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - if mustQuery(t, result.AuthorizationURL, "state") == "" { - t.Fatal("authorization URL missing state") - } - state := repo.states[0] - if state.PendingAccountName != "Work Codex" || state.PendingPriority != 25 || state.PendingEnabled == nil || *state.PendingEnabled { - t.Fatalf("state pending account fields = %+v", state) - } - if state.PendingFingerprintProfileID == nil || *state.PendingFingerprintProfileID != profileID { - t.Fatalf("state pending fingerprint profile = %+v, want %d", state.PendingFingerprintProfileID, profileID) - } - if state.FingerprintHash == "" || state.UserAgentHash == "" || state.IPHash == "" { - t.Fatalf("state fingerprint hashes incomplete: %+v", state) - } - if repo.ensureDefaultFingerprintProfileCalls != 0 { - t.Fatalf("EnsureDefaultCodexFingerprintProfile called %d times, want 0 (explicit profile provided)", repo.ensureDefaultFingerprintProfileCalls) - } - for _, cleartext := range []string{"browser-fingerprint", "Mozilla/5.0", "203.0.113.10"} { - if strings.Contains(state.FingerprintHash+state.UserAgentHash+state.IPHash, cleartext) { - t.Fatalf("fingerprint hash leaked cleartext %q", cleartext) - } - } -} - -func TestStartConnectStoresDefaultFingerprintProfileWhenUnset(t *testing.T) { - repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) - - result, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - if mustQuery(t, result.AuthorizationURL, "state") == "" { - t.Fatal("authorization URL missing state") - } - state := repo.states[0] - if state.PendingFingerprintProfileID == nil { - t.Fatal("PendingFingerprintProfileID is nil, want default Codex fingerprint profile ID") - } - if *state.PendingFingerprintProfileID != repo.defaultFingerprintProfileID { - t.Fatalf("PendingFingerprintProfileID = %d, want default %d", *state.PendingFingerprintProfileID, repo.defaultFingerprintProfileID) - } - if repo.ensureDefaultFingerprintProfileCalls != 1 { - t.Fatalf("EnsureDefaultCodexFingerprintProfile called %d times, want 1", repo.ensureDefaultFingerprintProfileCalls) - } -} - -func TestStartConnectPreservesExplicitFingerprintProfile(t *testing.T) { - repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) - customProfileID := int64(42) - repo.fingerprintProfiles[customProfileID] = FingerprintProfileData{UserAgent: "custom-agent"} - - result, err := service.StartConnect(context.Background(), ConnectOptions{ - RedirectAfter: "/", - FingerprintProfileID: &customProfileID, - }) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - if mustQuery(t, result.AuthorizationURL, "state") == "" { - t.Fatal("authorization URL missing state") - } - state := repo.states[0] - if state.PendingFingerprintProfileID == nil || *state.PendingFingerprintProfileID != customProfileID { - t.Fatalf("PendingFingerprintProfileID = %+v, want %d", state.PendingFingerprintProfileID, customProfileID) - } - if repo.ensureDefaultFingerprintProfileCalls != 0 { - t.Fatalf("EnsureDefaultCodexFingerprintProfile called %d times, want 0", repo.ensureDefaultFingerprintProfileCalls) - } -} - -func TestCompleteCallbackPreservesExistingFingerprintOnReconnect(t *testing.T) { - repo := newMemoryRepo() - existingProfileID := int64(7) - repo.fingerprintProfiles[existingProfileID] = FingerprintProfileData{UserAgent: "Mozilla/5.0"} - existing, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_same", - Name: "Existing", - DisplayName: "same@example.com", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "old-access"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "old-refresh"), - Enabled: true, - Priority: 12, - FingerprintProfileID: &existingProfileID, - Status: AccountStatusActive, - Metadata: map[string]string{"chatgpt_account_id": "acct_same"}, - }) - if err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - client := fakeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - AccountID: "acct_same", - Email: "same@example.com", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - account, err := service.CompleteCallback(context.Background(), "auth-code", mustQuery(t, started.AuthorizationURL, "state")) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.ID != existing.ID { - t.Fatalf("account ID = %d, want existing %d", account.ID, existing.ID) - } - if account.FingerprintProfileID == nil || *account.FingerprintProfileID != existingProfileID { - t.Fatalf("account FingerprintProfileID = %+v, want existing %d", account.FingerprintProfileID, existingProfileID) - } -} - -func TestCompleteCallbackReauthorizationUpdatesFingerprintProfile(t *testing.T) { - repo := newMemoryRepo() - existing, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_old", - Name: "Old Account", - DisplayName: "old@example.com", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "old-access"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "old-refresh"), - Enabled: true, - Priority: 30, - Status: AccountStatusActive, - Metadata: map[string]string{"chatgpt_account_id": "acct_old"}, - }) - if err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - client := fakeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - AccountID: "acct_new", - Email: "new@example.com", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{ - RedirectAfter: "/", - TargetAccountID: existing.ID, - }) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - account, err := service.CompleteCallback(context.Background(), "auth-code", mustQuery(t, started.AuthorizationURL, "state")) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.ID != existing.ID { - t.Fatalf("account ID = %d, want target %d", account.ID, existing.ID) - } - if account.FingerprintProfileID == nil || *account.FingerprintProfileID != repo.defaultFingerprintProfileID { - t.Fatalf("account FingerprintProfileID = %+v, want default Codex profile %d", account.FingerprintProfileID, repo.defaultFingerprintProfileID) - } -} - -func TestCompleteCallbackRejectsInvalidState(t *testing.T) { - service := newConfiguredService(newMemoryRepo(), fakeOAuthClient{}) - if _, err := service.CompleteCallback(context.Background(), "code", "missing-state"); !errors.Is(err, ErrInvalidState) { - t.Fatalf("CompleteCallback error = %v, want ErrInvalidState", err) - } -} - -func TestCompleteCallbackStoresEncryptedTokensAndConsumesState(t *testing.T) { - repo := newMemoryRepo() - client := fakeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "access-token", - RefreshToken: "refresh-token", - ExpiresIn: 3600, - Subject: "acct_1", - DisplayName: "Codex Account", - }, - } - service := newConfiguredService(repo, client) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - state := mustQuery(t, started.AuthorizationURL, "state") - account, err := service.CompleteCallback(context.Background(), "auth-code", state) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.DisplayName != "Codex Account" { - t.Fatalf("DisplayName = %q", account.DisplayName) - } - if repo.accounts[0].EncryptedAccessToken == "access-token" || repo.accounts[0].EncryptedRefreshToken == "refresh-token" { - t.Fatal("repository stored cleartext tokens") - } - if repo.states[0].ConsumedAt == nil { - t.Fatal("state was not consumed") - } -} - -func TestCompleteCallbackPassesPKCEVerifierAndStoresTokenMetadata(t *testing.T) { - repo := newMemoryRepo() - client := &captureExchangeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "access-token", - RefreshToken: "refresh-token", - IDToken: "id-token", - ExpiresIn: 3600, - AccountID: "acct_chatgpt", - Email: "owner@example.com", - PlanType: "plus", - ClientID: "codex-client", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - state := mustQuery(t, started.AuthorizationURL, "state") - account, err := service.CompleteCallback(context.Background(), "auth-code", state) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if client.gotCodeVerifier == "" { - t.Fatal("ExchangeCode did not receive PKCE code verifier") - } - if account.Subject != "acct_chatgpt" || account.DisplayName != "owner@example.com" { - t.Fatalf("account identity = %q/%q, want account id and email display", account.Subject, account.DisplayName) - } - if account.Metadata["email"] != "owner@example.com" || account.Metadata["account_id"] != "acct_chatgpt" || account.Metadata["plan_type"] != "plus" || account.Metadata["client_id"] != "codex-client" { - t.Fatalf("metadata = %+v", account.Metadata) - } - idToken, err := decryptForTest(t, "encryption-secret", secret.SecretKindOAuthIDToken, account.EncryptedIDToken) - if err != nil { - t.Fatalf("decrypt id token returned error: %v", err) - } - if idToken != "id-token" { - t.Fatalf("id token = %q, want id-token", idToken) - } -} - -func TestCompleteCallbackCreatesNamedAccountWithIsolatedFingerprint(t *testing.T) { - repo := newMemoryRepo() - client := &captureExchangeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "access-token", - RefreshToken: "refresh-token", - ExpiresIn: 3600, - AccountID: "acct_work", - Email: "work@example.com", - ClientID: "codex-client", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{ - RedirectAfter: "/", - Name: "Work Account", - Priority: 7, - Fingerprint: Fingerprint{ - Value: "browser-fingerprint", - UserAgent: "Mozilla/5.0", - IP: "203.0.113.10", - }, - }) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - account, err := service.CompleteCallback(context.Background(), "auth-code", mustQuery(t, started.AuthorizationURL, "state")) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.Name != "Work Account" || account.Priority != 7 || account.Status != AccountStatusActive { - t.Fatalf("account = %+v, want named active priority 7 account", account) - } - if account.FingerprintHash == "" || account.UserAgentHash == "" || account.IPHash == "" { - t.Fatalf("account fingerprint hashes incomplete: %+v", account) - } - if account.Metadata["email"] != "work@example.com" || account.Metadata["chatgpt_account_id"] != "acct_work" || account.Metadata["access_token_sha256"] == "" { - t.Fatalf("metadata = %+v", account.Metadata) - } - for _, intent := range repo.intents { - if intent.Action == systemevent.ActionProviderAccountRecovered { - t.Fatalf("new account connect emitted recovery intent: %+v", intent) - } - } -} - -func TestCompleteCallbackReauthorizesTargetAccountInsteadOfMatchingDifferentIdentity(t *testing.T) { - repo := newMemoryRepo() - existing, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_old", - Name: "Old Account", - DisplayName: "old@example.com", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "old-access"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "old-refresh"), - Enabled: true, - Priority: 30, - Status: AccountStatusActive, - Metadata: map[string]string{"chatgpt_account_id": "acct_old"}, - }) - if err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - now := time.Now() - until := now.Add(time.Hour) - repo.accounts[0].Status = AccountStatusExpired - repo.accounts[0].StatusReason = "existing authorization failure" - repo.accounts[0].LastError = "existing authorization failure" - repo.accounts[0].LastErrorAt = &now - repo.accounts[0].FailureCount = 2 - repo.accounts[0].CircuitOpenUntil = &until - client := &captureExchangeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - AccountID: "acct_new_identity", - Email: "new@example.com", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{ - RedirectAfter: "/", - TargetAccountID: existing.ID, - Name: "Renamed Account", - }) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - account, err := service.CompleteCallback(context.Background(), "auth-code", mustQuery(t, started.AuthorizationURL, "state")) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.ID != existing.ID { - t.Fatalf("account ID = %d, want target account %d", account.ID, existing.ID) - } - if account.Name != "Renamed Account" || account.Metadata["chatgpt_account_id"] != "acct_new_identity" { - t.Fatalf("account after reauth = %+v", account) - } - token, err := decryptForTest(t, "encryption-secret", secret.SecretKindOAuthAccessToken, account.EncryptedAccessToken) - if err != nil { - t.Fatalf("DecryptString returned error: %v", err) - } - if token != "new-access" { - t.Fatalf("token = %q, want new-access", token) - } - if account.Status != AccountStatusExpired || account.StatusReason != "existing authorization failure" || account.LastError != "existing authorization failure" || - account.LastErrorAt == nil || account.FailureCount != 2 || account.CircuitOpenUntil == nil { - t.Fatalf("account health after reauthorization = %+v, want preserved until confirmed recovery", account) - } -} - -func TestCompleteCallbackUpdatesExistingAccountByIdentityWhenNoTarget(t *testing.T) { - repo := newMemoryRepo() - existing, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_same", - Name: "Existing", - DisplayName: "same@example.com", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "old-access"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "old-refresh"), - Enabled: true, - Priority: 12, - Status: AccountStatusActive, - Metadata: map[string]string{"chatgpt_account_id": "acct_same"}, - }) - if err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - client := fakeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - AccountID: "acct_same", - Email: "same@example.com", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/", Name: "Incoming Duplicate"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - account, err := service.CompleteCallback(context.Background(), "auth-code", mustQuery(t, started.AuthorizationURL, "state")) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.ID != existing.ID { - t.Fatalf("account ID = %d, want existing account %d", account.ID, existing.ID) - } - if len(repo.accounts) != 1 { - t.Fatalf("account count = %d, want duplicate update", len(repo.accounts)) - } - if account.Name != "Existing" { - t.Fatalf("account name = %q, want existing name preserved", account.Name) - } -} - -func TestCompleteCallbackExtractsIdentityMetadataFromIDTokenClaims(t *testing.T) { - repo := newMemoryRepo() - client := fakeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "access-token", - RefreshToken: "refresh-token", - IDToken: mustUnsignedIDToken(t, map[string]any{ - "sub": "user-subject", - "email": "owner@example.com", - "https://api.openai.com/auth": map[string]any{ - "chatgpt_account_id": "acct_chatgpt", - "chatgpt_plan_type": "plus", - }, - }), - ExpiresIn: 3600, - ClientID: "codex-client", - }, - } - service := NewService(repo, client, Config{ - Provider: "openai", - RedirectURL: "http://localhost:3000/oauth/openai/callback", - Secret: "encryption-secret", - }) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - state := mustQuery(t, started.AuthorizationURL, "state") - account, err := service.CompleteCallback(context.Background(), "auth-code", state) - if err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if account.Subject != "user-subject" || account.DisplayName != "owner@example.com" { - t.Fatalf("account identity = %q/%q, want id token subject and email", account.Subject, account.DisplayName) - } - if account.Metadata["account_id"] != "acct_chatgpt" || account.Metadata["plan_type"] != "plus" { - t.Fatalf("metadata = %+v", account.Metadata) - } -} - -func TestCompleteCallbackConsumesStateWhenTokenExchangeFails(t *testing.T) { - repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{exchangeErr: errors.New("token endpoint unavailable")}) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - state := mustQuery(t, started.AuthorizationURL, "state") - if _, err := service.CompleteCallback(context.Background(), "auth-code", state); err == nil { - t.Fatal("CompleteCallback returned nil error, want token exchange error") - } - if repo.states[0].ConsumedAt == nil { - t.Fatal("state was not consumed") - } -} - -func TestCompleteCallbackClaimsStateBeforeSavingTokens(t *testing.T) { - repo := newMemoryRepo() - client := fakeOAuthClient{ - exchange: TokenResponse{ - AccessToken: "access-token", - RefreshToken: "refresh-token", - ExpiresIn: 3600, - Subject: "acct_1", - }, - } - service := newConfiguredService(repo, client) - - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - state := mustQuery(t, started.AuthorizationURL, "state") - if _, err := service.CompleteCallback(context.Background(), "auth-code", state); err != nil { - t.Fatalf("CompleteCallback returned error: %v", err) - } - if _, err := service.CompleteCallback(context.Background(), "auth-code", state); !errors.Is(err, ErrInvalidState) { - t.Fatalf("replay error = %v, want ErrInvalidState", err) - } - if repo.saveCount != 1 { - t.Fatalf("saveCount = %d, want 1", repo.saveCount) - } -} - -func TestCompleteCallbackGeneratesLocalSubjectsForBlankTokenSubject(t *testing.T) { - repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{ - exchanges: []TokenResponse{ - {AccessToken: "access-token-1", RefreshToken: "refresh-token-1", ExpiresIn: 3600}, - {AccessToken: "access-token-2", RefreshToken: "refresh-token-2", ExpiresIn: 3600}, - }, - }) - - for i := 0; i < 2; i++ { - started, err := service.StartConnect(context.Background(), ConnectOptions{RedirectAfter: "/"}) - if err != nil { - t.Fatalf("StartConnect %d returned error: %v", i, err) - } - state := mustQuery(t, started.AuthorizationURL, "state") - if _, err := service.CompleteCallback(context.Background(), "auth-code", state); err != nil { - t.Fatalf("CompleteCallback %d returned error: %v", i, err) - } - } - - if len(repo.accounts) != 2 { - t.Fatalf("account count = %d, want 2", len(repo.accounts)) - } - if strings.TrimSpace(repo.accounts[0].Subject) == "" || strings.TrimSpace(repo.accounts[1].Subject) == "" { - t.Fatalf("subjects = %q/%q, want non-empty local subjects", repo.accounts[0].Subject, repo.accounts[1].Subject) - } - if repo.accounts[0].Subject == repo.accounts[1].Subject { - t.Fatalf("subjects matched: %q", repo.accounts[0].Subject) - } -} - -func TestAccessTokenReturnsUnexpiredToken(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - if _, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_1", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "access-token"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token"), - AccessTokenExpiresAt: &expiresAt, - }); err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - service := newConfiguredService(repo, fakeOAuthClient{}) - - token, err := service.AccessToken(context.Background()) - if err != nil { - t.Fatalf("AccessToken returned error: %v", err) - } - if token != "access-token" { - t.Fatalf("token = %q, want access-token", token) - } -} - -func TestRefreshAccountAuthorizationRefreshesRejectedUnexpiredToken(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expiresAt) - repo.accounts = []Account{account} - client := &captureRefreshOAuthClient{refresh: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - }} - service := newConfiguredService(repo, client) - - token, retry, failureRecorded, err := service.RefreshAccountAuthorization( - context.Background(), account.ID, "old-access", http.StatusUnauthorized, "invalid access token", - ) - if err != nil { - t.Fatalf("RefreshAccountAuthorization returned error: %v", err) - } - if !retry || token != "new-access" { - t.Fatalf("refresh result = token %q retry %v, want new-access/true", token, retry) - } - if failureRecorded { - t.Fatal("successful refresh reported an account failure") - } - if client.calls != 1 { - t.Fatalf("refresh calls = %d, want 1", client.calls) - } - if repo.accounts[0].LastRefreshAt == nil { - t.Fatal("LastRefreshAt was not updated") - } -} - -func TestRefreshAccountAuthorizationReusesConcurrentlyRotatedToken(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testExpiredAccount(t, 7, true, 3, "new-access", "new-refresh", expiresAt) - repo.accounts = []Account{account} - client := &captureRefreshOAuthClient{refresh: TokenResponse{AccessToken: "unexpected-refresh", ExpiresIn: 3600}} - service := newConfiguredService(repo, client) - - token, retry, failureRecorded, err := service.RefreshAccountAuthorization( - context.Background(), account.ID, "old-access", http.StatusUnauthorized, "invalid access token", - ) - if err != nil { - t.Fatalf("RefreshAccountAuthorization returned error: %v", err) - } - if !retry || token != "new-access" { - t.Fatalf("refresh result = token %q retry %v, want new-access/true", token, retry) - } - if failureRecorded { - t.Fatal("concurrently rotated token reported an account failure") - } - if client.calls != 0 { - t.Fatalf("refresh calls = %d, want 0 for already rotated token", client.calls) - } -} - -func TestRefreshAccountAuthorizationSerializesConcurrentRejectedTokenRefresh(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expiresAt) - repo.accounts = []Account{account} - client := &blockingOAuthClient{ - refresh: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - }, - entered: make(chan struct{}), - release: make(chan struct{}), - } - service := newConfiguredService(repo, client) - - type result struct { - token string - retry bool - failureRecorded bool - err error - } - results := make(chan result, 2) - for i := 0; i < 2; i++ { - go func() { - token, retry, failureRecorded, err := service.RefreshAccountAuthorization( - context.Background(), account.ID, "old-access", http.StatusUnauthorized, "invalid access token", - ) - results <- result{token: token, retry: retry, failureRecorded: failureRecorded, err: err} - }() - } - - <-client.entered - client.release <- struct{}{} - for i := 0; i < 2; i++ { - got := <-results - if got.err != nil || !got.retry || got.failureRecorded || got.token != "new-access" { - t.Fatalf("refresh result = token %q retry %v failureRecorded %v error %v", got.token, got.retry, got.failureRecorded, got.err) - } - } - if client.calls != 1 { - t.Fatalf("refresh calls = %d, want 1", client.calls) - } -} - -func TestRefreshAccountAuthorizationSkipsScopePermissionFailure(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testExpiredAccount(t, 7, true, 3, "access-token", "refresh-token", expiresAt) - repo.accounts = []Account{account} - client := &captureRefreshOAuthClient{refresh: TokenResponse{AccessToken: "unexpected-refresh", ExpiresIn: 3600}} - service := newConfiguredService(repo, client) - - token, retry, failureRecorded, err := service.RefreshAccountAuthorization( - context.Background(), account.ID, "access-token", http.StatusForbidden, "missing scopes: api.responses.write", - ) - if err != nil { - t.Fatalf("RefreshAccountAuthorization returned error: %v", err) - } - if retry || token != "" { - t.Fatalf("refresh result = token %q retry %v, want empty/false", token, retry) - } - if failureRecorded { - t.Fatal("scope permission failure reported an OAuth refresh failure") - } - if client.calls != 0 { - t.Fatalf("refresh calls = %d, want 0 for scope permission failure", client.calls) - } -} - -func TestRefreshAccountAuthorizationReportsPersistedRefreshFailure(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testExpiredAccount(t, 7, true, 3, "access-token", "refresh-token", expiresAt) - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh rejected")}) - - token, retry, failureRecorded, err := service.RefreshAccountAuthorization( - context.Background(), account.ID, "access-token", http.StatusUnauthorized, "invalid access token", - ) - if err == nil { - t.Fatal("RefreshAccountAuthorization returned nil error") - } - if token != "" || !retry || !failureRecorded { - t.Fatalf("refresh result = token %q retry %v failureRecorded %v, want empty/true/true", token, retry, failureRecorded) - } - if repo.accounts[0].FailureCount != 1 { - t.Fatalf("failure count = %d, want 1", repo.accounts[0].FailureCount) - } -} - -func TestRefreshAccountAuthorizationReportsUnpersistedCredentialError(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testExpiredAccount(t, 7, true, 3, "access-token", "refresh-token", expiresAt) - account.EncryptedRefreshToken = "invalid-ciphertext" - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{}) - - token, retry, failureRecorded, err := service.RefreshAccountAuthorization( - context.Background(), account.ID, "access-token", http.StatusUnauthorized, "invalid access token", - ) - if err == nil { - t.Fatal("RefreshAccountAuthorization returned nil error") - } - if token != "" || !retry || failureRecorded { - t.Fatalf("refresh result = token %q retry %v failureRecorded %v, want empty/true/false", token, retry, failureRecorded) - } - if repo.accounts[0].FailureCount != 0 { - t.Fatalf("failure count = %d, want no provider refresh failure record", repo.accounts[0].FailureCount) - } -} - -func TestAccessTokenRefreshesExpiredToken(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - if _, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - Subject: "acct_1", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, "old-access"), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token"), - AccessTokenExpiresAt: &expired, - }); err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - service := newConfiguredService(repo, fakeOAuthClient{ - refresh: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - }, - }) - - token, err := service.AccessToken(context.Background()) - if err != nil { - t.Fatalf("AccessToken returned error: %v", err) - } - if token != "new-access" { - t.Fatalf("token = %q, want new-access", token) - } - if repo.accounts[0].LastRefreshAt == nil { - t.Fatal("LastRefreshAt was not updated") - } -} - -func TestAccessTokenForAccountRefreshPreservesExistingIdentityAndRefreshToken(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - repo.accounts = []Account{ - testExpiredAccount(t, 7, false, 3, "old-access", "old-refresh", expired), - } - repo.accounts[0].Subject = "acct_original" - service := newConfiguredService(repo, fakeOAuthClient{ - refresh: TokenResponse{ - AccessToken: "new-access", - ExpiresIn: 3600, - Subject: "acct_changed", - DisplayName: "Updated Name", - }, - }) - - token, err := service.AccessTokenForAccount(context.Background(), repo.accounts[0]) - if err != nil { - t.Fatalf("AccessTokenForAccount returned error: %v", err) - } - if token != "new-access" { - t.Fatalf("token = %q, want new-access", token) - } - if len(repo.accounts) != 1 { - t.Fatalf("account count = %d, want 1", len(repo.accounts)) - } - account := repo.accounts[0] - if account.ID != 7 || account.Subject != "acct_original" || account.Enabled || account.Priority != 3 { - t.Fatalf("account = %+v, want original id/subject/enabled/priority preserved", account) - } - refreshToken, err := decryptForTest(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, account.EncryptedRefreshToken) - if err != nil { - t.Fatalf("DecryptString returned error: %v", err) - } - if refreshToken != "old-refresh" { - t.Fatalf("refreshToken = %q, want old-refresh", refreshToken) - } -} - -func TestAccessTokenForAccountRefreshPassesConfiguredProxyURL(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expired) - account.Credential.EncryptedProxyURL = mustEncrypt(t, "encryption-secret", secret.SecretKindProviderProxyURL, "http://proxy.example.test:8080") - repo.accounts = []Account{account} - client := &captureRefreshOAuthClient{refresh: TokenResponse{AccessToken: "new-access", ExpiresIn: 3600, Subject: "acct_7"}} - service := newConfiguredService(repo, client) - - if _, err := service.AccessTokenForAccount(context.Background(), repo.accounts[0]); err != nil { - t.Fatalf("AccessTokenForAccount returned error: %v", err) - } - if client.gotConfig.ProxyURL != "http://proxy.example.test:8080" { - t.Fatalf("refresh proxy URL = %q, want account proxy URL", client.gotConfig.ProxyURL) - } -} - -func TestAccessTokenForAccountRefreshRejectsUnreadableProxyWithoutBypass(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expired) - account.Credential.EncryptedProxyURL = "n2api:v1:missing:provider-proxy-url:AAAA" - repo.accounts = []Account{account} - client := &captureRefreshOAuthClient{refresh: TokenResponse{AccessToken: "new-access", ExpiresIn: 3600}} - service := newConfiguredService(repo, client) - - if _, err := service.AccessTokenForAccount(context.Background(), account); err == nil { - t.Fatal("AccessTokenForAccount returned nil error for unreadable proxy") - } - if client.calls != 0 { - t.Fatalf("refresh calls = %d, want 0", client.calls) - } -} - -func TestAccessTokenForAccountSerializesConcurrentRefresh(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - repo.accounts = []Account{ - testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expired), - } - client := &blockingOAuthClient{ - refresh: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - }, - entered: make(chan struct{}), - release: make(chan struct{}), - } - service := newConfiguredService(repo, client) - account := repo.accounts[0] - - errs := make(chan error, 2) - tokens := make(chan string, 2) - for i := 0; i < 2; i++ { - go func() { - token, err := service.AccessTokenForAccount(context.Background(), account) - if err != nil { - errs <- err - return - } - tokens <- token - errs <- nil - }() + if err != nil { + t.Fatalf("NewKeyring returned error: %v", err) } + service := NewService(newMemoryRepo(), fakeProbeClient{}, Config{EncryptionKeyring: keyring}) - <-client.entered - client.release <- struct{}{} - for i := 0; i < 2; i++ { - if err := <-errs; err != nil { - t.Fatalf("AccessTokenForAccount returned error: %v", err) - } + encrypted, err := service.encryptString(secret.SecretKindOAuthAccessToken, "current-provider-token") + if err != nil { + t.Fatalf("encryptString returned error: %v", err) } - for i := 0; i < 2; i++ { - if token := <-tokens; token != "new-access" { - t.Fatalf("token = %q, want new-access", token) - } + if !strings.HasPrefix(encrypted, "n2api:v1:current-202607:oauth-access-token:") { + t.Fatalf("encryptString = %q, want current key envelope", encrypted) + } + if _, err := service.decryptString(secret.SecretKindOAuthRefreshToken, encrypted); err == nil { + t.Fatal("decryptString accepted an access-token envelope as a refresh token") + } + decrypted, err := service.decryptString(secret.SecretKindOAuthRefreshToken, legacyCiphertext) + if err != nil { + t.Fatalf("decryptString returned error for legacy previous key: %v", err) } - if client.calls != 1 { - t.Fatalf("refresh calls = %d, want 1", client.calls) + if decrypted != "legacy-oauth-refresh-token" { + t.Fatalf("decryptString = %q, want legacy-oauth-refresh-token", decrypted) } } @@ -1102,7 +83,7 @@ func TestSelectAccountForModelSkipsDisabledAndUsesPriority(t *testing.T) { testAccount(t, 1, false, 1, "disabled-token"), testAccount(t, 2, true, 10, "enabled-token"), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -1113,24 +94,6 @@ func TestSelectAccountForModelSkipsDisabledAndUsesPriority(t *testing.T) { } } -func TestSelectAccountForModelReturnsChatGPTAccountMetadata(t *testing.T) { - repo := newMemoryRepo() - repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - repo.accounts[0].Metadata = map[string]string{"chatgpt_account_id": "acct_chatgpt"} - service := newConfiguredService(repo, fakeOAuthClient{}) - - selected, err := service.SelectAccountForModel(context.Background(), "") - if err != nil { - t.Fatalf("SelectAccountForModel returned error: %v", err) - } - if selected.AccountID != 7 || selected.AuthorizationToken != "access-token" { - t.Fatalf("selected = %+v, want account 7 authorization token", selected) - } - if selected.ChatGPTAccountID != "acct_chatgpt" { - t.Fatalf("ChatGPTAccountID = %q, want metadata value", selected.ChatGPTAccountID) - } -} - func TestSelectAccountForModelReturnsAPIUpstreamCredentialAndBaseURL(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{{ @@ -1149,7 +112,7 @@ func TestSelectAccountForModelReturnsAPIUpstreamCredentialAndBaseURL(t *testing. Status: AccountStatusActive, Metadata: map[string]string{}, }} - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("oauth refresh should not be called")}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -1206,7 +169,7 @@ func TestSelectAccountForModelSkipsAPIUpstreamWithInvalidBaseURL(t *testing.T) { Metadata: map[string]string{}, }, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -1260,7 +223,7 @@ func TestSelectAccountForModelSkipsAPIUpstreamWithHTTPBaseURLUnlessAllowed(t *te Metadata: map[string]string{}, }, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -1277,13 +240,8 @@ func TestSelectAccountForModelSkipsAPIUpstreamWithHTTPBaseURLUnlessAllowed(t *te repo.accounts[0].LastError = "" repo.accounts[0].LastErrorAt = nil repo.accounts[0].CircuitOpenUntil = nil - allowed := NewService(repo, fakeOAuthClient{}, Config{ + allowed := NewService(repo, fakeProbeClient{}, Config{ Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", Secret: "encryption-secret", AllowHTTPAPIUpstreams: true, }) @@ -1299,18 +257,17 @@ func TestSelectAccountForModelSkipsAPIUpstreamWithHTTPBaseURLUnlessAllowed(t *te func TestSelectAccountForModelSkipsRateLimitedCircuitOpenAndExpiredAccounts(t *testing.T) { repo := newMemoryRepo() now := time.Now() - expired := now.Add(-time.Hour) future := now.Add(time.Hour) repo.accounts = []Account{ testAccount(t, 1, true, 1, "rate-limited-token"), testAccount(t, 2, true, 2, "circuit-open-token"), - testExpiredAccount(t, 3, true, 3, "expired-token", "expired-refresh", expired), + testAccount(t, 3, true, 3, "expired-token"), testAccount(t, 4, true, 4, "usable-token"), } repo.accounts[0].RateLimitedUntil = &future repo.accounts[1].CircuitOpenUntil = &future repo.accounts[2].Status = AccountStatusExpired - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh failed")}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -1329,7 +286,7 @@ func TestSelectAccountForModelAllowsExpiredRateLimitAndCircuitWindows(t *testing } repo.accounts[0].Status = AccountStatusRateLimited repo.accounts[0].RateLimitedUntil = &past - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -1350,199 +307,6 @@ func TestSelectAccountForModelAllowsExpiredRateLimitAndCircuitWindows(t *testing } } -func TestAccessTokenForAccountRefreshFailureOpensCircuitAfterThreshold(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expired) - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh failed")}) - - for i := 0; i < refreshFailureCircuitThreshold; i++ { - _, _ = service.AccessTokenForAccount(context.Background(), repo.accounts[0]) - } - if repo.accounts[0].FailureCount != refreshFailureCircuitThreshold { - t.Fatalf("FailureCount = %d, want %d", repo.accounts[0].FailureCount, refreshFailureCircuitThreshold) - } - if repo.accounts[0].CircuitOpenUntil == nil || !repo.accounts[0].CircuitOpenUntil.After(time.Now()) { - t.Fatalf("CircuitOpenUntil = %v, want future circuit", repo.accounts[0].CircuitOpenUntil) - } - if repo.accounts[0].Status != AccountStatusCircuitOpen { - t.Fatalf("Status = %q, want circuit_open", repo.accounts[0].Status) - } -} - -func TestRefreshAccountForcesOAuthTokenRefreshAndClearsFailureState(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - repo.accounts = []Account{ - testAccount(t, 7, true, 3, "old-access"), - } - repo.accounts[0].EncryptedRefreshToken = mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token") - repo.accounts[0].AccessTokenExpiresAt = &expiresAt - repo.accounts[0].Status = AccountStatusCircuitOpen - repo.accounts[0].StatusReason = "previous failure" - repo.accounts[0].FailureCount = 3 - future := time.Now().Add(time.Hour) - repo.accounts[0].CircuitOpenUntil = &future - service := newConfiguredService(repo, fakeOAuthClient{ - refresh: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - }, - }) - - account, err := service.RefreshAccount(context.Background(), 7) - if err != nil { - t.Fatalf("RefreshAccount returned error: %v", err) - } - token, err := decryptForTest(t, "encryption-secret", secret.SecretKindOAuthAccessToken, account.EncryptedAccessToken) - if err != nil { - t.Fatalf("DecryptString returned error: %v", err) - } - if token != "new-access" { - t.Fatalf("token = %q, want new-access", token) - } - if account.Status != AccountStatusActive || account.FailureCount != 0 || account.CircuitOpenUntil != nil || account.LastRefreshAt == nil { - t.Fatalf("account after refresh = %+v", account) - } -} - -func TestProviderRefreshMetricsDistinguishModesAndOutcomes(t *testing.T) { - t.Run("manual success", func(t *testing.T) { - repo := newMemoryRepo() - account := testAccount(t, 7, true, 0, "old-access") - account.EncryptedRefreshToken = mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token") - repo.accounts = []Account{account} - metrics := &captureProviderMetrics{} - service := newConfiguredService(repo, fakeOAuthClient{refresh: TokenResponse{AccessToken: "new-access", RefreshToken: "new-refresh", ExpiresIn: 3600}}) - service.metrics = metrics - if _, err := service.RefreshAccount(context.Background(), account.ID); err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(metrics.refreshes, [][2]string{{"manual", "success"}}) { - t.Fatalf("refresh metrics = %+v", metrics.refreshes) - } - }) - - t.Run("rejected token skipped", func(t *testing.T) { - metrics := &captureProviderMetrics{} - service := newConfiguredService(newMemoryRepo(), fakeOAuthClient{}) - service.metrics = metrics - _, _, _, err := service.RefreshAccountAuthorization(context.Background(), 7, "token", http.StatusForbidden, "missing scopes: api.responses.write") - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(metrics.refreshes, [][2]string{{"rejected_token", "skipped"}}) { - t.Fatalf("refresh metrics = %+v", metrics.refreshes) - } - }) - - t.Run("automatic failure", func(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - account := testExpiredAccount(t, 7, true, 0, "old-access", "old-refresh", expired) - repo.accounts = []Account{account} - metrics := &captureProviderMetrics{} - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh failed")}) - service.metrics = metrics - _, _ = service.AccessTokenForAccount(context.Background(), account) - if !reflect.DeepEqual(metrics.refreshes, [][2]string{{"automatic", "failure"}}) { - t.Fatalf("refresh metrics = %+v", metrics.refreshes) - } - }) -} - -func TestRefreshAccountRejectsAPIUpstreamAccount(t *testing.T) { - repo := newMemoryRepo() - repo.accounts = []Account{ - testAccount(t, 7, true, 3, "upstream-key"), - } - repo.accounts[0].AccountType = AccountTypeAPIUpstream - repo.accounts[0].Credential.CredentialType = CredentialTypeAPIKey - repo.accounts[0].EncryptedRefreshToken = "" - repo.accounts[0].Credential.EncryptedRefreshToken = "" - service := newConfiguredService(repo, fakeOAuthClient{}) - - if _, err := service.RefreshAccount(context.Background(), 7); !errors.Is(err, ErrInvalidInput) { - t.Fatalf("RefreshAccount error = %v, want ErrInvalidInput", err) - } -} - -func TestRefreshAccountProbesLatestStatusAfterTokenRefresh(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - repo.accounts = []Account{ - testAccount(t, 7, true, 3, "old-access"), - } - repo.accounts[0].EncryptedRefreshToken = mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token") - repo.accounts[0].AccessTokenExpiresAt = &expiresAt - service := newConfiguredService(repo, fakeOAuthClient{ - refresh: TokenResponse{ - AccessToken: "new-access", - RefreshToken: "new-refresh", - ExpiresIn: 3600, - }, - probe: probeResult{ - statusCode: http.StatusTooManyRequests, - retryAfter: "120", - message: "usage limit reached", - }, - }) - - account, err := service.RefreshAccount(context.Background(), 7) - if err != nil { - t.Fatalf("RefreshAccount returned error: %v", err) - } - if account.Status != AccountStatusRateLimited { - t.Fatalf("Status = %q, want rate_limited", account.Status) - } - if account.StatusReason != "usage limit reached" || account.LastError != "usage limit reached" { - t.Fatalf("account failure message not recorded: %+v", account) - } - if account.RateLimitedUntil == nil || !account.RateLimitedUntil.After(time.Now().Add(100*time.Second)) { - t.Fatalf("RateLimitedUntil = %v, want retry-after window", account.RateLimitedUntil) - } - if account.LastTestStatus != AccountTestStatusFailed || account.LastTestError != "usage limit reached" { - t.Fatalf("test status/error = %q/%q, want failed probe result", account.LastTestStatus, account.LastTestError) - } -} - -func TestRefreshAccountPreservesHealthWhenProbeCannotConfirmRecovery(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(time.Hour) - account := testAccount(t, 7, true, 3, "old-access") - account.EncryptedRefreshToken = mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, "refresh-token") - account.AccessTokenExpiresAt = &expiresAt - account.Status = AccountStatusCircuitOpen - account.StatusReason = "existing circuit" - account.LastError = "existing circuit" - account.FailureCount = 3 - until := time.Now().Add(time.Hour) - account.CircuitOpenUntil = &until - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{ - refresh: TokenResponse{AccessToken: "new-access", RefreshToken: "new-refresh", ExpiresIn: 3600}, - probeErr: errors.New("probe network unavailable"), - }) - - refreshed, err := service.RefreshAccount(context.Background(), account.ID) - if err != nil { - t.Fatalf("RefreshAccount returned error: %v", err) - } - if refreshed.Status != AccountStatusCircuitOpen || refreshed.StatusReason != "existing circuit" || refreshed.LastError != "existing circuit" || - refreshed.FailureCount != 3 || refreshed.CircuitOpenUntil == nil { - t.Fatalf("account health = %+v, want preserved until confirmed probe recovery", refreshed) - } - if refreshed.LastTestStatus != AccountTestStatusFailed || refreshed.LastTestError != "probe network unavailable" { - t.Fatalf("test status/error = %q/%q, want failed network probe result", refreshed.LastTestStatus, refreshed.LastTestError) - } - token, err := decryptForTest(t, "encryption-secret", secret.SecretKindOAuthAccessToken, refreshed.EncryptedAccessToken) - if err != nil || token != "new-access" { - t.Fatalf("refreshed token = %q, err=%v", token, err) - } -} - func TestTestAccountProbesAPIUpstreamAndClearsFailureState(t *testing.T) { repo := newMemoryRepo() account := testAccount(t, 7, true, 3, "unused-oauth-token") @@ -1559,7 +323,7 @@ func TestTestAccountProbesAPIUpstreamAndClearsFailureState(t *testing.T) { account.CircuitOpenUntil = &circuitOpenUntil account.FailureCount = 2 repo.accounts = []Account{account} - client := &captureProbeOAuthClient{probe: probeResult{statusCode: http.StatusOK}} + client := &captureProbeClient{probe: probeResult{statusCode: http.StatusOK}} service := newConfiguredService(repo, client) requestLogger := &captureAccountTestRequestLogger{err: errors.New("request log unavailable")} service.accountTestRequestLogger = requestLogger @@ -1572,8 +336,8 @@ func TestTestAccountProbesAPIUpstreamAndClearsFailureState(t *testing.T) { t.Fatalf("TestAccount returned error: %v", err) } - if client.gotAccessToken != "upstream-secret" || client.gotConfig.APIBaseURL != "https://upstream.example.test" || client.gotConfig.ProbeChatGPTAccountID != "" { - t.Fatalf("probe call token=%q apiBase=%q chatgpt=%q", client.gotAccessToken, client.gotConfig.APIBaseURL, client.gotConfig.ProbeChatGPTAccountID) + if client.gotAccessToken != "upstream-secret" || client.gotConfig.APIBaseURL != "https://upstream.example.test" { + t.Fatalf("probe call token=%q apiBase=%q", client.gotAccessToken, client.gotConfig.APIBaseURL) } if tested.Status != AccountStatusActive || tested.StatusReason != "" || tested.LastError != "" || tested.LastErrorAt != nil || tested.CircuitOpenUntil != nil || tested.FailureCount != 0 { t.Fatalf("tested account = %+v, want local failure state cleared", tested) @@ -1596,93 +360,6 @@ func TestTestAccountProbesAPIUpstreamAndClearsFailureState(t *testing.T) { } } -func TestTestAccountOAuthRecoveryClearsRateLimitState(t *testing.T) { - repo := newMemoryRepo() - account := testAccount(t, 7, true, 3, "oauth-access-token") - account.AccountType = AccountTypeCodexOAuth - account.Metadata = map[string]string{"chatgpt_account_id": "acct_chatgpt"} - account.Status = AccountStatusRateLimited - account.StatusReason = "previous rate limit" - account.LastError = "previous rate limit" - now := time.Now() - rateLimitedUntil := now.Add(-time.Minute) - account.LastErrorAt = &now - account.RateLimitedUntil = &rateLimitedUntil - repo.accounts = []Account{account} - client := &captureProbeOAuthClient{probe: probeResult{statusCode: http.StatusOK}} - service := newConfiguredService(repo, client) - - tested, err := service.TestAccount(context.Background(), account.ID) - if err != nil { - t.Fatalf("TestAccount returned error: %v", err) - } - - if client.gotConfig.ProbeChatGPTAccountID != "acct_chatgpt" || client.gotAccessToken != "oauth-access-token" { - t.Fatalf("probe account/token = %q/%q, want acct_chatgpt/oauth-access-token", client.gotConfig.ProbeChatGPTAccountID, client.gotAccessToken) - } - if tested.Status != AccountStatusActive || tested.StatusReason != "" || tested.LastError != "" || tested.LastErrorAt != nil || tested.RateLimitedUntil != nil { - t.Fatalf("tested account = %+v, want recovered active account", tested) - } - if tested.LastTestAt == nil || tested.LastTestStatus != AccountTestStatusPassed || tested.LastTestError != "" { - t.Fatalf("test result = at:%v status:%q error:%q, want passed result", tested.LastTestAt, tested.LastTestStatus, tested.LastTestError) - } -} - -func TestTestAccountLogsModelOnlyForCodexResponsesProbe(t *testing.T) { - testCases := []struct { - name string - chatGPTAccountID string - wantModel string - wantRoute string - wantMethod string - wantProbeAccountID string - }{ - { - name: "missing ChatGPT account ID", - wantRoute: "/v1/models", - wantMethod: http.MethodGet, - }, - { - name: "with ChatGPT account ID", - chatGPTAccountID: "acct_chatgpt", - wantModel: "gpt-5.4-mini", - wantRoute: "/backend-api/codex/responses", - wantMethod: http.MethodPost, - wantProbeAccountID: "acct_chatgpt", - }, - } - - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - repo := newMemoryRepo() - account := testAccount(t, 7, true, 3, "oauth-access-token") - account.AccountType = AccountTypeCodexOAuth - if testCase.chatGPTAccountID != "" { - account.Metadata = map[string]string{"chatgpt_account_id": testCase.chatGPTAccountID} - } - repo.accounts = []Account{account} - client := &captureProbeOAuthClient{probe: probeResult{statusCode: http.StatusOK}} - service := newConfiguredService(repo, client) - requestLogger := &captureAccountTestRequestLogger{} - service.accountTestRequestLogger = requestLogger - - if _, err := service.TestAccount(context.Background(), account.ID); err != nil { - t.Fatalf("TestAccount returned error: %v", err) - } - if client.gotConfig.ProbeChatGPTAccountID != testCase.wantProbeAccountID { - t.Fatalf("probe ChatGPT account ID = %q, want %q", client.gotConfig.ProbeChatGPTAccountID, testCase.wantProbeAccountID) - } - if len(requestLogger.entries) != 1 { - t.Fatalf("request log count = %d, want 1", len(requestLogger.entries)) - } - entry := requestLogger.entries[0] - if entry.Model != testCase.wantModel || entry.Route != testCase.wantRoute || entry.Method != testCase.wantMethod { - t.Fatalf("request log model/route/method = %q/%q/%q, want %q/%q/%q", entry.Model, entry.Route, entry.Method, testCase.wantModel, testCase.wantRoute, testCase.wantMethod) - } - }) - } -} - func TestTestAccountRecordsAPIUpstreamFailure(t *testing.T) { repo := newMemoryRepo() account := testAccount(t, 7, true, 3, "unused-oauth-token") @@ -1691,7 +368,7 @@ func TestTestAccountRecordsAPIUpstreamFailure(t *testing.T) { account.Credential.EncryptedAPIKey = mustEncrypt(t, "encryption-secret", secret.SecretKindProviderAPIKey, "upstream-secret") account.Credential.BaseURL = "https://upstream.example.test" repo.accounts = []Account{account} - client := &captureProbeOAuthClient{probe: probeResult{statusCode: http.StatusTooManyRequests, retryAfter: "120", message: "quota window"}} + client := &captureProbeClient{probe: probeResult{statusCode: http.StatusTooManyRequests, retryAfter: "120", message: "quota window"}} service := newConfiguredService(repo, client) requestLogger := &captureAccountTestRequestLogger{} service.accountTestRequestLogger = requestLogger @@ -1727,7 +404,7 @@ func TestTestAccountRequiresTwoHundredStatusForRecovery(t *testing.T) { until := time.Now().Add(time.Hour) account.CircuitOpenUntil = &until repo.accounts = []Account{account} - client := &captureProbeOAuthClient{probe: probeResult{statusCode: statusCode, message: "non-success probe"}} + client := &captureProbeClient{probe: probeResult{statusCode: statusCode, message: "non-success probe"}} service := newConfiguredService(repo, client) tested, err := service.TestAccount(context.Background(), account.ID) @@ -1766,7 +443,7 @@ func TestTestAccountsProbesEveryProviderAccount(t *testing.T) { second.LastErrorAt = &now second.CircuitOpenUntil = &until repo.accounts = []Account{first, second} - client := &captureProbeOAuthClient{probes: []probeResult{{statusCode: http.StatusOK}, {statusCode: http.StatusOK}}} + client := &captureProbeClient{probes: []probeResult{{statusCode: http.StatusOK}, {statusCode: http.StatusOK}}} service := newConfiguredService(repo, client) tested, err := service.TestAccounts(context.Background()) @@ -1794,7 +471,7 @@ func TestTestAccountPassesConfiguredProxyURLToProbe(t *testing.T) { account.Credential.EncryptedProxyURL = mustEncrypt(t, "encryption-secret", secret.SecretKindProviderProxyURL, "http://proxy.example.test:8080") account.Credential.BaseURL = "https://upstream.example.test" repo.accounts = []Account{account} - client := &captureProbeOAuthClient{probe: probeResult{statusCode: http.StatusOK}} + client := &captureProbeClient{probe: probeResult{statusCode: http.StatusOK}} service := newConfiguredService(repo, client) if _, err := service.TestAccount(context.Background(), account.ID); err != nil { @@ -1822,11 +499,6 @@ func TestTestAccountModelUsesExactAPIUpstreamAndPersistsFailureWithoutChangingHe profileID := int64(9) account.FingerprintProfileID = &profileID repo.accounts = []Account{account} - repo.fingerprintProfiles[profileID] = FingerprintProfileData{ - UserAgent: "model-probe-agent", - TLSFingerprint: "chrome", - Headers: map[string]string{"X-Probe": "profile"}, - } _, err := repo.ReplaceAccountModels(context.Background(), "openai", account.ID, []AccountModelInput{{Model: "gpt-test", Enabled: false}}) if err != nil { t.Fatalf("ReplaceAccountModels returned error: %v", err) @@ -1836,7 +508,7 @@ func TestTestAccountModelUsesExactAPIUpstreamAndPersistsFailureWithoutChangingHe errorCode: "rate_limited", message: " quota upstream-secret window\nreached ", }} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) service.modelProber = prober requestLogger := &captureAccountTestRequestLogger{} service.accountTestRequestLogger = requestLogger @@ -1848,9 +520,6 @@ func TestTestAccountModelUsesExactAPIUpstreamAndPersistsFailureWithoutChangingHe if prober.model != "gpt-test" || prober.selected.AccountID != account.ID || prober.selected.AuthorizationToken != "upstream-secret" || prober.selected.BaseURL != "https://upstream.example.test/v1" || prober.selected.ProxyURL != "http://proxy.example.test:8080" { t.Fatalf("model probe selected account = %+v model=%q", prober.selected, prober.model) } - if prober.selected.FingerprintUA != "model-probe-agent" || prober.selected.FingerprintTLS != "chrome" || prober.selected.FingerprintHeaders["X-Probe"] != "profile" { - t.Fatalf("model probe fingerprint = ua:%q tls:%q headers:%v", prober.selected.FingerprintUA, prober.selected.FingerprintTLS, prober.selected.FingerprintHeaders) - } if result.Status != AccountTestStatusFailed || result.ErrorCode != "rate_limited" || result.HTTPStatus != http.StatusTooManyRequests || result.Message != "quota [redacted] window reached" || result.CheckedAt.IsZero() { t.Fatalf("result = %+v", result) } @@ -1888,7 +557,7 @@ func TestTestAccountModelRejectsModelsNotConfiguredForAccount(t *testing.T) { t.Fatalf("ReplaceAccountModels returned error: %v", err) } prober := &captureAccountModelProber{} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) service.modelProber = prober _, err = service.TestAccountModel(context.Background(), 7, "other-model") @@ -1900,73 +569,6 @@ func TestTestAccountModelRejectsModelsNotConfiguredForAccount(t *testing.T) { } } -func TestTestAccountModelRefreshFailureDoesNotChangeAccountHealth(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(-time.Minute) - account := testExpiredAccount(t, 7, true, 3, "expired-access", "refresh-token", expiresAt) - account.Status = AccountStatusActive - repo.accounts = []Account{account} - _, err := repo.ReplaceAccountModels(context.Background(), "openai", 7, []AccountModelInput{{Model: "gpt-test", Enabled: true}}) - if err != nil { - t.Fatalf("ReplaceAccountModels returned error: %v", err) - } - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh unavailable")}) - - _, err = service.TestAccountModel(context.Background(), 7, "gpt-test") - if err == nil { - t.Fatal("TestAccountModel returned nil error, want refresh failure") - } - unchanged, err := repo.FindAccountByID(context.Background(), "openai", 7) - if err != nil { - t.Fatalf("FindAccountByID returned error: %v", err) - } - if unchanged.Status != AccountStatusActive || unchanged.FailureCount != 0 || unchanged.LastRefreshError != "" { - t.Fatalf("refresh failure changed diagnostic account health: %+v", unchanged) - } -} - -func TestTestAccountModelSuccessfulRefreshUpdatesOnlyCredentialState(t *testing.T) { - repo := newMemoryRepo() - expiresAt := time.Now().Add(-time.Minute) - account := testExpiredAccount(t, 7, true, 3, "expired-access", "refresh-token", expiresAt) - account.Status = AccountStatusCircuitOpen - account.StatusReason = "existing health state" - account.LastError = "existing health state" - account.FailureCount = 4 - until := time.Now().Add(time.Hour) - account.CircuitOpenUntil = &until - repo.accounts = []Account{account} - _, err := repo.ReplaceAccountModels(context.Background(), "openai", 7, []AccountModelInput{{Model: "gpt-test", Enabled: true}}) - if err != nil { - t.Fatalf("ReplaceAccountModels returned error: %v", err) - } - prober := &captureAccountModelProber{result: modelProbeResult{statusCode: http.StatusOK}} - service := newConfiguredService(repo, fakeOAuthClient{refresh: TokenResponse{ - AccessToken: "refreshed-access", - RefreshToken: "refreshed-refresh", - ExpiresIn: 3600, - }}) - service.modelProber = prober - - if _, err := service.TestAccountModel(context.Background(), 7, "gpt-test"); err != nil { - t.Fatalf("TestAccountModel returned error: %v", err) - } - if prober.selected.AuthorizationToken != "refreshed-access" { - t.Fatalf("model probe token = %q, want refreshed-access", prober.selected.AuthorizationToken) - } - unchanged, err := repo.FindAccountByID(context.Background(), "openai", 7) - if err != nil { - t.Fatalf("FindAccountByID returned error: %v", err) - } - if unchanged.Status != AccountStatusCircuitOpen || unchanged.StatusReason != "existing health state" || unchanged.LastError != "existing health state" || unchanged.FailureCount != 4 || unchanged.CircuitOpenUntil == nil { - t.Fatalf("successful diagnostic refresh changed account health: %+v", unchanged) - } - refreshedToken, err := decryptForTest(t, "encryption-secret", secret.SecretKindOAuthAccessToken, unchanged.Credential.EncryptedAccessToken) - if err != nil || refreshedToken != "refreshed-access" { - t.Fatalf("refreshed credential token = %q error=%v", refreshedToken, err) - } -} - func TestListAccountTestResultsValidatesAndNormalizesLimit(t *testing.T) { baseTime := time.Now().UTC().Truncate(time.Second) repo := newMemoryRepo() @@ -1982,7 +584,7 @@ func TestListAccountTestResultsValidatesAndNormalizesLimit(t *testing.T) { CreatedAt: checkedAt, }) } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.ListAccountTestResults(context.Background(), 0, 20); !errors.Is(err, ErrInvalidInput) { t.Fatalf("invalid id error = %v, want ErrInvalidInput", err) @@ -2009,7 +611,7 @@ func TestListAccountTestResultsValidatesAndNormalizesLimit(t *testing.T) { func TestPauseAccountSchedulingTemporarilyOpensCircuit(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 3, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) paused, err := service.PauseAccountScheduling(context.Background(), 7, 5*time.Minute) if err != nil { @@ -2030,7 +632,7 @@ func TestPauseAccountSchedulingTemporarilyOpensCircuit(t *testing.T) { func TestRecordAccountFailureMapsUpstreamStatusesToAccountState(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if err := service.RecordAccountFailure(context.Background(), 7, 429, "120", "rate limited"); err != nil { t.Fatalf("RecordAccountFailure rate limit returned error: %v", err) @@ -2075,7 +677,7 @@ func TestRecordAccountFailureDoesNotExpireAccountForResponsesScopeForbidden(t *t repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} repo.accounts[0].Status = AccountStatusActive - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) message := "You have insufficient permissions for this operation. Missing scopes: api.responses.write." if err := service.RecordAccountFailure(context.Background(), 7, 403, "", message); err != nil { @@ -2104,7 +706,7 @@ func TestResetAccountStatusClearsLocalFailureWindows(t *testing.T) { repo.accounts[0].RateLimitedUntil = &future repo.accounts[0].CircuitOpenUntil = &future repo.accounts[0].FailureCount = 3 - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) account, err := service.ResetAccountStatus(context.Background(), 7) if err != nil { @@ -2124,7 +726,7 @@ func TestSelectAccountForModelUsesPriorityOrder(t *testing.T) { testAccount(t, 1, true, 10, "low-priority-token"), testAccount(t, 2, true, 1, "high-priority-token"), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -2142,7 +744,7 @@ func TestSelectAccountForModelUsesLoadFactorWithinPriority(t *testing.T) { second := testAccount(t, 2, true, 1, "high-capacity-token") second.LoadFactor = 5 repo.accounts = []Account{first, second} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -2158,7 +760,7 @@ func TestSelectedAccountIncludesMaxConcurrentRequests(t *testing.T) { account := testAccount(t, 7, true, 1, "access-token") account.MaxConcurrentRequests = 2 repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "") if err != nil { @@ -2172,7 +774,7 @@ func TestSelectedAccountIncludesMaxConcurrentRequests(t *testing.T) { func TestUpdateAccountCanRenameLocalAccountLabel(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) name := " Work Codex " account, err := service.UpdateAccount(context.Background(), 7, AccountUpdate{Name: &name}) @@ -2210,7 +812,7 @@ func TestUpdateAccountCanRotateAPIUpstreamCredential(t *testing.T) { FailureCount: 3, CircuitOpenUntil: &openUntil, }} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) oldEncryptedAPIKey := repo.accounts[0].Credential.EncryptedAPIKey baseURL := "https://new.example.test/v1/" apiKey := "new-secret" @@ -2244,7 +846,7 @@ func TestUpdateAccountCanRotateAPIUpstreamCredential(t *testing.T) { func TestUpdateAccountRejectsNegativeMaxConcurrentRequests(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) enabled := true maxConcurrentRequests := -1 @@ -2253,21 +855,10 @@ func TestUpdateAccountRejectsNegativeMaxConcurrentRequests(t *testing.T) { } } -func TestUpdateAccountRejectsAPIUpstreamCredentialPatchForOAuthAccount(t *testing.T) { - repo := newMemoryRepo() - repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) - baseURL := "https://new.example.test/v1" - - if _, err := service.UpdateAccount(context.Background(), 7, AccountUpdate{APIUpstreamBaseURL: &baseURL}); !errors.Is(err, ErrInvalidInput) { - t.Fatalf("UpdateAccount error = %v, want ErrInvalidInput", err) - } -} - func TestUpdateAccountRejectsEmptyLocalAccountLabel(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) name := " " if _, err := service.UpdateAccount(context.Background(), 7, AccountUpdate{Name: &name}); !errors.Is(err, ErrInvalidInput) { @@ -2275,57 +866,13 @@ func TestUpdateAccountRejectsEmptyLocalAccountLabel(t *testing.T) { } } -func TestUpdateAccountRejectsUnknownFingerprintProfile(t *testing.T) { - repo := newMemoryRepo() - repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) - profileID := int64(999) - - if _, err := service.UpdateAccount(context.Background(), 7, AccountUpdate{FingerprintProfileIDSet: true, FingerprintProfileID: &profileID}); !errors.Is(err, ErrInvalidInput) { - t.Fatalf("UpdateAccount error = %v, want ErrInvalidInput", err) - } -} - -func TestUpdateAccountCanSetKnownFingerprintProfile(t *testing.T) { - profileID := int64(7) - repo := newMemoryRepo() - repo.accounts = []Account{testAccount(t, 7, true, 1, "access-token")} - repo.fingerprintProfiles[profileID] = FingerprintProfileData{UserAgent: "Mozilla/5.0"} - service := newConfiguredService(repo, fakeOAuthClient{}) - - updated, err := service.UpdateAccount(context.Background(), 7, AccountUpdate{FingerprintProfileIDSet: true, FingerprintProfileID: &profileID}) - if err != nil { - t.Fatalf("UpdateAccount returned error: %v", err) - } - if updated.FingerprintProfileID == nil || *updated.FingerprintProfileID != profileID { - t.Fatalf("FingerprintProfileID = %+v, want %d", updated.FingerprintProfileID, profileID) - } -} - -func TestUpdateAccountAllowsClearingFingerprintProfile(t *testing.T) { - profileID := int64(7) - repo := newMemoryRepo() - account := testAccount(t, 7, true, 1, "access-token") - account.FingerprintProfileID = &profileID - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{}) - - updated, err := service.UpdateAccount(context.Background(), 7, AccountUpdate{FingerprintProfileIDSet: true}) - if err != nil { - t.Fatalf("UpdateAccount returned error: %v", err) - } - if updated.FingerprintProfileID != nil { - t.Fatalf("FingerprintProfileID = %v, want cleared nil", *updated.FingerprintProfileID) - } -} - func TestSelectAccountForModelSkipsExcludedAccount(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{ testAccount(t, 1, true, 1, "first-token"), testAccount(t, 2, true, 2, "fallback-token"), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "", 1) if err != nil { @@ -2346,7 +893,7 @@ func TestSelectAccountForModelFiltersByRequestedModel(t *testing.T) { 1: {{AccountID: 1, Provider: "openai", Model: "gpt-4.1", Enabled: true}}, 2: {{AccountID: 2, Provider: "openai", Model: "gpt-5", Enabled: true}}, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "gpt-5") if err != nil { @@ -2365,7 +912,7 @@ func TestSelectAccountForModelReturnsModelUnavailableWhenNoAccountSupportsModel( repo.accountModels = map[int64][]AccountModel{ 1: {{AccountID: 1, Provider: "openai", Model: "gpt-4.1", Enabled: true}}, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), "gpt-5"); !errors.Is(err, ErrModelUnavailable) { t.Fatalf("SelectAccountForModel error = %v, want ErrModelUnavailable", err) @@ -2380,7 +927,7 @@ func TestSelectAccountForModelReturnsAccountsDisabledWhenAllModelAccountsDisable repo.accountModels = map[int64][]AccountModel{ 1: {{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}}, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), "gpt-5"); !errors.Is(err, ErrAccountsDisabled) { t.Fatalf("SelectAccountForModel error = %v, want ErrAccountsDisabled", err) @@ -2395,7 +942,7 @@ func TestSelectAccountForModelReturnsAccountsUnavailableWhenOnlyModelAccountExcl repo.accountModels = map[int64][]AccountModel{ 1: {{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}}, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), "gpt-5", 1); !errors.Is(err, ErrAccountsUnavailable) { t.Fatalf("SelectAccountForModel error = %v, want ErrAccountsUnavailable", err) @@ -2405,12 +952,12 @@ func TestSelectAccountForModelReturnsAccountsUnavailableWhenOnlyModelAccountExcl func TestSelectAccountForModelReturnsAccountsUnavailableWhenModelAccountsCannotProvideToken(t *testing.T) { repo := newMemoryRepo() badToken := testAccount(t, 1, true, 1, "access-token") - badToken.EncryptedAccessToken = "not-encrypted" + badToken.Credential.EncryptedAPIKey = "not-encrypted" repo.accounts = []Account{badToken} repo.accountModels = map[int64][]AccountModel{ 1: {{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}}, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), "gpt-5"); !errors.Is(err, ErrAccountsUnavailable) { t.Fatalf("SelectAccountForModel error = %v, want ErrAccountsUnavailable", err) @@ -2430,7 +977,7 @@ func TestSelectAccountForModelIgnoresDisabledModelRows(t *testing.T) { 1: {{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: false}}, 2: {{AccountID: 2, Provider: "openai", Model: "gpt-5", Enabled: true}}, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModel(context.Background(), "gpt-5") if err != nil { @@ -2443,93 +990,22 @@ func TestSelectAccountForModelIgnoresDisabledModelRows(t *testing.T) { func TestSelectAccountForModelRespectsExcludedAccountIDsWithModelFilter(t *testing.T) { repo := newMemoryRepo() - repo.accounts = []Account{ - testAccount(t, 1, true, 1, "first-token"), - testAccount(t, 2, true, 2, "fallback-token"), - } - repo.accountModels = map[int64][]AccountModel{ - 1: {{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}}, - 2: {{AccountID: 2, Provider: "openai", Model: "gpt-5", Enabled: true}}, - } - service := newConfiguredService(repo, fakeOAuthClient{}) - - selected, err := service.SelectAccountForModel(context.Background(), "gpt-5", 1) - if err != nil { - t.Fatalf("SelectAccountForModel returned error: %v", err) - } - if selected.AccountID != 2 || selected.AuthorizationToken != "fallback-token" { - t.Fatalf("selected = %+v, want non-excluded account 2", selected) - } -} - -func TestSelectAccountAppliesFingerprintProfileForOAuthAndAPIUpstream(t *testing.T) { - profileID := int64(7) - for _, tc := range []struct { - name string - account Account - wantToken string - wantBaseURL string - }{ - { - name: "codex oauth", - account: func() Account { - account := testAccount(t, 1, true, 1, "oauth-token") - account.FingerprintProfileID = &profileID - return account - }(), - wantToken: "oauth-token", - wantBaseURL: "https://api.openai.example.test", - }, - { - name: "api upstream", - account: Account{ - ID: 2, - Provider: "openai", - AccountType: AccountTypeAPIUpstream, - Subject: "api-upstream", - DisplayName: "API upstream", - Enabled: true, - Priority: 1, - FingerprintProfileID: &profileID, - Credential: AccountCredential{ - BaseURL: "https://upstream.example.test/v1", - EncryptedAPIKey: mustEncrypt(t, "encryption-secret", secret.SecretKindProviderAPIKey, "api-token"), - }, - }, - wantToken: "api-token", - wantBaseURL: "https://upstream.example.test/v1", - }, - } { - t.Run(tc.name, func(t *testing.T) { - repo := newMemoryRepo() - repo.accounts = []Account{tc.account} - repo.accountModels[tc.account.ID] = []AccountModel{{AccountID: tc.account.ID, Provider: "openai", Model: "gpt-5", Enabled: true}} - repo.fingerprintProfiles[profileID] = FingerprintProfileData{ - UserAgent: "Mozilla/5.0", - TLSFingerprint: "chrome", - Headers: map[string]string{"X-Fingerprint": "enabled"}, - } - service := newConfiguredService(repo, fakeOAuthClient{}) - service.cfg.APIBaseURL = "https://api.openai.example.test" - - selected, err := service.SelectAccountForModel(context.Background(), "gpt-5") - if err != nil { - t.Fatalf("SelectAccountForModel returned error: %v", err) - } + repo.accounts = []Account{ + testAccount(t, 1, true, 1, "first-token"), + testAccount(t, 2, true, 2, "fallback-token"), + } + repo.accountModels = map[int64][]AccountModel{ + 1: {{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}}, + 2: {{AccountID: 2, Provider: "openai", Model: "gpt-5", Enabled: true}}, + } + service := newConfiguredService(repo, fakeProbeClient{}) - if selected.AuthorizationToken != tc.wantToken || selected.BaseURL != tc.wantBaseURL { - t.Fatalf("selected token/baseURL = %q/%q, want %q/%q", selected.AuthorizationToken, selected.BaseURL, tc.wantToken, tc.wantBaseURL) - } - if selected.FingerprintUA != "Mozilla/5.0" { - t.Fatalf("fingerprint UA = %q, want Mozilla/5.0", selected.FingerprintUA) - } - if selected.FingerprintTLS != "chrome" { - t.Fatalf("fingerprint TLS = %q, want chrome", selected.FingerprintTLS) - } - if selected.FingerprintHeaders["X-Fingerprint"] != "enabled" { - t.Fatalf("fingerprint headers = %+v, want custom header", selected.FingerprintHeaders) - } - }) + selected, err := service.SelectAccountForModel(context.Background(), "gpt-5", 1) + if err != nil { + t.Fatalf("SelectAccountForModel returned error: %v", err) + } + if selected.AccountID != 2 || selected.AuthorizationToken != "fallback-token" { + t.Fatalf("selected = %+v, want non-excluded account 2", selected) } } @@ -2549,7 +1025,7 @@ func TestSelectAccountForModelAndSessionUsesStickyHashAcrossCandidateOrder(t *te {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelAndSession(context.Background(), "gpt-5", "workspace-123") if err != nil { @@ -2589,7 +1065,7 @@ func TestSelectAccountForModelAndSessionPersistsAndReusesBinding(t *testing.T) { {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelAndSession(context.Background(), "gpt-5", "workspace-123") if err != nil { @@ -2624,7 +1100,7 @@ func TestSelectAccountForModelInRoutingPoolScopesCandidates(t *testing.T) { {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPool(context.Background(), 7, "gpt-5") if err != nil { @@ -2651,7 +1127,7 @@ func TestSelectAccountForModelInRoutingPoolUsesMembershipPriority(t *testing.T) {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPool(context.Background(), 7, "gpt-5") if err != nil { @@ -2680,7 +1156,7 @@ func TestSelectAccountForModelAndSessionInRoutingPoolKeepsStickyHashInsideGlobal {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selectedIDs := map[int64]bool{} for i := 0; i < 100; i++ { @@ -2713,7 +1189,7 @@ func TestSelectAccountForModelInRoutingPoolMissingPoolFailsClosed(t *testing.T) testAccount(t, 1, true, 1, "global-token"), } repo.accountModels[1] = []AccountModel{{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModelInRoutingPool(context.Background(), 99, "gpt-5"); !errors.Is(err, ErrRoutingPoolNotFound) { t.Fatalf("SelectAccountForModelInRoutingPool error = %v, want ErrRoutingPoolNotFound", err) @@ -2732,7 +1208,7 @@ func TestSelectAccountForModelInRoutingPoolChainFallsBackByModel(t *testing.T) { repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20, Priority: 0}} repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-4", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if err != nil { @@ -2758,7 +1234,7 @@ func TestSelectResponseAffinityAccountByIDAcrossRoutingPoolChain(t *testing.T) { repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20}} repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-5", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountByIDInRoutingPoolChain(context.Background(), 1, 20, "gpt-5") if err != nil { @@ -2785,7 +1261,7 @@ func TestSelectSingleResponseAffinityAccountRequiresUniqueScope(t *testing.T) { repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} } - return newConfiguredService(repo, fakeOAuthClient{}) + return newConfiguredService(repo, fakeProbeClient{}) } selected, unique, err := newService(false).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") @@ -2859,7 +1335,7 @@ func TestSelectSingleResponseAffinityAccountUsesFullTopology(t *testing.T) { repo.accountModels[second.ID] = []AccountModel{{AccountID: second.ID, Provider: "openai", Model: "gpt-5", Enabled: true}} testCase.configureSecond(repo, &repo.accounts[1]) - selected, unique, err := newConfiguredService(repo, fakeOAuthClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") + selected, unique, err := newConfiguredService(repo, fakeProbeClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") if err != nil || unique || selected.AccountID != 0 { t.Fatalf("topology selection = %+v/%v/%v, want empty/false/nil", selected, unique, err) } @@ -2874,7 +1350,7 @@ func TestSelectSingleResponseAffinityAccountValidatesUniqueAccountScheduling(t * repo.routingPoolAccounts[1] = []RoutingPoolAccount{{AccountID: 10}} repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-5", Enabled: true}} - selected, unique, err := newConfiguredService(repo, fakeOAuthClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") + selected, unique, err := newConfiguredService(repo, fakeProbeClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrAccountsUnavailable) || unique || selected.AccountID != 0 { t.Fatalf("disabled unique selection = %+v/%v/%v, want empty/false/ErrAccountsUnavailable", selected, unique, err) } @@ -2889,7 +1365,7 @@ func TestSelectSingleResponseAffinityAccountDeduplicatesMembershipsAcrossChain(t repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 10}} repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-5", Enabled: true}} - selected, unique, err := newConfiguredService(repo, fakeOAuthClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") + selected, unique, err := newConfiguredService(repo, fakeProbeClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") if err != nil || !unique || selected.AccountID != 10 || selected.RoutingPoolID != 1 { t.Fatalf("shared topology selection = %+v/%v/%v, want account 10 in primary pool", selected, unique, err) } @@ -2908,7 +1384,7 @@ func TestSelectSingleResponseAffinityAccountCountsDisabledFallbackTopology(t *te repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-5", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} - selected, unique, err := newConfiguredService(repo, fakeOAuthClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") + selected, unique, err := newConfiguredService(repo, fakeProbeClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") if err != nil || unique || selected.AccountID != 0 { t.Fatalf("disabled fallback topology selection = %+v/%v/%v, want empty/false/nil", selected, unique, err) } @@ -2922,7 +1398,7 @@ func TestSelectSingleResponseAffinityAccountSelectsUniqueFallbackAccount(t *test repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} - selected, unique, err := newConfiguredService(repo, fakeOAuthClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") + selected, unique, err := newConfiguredService(repo, fakeProbeClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") if err != nil || !unique || selected.AccountID != 20 || selected.RoutingPoolID != 2 || selected.RoutingPoolFallbackDepth != 1 { t.Fatalf("fallback topology selection = %+v/%v/%v, want account 20 in fallback pool", selected, unique, err) } @@ -2936,7 +1412,7 @@ func TestSelectSingleResponseAffinityAccountRejectsDisabledPrimaryPool(t *testin repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} - selected, unique, err := newConfiguredService(repo, fakeOAuthClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") + selected, unique, err := newConfiguredService(repo, fakeProbeClient{}).SelectSingleAccountInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrAccountsDisabled) || unique || selected.AccountID != 0 { t.Fatalf("disabled primary selection = %+v/%v/%v, want empty/false/ErrAccountsDisabled", selected, unique, err) } @@ -2961,7 +1437,7 @@ func TestSelectAccountForModelInRoutingPoolChainSkipsDisabledFallbackPool(t *tes repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-4", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} repo.accountModels[30] = []AccountModel{{AccountID: 30, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if err != nil { @@ -2979,7 +1455,7 @@ func TestSelectAccountForModelInRoutingPoolChainRejectsCycle(t *testing.T) { repo := newMemoryRepo() repo.routingPools[1] = RoutingPool{ID: 1, Name: "primary", Enabled: true, FallbackPoolID: ptrInt64(2)} repo.routingPools[2] = RoutingPool{ID: 2, Name: "secondary", Enabled: true, FallbackPoolID: ptrInt64(1)} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrRoutingPoolCycle) { @@ -2991,7 +1467,7 @@ func TestSelectAccountForModelInRoutingPoolChainRejectsCycle(t *testing.T) { } func TestSelectAccountForModelInRoutingPoolChainMarksUnavailableDiagnostics(t *testing.T) { - service := newConfiguredService(newMemoryRepo(), fakeOAuthClient{}) + service := newConfiguredService(newMemoryRepo(), fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 99, "gpt-5") if !errors.Is(err, ErrRoutingPoolNotFound) { @@ -3005,7 +1481,7 @@ func TestSelectAccountForModelInRoutingPoolChainMarksUnavailableDiagnostics(t *t func TestSelectAccountForModelInRoutingPoolChainMarksMissingFallbackAsExhausted(t *testing.T) { repo := newMemoryRepo() repo.routingPools[1] = RoutingPool{ID: 1, Name: "primary", Enabled: true, FallbackPoolID: ptrInt64(2)} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrRoutingPoolExhausted) { @@ -3031,7 +1507,7 @@ func TestSelectAccountForModelInRoutingPoolChainMarksExhaustedDiagnostics(t *tes repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20, Priority: 0}} repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-4", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-4", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrModelUnavailable) { @@ -3049,7 +1525,7 @@ func TestSelectAccountForModelInRoutingPoolChainMarksDisabledPrimaryDiagnostics( repo := newMemoryRepo() repo.routingPools[1] = RoutingPool{ID: 1, Name: "primary", Enabled: false, FallbackPoolID: ptrInt64(2)} repo.routingPools[2] = RoutingPool{ID: 2, Name: "secondary", Enabled: true} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrAccountsDisabled) { @@ -3066,7 +1542,7 @@ func TestSelectAccountForModelInRoutingPoolChainMarksDisabledPrimaryDiagnostics( func TestSelectAccountForModelInRoutingPoolChainMarksEmptyPrimaryDiagnostics(t *testing.T) { repo := newMemoryRepo() repo.routingPools[1] = RoutingPool{ID: 1, Name: "primary", Enabled: true} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelInRoutingPoolChain(context.Background(), 1, "gpt-5") if !errors.Is(err, ErrRoutingPoolEmpty) { @@ -3094,7 +1570,7 @@ func TestListExposedModelsForRoutingPoolChainIncludesFallbackModels(t *testing.T repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-4", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} repo.accountModels[30] = []AccountModel{{AccountID: 30, Provider: "openai", Model: "global-only", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) models, err := service.ListExposedModelsForRoutingPoolChain(context.Background(), 1) if err != nil { @@ -3120,7 +1596,7 @@ func TestSelectAccountForModelAndSessionInRoutingPoolDoesNotCrossScope(t *testin {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) first, err := service.SelectAccountForModelAndSessionInRoutingPool(context.Background(), 7, "gpt-5", "workspace-123") if err != nil { @@ -3159,7 +1635,7 @@ func TestSelectAccountForModelAndSessionInRoutingPoolRebindsWhenBoundAccountExcl AccountID: 1, CreatedAt: time.Now().Add(-time.Hour), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelAndSessionInRoutingPool(context.Background(), 7, "gpt-5", "workspace-123", 1) if err != nil { @@ -3196,7 +1672,7 @@ func TestSelectAccountForModelAndSessionRebindsWhenBoundAccountExcluded(t *testi AccountID: 1, CreatedAt: time.Now().Add(-time.Hour), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelAndSession(context.Background(), "gpt-5", "workspace-123", 1) if err != nil { @@ -3217,7 +1693,7 @@ func TestSelectAccountForModelDoesNotCreateSessionBinding(t *testing.T) { testAccount(t, 1, true, 1, "first-token"), } repo.accountModels[1] = []AccountModel{{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), "gpt-5"); err != nil { t.Fatalf("SelectAccountForModel returned error: %v", err) @@ -3239,7 +1715,7 @@ func TestSelectAccountForModelAndSessionKeepsStickySelectionInsideHighestPriorit {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) for _, sessionID := range []string{ "workspace-1", @@ -3274,7 +1750,7 @@ func TestPreviewAccountSelectionMeasuresStrictLoadFactorTierAcrossTenThousandSes {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) const sessionCount = 10_000 counts := map[int64]int{} @@ -3326,7 +1802,7 @@ func TestSelectAccountForModelAndSessionDoesNotPromoteErroredPriorityPeer(t *tes if sessionID == "" { t.Fatal("could not find deterministic sticky session fixture") } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) selected, err := service.SelectAccountForModelAndSession(context.Background(), "gpt-5", sessionID) if err != nil { @@ -3355,7 +1831,7 @@ func TestPreviewAccountSelectionUsesStickySessionWithoutMarkingAccountUsed(t *te {AccountID: repo.accounts[i].ID, Provider: "openai", Model: "gpt-5", Enabled: true}, } } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) sessionID := "workspace-123" expectedStart := stickyAccountIndex(sessionID, 2) + 1 @@ -3420,7 +1896,7 @@ func TestPreviewAccountSelectionIncludesScheduleReasons(t *testing.T) { SessionID: "workspace-123", AccountID: 2, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelection(context.Background(), "gpt-5", "workspace-123") if err != nil { @@ -3470,7 +1946,7 @@ func TestPreviewAccountSelectionReportsStoredStickyBindingWithoutMutation(t *tes UpdatedAt: createdAt, LastUsedAt: createdAt, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelection(context.Background(), "gpt-5", "workspace-123") if err != nil { @@ -3509,7 +1985,7 @@ func TestPreviewAccountSelectionReportsStoredStickyBindingForSingleCandidate(t * UpdatedAt: createdAt, LastUsedAt: createdAt, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelection(context.Background(), "gpt-5", "workspace-123") if err != nil { @@ -3539,7 +2015,7 @@ func TestPreviewAccountSelectionIncludesUnschedulableReasons(t *testing.T) { repo.accountModels[2] = []AccountModel{{AccountID: 2, Provider: "openai", Model: "gpt-5", Enabled: true}} repo.accountModels[3] = []AccountModel{{AccountID: 3, Provider: "openai", Model: "gpt-4.1", Enabled: true}} repo.accountModels[4] = []AccountModel{{AccountID: 4, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelection(context.Background(), "gpt-5", "", 4) if err != nil { @@ -3582,7 +2058,7 @@ func TestPreviewAccountSelectionReturnsBlockedCandidatesWhenNoneSchedulable(t *t } repo.accountModels[1] = []AccountModel{{AccountID: 1, Provider: "openai", Model: "gpt-5", Enabled: true}} repo.accountModels[2] = []AccountModel{{AccountID: 2, Provider: "openai", Model: "gpt-4.1", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelection(context.Background(), "gpt-5", "") if err != nil { @@ -3642,7 +2118,7 @@ func TestPreviewAccountSelectionInRoutingPoolScopesCandidatesAndStickyBinding(t UpdatedAt: createdAt, LastUsedAt: createdAt, } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelectionInRoutingPool(context.Background(), 7, "gpt-5", "workspace-123") if err != nil { @@ -3688,7 +2164,7 @@ func TestPreviewAccountSelectionInRoutingPoolScopesCandidatesAndStickyBinding(t func TestPreviewAccountSelectionInRoutingPoolMarksMissingFallbackAsExhausted(t *testing.T) { repo := newMemoryRepo() repo.routingPools[1] = RoutingPool{ID: 1, Name: "primary", Enabled: true, FallbackPoolID: ptrInt64(2)} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelectionInRoutingPool(context.Background(), 1, "gpt-5", "") if !errors.Is(err, ErrRoutingPoolExhausted) { @@ -3714,7 +2190,7 @@ func TestPreviewAccountSelectionInRoutingPoolFollowsFallbackChain(t *testing.T) repo.routingPoolAccounts[2] = []RoutingPoolAccount{{AccountID: 20, Priority: 0}} repo.accountModels[10] = []AccountModel{{AccountID: 10, Provider: "openai", Model: "gpt-4.1", Enabled: true}} repo.accountModels[20] = []AccountModel{{AccountID: 20, Provider: "openai", Model: "gpt-5", Enabled: true}} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) preview, err := service.PreviewAccountSelectionInRoutingPool(context.Background(), 1, "gpt-5", "") if err != nil { @@ -3737,7 +2213,7 @@ func TestReplaceAndListAccountModelsNormalizeInputs(t *testing.T) { repo.accounts = []Account{ testAccount(t, 7, true, 1, "access-token"), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) models, err := service.ReplaceAccountModels(context.Background(), 7, []AccountModelInput{ {Model: " gpt-5 ", Enabled: false}, @@ -3774,7 +2250,7 @@ func TestReplaceAndListAccountModelsNormalizeInputs(t *testing.T) { func TestCreateAPIUpstreamAccountSavesEncryptedKeyAndEnabledModels(t *testing.T) { repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ Name: " OpenAI proxy ", @@ -3826,7 +2302,7 @@ func TestCreateAPIUpstreamAccountSavesEncryptedKeyAndEnabledModels(t *testing.T) func TestCreateAPIUpstreamAccountStoresEncryptedProxyURL(t *testing.T) { repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ Name: "Upstream", @@ -3853,32 +2329,9 @@ func TestCreateAPIUpstreamAccountStoresEncryptedProxyURL(t *testing.T) { } } -func TestCreateAPIUpstreamAccountSetsKnownFingerprintProfile(t *testing.T) { - repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) - profileID := int64(9) - repo.fingerprintProfiles[profileID] = FingerprintProfileData{UserAgent: "Mozilla/5.0"} - - account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ - Name: "Upstream", - BaseURL: "https://upstream.example.test/v1", - APIKey: "sk-upstream", - FingerprintProfileID: &profileID, - }) - if err != nil { - t.Fatalf("CreateAPIUpstreamAccount returned error: %v", err) - } - if account.FingerprintProfileID == nil || *account.FingerprintProfileID != profileID { - t.Fatalf("FingerprintProfileID = %+v, want %d", account.FingerprintProfileID, profileID) - } - if repo.accounts[0].FingerprintProfileID == nil || *repo.accounts[0].FingerprintProfileID != profileID { - t.Fatalf("saved FingerprintProfileID = %+v, want %d", repo.accounts[0].FingerprintProfileID, profileID) - } -} - func TestUpdateAccountCanSetAndClearProxyURL(t *testing.T) { repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ Name: "Upstream", BaseURL: "https://upstream.example.test/v1", @@ -3924,7 +2377,7 @@ func TestUpdateAccountCanSetAndClearProxyURL(t *testing.T) { func TestCreateAPIUpstreamAccountDefaultsEnabledWhenOmitted(t *testing.T) { repo := newMemoryRepo() - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ Name: "OpenAI proxy", @@ -3945,7 +2398,7 @@ func TestCreateAPIUpstreamAccountDefaultsEnabledWhenOmitted(t *testing.T) { func TestCreateAPIUpstreamAccountDeletesAccountWhenInitialModelsFail(t *testing.T) { repo := newMemoryRepo() repo.replaceModelsErr = errors.New("replace models failed") - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ Name: "Upstream", @@ -3961,7 +2414,7 @@ func TestCreateAPIUpstreamAccountDeletesAccountWhenInitialModelsFail(t *testing. } func TestCreateAPIUpstreamAccountRejectsInvalidInput(t *testing.T) { - service := newConfiguredService(newMemoryRepo(), fakeOAuthClient{}) + service := newConfiguredService(newMemoryRepo(), fakeProbeClient{}) valid := APIUpstreamInput{Name: "Upstream", BaseURL: "https://upstream.example.test/v1", APIKey: "secret"} for _, tc := range []struct { @@ -3989,13 +2442,8 @@ func TestCreateAPIUpstreamAccountRejectsInvalidInput(t *testing.T) { func TestCreateAPIUpstreamAccountAllowsHTTPBaseURLWhenConfigured(t *testing.T) { repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ + service := NewService(repo, fakeProbeClient{}, Config{ Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", Secret: "encryption-secret", AllowHTTPAPIUpstreams: true, }) @@ -4031,13 +2479,8 @@ func TestSyncUpstreamAccountModelsFetchesAndSyncs(t *testing.T) { defer ts.Close() repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ + service := NewService(repo, fakeProbeClient{}, Config{ Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", Secret: "encryption-secret", AllowHTTPAPIUpstreams: true, }) @@ -4045,662 +2488,144 @@ func TestSyncUpstreamAccountModelsFetchesAndSyncs(t *testing.T) { account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ Name: "Test Upstream", BaseURL: ts.URL + "/v1", - APIKey: "sk-upstream-key", - }) - if err != nil { - t.Fatalf("CreateAPIUpstreamAccount returned error: %v", err) - } - - models, summary, err := service.SyncUpstreamAccountModels(context.Background(), account.ID) - if err != nil { - t.Fatalf("SyncUpstreamAccountModels returned error: %v", err) - } - if summary.Total != 3 { - t.Fatalf("summary.Total = %d, want 3 (deduped)", summary.Total) - } - if summary.New != 3 { - t.Fatalf("summary.New = %d, want 3", summary.New) - } - if len(models) != 3 { - t.Fatalf("len(models) = %d, want 3", len(models)) - } - names := make([]string, 0, len(models)) - for _, m := range models { - names = append(names, m.Model) - } - got := strings.Join(names, ",") - if got != "gpt-4,gpt-4-turbo,gpt-5" { - t.Fatalf("models = %s, want gpt-4,gpt-4-turbo,gpt-5", got) - } - // New synced rows should be disabled by default. - for _, m := range models { - if m.Enabled { - t.Fatalf("model %s is enabled, want disabled for new upstream sync rows", m.Model) - } - } - // Second sync should preserve existing rows. - models2, summary2, err := service.SyncUpstreamAccountModels(context.Background(), account.ID) - if err != nil { - t.Fatalf("second SyncUpstreamAccountModels returned error: %v", err) - } - if summary2.New != 0 || summary2.Preserved != 3 { - t.Fatalf("second summary: new=%d preserved=%d, want new=0 preserved=3", summary2.New, summary2.Preserved) - } - for _, m := range models2 { - if m.Enabled { - t.Fatalf("model %s is enabled on second sync, want preserved as disabled", m.Model) - } - } -} - -func TestSyncUpstreamAccountModelsDoesNotDoubleV1BaseURL(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/v1/models" { - t.Errorf("path = %s, want /v1/models", r.URL.Path) - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - w.Write([]byte(`{"data":[{"id":"gpt-4"}]}`)) - })) - defer ts.Close() - - repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", - Secret: "encryption-secret", - AllowHTTPAPIUpstreams: true, - }) - - account, err := repo.SaveAccount(context.Background(), Account{ - Provider: "openai", - AccountType: AccountTypeAPIUpstream, - Name: "Legacy upstream", - DisplayName: "Legacy upstream", - Enabled: true, - Priority: 100, - LoadFactor: 1, - Status: AccountStatusActive, - Credential: AccountCredential{ - CredentialType: CredentialTypeAPIKey, - EncryptedAPIKey: mustEncrypt(t, "encryption-secret", secret.SecretKindProviderAPIKey, "sk-upstream-key"), - BaseURL: ts.URL + "/v1", - }, - }) - if err != nil { - t.Fatalf("SaveAccount returned error: %v", err) - } - - if _, _, err := service.SyncUpstreamAccountModels(context.Background(), account.ID); err != nil { - t.Fatalf("SyncUpstreamAccountModels returned error: %v", err) - } -} - -func TestSyncUpstreamAccountModelsRejectsNonAPIUpstream(t *testing.T) { - repo := newMemoryRepo() - account := testAccount(t, 5, true, 1, "oauth-token") - account.AccountType = AccountTypeCodexOAuth - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{}) - - _, _, err := service.SyncUpstreamAccountModels(context.Background(), 5) - if err != ErrInvalidInput { - t.Fatalf("err = %v, want ErrInvalidInput for non-api_upstream account", err) - } -} - -func TestSyncUpstreamAccountModelsNon2xxDoesNotUpdateRows(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusUnauthorized) - })) - defer ts.Close() - - repo := newMemoryRepo() - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", - Secret: "encryption-secret", - AllowHTTPAPIUpstreams: true, - }) - - account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ - Name: "Bad Upstream", - BaseURL: ts.URL + "/v1", - APIKey: "sk-bad", - }) - if err != nil { - t.Fatalf("CreateAPIUpstreamAccount returned error: %v", err) - } - - _, _, err = service.SyncUpstreamAccountModels(context.Background(), account.ID) - if err == nil { - t.Fatal("expected error for non-2xx response") - } - - // Verify no models were synced. - models, err := service.ListAccountModels(context.Background(), account.ID) - if err != nil { - t.Fatalf("ListAccountModels returned error: %v", err) - } - if len(models) != 0 { - t.Fatalf("len(models) = %d, want 0 (no rows synced after non-2xx)", len(models)) - } -} - -func TestSyncOAuthAccountModelsFetchesCurrentCatalogAndEnablesNewModels(t *testing.T) { - var requestCount int - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestCount++ - if r.Method != http.MethodGet { - t.Errorf("method = %s, want GET", r.Method) - } - if r.URL.Path != "/backend-api/codex/models" { - t.Errorf("path = %s, want /backend-api/codex/models", r.URL.Path) - } - if r.URL.Query().Get("client_version") != DefaultCodexFingerprintVersion { - t.Errorf("client_version = %q, want %q", r.URL.Query().Get("client_version"), DefaultCodexFingerprintVersion) - } - if auth := r.Header.Get("Authorization"); auth != "Bearer oauth-access" { - t.Errorf("Authorization = %q, want OAuth bearer token", auth) - } - if accountID := r.Header.Get("ChatGPT-Account-ID"); accountID != "chatgpt-account-7" { - t.Errorf("ChatGPT-Account-ID = %q, want chatgpt-account-7", accountID) - } - if originator := r.Header.Get("originator"); originator != DefaultCodexFingerprintOriginator { - t.Errorf("originator = %q, want %q", originator, DefaultCodexFingerprintOriginator) - } - - models := `[ - {"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}, - {"slug":"gpt-5.6-terra","visibility":"list","supported_in_api":true,"upgrade":null}, - {"slug":"gpt-5.2","visibility":"list","supported_in_api":true,"upgrade":null}, - {"slug":"gpt-5.3-codex","visibility":"list","supported_in_api":true,"upgrade":null}, - {"slug":"gpt-5.4","visibility":"hide","supported_in_api":true,"upgrade":{"model":"gpt-5.6-terra"}}, - {"slug":"codex-auto-review","visibility":"hide","supported_in_api":true,"upgrade":null}, - {"slug":"not-api-supported","visibility":"list","supported_in_api":false,"upgrade":null}, - {"slug":"retired-model","visibility":"list","supported_in_api":true,"upgrade":{"model":"gpt-5.6-sol"}} - ]` - if requestCount > 1 { - models = `[ - {"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}, - {"slug":"gpt-5.6-luna","visibility":"list","supported_in_api":true,"upgrade":null} - ]` - } - w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"models":` + models + `}`)) - })) - defer ts.Close() - - repo := newMemoryRepo() - account := testAccount(t, 7, true, 1, "oauth-access") - account.AccountType = AccountTypeCodexOAuth - account.Metadata = map[string]string{"chatgpt_account_id": "chatgpt-account-7", "plan_type": "plus"} - repo.accounts = []Account{account} - catalogNow := time.Date(2026, 7, 31, 8, 0, 0, 0, time.UTC) - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - Secret: "encryption-secret", - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - OAuthModelCatalogCacheTTL: time.Hour, - }) - service.oauthModelCatalogNow = func() time.Time { return catalogNow } - - models, summary, err := service.SyncOAuthAccountModels(context.Background(), account.ID) - if err != nil { - t.Fatalf("SyncOAuthAccountModels returned error: %v", err) - } - if summary.Total != 2 || summary.New != 2 || summary.Preserved != 0 { - t.Fatalf("first summary = %+v, want two new current models", summary) - } - if got := accountModelNames(models); !reflect.DeepEqual(got, []string{"gpt-5.6-sol", "gpt-5.6-terra"}) { - t.Fatalf("models = %v, want only current visible models", got) - } - for _, model := range models { - if !model.Enabled || model.Source != AccountModelSourceOAuthCatalog { - t.Fatalf("model = %+v, want enabled OAuth catalog row", model) - } - } - - repo.accountModels[account.ID][0].Enabled = false - catalogNow = catalogNow.Add(time.Hour + time.Second) - models, summary, err = service.SyncOAuthAccountModels(context.Background(), account.ID) - if err != nil { - t.Fatalf("second SyncOAuthAccountModels returned error: %v", err) - } - if summary.Total != 2 || summary.New != 1 || summary.Preserved != 1 { - t.Fatalf("second summary = %+v, want one preserved and one new model", summary) - } - if got := accountModelNames(models); !reflect.DeepEqual(got, []string{"gpt-5.6-luna", "gpt-5.6-sol"}) { - t.Fatalf("models = %v, want stale catalog model removed", got) - } - for _, model := range models { - if model.Model == "gpt-5.6-sol" && model.Enabled { - t.Fatalf("preserved model = %+v, want existing disabled state", model) - } - if model.Model == "gpt-5.6-luna" && !model.Enabled { - t.Fatalf("new model = %+v, want enabled by default", model) - } - } - if requestCount != 2 { - t.Fatalf("catalog requests = %d, want 2 after cache expiry", requestCount) - } -} - -func TestSyncOAuthAccountModelsDoesNotShareCatalogAcrossAccounts(t *testing.T) { - var requestCount atomic.Int32 - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - requestCount.Add(1) - model := "gpt-5.6-sol" - if r.Header.Get("ChatGPT-Account-ID") == "chatgpt-account-8" { - model = "gpt-5.6-terra" - } - _, _ = fmt.Fprintf(w, `{"models":[{"slug":%q,"visibility":"list","supported_in_api":true,"upgrade":null}]}`, model) - })) - defer ts.Close() - - repo := newMemoryRepo() - first := testAccount(t, 7, true, 1, "first-access") - first.AccountType = AccountTypeCodexOAuth - first.Metadata = map[string]string{"chatgpt_account_id": "chatgpt-account-7", "plan_type": "Plus"} - second := testAccount(t, 8, true, 1, "second-access") - second.AccountType = AccountTypeCodexOAuth - second.Metadata = map[string]string{"chatgpt_account_id": "chatgpt-account-8", "plan_type": " plus "} - repo.accounts = []Account{first, second} - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - Secret: "encryption-secret", - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - }) - - for i, accountID := range []int64{first.ID, second.ID} { - models, _, err := service.SyncOAuthAccountModels(context.Background(), accountID) - if err != nil { - t.Fatalf("SyncOAuthAccountModels(%d): %v", accountID, err) - } - want := []string{"gpt-5.6-sol"} - if i == 1 { - want = []string{"gpt-5.6-terra"} - } - if got := accountModelNames(models); !reflect.DeepEqual(got, want) { - t.Fatalf("account %d models = %v, want %v", accountID, got, want) - } - } - models, _, err := service.SyncOAuthAccountModels(context.Background(), first.ID) - if err != nil { - t.Fatalf("repeat SyncOAuthAccountModels(%d): %v", first.ID, err) - } - if got, want := accountModelNames(models), []string{"gpt-5.6-sol"}; !reflect.DeepEqual(got, want) { - t.Fatalf("repeated account %d models = %v, want %v", first.ID, got, want) - } - if got := requestCount.Load(); got != 2 { - t.Fatalf("catalog requests = %d, want 2 account-scoped requests with same-account reuse", got) - } -} - -func TestOAuthModelCatalogCacheKeySeparatesAccountPlanAndClientVersion(t *testing.T) { - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{CodexResponsesBaseURL: "https://catalog.example.test/codex"}) - account := Account{ID: 7, Metadata: map[string]string{"plan_type": "Plus"}} - selected := SelectedAccount{FingerprintHeaders: map[string]string{"Version": "1.2.3"}} - base := service.oauthModelCatalogCacheKey(account, selected) - - account.ID = 8 - if got := service.oauthModelCatalogCacheKey(account, selected); got == base { - t.Fatal("different accounts shared an OAuth model catalog cache key") - } - account.ID = 7 - account.Metadata["plan_type"] = "Pro" - if got := service.oauthModelCatalogCacheKey(account, selected); got == base { - t.Fatal("different plan types shared an OAuth model catalog cache key") - } - account.Metadata["plan_type"] = "Plus" - selected.FingerprintHeaders["Version"] = "2.0.0" - if got := service.oauthModelCatalogCacheKey(account, selected); got == base { - t.Fatal("different client versions shared an OAuth model catalog cache key") - } -} - -func TestOAuthModelCatalogCacheEvictsSnapshotNearestExpiry(t *testing.T) { - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{}) - base := time.Date(2026, 7, 31, 8, 0, 0, 0, time.UTC) - for i := range maxOAuthModelCatalogCacheEntries + 1 { - key := oauthModelCatalogCacheKey{baseURL: "https://catalog.example.test", clientVersion: strconv.Itoa(i), planType: "plus"} - service.storeOAuthModelCatalogCacheLocked(key, oauthModelCatalogCacheEntry{ - models: []AccountModelInput{{Model: "gpt-5.6-sol", Enabled: true}}, - expiresAt: base.Add(time.Duration(i) * time.Minute), - }) - } - if got := len(service.oauthModelCatalogCache); got != maxOAuthModelCatalogCacheEntries { - t.Fatalf("cache entries = %d, want %d", got, maxOAuthModelCatalogCacheEntries) - } - oldest := oauthModelCatalogCacheKey{baseURL: "https://catalog.example.test", clientVersion: "0", planType: "plus"} - if _, exists := service.oauthModelCatalogCache[oldest]; exists { - t.Fatal("snapshot nearest expiry was not evicted") - } -} - -func TestOAuthModelCatalogCoalescesConcurrentRefreshes(t *testing.T) { - var requestCount atomic.Int32 - started := make(chan struct{}, 8) - release := make(chan struct{}) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - requestCount.Add(1) - started <- struct{}{} - <-release - _, _ = w.Write([]byte(`{"models":[{"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - })) - defer ts.Close() - - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{CodexResponsesBaseURL: ts.URL + "/backend-api/codex"}) - account := Account{ID: 7, Metadata: map[string]string{"plan_type": "plus"}} - selected := SelectedAccount{ - AuthorizationToken: "oauth-access", - ChatGPTAccountID: "chatgpt-account-7", - } - - const callers = 8 - var wg sync.WaitGroup - errs := make(chan error, callers) - for range callers { - wg.Add(1) - go func() { - defer wg.Done() - models, err := service.oauthModelCatalog(context.Background(), account, selected) - if err == nil && !reflect.DeepEqual(models, []AccountModelInput{{Model: "gpt-5.6-sol", Enabled: true}}) { - err = fmt.Errorf("models = %+v", models) - } - errs <- err - }() - } - <-started - close(release) - wg.Wait() - close(errs) - for err := range errs { - if err != nil { - t.Fatal(err) - } - } - if got := requestCount.Load(); got != 1 { - t.Fatalf("catalog requests = %d, want 1 coalesced refresh", got) - } -} - -func TestRefreshOAuthAccountModelsBypassesCompletedCache(t *testing.T) { - var requestCount atomic.Int32 - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - if requestCount.Add(1) == 1 { - _, _ = w.Write([]byte(`{"models":[{"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - return - } - _, _ = w.Write([]byte(`{"models":[{"slug":"gpt-5.6-luna","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - })) - defer ts.Close() - - repo := newMemoryRepo() - account := testAccount(t, 7, true, 1, "oauth-access") - account.AccountType = AccountTypeCodexOAuth - account.Metadata = map[string]string{"chatgpt_account_id": "chatgpt-account-7", "plan_type": "plus"} - repo.accounts = []Account{account} - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - Secret: "encryption-secret", - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - OAuthModelCatalogCacheTTL: time.Hour, + APIKey: "sk-upstream-key", }) - - if _, _, err := service.SyncOAuthAccountModels(context.Background(), account.ID); err != nil { - t.Fatalf("ordinary catalog sync returned error: %v", err) - } - models, _, err := service.RefreshOAuthAccountModels(context.Background(), account.ID) if err != nil { - t.Fatalf("forced catalog refresh returned error: %v", err) + t.Fatalf("CreateAPIUpstreamAccount returned error: %v", err) } - if got, want := accountModelNames(models), []string{"gpt-5.6-luna"}; !reflect.DeepEqual(got, want) { - t.Fatalf("refreshed models = %v, want %v", got, want) + + models, summary, err := service.SyncUpstreamAccountModels(context.Background(), account.ID) + if err != nil { + t.Fatalf("SyncUpstreamAccountModels returned error: %v", err) } - if got := requestCount.Load(); got != 2 { - t.Fatalf("catalog requests = %d, want 2 after forced refresh", got) + if summary.Total != 3 { + t.Fatalf("summary.Total = %d, want 3 (deduped)", summary.Total) } - status, err := service.OAuthModelCatalogStatus(context.Background(), account.ID) - if err != nil { - t.Fatalf("OAuthModelCatalogStatus returned error: %v", err) + if summary.New != 3 { + t.Fatalf("summary.New = %d, want 3", summary.New) } - if status.Source != "upstream" || status.CacheHit == nil || *status.CacheHit { - t.Fatalf("status = %+v, want latest forced upstream fetch", status) + if len(models) != 3 { + t.Fatalf("len(models) = %d, want 3", len(models)) } - if status.LastAttemptAt == nil || status.LastSuccessAt == nil || status.UpstreamFetchMS == nil || status.LocalApplyMS == nil { - t.Fatalf("status = %+v, want attempt, success, and timing observations", status) + names := make([]string, 0, len(models)) + for _, m := range models { + names = append(names, m.Model) } -} - -func TestSyncOAuthAccountModelsRecordsCacheHitFreshness(t *testing.T) { - var requestCount atomic.Int32 - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - requestCount.Add(1) - _, _ = w.Write([]byte(`{"models":[{"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - })) - defer ts.Close() - - repo := newMemoryRepo() - account := testAccount(t, 7, true, 1, "oauth-access") - account.AccountType = AccountTypeCodexOAuth - account.Metadata = map[string]string{"chatgpt_account_id": "chatgpt-account-7", "plan_type": "plus"} - repo.accounts = []Account{account} - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - Secret: "encryption-secret", - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - OAuthModelCatalogCacheTTL: time.Hour, - }) - - if _, _, err := service.SyncOAuthAccountModels(context.Background(), account.ID); err != nil { - t.Fatalf("initial catalog sync returned error: %v", err) + got := strings.Join(names, ",") + if got != "gpt-4,gpt-4-turbo,gpt-5" { + t.Fatalf("models = %s, want gpt-4,gpt-4-turbo,gpt-5", got) } - if _, _, err := service.SyncOAuthAccountModels(context.Background(), account.ID); err != nil { - t.Fatalf("cache-hit catalog sync returned error: %v", err) + // New synced rows should be disabled by default. + for _, m := range models { + if m.Enabled { + t.Fatalf("model %s is enabled, want disabled for new upstream sync rows", m.Model) + } } - status, err := service.OAuthModelCatalogStatus(context.Background(), account.ID) + // Second sync should preserve existing rows. + models2, summary2, err := service.SyncUpstreamAccountModels(context.Background(), account.ID) if err != nil { - t.Fatalf("OAuthModelCatalogStatus returned error: %v", err) - } - if got := requestCount.Load(); got != 1 { - t.Fatalf("catalog requests = %d, want one upstream request", got) + t.Fatalf("second SyncUpstreamAccountModels returned error: %v", err) } - if status.Source != "cache" || status.CacheHit == nil || !*status.CacheHit { - t.Fatalf("status = %+v, want cache hit", status) + if summary2.New != 0 || summary2.Preserved != 3 { + t.Fatalf("second summary: new=%d preserved=%d, want new=0 preserved=3", summary2.New, summary2.Preserved) } - if status.UpstreamFetchMS != nil || status.LocalApplyMS == nil || status.LastAttemptAt == nil || status.LastSuccessAt == nil { - t.Fatalf("status = %+v, want cache and local-apply observations", status) + for _, m := range models2 { + if m.Enabled { + t.Fatalf("model %s is enabled on second sync, want preserved as disabled", m.Model) + } } } -func TestOAuthModelCatalogFailureRetainsPriorModelsAndFreshness(t *testing.T) { - var requestCount atomic.Int32 - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - if requestCount.Add(1) == 1 { - _, _ = w.Write([]byte(`{"models":[{"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - return +func TestSyncUpstreamAccountModelsDoesNotDoubleV1BaseURL(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/models" { + t.Errorf("path = %s, want /v1/models", r.URL.Path) } - w.WriteHeader(http.StatusBadGateway) - _, _ = w.Write([]byte(`upstream unavailable`)) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data":[{"id":"gpt-4"}]}`)) })) defer ts.Close() repo := newMemoryRepo() - account := testAccount(t, 7, true, 1, "oauth-access") - account.AccountType = AccountTypeCodexOAuth - account.Metadata = map[string]string{"chatgpt_account_id": "chatgpt-account-7", "plan_type": "plus"} - repo.accounts = []Account{account} - service := NewService(repo, fakeOAuthClient{}, Config{ - Provider: "openai", - Secret: "encryption-secret", - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - OAuthModelCatalogCacheTTL: time.Hour, + service := NewService(repo, fakeProbeClient{}, Config{ + Provider: "openai", + Secret: "encryption-secret", + AllowHTTPAPIUpstreams: true, }) - if _, _, err := service.SyncOAuthAccountModels(context.Background(), account.ID); err != nil { - t.Fatalf("initial catalog sync returned error: %v", err) - } - if _, _, err := service.RefreshOAuthAccountModels(context.Background(), account.ID); err == nil { - t.Fatal("forced catalog refresh succeeded for a failed upstream") - } - models, err := service.ListAccountModels(context.Background(), account.ID) - if err != nil { - t.Fatalf("ListAccountModels returned error: %v", err) - } - if got, want := accountModelNames(models), []string{"gpt-5.6-sol"}; !reflect.DeepEqual(got, want) { - t.Fatalf("models after failed refresh = %v, want retained %v", got, want) - } - status, err := service.OAuthModelCatalogStatus(context.Background(), account.ID) + account, err := repo.SaveAccount(context.Background(), Account{ + Provider: "openai", + AccountType: AccountTypeAPIUpstream, + Name: "Legacy upstream", + DisplayName: "Legacy upstream", + Enabled: true, + Priority: 100, + LoadFactor: 1, + Status: AccountStatusActive, + Credential: AccountCredential{ + CredentialType: CredentialTypeAPIKey, + EncryptedAPIKey: mustEncrypt(t, "encryption-secret", secret.SecretKindProviderAPIKey, "sk-upstream-key"), + BaseURL: ts.URL + "/v1", + }, + }) if err != nil { - t.Fatalf("OAuthModelCatalogStatus returned error: %v", err) - } - if status.LastSuccessAt == nil || status.LastFailureAt == nil || status.LastError == "" { - t.Fatalf("status = %+v, want retained success and recorded failure", status) - } - if status.Source != "upstream" || status.CacheHit == nil || *status.CacheHit { - t.Fatalf("status = %+v, want failed upstream attempt", status) + t.Fatalf("SaveAccount returned error: %v", err) } - if status.LocalApplyMS != nil { - t.Fatalf("status.localApplyMs = %v, want unknown after fetch failure", *status.LocalApplyMS) + + if _, _, err := service.SyncUpstreamAccountModels(context.Background(), account.ID); err != nil { + t.Fatalf("SyncUpstreamAccountModels returned error: %v", err) } } -func TestOAuthModelCatalogForcedRefreshCoalescesWithBoundedAdmission(t *testing.T) { - var requestCount atomic.Int32 - started := make(chan struct{}, 1) - release := make(chan struct{}) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - request := requestCount.Add(1) - if request > 1 { - started <- struct{}{} - <-release - } - _, _ = w.Write([]byte(`{"models":[{"slug":"gpt-5.6-sol","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - })) - defer ts.Close() +func TestSyncUpstreamAccountModelsRejectsNonAPIUpstream(t *testing.T) { + repo := newMemoryRepo() + account := testAccount(t, 5, true, 1, "oauth-token") + account.AccountType = AccountTypeCodexOAuth + repo.accounts = []Account{account} + service := newConfiguredService(repo, fakeProbeClient{}) - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{ - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - OAuthModelCatalogCacheTTL: time.Hour, - }) - account := Account{ID: 7, AccountType: AccountTypeCodexOAuth, Metadata: map[string]string{"plan_type": "plus"}} - selected := SelectedAccount{AuthorizationToken: "oauth-access", ChatGPTAccountID: "chatgpt-account-7"} - if _, err := service.oauthModelCatalogWithOptions(context.Background(), account, selected, false); err != nil { - t.Fatalf("initial catalog fetch returned error: %v", err) - } - - const callers = 8 - var wg sync.WaitGroup - errs := make(chan error, callers) - for range callers { - wg.Add(1) - go func() { - defer wg.Done() - _, err := service.oauthModelCatalogWithOptions(context.Background(), account, selected, true) - errs <- err - }() - } - <-started - close(release) - wg.Wait() - close(errs) - for err := range errs { - if err != nil { - t.Fatal(err) - } - } - if got := requestCount.Load(); got != 2 { - t.Fatalf("catalog requests = %d, want one initial and one coalesced forced fetch", got) + _, _, err := service.SyncUpstreamAccountModels(context.Background(), 5) + if err != ErrNotConnected { + t.Fatalf("err = %v, want ErrNotConnected for non-api_upstream account", err) } } -func TestOAuthModelCatalogGenerationRejectsLateCacheWrites(t *testing.T) { - var requestCount atomic.Int32 - started := make(chan struct{}, 1) - release := make(chan struct{}) - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - request := requestCount.Add(1) - if request == 1 { - started <- struct{}{} - <-release - _, _ = w.Write([]byte(`{"models":[{"slug":"old-model","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) - return - } - _, _ = w.Write([]byte(`{"models":[{"slug":"new-model","visibility":"list","supported_in_api":true,"upgrade":null}]}`)) +func TestSyncUpstreamAccountModelsNon2xxDoesNotUpdateRows(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusUnauthorized) })) defer ts.Close() - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{ - CodexResponsesBaseURL: ts.URL + "/backend-api/codex", - OAuthModelCatalogCacheTTL: time.Hour, + repo := newMemoryRepo() + service := NewService(repo, fakeProbeClient{}, Config{ + Provider: "openai", + Secret: "encryption-secret", + AllowHTTPAPIUpstreams: true, }) - account := Account{ID: 7, AccountType: AccountTypeCodexOAuth, Metadata: map[string]string{"plan_type": "plus"}} - selected := SelectedAccount{AuthorizationToken: "old-access", ChatGPTAccountID: "old-chatgpt-account"} - oldResult := make(chan error, 1) - go func() { - _, err := service.oauthModelCatalog(context.Background(), account, selected) - oldResult <- err - }() - <-started - service.invalidateOAuthModelCatalog(account.ID) - close(release) - if err := <-oldResult; err != nil { - t.Fatalf("late old catalog request returned error: %v", err) - } - - models, err := service.oauthModelCatalog(context.Background(), account, selected) - if err != nil { - t.Fatalf("new-generation catalog request returned error: %v", err) - } - if got, want := accountModelInputNames(models), []string{"new-model"}; !reflect.DeepEqual(got, want) { - t.Fatalf("new-generation models = %v, want %v", got, want) - } - if got := requestCount.Load(); got != 2 { - t.Fatalf("catalog requests = %d, want late old response excluded from cache", got) - } -} -func TestOAuthModelCatalogAdmissionIsBounded(t *testing.T) { - service := NewService(newMemoryRepo(), fakeOAuthClient{}, Config{CodexResponsesBaseURL: "https://catalog.example.test/codex"}) - service.oauthModelCatalogMu.Lock() - for i := 0; i < maxOAuthModelCatalogFlights; i++ { - service.oauthModelCatalogFlights[oauthModelCatalogCacheKey{accountID: int64(i + 1)}] = &oauthModelCatalogFlight{done: make(chan struct{})} + account, err := service.CreateAPIUpstreamAccount(context.Background(), APIUpstreamInput{ + Name: "Bad Upstream", + BaseURL: ts.URL + "/v1", + APIKey: "sk-bad", + }) + if err != nil { + t.Fatalf("CreateAPIUpstreamAccount returned error: %v", err) } - service.oauthModelCatalogMu.Unlock() - account := Account{ID: maxOAuthModelCatalogFlights + 1, AccountType: AccountTypeCodexOAuth} - selected := SelectedAccount{AuthorizationToken: "oauth-access"} - _, err := service.oauthModelCatalogWithOptions(context.Background(), account, selected, true) - if !errors.Is(err, ErrOAuthModelCatalogBusy) { - t.Fatalf("oauthModelCatalogWithOptions error = %v, want bounded admission error", err) + _, _, err = service.SyncUpstreamAccountModels(context.Background(), account.ID) + if err == nil { + t.Fatal("expected error for non-2xx response") } -} -func accountModelNames(models []AccountModel) []string { - names := make([]string, 0, len(models)) - for _, model := range models { - names = append(names, model.Model) + // Verify no models were synced. + models, err := service.ListAccountModels(context.Background(), account.ID) + if err != nil { + t.Fatalf("ListAccountModels returned error: %v", err) } - sort.Strings(names) - return names -} - -func accountModelInputNames(models []AccountModelInput) []string { - names := make([]string, 0, len(models)) - for _, model := range models { - names = append(names, model.Model) + if len(models) != 0 { + t.Fatalf("len(models) = %d, want 0 (no rows synced after non-2xx)", len(models)) } - sort.Strings(names) - return names } func TestSelectAccountForModelReturnsUnavailableWhenAllEnabledAccountsExcluded(t *testing.T) { @@ -4708,43 +2633,22 @@ func TestSelectAccountForModelReturnsUnavailableWhenAllEnabledAccountsExcluded(t repo.accounts = []Account{ testAccount(t, 1, true, 1, "only-token"), } - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), "", 1); !errors.Is(err, ErrAccountsUnavailable) { t.Fatalf("SelectAccountForModel error = %v, want accounts unavailable", err) } } -func TestSelectAccountForModelFallsBackWhenRefreshFails(t *testing.T) { - repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) - repo.accounts = []Account{ - testExpiredAccount(t, 1, true, 1, "old-token", "bad-refresh", expired), - testAccount(t, 2, true, 2, "fallback-token"), - } - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh failed")}) - - selected, err := service.SelectAccountForModel(context.Background(), "") - if err != nil { - t.Fatalf("SelectAccountForModel returned error: %v", err) - } - if selected.AccountID != 2 || selected.AuthorizationToken != "fallback-token" { - t.Fatalf("selected = %+v", selected) - } - if repo.accounts[0].LastError == "" { - t.Fatal("first account error was not marked") - } -} - func TestSelectAccountForModelReturnsMarkAccountErrorFailure(t *testing.T) { repo := newMemoryRepo() - expired := time.Now().Add(-time.Minute) repo.accounts = []Account{ - testExpiredAccount(t, 1, true, 1, "old-token", "bad-refresh", expired), + testAccount(t, 1, true, 1, "bad-key"), testAccount(t, 2, true, 2, "fallback-token"), } + repo.accounts[0].Credential.EncryptedAPIKey = "invalid-envelope" repo.markAccountErrorErr = errors.New("mark account error failed") - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("refresh failed")}) + service := newConfiguredService(repo, fakeProbeClient{}) if _, err := service.SelectAccountForModel(context.Background(), ""); !errors.Is(err, repo.markAccountErrorErr) { t.Fatalf("SelectAccountForModel error = %v, want mark account error failure", err) @@ -4764,7 +2668,7 @@ func TestRecordAccountUsedOnlyMarksAttemptWithoutClearingHealth(t *testing.T) { account.CircuitOpenUntil = &until account.LastRefreshError = "refresh diagnostic" repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if err := service.RecordAccountUsed(context.Background(), 1); err != nil { t.Fatalf("RecordAccountUsed returned error: %v", err) @@ -4796,7 +2700,7 @@ func TestRecordAccountRecoveredClearsHealthWithRecoveryIntent(t *testing.T) { account.RateLimitedUntil = &until account.LastRefreshError = "preserved refresh diagnostic" repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if err := service.RecordAccountRecovered(context.Background(), 1); err != nil { t.Fatalf("RecordAccountRecovered returned error: %v", err) @@ -4819,7 +2723,7 @@ func TestRecordAccountUsedReturnsMarkAccountUsedFailure(t *testing.T) { testAccount(t, 1, true, 1, "access-token"), } repo.markAccountUsedErr = errors.New("mark account used failed") - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) if err := service.RecordAccountUsed(context.Background(), 1); !errors.Is(err, repo.markAccountUsedErr) { t.Fatalf("RecordAccountUsed error = %v, want mark account used failure", err) @@ -4829,7 +2733,7 @@ func TestRecordAccountUsedReturnsMarkAccountUsedFailure(t *testing.T) { func TestProviderServiceAttachesLifecycleIntentsAndPreservesBatchID(t *testing.T) { repo := newMemoryRepo() repo.accounts = []Account{testAccount(t, 7, true, 3, "access-token")} - service := newConfiguredService(repo, fakeOAuthClient{}) + service := newConfiguredService(repo, fakeProbeClient{}) ctx := systemevent.WithIntent(context.Background(), systemevent.EventIntent{ Metadata: map[string]any{"batch_id": "batch-123"}, }) @@ -4849,125 +2753,12 @@ func TestProviderServiceAttachesLifecycleIntentsAndPreservesBatchID(t *testing.T } } -func TestOAuthRefreshIntentsDistinguishManualAndAutomaticWithoutSecrets(t *testing.T) { - expired := time.Now().Add(-time.Minute) - for _, testCase := range []struct { - name string - refresh func(*Service, Account) error - wantAction systemevent.Action - wantTrigger string - wantSeverity systemevent.Severity - }{ - { - name: "manual", - refresh: func(service *Service, account Account) error { - _, err := service.RefreshAccount(context.Background(), account.ID) - return err - }, - wantAction: systemevent.ActionOAuthRefreshManualFailed, - wantTrigger: string(RefreshTriggerManual), - wantSeverity: systemevent.SeverityWarning, - }, - { - name: "gateway request", - refresh: func(service *Service, account Account) error { - _, err := service.AccessTokenForAccount(context.Background(), account) - return err - }, - wantAction: systemevent.ActionOAuthRefreshAutomaticFailed, - wantTrigger: string(RefreshTriggerGatewayRequest), - wantSeverity: systemevent.SeverityWarning, - }, - { - name: "model test", - refresh: func(service *Service, account Account) error { - _, err := service.selectedAccountForDiagnostic(context.Background(), account) - return err - }, - wantAction: systemevent.ActionOAuthRefreshDiagnosticFailed, - wantTrigger: string(RefreshTriggerModelTest), - wantSeverity: systemevent.SeverityWarning, - }, - } { - t.Run(testCase.name, func(t *testing.T) { - repo := newMemoryRepo() - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expired) - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{refreshErr: errors.New("raw upstream refresh failure")}) - if err := testCase.refresh(service, account); err == nil { - t.Fatal("refresh returned nil error, want upstream failure") - } - if len(repo.intents) == 0 { - t.Fatal("refresh did not attach an event intent") - } - intent := repo.intents[len(repo.intents)-1] - if intent.Action != testCase.wantAction || intent.Metadata["trigger"] != testCase.wantTrigger || intent.Severity != testCase.wantSeverity { - t.Fatalf("intent = %+v, want action %q trigger %q", intent, testCase.wantAction, testCase.wantTrigger) - } - encoded, err := json.Marshal(intent) - if err != nil { - t.Fatal(err) - } - for _, forbidden := range []string{"raw upstream refresh failure", "old-access", "old-refresh"} { - if strings.Contains(string(encoded), forbidden) { - t.Fatalf("intent contains sensitive/raw value %q: %s", forbidden, encoded) - } - } - if err := systemevent.ValidateIntent(intent); err != nil { - t.Fatalf("ValidateIntent returned error: %v", err) - } - }) - } -} - -func TestDiagnosticRefreshSuccessUsesAutomaticRecoveryAction(t *testing.T) { - expired := time.Now().Add(-time.Minute) - repo := newMemoryRepo() - account := testExpiredAccount(t, 7, true, 3, "old-access", "old-refresh", expired) - repo.accounts = []Account{account} - service := newConfiguredService(repo, fakeOAuthClient{refresh: TokenResponse{ - AccessToken: "new-access", RefreshToken: "new-refresh", ExpiresIn: 3600, - }}) - - if _, err := service.selectedAccountForDiagnostic(context.Background(), account); err != nil { - t.Fatalf("selectedAccountForDiagnostic returned error: %v", err) - } - if len(repo.intents) == 0 { - t.Fatal("diagnostic refresh success did not attach an event intent") - } - intent := repo.intents[len(repo.intents)-1] - if intent.Action != systemevent.ActionOAuthRefreshAutomaticSucceeded || intent.Severity != systemevent.SeverityInfo || intent.Metadata["trigger"] != string(RefreshTriggerModelTest) { - t.Fatalf("intent = %+v, want automatic refresh recovery", intent) - } -} - -func TestStartConnectAttachesOAuthIntentWithoutAuthorizationMaterial(t *testing.T) { - repo := newMemoryRepo() - repo.defaultFingerprintProfileID = 9 - service := newConfiguredService(repo, fakeOAuthClient{}) - if _, err := service.StartConnect(context.Background(), ConnectOptions{}); err != nil { - t.Fatalf("StartConnect returned error: %v", err) - } - if len(repo.intents) != 1 || repo.intents[0].Action != systemevent.ActionOAuthConnectStarted { - t.Fatalf("intents = %+v, want oauth connect started", repo.intents) - } - encoded, err := json.Marshal(repo.intents[0]) - if err != nil { - t.Fatal(err) - } - for _, forbidden := range []string{"authorize", "state", "verifier", "challenge"} { - if strings.Contains(strings.ToLower(string(encoded)), forbidden) { - t.Fatalf("connect intent contains forbidden material %q: %s", forbidden, encoded) - } - } -} - func TestAccountMutationsInvalidateGatewayTransportsAfterSuccess(t *testing.T) { repo := &memoryRepo{accounts: []Account{ {ID: 7, Provider: "openai", AccountType: AccountTypeAPIUpstream, Name: "first"}, {ID: 8, Provider: "openai", AccountType: AccountTypeAPIUpstream, Name: "second"}, }} - service := NewService(repo, &fakeOAuthClient{}, Config{Provider: "openai", Secret: "encryption-secret"}) + service := NewService(repo, &fakeProbeClient{}, Config{Provider: "openai", Secret: "encryption-secret"}) invalidator := &recordingTransportInvalidator{} service.SetAccountTransportInvalidator(invalidator) @@ -4984,15 +2775,12 @@ func TestAccountMutationsInvalidateGatewayTransportsAfterSuccess(t *testing.T) { if err := service.DisconnectAccount(context.Background(), 999); !errors.Is(err, ErrNotConnected) { t.Fatalf("failed DisconnectAccount error = %v, want ErrNotConnected", err) } - if err := service.Disconnect(context.Background()); err != nil { - t.Fatalf("Disconnect returned error: %v", err) - } if got, want := invalidator.accountIDs, []int64{7, 8}; !reflect.DeepEqual(got, want) { t.Fatalf("invalidated account IDs = %v, want %v", got, want) } - if invalidator.allCalls != 1 { - t.Fatalf("invalidate all calls = %d, want 1", invalidator.allCalls) + if invalidator.allCalls != 0 { + t.Fatalf("invalidate all calls = %d, want 0", invalidator.allCalls) } } @@ -5016,20 +2804,17 @@ type memoryRepo struct { sessionBindings map[string]SessionBinding routingPools map[int64]RoutingPool routingPoolAccounts map[int64][]RoutingPoolAccount - fingerprintProfiles map[int64]FingerprintProfileData states []OAuthState - saveCount int - nextID int64 - defaultFingerprintProfileID int64 - ensureDefaultFingerprintProfileCalls int - markAccountErrorErr error - markAccountUsedErr error - replaceModelsErr error - listAccountModelsCalls int - listAccountModelsBatchCalls int - lastSavedAccount Account - intents []systemevent.EventIntent + saveCount int + nextID int64 + markAccountErrorErr error + markAccountUsedErr error + replaceModelsErr error + listAccountModelsCalls int + listAccountModelsBatchCalls int + lastSavedAccount Account + intents []systemevent.EventIntent } func (r *memoryRepo) captureIntent(ctx context.Context) { @@ -5044,7 +2829,6 @@ func newMemoryRepo() *memoryRepo { sessionBindings: make(map[string]SessionBinding), routingPools: make(map[int64]RoutingPool), routingPoolAccounts: make(map[int64][]RoutingPoolAccount), - fingerprintProfiles: make(map[int64]FingerprintProfileData), nextID: 1, } } @@ -5313,29 +3097,6 @@ func (r *memoryRepo) DeleteAccounts(ctx context.Context, providerName string) er return nil } -func (r *memoryRepo) FindFingerprintProfileByID(_ context.Context, id int64) (FingerprintProfileData, error) { - profile, ok := r.fingerprintProfiles[id] - if !ok { - return FingerprintProfileData{}, ErrNotConnected - } - return profile, nil -} - -func (r *memoryRepo) EnsureDefaultCodexFingerprintProfile(_ context.Context) (int64, error) { - r.ensureDefaultFingerprintProfileCalls++ - if r.defaultFingerprintProfileID == 0 { - r.defaultFingerprintProfileID = 9001 - } - if r.fingerprintProfiles == nil { - r.fingerprintProfiles = make(map[int64]FingerprintProfileData) - } - r.fingerprintProfiles[r.defaultFingerprintProfileID] = FingerprintProfileData{ - UserAgent: DefaultCodexFingerprintUserAgent, - Headers: DefaultCodexFingerprintHeaders(), - } - return r.defaultFingerprintProfileID, nil -} - func (r *memoryRepo) MarkAccountUsed(ctx context.Context, providerName string, id int64, usedAt time.Time) error { if r.markAccountUsedErr != nil { return r.markAccountUsedErr @@ -5888,41 +3649,12 @@ func (r *memoryRepo) ClaimState(ctx context.Context, providerName, stateHash str return OAuthState{}, ErrInvalidState } -type fakeOAuthClient struct { - exchange TokenResponse - exchanges []TokenResponse - exchangeErr error - refresh TokenResponse - probe probeResult - refreshErr error - probeErr error -} - -func (c fakeOAuthClient) ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) { - if c.exchangeErr != nil { - return TokenResponse{}, c.exchangeErr - } - if len(c.exchanges) > 0 { - return c.exchanges[0], nil - } - return c.exchange, nil -} - -type captureExchangeOAuthClient struct { - exchange TokenResponse - gotCodeVerifier string -} - -func (c *captureExchangeOAuthClient) ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) { - c.gotCodeVerifier = cfg.CodeVerifier - return c.exchange, nil -} - -func (c *captureExchangeOAuthClient) RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) { - return TokenResponse{}, errors.New("unexpected refresh") +type fakeProbeClient struct { + probe probeResult + probeErr error } -type captureProbeOAuthClient struct { +type captureProbeClient struct { probe probeResult probes []probeResult gotConfig Config @@ -5964,15 +3696,7 @@ func (p *captureAccountModelProber) ProbeAccountModel(_ context.Context, _ Confi return p.result } -func (c *captureProbeOAuthClient) ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) { - return TokenResponse{}, errors.New("unexpected exchange") -} - -func (c *captureProbeOAuthClient) RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) { - return TokenResponse{}, errors.New("unexpected refresh") -} - -func (c *captureProbeOAuthClient) ProbeAccountStatus(ctx context.Context, cfg Config, accessToken string) (probeResult, error) { +func (c *captureProbeClient) ProbeAccountStatus(ctx context.Context, cfg Config, accessToken string) (probeResult, error) { c.gotConfig = cfg c.gotAccessToken = accessToken c.gotAccessTokens = append(c.gotAccessTokens, accessToken) @@ -5990,30 +3714,7 @@ func (c *captureProbeOAuthClient) ProbeAccountStatus(ctx context.Context, cfg Co return c.probe, nil } -type captureRefreshOAuthClient struct { - refresh TokenResponse - gotConfig Config - calls int -} - -func (c *captureRefreshOAuthClient) ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) { - return TokenResponse{}, errors.New("unexpected exchange") -} - -func (c *captureRefreshOAuthClient) RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) { - c.calls++ - c.gotConfig = cfg - return c.refresh, nil -} - -func (c fakeOAuthClient) RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) { - if c.refreshErr != nil { - return TokenResponse{}, c.refreshErr - } - return c.refresh, nil -} - -func (c fakeOAuthClient) ProbeAccountStatus(ctx context.Context, cfg Config, accessToken string) (probeResult, error) { +func (c fakeProbeClient) ProbeAccountStatus(ctx context.Context, cfg Config, accessToken string) (probeResult, error) { if c.probeErr != nil { return probeResult{}, c.probeErr } @@ -6023,35 +3724,10 @@ func (c fakeOAuthClient) ProbeAccountStatus(ctx context.Context, cfg Config, acc return c.probe, nil } -type blockingOAuthClient struct { - refresh TokenResponse - entered chan struct{} - release chan struct{} - calls int -} - -func (c *blockingOAuthClient) ExchangeCode(ctx context.Context, cfg Config, code string) (TokenResponse, error) { - return TokenResponse{}, errors.New("unexpected exchange") -} - -func (c *blockingOAuthClient) RefreshToken(ctx context.Context, cfg Config, refreshToken string) (TokenResponse, error) { - c.calls++ - if c.calls == 1 { - c.entered <- struct{}{} - <-c.release - } - return c.refresh, nil -} - -func newConfiguredService(repo Repository, client OAuthClient) *Service { +func newConfiguredService(repo Repository, client any) *Service { return NewService(repo, client, Config{ - Provider: "openai", - ClientID: "client-id", - ClientSecret: "client-secret", - RedirectURL: "http://localhost/oauth/openai/callback", - AuthURL: "https://auth.example.test/authorize", - TokenURL: "https://auth.example.test/token", - Secret: "encryption-secret", + Provider: "openai", + Secret: "encryption-secret", }) } @@ -6081,28 +3757,6 @@ func boolPtr(value bool) *bool { return &value } -func mustQuery(t *testing.T, rawURL, key string) string { - t.Helper() - parsed, err := url.Parse(rawURL) - if err != nil { - t.Fatalf("url.Parse returned error: %v", err) - } - return parsed.Query().Get(key) -} - -func mustUnsignedIDToken(t *testing.T, claims map[string]any) string { - t.Helper() - header, err := json.Marshal(map[string]string{"alg": "none", "typ": "JWT"}) - if err != nil { - t.Fatalf("json.Marshal header returned error: %v", err) - } - payload, err := json.Marshal(claims) - if err != nil { - t.Fatalf("json.Marshal claims returned error: %v", err) - } - return base64.RawURLEncoding.EncodeToString(header) + "." + base64.RawURLEncoding.EncodeToString(payload) + "." -} - func modelNamesAndEnabled(models []AccountModel) []string { values := make([]string, 0, len(models)) for _, model := range models { @@ -6121,27 +3775,7 @@ func exposedModelIDs(models []ExposedModel) []string { func testAccount(t *testing.T, id int64, enabled bool, priority int, accessToken string) Account { t.Helper() - expiresAt := time.Now().Add(time.Hour) - return testExpiredAccount(t, id, enabled, priority, accessToken, "refresh-token", expiresAt) -} - -func testExpiredAccount(t *testing.T, id int64, enabled bool, priority int, accessToken, refreshToken string, expiresAt time.Time) Account { - t.Helper() - now := time.Now() - return Account{ - ID: id, - Provider: "openai", - Subject: "acct_" + string(rune('0'+id)), - DisplayName: "Account", - EncryptedAccessToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthAccessToken, accessToken), - EncryptedRefreshToken: mustEncrypt(t, "encryption-secret", secret.SecretKindOAuthRefreshToken, refreshToken), - AccessTokenExpiresAt: &expiresAt, - Enabled: enabled, - Priority: priority, - LoadFactor: 1, - CreatedAt: now, - UpdatedAt: now, - } + return Account{ID: id, Provider: "openai", AccountType: AccountTypeAPIUpstream, Name: "Account", DisplayName: "Account", Enabled: enabled, Priority: priority, LoadFactor: 1, Credential: AccountCredential{CredentialType: CredentialTypeAPIKey, BaseURL: "https://api.openai.com", EncryptedAPIKey: mustEncrypt(t, "encryption-secret", secret.SecretKindProviderAPIKey, accessToken)}} } func ptrInt64(value int64) *int64 { diff --git a/backend/internal/store/audit_coverage_test.go b/backend/internal/store/audit_coverage_test.go index 2830a358..93af7aa6 100644 --- a/backend/internal/store/audit_coverage_test.go +++ b/backend/internal/store/audit_coverage_test.go @@ -25,10 +25,6 @@ func TestAuditedStoreMutationsConsumeEventIntent(t *testing.T) { "SaveUsagePricing": "insertIntentSystemEvent", "SaveModelSettings": "insertIntentSystemEvent", "SaveGatewaySettings": "insertIntentSystemEvent", }, - "fingerprint.go": { - "CreateFingerprintProfile": "insertIntentSystemEvent", "UpdateFingerprintProfile": "insertIntentSystemEvent", - "DeleteFingerprintProfile": "insertIntentSystemEvent", - }, "error_passthrough.go": { "CreateErrorPassthroughRule": "insertIntentSystemEvent", "UpdateErrorPassthroughRule": "insertIntentSystemEvent", "DeleteErrorPassthroughRule": "insertIntentSystemEvent", diff --git a/backend/internal/store/fingerprint.go b/backend/internal/store/fingerprint.go deleted file mode 100644 index cdc6fc3d..00000000 --- a/backend/internal/store/fingerprint.go +++ /dev/null @@ -1,139 +0,0 @@ -package store - -import ( - "context" - "encoding/json" - "strconv" - - "github.com/KnowSky404/N2API/backend/internal/admin" - "github.com/KnowSky404/N2API/backend/internal/systemevent" - "github.com/jackc/pgx/v5" -) - -func (r *AdminRepository) ListFingerprintProfiles(ctx context.Context) ([]admin.FingerprintProfile, error) { - rows, err := r.pool.Query(ctx, ` - SELECT id, system_key, name, description, user_agent, tls_fingerprint, headers_json, enabled, created_at, updated_at - FROM fingerprint_profiles - ORDER BY CASE WHEN system_key <> '' THEN 0 ELSE 1 END, name ASC - `) - if err != nil { - return nil, err - } - defer rows.Close() - - profiles := []admin.FingerprintProfile{} - for rows.Next() { - var fp admin.FingerprintProfile - var headersRaw []byte - if err := rows.Scan( - &fp.ID, &fp.SystemKey, &fp.Name, &fp.Description, &fp.UserAgent, &fp.TLSFingerprint, - &headersRaw, &fp.Enabled, &fp.CreatedAt, &fp.UpdatedAt, - ); err != nil { - return nil, err - } - if len(headersRaw) > 0 { - _ = json.Unmarshal(headersRaw, &fp.Headers) - } - if fp.Headers == nil { - fp.Headers = map[string]string{} - } - profiles = append(profiles, fp) - } - return profiles, rows.Err() -} - -func (r *AdminRepository) CreateFingerprintProfile(ctx context.Context, input admin.FingerprintProfileInput) (admin.FingerprintProfile, error) { - headersJSON, err := json.Marshal(input.Headers) - if err != nil { - return admin.FingerprintProfile{}, err - } - - tx, err := r.pool.Begin(ctx) - if err != nil { - return admin.FingerprintProfile{}, err - } - defer tx.Rollback(ctx) - var fp admin.FingerprintProfile - err = tx.QueryRow(ctx, ` - INSERT INTO fingerprint_profiles (name, description, user_agent, tls_fingerprint, headers_json, enabled) - VALUES ($1, $2, $3, $4, $5, $6) - RETURNING id, system_key, name, description, user_agent, tls_fingerprint, headers_json, enabled, created_at, updated_at - `, input.Name, input.Description, input.UserAgent, input.TLSFingerprint, headersJSON, input.Enabled).Scan( - &fp.ID, &fp.SystemKey, &fp.Name, &fp.Description, &fp.UserAgent, &fp.TLSFingerprint, - &headersJSON, &fp.Enabled, &fp.CreatedAt, &fp.UpdatedAt, - ) - if err != nil { - return admin.FingerprintProfile{}, err - } - if err := insertIntentSystemEvent(ctx, tx, systemevent.Target{Type: "fingerprint_profile", ID: strconv.FormatInt(fp.ID, 10), Name: fp.Name}, nil); err != nil { - return admin.FingerprintProfile{}, err - } - if err := tx.Commit(ctx); err != nil { - return admin.FingerprintProfile{}, err - } - _ = json.Unmarshal(headersJSON, &fp.Headers) - if fp.Headers == nil { - fp.Headers = map[string]string{} - } - return fp, nil -} - -func (r *AdminRepository) UpdateFingerprintProfile(ctx context.Context, id int64, input admin.FingerprintProfileInput) (admin.FingerprintProfile, error) { - headersJSON, err := json.Marshal(input.Headers) - if err != nil { - return admin.FingerprintProfile{}, err - } - - tx, err := r.pool.Begin(ctx) - if err != nil { - return admin.FingerprintProfile{}, err - } - defer tx.Rollback(ctx) - var fp admin.FingerprintProfile - err = tx.QueryRow(ctx, ` - UPDATE fingerprint_profiles - SET name = $2, description = $3, user_agent = $4, tls_fingerprint = $5, headers_json = $6, enabled = $7, updated_at = now() - WHERE id = $1 AND system_key = '' - RETURNING id, system_key, name, description, user_agent, tls_fingerprint, headers_json, enabled, created_at, updated_at - `, id, input.Name, input.Description, input.UserAgent, input.TLSFingerprint, headersJSON, input.Enabled).Scan( - &fp.ID, &fp.SystemKey, &fp.Name, &fp.Description, &fp.UserAgent, &fp.TLSFingerprint, - &headersJSON, &fp.Enabled, &fp.CreatedAt, &fp.UpdatedAt, - ) - if err != nil { - if err == pgx.ErrNoRows { - return admin.FingerprintProfile{}, admin.ErrNotFound - } - return admin.FingerprintProfile{}, err - } - if err := insertIntentSystemEvent(ctx, tx, systemevent.Target{Type: "fingerprint_profile", ID: strconv.FormatInt(fp.ID, 10), Name: fp.Name}, nil); err != nil { - return admin.FingerprintProfile{}, err - } - if err := tx.Commit(ctx); err != nil { - return admin.FingerprintProfile{}, err - } - _ = json.Unmarshal(headersJSON, &fp.Headers) - if fp.Headers == nil { - fp.Headers = map[string]string{} - } - return fp, nil -} - -func (r *AdminRepository) DeleteFingerprintProfile(ctx context.Context, id int64) error { - tx, err := r.pool.Begin(ctx) - if err != nil { - return err - } - defer tx.Rollback(ctx) - var name string - err = tx.QueryRow(ctx, `DELETE FROM fingerprint_profiles WHERE id = $1 AND system_key = '' RETURNING name`, id).Scan(&name) - if err == pgx.ErrNoRows { - return admin.ErrNotFound - } - if err != nil { - return err - } - if err := insertIntentSystemEvent(ctx, tx, systemevent.Target{Type: "fingerprint_profile", ID: strconv.FormatInt(id, 10), Name: name}, nil); err != nil { - return err - } - return tx.Commit(ctx) -} diff --git a/backend/internal/store/ops.go b/backend/internal/store/ops.go index bf8c53ff..d1a5d358 100644 --- a/backend/internal/store/ops.go +++ b/backend/internal/store/ops.go @@ -335,6 +335,7 @@ func (r *AdminRepository) GetOpsAccountHealth(ctx context.Context, since time.Ti COALESCE(SUM(CASE WHEN last_test_at IS NULL THEN 1 ELSE 0 END), 0), COALESCE(SUM(CASE WHEN last_test_at >= $1 AND last_test_status = 'failed' THEN 1 ELSE 0 END), 0) FROM provider_accounts + WHERE account_type = 'api_upstream' `, since, now) if err := row.Scan( &health.TotalAccounts, diff --git a/backend/internal/store/ops_test.go b/backend/internal/store/ops_test.go new file mode 100644 index 00000000..18031adf --- /dev/null +++ b/backend/internal/store/ops_test.go @@ -0,0 +1,53 @@ +package store + +import ( + "context" + "testing" + "time" + + "github.com/KnowSky404/N2API/backend/internal/provider" +) + +func TestOpsAccountHealthExcludesRetiredOAuthAccounts(t *testing.T) { + repo, cleanup := newProviderRepositoryForTest(t) + defer cleanup() + ctx := context.Background() + now := time.Now().UTC() + until := now.Add(time.Hour) + apiAccount := saveProviderTestAccount(t, repo, provider.Account{ + Provider: "openai", AccountType: provider.AccountTypeAPIUpstream, + Subject: "api-upstream", DisplayName: "API upstream", Enabled: true, + Status: provider.AccountStatusCircuitOpen, CircuitOpenUntil: &until, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, + }) + legacyAccount := saveProviderTestAccount(t, repo, provider.Account{ + Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, + Subject: "legacy-oauth", DisplayName: "Legacy OAuth", Enabled: true, + Status: provider.AccountStatusActive, EncryptedAccessToken: "encrypted-access-token", + EncryptedRefreshToken: "encrypted-refresh-token", + }) + for _, test := range []struct { + accountID int64 + status string + }{{apiAccount.ID, provider.AccountTestStatusFailed}, {legacyAccount.ID, provider.AccountTestStatusPassed}} { + if err := repo.RecordAccountTestResult(ctx, "openai", test.accountID, test.status, "", now); err != nil { + t.Fatal(err) + } + } + adminRepo := NewAdminRepository(repo.pool, "ops-health-test") + health, err := adminRepo.GetOpsAccountHealth(ctx, now.Add(-time.Hour)) + if err != nil { + t.Fatal(err) + } + if health.TotalAccounts != 1 || health.EnabledAccounts != 1 || health.Schedulable != 0 || health.CircuitOpen != 1 || + health.TestedAccounts != 1 || health.TestPassed != 0 || health.TestFailed != 1 || health.RecentTestFailure != 1 { + t.Fatalf("API-only account health = %+v, want one unavailable API upstream", health) + } + history, err := adminRepo.ListOpsAccountTests(ctx, now.Add(-time.Hour), 10) + if err != nil { + t.Fatal(err) + } + if len(history) != 2 { + t.Fatalf("historical account tests = %d, want both API and retired OAuth results", len(history)) + } +} diff --git a/backend/internal/store/provider.go b/backend/internal/store/provider.go index dce9cbd7..ad6091ff 100644 --- a/backend/internal/store/provider.go +++ b/backend/internal/store/provider.go @@ -1784,6 +1784,7 @@ func (r *ProviderRepository) ListExposedModelsForRoutingPools(ctx context.Contex AND rpa.pool_id = ANY($2) AND m.enabled = true AND a.enabled = true + AND a.account_type = 'api_upstream' AND ( a.status IN ('', 'active') OR (a.status = 'rate_limited' AND a.rate_limited_until IS NOT NULL AND a.rate_limited_until <= now()) @@ -1964,59 +1965,3 @@ func metadataJSON(metadata map[string]string) []byte { } return payload } - -func (r *ProviderRepository) FindFingerprintProfileByID(ctx context.Context, id int64) (provider.FingerprintProfileData, error) { - var data provider.FingerprintProfileData - var headersRaw []byte - err := r.pool.QueryRow(ctx, ` - SELECT user_agent, tls_fingerprint, headers_json - FROM fingerprint_profiles - WHERE id = $1 AND enabled = true - `, id).Scan(&data.UserAgent, &data.TLSFingerprint, &headersRaw) - if err != nil { - return provider.FingerprintProfileData{}, err - } - if len(headersRaw) > 0 { - _ = json.Unmarshal(headersRaw, &data.Headers) - } - if data.Headers == nil { - data.Headers = map[string]string{} - } - return data, nil -} - -func (r *ProviderRepository) EnsureDefaultCodexFingerprintProfile(ctx context.Context) (int64, error) { - headersJSON, err := json.Marshal(provider.DefaultCodexFingerprintHeaders()) - if err != nil { - return 0, err - } - - var id int64 - err = r.pool.QueryRow(ctx, ` - INSERT INTO fingerprint_profiles ( - system_key, name, description, user_agent, tls_fingerprint, headers_json, enabled - ) - VALUES ($1, $2, $3, $4, $5, $6, true) - ON CONFLICT (system_key) WHERE system_key <> '' - DO UPDATE SET - name = EXCLUDED.name, - description = EXCLUDED.description, - user_agent = EXCLUDED.user_agent, - tls_fingerprint = EXCLUDED.tls_fingerprint, - headers_json = EXCLUDED.headers_json, - enabled = true, - updated_at = now() - RETURNING id - `, - provider.DefaultCodexFingerprintSystemKey, - provider.DefaultCodexFingerprintName, - provider.DefaultCodexFingerprintDescription, - provider.DefaultCodexFingerprintUserAgent, - provider.DefaultCodexFingerprintTLS, - headersJSON, - ).Scan(&id) - if err != nil { - return 0, err - } - return id, nil -} diff --git a/backend/internal/store/provider_test.go b/backend/internal/store/provider_test.go index 97440fad..f3f082e2 100644 --- a/backend/internal/store/provider_test.go +++ b/backend/internal/store/provider_test.go @@ -402,58 +402,6 @@ func TestHasEnabledAccountsChecksEnabledProviderRows(t *testing.T) { } } -func TestFindFingerprintProfileByIDOnlyReturnsEnabledProfiles(t *testing.T) { - source, err := os.ReadFile("provider.go") - if err != nil { - t.Fatalf("ReadFile provider.go returned error: %v", err) - } - sql := strings.ToUpper(string(source)) - if !strings.Contains(sql, "FUNC (R *PROVIDERREPOSITORY) FINDFINGERPRINTPROFILEBYID") || - !strings.Contains(sql, "FROM FINGERPRINT_PROFILES") || - !strings.Contains(sql, "WHERE ID = $1 AND ENABLED = TRUE") { - t.Fatal("FindFingerprintProfileByID must only return enabled fingerprint profiles") - } -} - -func TestEnsureDefaultCodexFingerprintProfileUsesSystemKey(t *testing.T) { - source, err := os.ReadFile("provider.go") - if err != nil { - t.Fatalf("ReadFile provider.go returned error: %v", err) - } - sql := string(source) - for _, want := range []string{ - "func (r *ProviderRepository) EnsureDefaultCodexFingerprintProfile", - "provider.DefaultCodexFingerprintSystemKey", - "ON CONFLICT (system_key) WHERE system_key <> ''", - "name = EXCLUDED.name", - "user_agent = EXCLUDED.user_agent", - "headers_json = EXCLUDED.headers_json", - "enabled = true", - "RETURNING id", - } { - if !strings.Contains(sql, want) { - t.Fatalf("provider store source missing %q", want) - } - } -} - -func TestSystemFingerprintProfilesAreReadOnly(t *testing.T) { - source, err := os.ReadFile("fingerprint.go") - if err != nil { - t.Fatalf("ReadFile fingerprint.go returned error: %v", err) - } - sql := string(source) - for _, want := range []string{ - "WHERE id = $1 AND system_key = ''", - "DELETE FROM fingerprint_profiles WHERE id = $1 AND system_key = ''", - "ORDER BY CASE WHEN system_key <> '' THEN 0 ELSE 1 END", - } { - if !strings.Contains(sql, want) { - t.Fatalf("fingerprint store source missing %q", want) - } - } -} - func TestProviderRepositorySavesAPIUpstreamAccount(t *testing.T) { repo, cleanup := newProviderRepositoryForTest(t) defer cleanup() @@ -694,7 +642,8 @@ func TestProviderRepositoryListExposedModelsForRoutingPools(t *testing.T) { ctx := context.Background() global := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", - AccountType: provider.AccountTypeCodexOAuth, + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "global-account", DisplayName: "Global Account", EncryptedAccessToken: "global-token", @@ -705,7 +654,8 @@ func TestProviderRepositoryListExposedModelsForRoutingPools(t *testing.T) { }) pooled := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", - AccountType: provider.AccountTypeCodexOAuth, + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "pooled-account", DisplayName: "Pooled Account", EncryptedAccessToken: "pool-token", @@ -722,6 +672,18 @@ func TestProviderRepositoryListExposedModelsForRoutingPools(t *testing.T) { } poolID := insertProviderRoutingPool(t, repo.pool, "primary", pooled.ID) + legacy := saveProviderTestAccount(t, repo, provider.Account{ + Provider: "openai", AccountType: provider.AccountTypeCodexOAuth, + Subject: "retired-oauth", DisplayName: "Retired OAuth", Enabled: true, Status: provider.AccountStatusActive, + EncryptedAccessToken: "legacy-token", EncryptedRefreshToken: "legacy-refresh", + }) + if _, err := repo.ReplaceAccountModels(ctx, "openai", legacy.ID, []provider.AccountModelInput{{Model: "oauth-only", Enabled: true}}); err != nil { + t.Fatal(err) + } + if _, err := repo.pool.Exec(ctx, `INSERT INTO routing_pool_accounts (pool_id, account_id) VALUES ($1, $2)`, poolID, legacy.ID); err != nil { + t.Fatal(err) + } + models, err := repo.ListExposedModelsForRoutingPools(ctx, "openai", []int64{poolID}) if err != nil { t.Fatalf("ListExposedModelsForRoutingPools returned error: %v", err) @@ -1063,45 +1025,6 @@ func TestUpdateAccountClearStatusClearsLocalFailureStateColumns(t *testing.T) { } } -func TestUpdateAccountCanClearFingerprintProfileColumn(t *testing.T) { - source, err := os.ReadFile("provider.go") - if err != nil { - t.Fatalf("ReadFile provider.go returned error: %v", err) - } - sql := strings.ToUpper(string(source)) - if strings.Contains(sql, "FINGERPRINT_PROFILE_ID = COALESCE($10, FINGERPRINT_PROFILE_ID)") { - t.Fatal("UpdateAccount must allow clearing fingerprint_profile_id instead of preserving it with COALESCE") - } - for _, want := range []string{ - "FINGERPRINT_PROFILE_ID = CASE", - "WHEN $10 THEN $11", - "ELSE FINGERPRINT_PROFILE_ID", - } { - if !strings.Contains(sql, want) { - t.Fatalf("UpdateAccount must distinguish unset and null fingerprint updates, missing %q", want) - } - } -} - -func TestProviderAccountSaveAndScanIncludeFingerprintProfileID(t *testing.T) { - source, err := os.ReadFile("provider.go") - if err != nil { - t.Fatalf("ReadFile provider.go returned error: %v", err) - } - sql := string(source) - for _, want := range []string{ - "a.fingerprint_profile_id", - "&account.FingerprintProfileID", - "fingerprint_profile_id = $", - "fingerprint_profile_id, updated_at", - "account.FingerprintProfileID", - } { - if !strings.Contains(sql, want) { - t.Fatalf("provider store source missing %q", want) - } - } -} - func TestUpdateAccountCanSetLocalAccountNameColumn(t *testing.T) { source, err := os.ReadFile("provider.go") if err != nil { @@ -1822,6 +1745,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. later := now.Add(time.Hour) account := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-enabled", DisplayName: "Exposed", EncryptedAccessToken: "access", @@ -1832,6 +1757,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) disabledAccount := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-disabled-account", DisplayName: "Disabled", EncryptedAccessToken: "access", @@ -1842,6 +1769,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) expiredAccount := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-expired", DisplayName: "Expired", EncryptedAccessToken: "access", @@ -1853,6 +1782,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) unknownStatus := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-unknown", DisplayName: "Unknown", EncryptedAccessToken: "access", @@ -1863,6 +1794,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) rateLimitedNull := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-rate-null", DisplayName: "Rate Null", EncryptedAccessToken: "access", @@ -1873,6 +1806,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) rateLimitedFuture := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-rate-future", DisplayName: "Rate Future", EncryptedAccessToken: "access", @@ -1884,6 +1819,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) rateLimitedPast := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-rate-past", DisplayName: "Rate Past", EncryptedAccessToken: "access", @@ -1895,6 +1832,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) circuitOpenNull := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-circuit-null", DisplayName: "Circuit Null", EncryptedAccessToken: "access", @@ -1905,6 +1844,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) circuitOpenFuture := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-circuit-future", DisplayName: "Circuit Future", EncryptedAccessToken: "access", @@ -1916,6 +1857,8 @@ func TestListExposedModelsForRoutingPoolFiltersUnschedulableAccounts(t *testing. }) circuitOpenPast := saveProviderTestAccount(t, repo, provider.Account{ Provider: "openai", + AccountType: provider.AccountTypeAPIUpstream, + Credential: provider.AccountCredential{CredentialType: provider.CredentialTypeAPIKey, EncryptedAPIKey: "encrypted-api-key"}, Subject: "exposed-circuit-past", DisplayName: "Circuit Past", EncryptedAccessToken: "access", diff --git a/deploy/compose.release.yaml b/deploy/compose.release.yaml index 08a5750d..80d7294b 100644 --- a/deploy/compose.release.yaml +++ b/deploy/compose.release.yaml @@ -35,7 +35,6 @@ services: N2API_ENCRYPTION_SECRET: ${N2API_ENCRYPTION_SECRET:-} N2API_ENCRYPTION_KEY_ID: ${N2API_ENCRYPTION_KEY_ID:-default} N2API_ENCRYPTION_PREVIOUS_KEYS: ${N2API_ENCRYPTION_PREVIOUS_KEYS:-} - OPENAI_OAUTH_CLIENT_SECRET: ${OPENAI_OAUTH_CLIENT_SECRET:-} ports: - name: http target: 3000 diff --git a/docs/manual.md b/docs/manual.md index 7aecfb6b..789c5254 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -67,8 +67,7 @@ and `database-plaintext` by default and uses separate local-only credentials when no `.env` exists. Set `N2API_ACCEPT_RISKS` explicitly in `.env` to narrow or replace those development defaults. -`OPENAI_API_BASE_URL` and the OpenAI OAuth endpoints are also parsed at -startup. OAuth authorization and token endpoints must use HTTPS. The API base +`OPENAI_API_BASE_URL` is parsed at startup. The API base URL may use HTTP only when `N2API_ALLOW_HTTP_API_UPSTREAMS=true`, matching the explicit opt-in used for API-key upstream accounts. @@ -230,10 +229,10 @@ headers. All responses include MIME sniffing, referrer, permissions, framing, and Content Security Policy headers. HSTS is emitted only when the trusted external scheme -is HTTPS. Admin API and OAuth callback responses use `Cache-Control: no-store`. +is HTTPS. Admin API responses use `Cache-Control: no-store`. The CSP permits only same-origin resources and `data:` images. SvelteKit emits build-specific SHA-256 hashes for the static bootstrap script and generated -style elements. Inline style attributes and the manual OAuth callback style +style elements. Inline style attributes remain allowed because the current UI requires them; remote scripts are not allowed. @@ -812,13 +811,7 @@ creates the rule disabled, and is idempotent by the persisted template key: retries return the existing rule without changing its delivery action, enabled state, or later owner edits. Deleting a template-derived rule is allowed; it is created again only when an owner explicitly installs the template again. The -catalog includes `oauth-refresh-repeated-v1`, which matches three -automatic OAuth refresh failures for the same event target within 15 minutes, -uses a one-hour cooldown, and can notify on the next automatic refresh success. -Model-test refresh failures use the separate -`oauth.refresh.diagnostic.failed` action and therefore do not contribute to -this operational threshold. It also includes -`request-log-retention-failed-v1`, which matches either complete or partial +catalog includes `request-log-retention-failed-v1`, which matches either complete or partial automatic Request Log retention failures, uses a 24-hour cooldown, and can notify when the next scheduled retention cycle succeeds. Shutdown cancellation updates task status but does not emit the failure signal. Disabling the runner, @@ -835,7 +828,7 @@ The `provider-account-expired-v1` template matches the first `provider_account.expired` Runtime warning for each Provider account, uses a 24-hour cooldown, and can notify when that same account emits `provider_account.recovered`. It starts disabled and is deduplicated by account -target. OAuth reauthorization and API-upstream configuration edits preserve the +target. API-upstream configuration edits preserve the current health state, so they do not recover a firing rule. Recovery follows a confirmed 2xx gateway/account-probe response or an explicit **Reset local status** operator override. @@ -1308,7 +1301,7 @@ not restart or mark the entire gateway unavailable. ## Downstream Codex CLI -After connecting and testing a Codex OAuth provider account, enable the models +After adding and testing an API-key upstream account, enable the models that account can serve and create a client key on the API Keys page. Save the following as `~/.codex/n2api.config.toml`: @@ -1345,16 +1338,18 @@ routing pool returns no models. ## Provider Accounts -Start the stack, log in as admin, and use Provider accounts to connect one or more Codex OAuth accounts or API-key upstream accounts. Provider accounts are gateway exits. N2API supports Codex OAuth accounts and API-key upstream accounts. Both account types share enabled state, priority, health status, and per-account model lists. +Use **Provider accounts** to add API-key upstreams. Set the upstream base URL, +API key, models, and optional proxy, then test the account and add it to a +routing pool. OAuth connection, reauthorization, token refresh, and fingerprint +profiles are no longer supported. Historical OAuth rows and credentials remain +in PostgreSQL for backup compatibility and do not participate in routing. +Replace their routing-pool memberships with API-key upstreams before use. -The default OAuth flow uses the Codex-compatible OpenAI OAuth client with PKCE, so the OAuth client id, client secret, auth URL, and token URL can usually stay blank in `.env`. -Keep the default `OPENAI_OAUTH_REDIRECT_URL=http://localhost:1455/auth/callback` unless you are using your own registered OpenAI OAuth client. The built-in Codex-compatible client expects that local callback URI; after OpenAI redirects there, copy the browser URL back into N2API's callback field. - -- Use the account row to set a display name, priority, and **Scheduling preference**. OAuth account creation also lets you choose whether the account should be enabled after login. +- Use the account row to set a display name, priority, and **Scheduling preference**. - Select rows on the Provider accounts page to bulk enable or disable provider accounts. Use **Enable selected** or **Disable selected** to change scheduling eligibility for the selected exits, and **Clear selection** to discard the selection without changing accounts. - Set **Bulk priority**, **Bulk scheduling preference**, or **Bulk max concurrency**, then use **Apply scheduling** to update selected provider accounts together; bulk priority, bulk scheduling preference, and bulk max concurrency fields use the same validation as each account row. - Configure supported models on each connected account. These per-account model rows describe account capability for gateway routing. -- Use the Providers table **Test models** action to diagnose one or more configured models against one exact account. The modal starts with no selection; row checkboxes support one or multiple models, and the tri-state header checkbox selects only the current filtered result set. Tests use that account's stored OAuth token or API-upstream key, never fall back to another account, and persist each model's latest status and latency. They do not enable or disable models or change account scheduling health. +- Use the Providers table **Test models** action to diagnose one or more configured models against one exact account. The modal starts with no selection; row checkboxes support one or multiple models, and the tri-state header checkbox selects only the current filtered result set. Tests use that account's stored API-upstream key, never fall back to another account, and persist each model's latest status and latency. They do not enable or disable models or change account scheduling health. - Selected provider accounts can also receive the same model capability list. Enter one model per line in **Bulk models**, then use **Apply models** to replace the selected accounts' manual model lists together; this controls which models the scheduler can route to those accounts. - Use the Provider accounts page to add or remove selected provider accounts from a routing pool without opening the pool editor. Choose **Bulk routing pool**, set **Pool priority** for new pool members, then use **Apply pool** to add the selected accounts to that pool or **Remove pool** to remove them while leaving the pool's other members unchanged. - Use API Keys to control the routing-pool scope and `all` or `selected` model policy. Account model configuration remains the source of truth for capability. The gateway `defaultModel` is used only when a POST request omits `model`, and the injected model must still be routable in that key's bound pool or explicit fallback chain. @@ -1365,15 +1360,11 @@ Keep the default `OPENAI_OAUTH_REDIRECT_URL=http://localhost:1455/auth/callback` - New API keys are stored with an encrypted reusable secret. The Prefix column on the API Keys page can copy the full API key again after creation for active or disabled keys; older keys created before encrypted secret storage may need to be rotated if their full value was not saved. - API keys have three visible states: active, disabled, and deleted. Active and disabled keys can be toggled directly from the API Keys table status column, and disabled keys cannot authenticate gateway requests. Deleting an active or disabled key performs an irreversible logical delete immediately, keeps the row visible during its 7 day retention window, and exposes the scheduled physical deletion time in the deleted status tooltip. Deleted keys can be physically deleted immediately with a second confirmed Delete action. Keys past the retention window are physically removed by startup and hourly cleanup, with API key listing cleanup as a fallback. - API key budgets are personal operational safeguards, not billing balances. Request budgets are strict rolling 24h and 30d limits backed by atomic PostgreSQL admission. Token and estimated cost limits are explicitly observed-usage budgets: admission rejects once settled observed usage has reached the limit, but one request or concurrent requests can cross it because no trustworthy universal pre-request upper bound exists. `0` disables a budget field. Existing budgeted keys fail closed with `budget_initializing` while the bounded upgrade backfill initializes their ledger from Request Logs. When a ready key is over budget, clients receive OpenAI-compatible `rate_limit_exceeded` responses while Request Logs store the precise local reason as `api_key_request_budget_exceeded`, `api_key_token_budget_exceeded`, or `api_key_cost_budget_exceeded`. -- Use **Refresh** to force a token refresh for one account. Saving fresh OAuth credentials preserves the account's current health state; only the follow-up account probe or a later gateway request with a 2xx upstream response confirms recovery and clears stale transient health state. -- Use **Reauthorize** on an existing row to bind a fresh OAuth login back to that account instead of creating a second row. Reauthorization replaces the saved OAuth credentials but preserves current account health until a later 2xx account probe or an explicit local-status reset. - API upstream credentials can be updated from the account row. Rotating the encrypted API key, base URL, or per-account outbound proxy URL preserves current account health; run **Test account** to confirm the new settings before an expired or circuit-open account becomes schedulable again. Proxy URLs are stored encrypted because they may include credentials, and the admin UI only shows a redacted proxy summary. -- New OAuth and API upstream account forms can bind a **Fingerprint profile** at creation time. OAuth profile selections are stored in the pending OAuth state and applied after callback completion; API upstream selections are written directly to the provider account. -- Use **Test account** before sending client traffic through an account. The action probes one provider account with its current OAuth token or API upstream key. Only a 2xx response confirms recovery and clears local failure status; network errors and every non-2xx response record a failed test without a recovery transition. The account row keeps the last test status, last test time, and last test error so manual checks remain visible after refresh. Each probe also writes provider account test history; use the Providers page **History action** to expand **Recent test history**, or fetch the same data from `GET /api/admin/provider-accounts/{id}/test-results`. +- Use **Test account** before sending client traffic through an account. The action probes one provider account with its current API upstream key. Only a 2xx response confirms recovery and clears local failure status; network errors and every non-2xx response record a failed test without a recovery transition. The account row keeps the last test status, last test time, and last test error so manual checks remain visible after refresh. Each probe also writes provider account test history; use the Providers page **History action** to expand **Recent test history**, or fetch the same data from `GET /api/admin/provider-accounts/{id}/test-results`. - Gateway account selection records only an attempt timestamp. It does not clear account health. A gateway request confirms account recovery only when its final upstream response is 2xx; redirects, client/server errors, authorization failures, network failures, and configured error passthrough do not recover the account. When fallback succeeds, only the account that produced the final 2xx response recovers. - The Ops Monitor page shows **Recent account tests** for the selected monitoring window so manual and automatic probe failures are visible without opening each provider account row. Fetch the same aggregate view from `GET /api/admin/ops/account-tests`. - Use **Test selected** to probe selected provider accounts without probing the whole account pool. It updates the same last-test fields, health fields, and test history as **Test account**. -- Use **Refresh selected** to force credential refresh for selected provider accounts together after rotating, restoring, or reauthorizing a subset of OAuth-backed exits. - Use **Disconnect account** when an exit should be removed from the gateway. It deletes the provider account, stops scheduling it for new traffic, and removes its stored credentials and account-scoped model configuration through the database cascade. - Use **Disconnect selected** when several exits should be removed together. It deletes the selected provider accounts, stops scheduling them for new traffic, and removes their stored credentials and account-scoped model configuration through the same database cascade. - Provider account auto tests are disabled by default. `N2API_PROVIDER_ACCOUNT_AUTO_TEST_ENABLED` and `N2API_PROVIDER_ACCOUNT_AUTO_TEST_INTERVAL_SECONDS` are startup defaults for Gateway Settings; after sign-in, use the Gateway Settings form to save the runtime auto-test setting. Enable it to run **Test all accounts** automatically in the backend, and use an interval of `300` seconds or higher for routine checks. Automatic tests update the same last test status, last test time, last test error, test history, and local account health fields shown in Provider accounts and Routing diagnostics. @@ -1547,8 +1538,6 @@ other than `n2api` before creating a client. | Timeout or disconnect before response headers | Go E2E verifies cancellation and pre-stream fallback without leaking response bodies | | Disconnect or clean EOF after the first SSE event | Go E2E verifies N2API never retries after streaming has begun and never fabricates a completion event | | Sticky routing, explicit fallback, and local key limits | Go E2E verifies persisted account selection, fallback counters, and OpenAI-compatible 429 responses | -| Concurrent OAuth refresh single-flight | Deterministic provider component tests verify one refresh and reuse of the rotated token | -| Real Codex OAuth and Codex CLI | Manual protected acceptance only; it is not a required PR check and must not upload request or response bodies | Run the SDK contracts locally through the managed lifecycle wrapper: diff --git a/docs/specs/2026-11-09-api-key-only-upstreams.md b/docs/specs/2026-11-09-api-key-only-upstreams.md new file mode 100644 index 00000000..718887d2 --- /dev/null +++ b/docs/specs/2026-11-09-api-key-only-upstreams.md @@ -0,0 +1,32 @@ +# API Key Only Upstreams + +## Scope and acceptance + +N2API accepts API-key upstream accounts only. Remove OAuth authorization, +callback, token refresh, and OAuth-specific gateway request/response conversion. +Remove fingerprint management, profile binding, synthesized Codex identity +headers, and TLS ClientHello emulation. Downstream Codex clients continue to +use the OpenAI-compatible API with a client key. + +Existing API keys, upstream API credentials, models, routing pools, proxy +configuration, response affinity, request logs, and streaming must continue to +work. Historical OAuth accounts must not be selected, refreshed, or exposed as +active upstreams. Existing database rows and migration history are retained for +backup compatibility; this change performs no credential or database deletion. + +## Implementation plan + +1. Remove frontend OAuth and fingerprint flows and their management endpoints. +2. Restrict account selection and diagnostics to API-key upstreams; remove + OAuth startup configuration and refresh behavior. +3. Use standard Go HTTP/TLS transports, retaining proxy pooling and deadlines. +4. Update API and gateway regression tests and current operator documentation. +5. Run unit checks, browser verification, and managed Compose rebuild/probes/ + teardown, then local review and the repository PR/CI workflow. + +## Upgrade behavior + +Installations using OAuth accounts must add an API-key upstream and update +routing pool membership. Old OAuth credentials and fingerprint profiles remain +in PostgreSQL and backups, but are inactive. No production migration or +real-provider acceptance is implied by local tests. diff --git a/frontend/src/lib/admin-state.svelte.js b/frontend/src/lib/admin-state.svelte.js index 28ad948d..9743c296 100644 --- a/frontend/src/lib/admin-state.svelte.js +++ b/frontend/src/lib/admin-state.svelte.js @@ -79,16 +79,6 @@ export { * @property {string} routingPoolName */ -/** - * @typedef {object} ProviderStatus - * @property {string} provider - * @property {boolean} configured - * @property {boolean} connected - * @property {string} displayName - * @property {string | null} accessTokenExpiresAt - * @property {string | null} lastRefreshAt - */ - /** * @typedef {object} AdminSession * @property {number} id @@ -129,7 +119,6 @@ export { * @property {string | null} lastTestAt * @property {string} lastTestStatus * @property {string} lastTestError - * @property {number | null} fingerprintProfileId */ /** @@ -394,18 +383,6 @@ export { * @property {number} skippedManual */ -/** - * @typedef {object} OAuthModelCatalogStatus - * @property {string | undefined} lastAttemptAt - * @property {string | undefined} lastSuccessAt - * @property {string | undefined} lastFailureAt - * @property {string | undefined} lastError - * @property {string | undefined} source - * @property {boolean | undefined} cacheHit - * @property {number | undefined} upstreamFetchMs - * @property {number | undefined} localApplyMs - */ - /** * @typedef {object} AccountModelsState * @property {boolean} loading @@ -415,13 +392,6 @@ export { * @property {string} syncError * @property {string} syncMessage * @property {AccountModelSyncSummary | null} syncSummary - * @property {boolean} refreshingCatalog - * @property {string} catalogRefreshError - * @property {string} catalogRefreshMessage - * @property {AccountModelSyncSummary | null} catalogRefreshSummary - * @property {OAuthModelCatalogStatus | null} catalogStatus - * @property {number} catalogRefreshSeq - * @property {AbortController | null} catalogAbortController * @property {boolean} saved * @property {string} text * @property {AccountModel[]} items @@ -615,13 +585,6 @@ let healthRequestVersion = 0; export const loginForm = $state({ username: '', password: '', submitting: false, error: '' }); /** @type {{ loading: boolean, error: string, items: AdminSession[], revokingId: number | null, revokingOthers: boolean }} */ export const adminSessions = $state({ loading: false, error: '', items: [], revokingId: null, revokingOthers: false }); -/** @type {{ loading: boolean, connecting: boolean, error: string, data: ProviderStatus | null }} */ -export const provider = $state({ - loading: false, - connecting: false, - error: '', - data: null -}); /** @type {{ loading: boolean, saving: boolean, error: string, items: ProviderAccount[] }} */ export const providerAccounts = $state({ loading: false, saving: false, error: '', items: [] }); /** @type {{ loading: boolean, loadingMore: boolean, saving: boolean, error: string, items: RoutingPool[], nextCursor: string, hasMore: boolean, appliedQuery: string, newPoolName: string, newPoolDescription: string, newPoolFallbackPoolId: string }} */ @@ -638,11 +601,9 @@ export const routingPools = $state({ newPoolDescription: '', newPoolFallbackPoolId: '0' }); -export const providerConnectForm = $state({ name: '', priority: 100, enabled: true, fingerprintProfileId: '0' }); export const providerAccountPauseForm = $state({ durationSeconds: 300 }); export const providerAccountBulkSchedulingForm = $state({ priority: '', loadFactor: '', maxConcurrentRequests: '' }); export const providerAccountBulkModelsForm = $state({ text: '' }); -export const providerOAuth = $state({ authorizationUrl: '', callbackUrl: '', completing: false, copied: false }); export const apiUpstreamForm = $state({ name: '', baseUrl: '', @@ -651,7 +612,6 @@ export const apiUpstreamForm = $state({ priority: 100, loadFactor: 1, enabled: true, - fingerprintProfileId: '0', modelsText: '', submitting: false, error: '' @@ -925,11 +885,7 @@ export function clearProviderAccountBulkModelsForm() { } export function getProviderStateLabel() { - return provider.data?.configured - ? provider.data.connected - ? 'connected' - : 'not connected' - : 'not configured'; + return providerAccounts.items.some((account) => account.enabled) ? 'connected' : 'not connected'; } export function getStatusItems() { @@ -938,7 +894,7 @@ export function getStatusItems() { { label: 'Database', value: health.database }, { label: 'Provider', - value: session.authenticated ? (provider.data ? getProviderStateLabel() : 'checking') : 'login required' + value: session.authenticated ? (providerAccounts.loading ? 'checking' : getProviderStateLabel()) : 'login required' } ]; } @@ -1309,20 +1265,6 @@ export async function revealAPIKeySecret(id, currentPassword) { } } -export async function copyAuthorizationURL() { - if (!providerOAuth.authorizationUrl) return; - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return; - - const copied = await copyText(providerOAuth.authorizationUrl); - if (!isCurrentAuthenticated(version)) return; - if (copied) { - providerOAuth.copied = true; - } else { - provider.error = 'Copy failed'; - } -} - function clearAPIKeys() { replaceState(apiKeys, { loading: false, @@ -1484,7 +1426,6 @@ function clearAuthenticatedAdminState(error = '', incrementVersion = true) { accountTests: null, costBreakdown: null }); - replaceState(fingerprintProfiles, { loading: false, error: '', items: [], saving: false }); replaceState(errorPassthroughRules, { loading: false, error: '', items: [], saving: false }); loginForm.password = ''; } @@ -1532,12 +1473,6 @@ function clearModelSettings() { } function clearProvider() { - replaceState(provider, { - loading: false, - connecting: false, - error: '', - data: null - }); replaceState(providerAccounts, { loading: false, saving: false, error: '', items: [] }); replaceState(routingPools, { loading: false, @@ -1552,17 +1487,12 @@ function clearProvider() { newPoolDescription: '', newPoolFallbackPoolId: '0' }); - for (const state of Object.values(accountModels)) { - state.catalogAbortController?.abort(); - } replaceState(accountModels, {}); replaceState(accountTestResults, {}); replaceState(selectedProviderAccountIds, {}); - replaceState(providerConnectForm, { name: '', priority: 100, enabled: true, fingerprintProfileId: '0' }); replaceState(providerAccountPauseForm, { durationSeconds: 300 }); replaceState(providerAccountBulkSchedulingForm, { priority: '', loadFactor: '', maxConcurrentRequests: '' }); replaceState(providerAccountBulkModelsForm, { text: '' }); - replaceState(providerOAuth, { authorizationUrl: '', callbackUrl: '', completing: false, copied: false }); resetAPIUpstreamForm(); } @@ -1575,7 +1505,6 @@ function resetAPIUpstreamForm() { priority: 100, loadFactor: 1, enabled: true, - fingerprintProfileId: '0', modelsText: '', submitting: false, error: '' @@ -1706,7 +1635,6 @@ export async function loadSession() { }); await loadHealth(); await loadUpdateStatus(); - await loadProvider(); await loadProviderAccounts(); await loadModelSettings(); await loadModelRouting(); @@ -2200,26 +2128,6 @@ export async function changePassword(event) { } } -export async function loadProvider() { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return; - - provider.loading = true; - provider.error = ''; - - try { - const payload = await requestJSON('/api/admin/provider-accounts/codex-oauth/status'); - if (!isCurrentAuthenticated(version)) return; - provider.data = payload; - } catch (error) { - if (!isCurrentAuthenticated(version)) return; - provider.error = error instanceof Error ? error.message : 'Failed to load provider status'; - } finally { - if (!isCurrentAuthenticated(version)) return; - provider.loading = false; - } -} - export async function loadProviderAccounts() { const version = sessionVersion; if (!isCurrentAuthenticated(version)) return; @@ -2262,13 +2170,6 @@ function ensureAccountModelsState(accountId) { syncError: '', syncMessage: '', syncSummary: null, - refreshingCatalog: false, - catalogRefreshError: '', - catalogRefreshMessage: '', - catalogRefreshSummary: null, - catalogStatus: null, - catalogRefreshSeq: 0, - catalogAbortController: null, saved: false, text: '', items: [], @@ -2340,7 +2241,6 @@ export async function loadAccountModels(accountId) { const models = payload.models ?? []; state.items = models; state.text = accountModelsText(models); - state.catalogStatus = payload.catalog ?? null; } catch (error) { if (!isCurrentAuthenticated(version)) return; if (!shouldApplyAccountModelsResponse(state, requestSeq)) return; @@ -2510,7 +2410,6 @@ export async function syncAccountModels(accountId) { } else { state.syncMessage = `Synced ${total} models.`; } - if (payload.catalog) state.catalogStatus = payload.catalog; await loadModelRouting(); return isCurrentAuthenticated(version); } catch (error) { @@ -2526,12 +2425,6 @@ export async function syncAccountModels(accountId) { } /** @param {number} accountId */ -export function cancelAccountModelCatalogRefresh(accountId) { - const state = ensureAccountModelsState(accountId); - if (!state.refreshingCatalog || !state.catalogAbortController) return false; - state.catalogAbortController.abort(); - return true; -} /** @param {unknown} error */ function isCanceledRequestError(error) { @@ -2539,58 +2432,6 @@ function isCanceledRequestError(error) { } /** @param {number} accountId */ -export async function refreshAccountModelCatalog(accountId) { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return false; - const state = ensureAccountModelsState(accountId); - if (state.refreshingCatalog) return false; - - state.requestSeq += 1; - const modelRequestSeq = state.requestSeq; - state.catalogRefreshSeq += 1; - const refreshSeq = state.catalogRefreshSeq; - const controller = new AbortController(); - state.catalogAbortController = controller; - state.refreshingCatalog = true; - state.catalogRefreshError = ''; - state.catalogRefreshMessage = ''; - state.catalogRefreshSummary = null; - state.saved = false; - try { - const payload = await requestJSON(`/api/admin/provider-accounts/${accountId}/models/refresh`, { - method: 'POST', - signal: controller.signal - }); - if (!isCurrentAuthenticated(version)) return false; - if (state.catalogRefreshSeq !== refreshSeq || state.requestSeq !== modelRequestSeq) return false; - const models = payload.models ?? []; - state.items = models; - state.text = accountModelsText(models); - state.catalogStatus = payload.catalog ?? state.catalogStatus; - state.catalogRefreshSummary = payload.synced ?? null; - const total = Number(payload.synced?.total ?? models.length); - const added = Number(payload.synced?.new ?? 0); - state.catalogRefreshMessage = added > 0 - ? `Catalog refreshed: ${total} models, ${added} new model${added === 1 ? '' : 's'}.` - : `Catalog refreshed: ${total} models.`; - await loadModelRouting(); - return isCurrentAuthenticated(version); - } catch (error) { - if (!isCurrentAuthenticated(version)) return false; - if (state.catalogRefreshSeq !== refreshSeq || state.requestSeq !== modelRequestSeq) return false; - state.catalogRefreshError = isCanceledRequestError(error) - ? 'Catalog refresh canceled.' - : error instanceof Error - ? error.message - : 'Catalog refresh failed'; - return false; - } finally { - if (isCurrentAuthenticated(version) && state.catalogRefreshSeq === refreshSeq) { - state.refreshingCatalog = false; - state.catalogAbortController = null; - } - } -} /** @param {ProviderAccount} account */ export function accountLabel(account) { @@ -2599,8 +2440,7 @@ export function accountLabel(account) { /** @param {ProviderAccount} account */ export function accountTypeLabel(account) { - if (account.accountType === 'api_upstream') return 'API upstream'; - return 'Codex OAuth'; + return 'API upstream'; } /** @param {string | null | undefined} status */ @@ -2609,92 +2449,13 @@ export function statusLabel(status) { return status.replaceAll('_', ' '); } -function browserFingerprint() { - if (typeof navigator === 'undefined') return ''; - const screenInfo = globalThis.screen; - return [ - navigator.userAgent, - navigator.language, - String(screenInfo?.width ?? ''), - String(screenInfo?.height ?? ''), - Intl.DateTimeFormat().resolvedOptions().timeZone - ].join('|'); -} - /** * @param {ProviderAccount | null} account */ -export async function connectProvider(account = null) { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return; - - provider.connecting = true; - provider.error = ''; - - try { - const payload = await requestJSON('/api/admin/provider-accounts/codex-oauth/connect', { - method: 'POST', - body: JSON.stringify({ - name: account ? account.name || account.displayName || '' : providerConnectForm.name, - priority: account ? account.priority : Number(providerConnectForm.priority), - enabled: account ? account.enabled : providerConnectForm.enabled, - targetAccountId: account?.id ?? 0, - fingerprintProfileId: account ? account.fingerprintProfileId ?? 0 : Number(providerConnectForm.fingerprintProfileId), - fingerprint: browserFingerprint() - }) - }); - if (!isCurrentAuthenticated(version)) return; - providerOAuth.authorizationUrl = payload.authorizationUrl ?? ''; - providerOAuth.callbackUrl = ''; - providerOAuth.copied = false; - } catch (error) { - if (!isCurrentAuthenticated(version)) return; - provider.error = error instanceof Error ? error.message : 'Failed to start provider connection'; - } finally { - if (!isCurrentAuthenticated(version)) return; - provider.connecting = false; - } -} - -export async function completeProviderCallback() { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return false; - - providerOAuth.completing = true; - provider.error = ''; - providerAccounts.error = ''; - try { - await requestJSON('/api/admin/provider-accounts/codex-oauth/callback', { - method: 'POST', - body: JSON.stringify({ callbackUrl: providerOAuth.callbackUrl }) - }); - if (!isCurrentAuthenticated(version)) return false; - replaceState(providerOAuth, { authorizationUrl: '', callbackUrl: '', completing: false, copied: false }); - await loadProvider(); - if (!isCurrentAuthenticated(version)) return false; - await loadProviderAccounts(); - return isCurrentAuthenticated(version) && !provider.error && !providerAccounts.error; - } catch (error) { - if (!isCurrentAuthenticated(version)) return false; - if (error instanceof Error && error.message === 'oauth_model_sync_failed') { - replaceState(providerOAuth, { authorizationUrl: '', callbackUrl: '', completing: false, copied: false }); - await loadProvider(); - if (!isCurrentAuthenticated(version)) return false; - await loadProviderAccounts(); - if (!isCurrentAuthenticated(version)) return false; - provider.error = 'OAuth account connected, but automatic model sync failed. Reauthorize the account to retry.'; - } else { - provider.error = error instanceof Error ? error.message : 'Failed to complete provider connection'; - } - return false; - } finally { - if (isCurrentAuthenticated(version)) providerOAuth.completing = false; - } -} /** * @param {ProviderAccount} account - * @param {Partial> & { apiKey?: string, proxyUrl?: string }} patch + * @param {Partial> & { apiKey?: string, proxyUrl?: string }} patch */ export async function updateProviderAccount(account, patch) { const version = sessionVersion; @@ -3021,7 +2782,6 @@ export async function createAPIUpstreamAccount() { priority: Number(apiUpstreamForm.priority) || 100, loadFactor: Number(apiUpstreamForm.loadFactor) || 1, enabled: apiUpstreamForm.enabled, - fingerprintProfileId: Number(apiUpstreamForm.fingerprintProfileId), models: parseModelLines(apiUpstreamForm.modelsText) }) }); @@ -3090,38 +2850,7 @@ export async function updateProviderAccountMaxConcurrentRequests(account, event) await updateProviderAccount(account, { maxConcurrentRequests }); } - -/** @param {any} account @param {string} fingerprintProfileId "0" means clear */ -export async function updateProviderAccountFingerprintProfile(account, fingerprintProfileId) { - const id = fingerprintProfileId === '0' ? null : (Number(fingerprintProfileId) || null); - await updateProviderAccount(account, { fingerprintProfileId: id }); -} - - /** @param {ProviderAccount} account */ -export async function refreshProviderAccount(account) { - const version = sessionVersion; - providerAccounts.saving = true; - providerAccounts.error = ''; - try { - await requestJSON(`/api/admin/provider-accounts/${account.id}/refresh`, { - method: 'POST' - }); - if (!isCurrentAuthenticated(version)) return; - await loadProvider(); - await loadProviderAccounts(); - await loadModelRouting(); - } catch (error) { - if (!isCurrentAuthenticated(version)) return; - const message = error instanceof Error ? error.message : 'Account refresh failed'; - providerAccounts.error = message; - await loadProviderAccounts(); - if (!isCurrentAuthenticated(version)) return; - providerAccounts.error = message; - } finally { - if (isCurrentAuthenticated(version)) providerAccounts.saving = false; - } -} /** @param {ProviderAccount} account */ export async function testProviderAccount(account) { @@ -3185,41 +2914,6 @@ export async function testSelectedProviderAccounts() { } } -export async function refreshSelectedProviderAccounts() { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return; - const accountIds = Object.keys(selectedProviderAccountIds) - .map((id) => Number(id)) - .filter((id) => Number.isFinite(id) && id > 0); - if (accountIds.length === 0) { - providerAccounts.error = 'Select at least one provider account'; - return; - } - - providerAccounts.saving = true; - providerAccounts.error = ''; - try { - await requestJSON('/api/admin/provider-accounts/bulk-refresh', { - method: 'POST', - body: JSON.stringify({ accountIds }) - }); - if (!isCurrentAuthenticated(version)) return; - clearProviderAccountSelection(); - await loadProvider(); - await loadProviderAccounts(); - await loadModelRouting(); - } catch (error) { - if (!isCurrentAuthenticated(version)) return; - const message = error instanceof Error ? error.message : 'Selected account refresh failed'; - providerAccounts.error = message; - await loadProviderAccounts(); - if (!isCurrentAuthenticated(version)) return; - providerAccounts.error = message; - } finally { - if (isCurrentAuthenticated(version)) providerAccounts.saving = false; - } -} - export async function disconnectSelectedProviderAccounts() { const version = sessionVersion; if (!isCurrentAuthenticated(version)) return; @@ -3240,7 +2934,6 @@ export async function disconnectSelectedProviderAccounts() { }); if (!isCurrentAuthenticated(version)) return; clearProviderAccountSelection(); - await loadProvider(); await loadProviderAccounts(); await loadModelRouting(); } catch (error) { @@ -3417,7 +3110,6 @@ export async function disconnectProviderAccount(account) { try { await requestJSON(`/api/admin/provider-accounts/${account.id}`, { method: 'DELETE' }); if (!isCurrentAuthenticated(version)) return; - await loadProvider(); await loadProviderAccounts(); await loadModelRouting(); } catch (error) { @@ -4732,8 +4424,6 @@ export async function deleteRevokedKey(id) { } } - - /** * @param {number | string} id * @param {boolean} selected @@ -5102,103 +4792,6 @@ export async function loadOpsDashboard(rangeSeconds) { } } -// --- Fingerprint profile state --- - -/** @type {{ loading: boolean; error: string; items: any[]; saving: boolean }} */ -export const fingerprintProfiles = $state({ - loading: false, - error: '', - items: [], - saving: false, -}); - -export async function loadFingerprintProfiles() { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return; - - fingerprintProfiles.loading = true; - fingerprintProfiles.error = ''; - try { - const data = await requestJSON('/api/admin/fingerprint-profiles'); - if (!isCurrentAuthenticated(version)) return; - fingerprintProfiles.items = data.profiles ?? []; - } catch (error) { - if (!isCurrentAuthenticated(version)) return; - fingerprintProfiles.error = error instanceof Error ? error.message : 'Failed to load fingerprint profiles'; - } finally { - if (!isCurrentAuthenticated(version)) return; - fingerprintProfiles.loading = false; - } -} - -/** @param {{name: string; description: string; userAgent: string; tlsFingerprint: string; headers: Record; enabled: boolean}} input */ -export async function createFingerprintProfile(input) { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return false; - - fingerprintProfiles.saving = true; - fingerprintProfiles.error = ''; - try { - await requestJSON('/api/admin/fingerprint-profiles', { - method: 'POST', - body: JSON.stringify(input), - }); - if (!isCurrentAuthenticated(version)) return false; - await loadFingerprintProfiles(); - return true; - } catch (error) { - if (!isCurrentAuthenticated(version)) return false; - fingerprintProfiles.error = error instanceof Error ? error.message : 'Failed to create fingerprint profile'; - return false; - } finally { - if (!isCurrentAuthenticated(version)) return; - fingerprintProfiles.saving = false; - } -} - -/** @param {number} id @param {any} input */ -export async function updateFingerprintProfile(id, input) { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return false; - - fingerprintProfiles.saving = true; - fingerprintProfiles.error = ''; - try { - await requestJSON(`/api/admin/fingerprint-profiles/${id}`, { - method: 'PATCH', - body: JSON.stringify(input), - }); - if (!isCurrentAuthenticated(version)) return false; - await loadFingerprintProfiles(); - return true; - } catch (error) { - if (!isCurrentAuthenticated(version)) return false; - fingerprintProfiles.error = error instanceof Error ? error.message : 'Failed to update fingerprint profile'; - return false; - } finally { - if (!isCurrentAuthenticated(version)) return; - fingerprintProfiles.saving = false; - } -} - -/** @param {number} id */ -export async function deleteFingerprintProfile(id) { - const version = sessionVersion; - if (!isCurrentAuthenticated(version)) return false; - - fingerprintProfiles.error = ''; - try { - await requestJSON(`/api/admin/fingerprint-profiles/${id}`, { method: 'DELETE' }); - if (!isCurrentAuthenticated(version)) return false; - await loadFingerprintProfiles(); - return true; - } catch (error) { - if (!isCurrentAuthenticated(version)) return false; - fingerprintProfiles.error = error instanceof Error ? error.message : 'Failed to delete fingerprint profile'; - return false; - } -} - // --- Error passthrough rules state --- /** @type {{ loading: boolean; error: string; items: any[]; saving: boolean }} */ export const errorPassthroughRules = $state({ diff --git a/frontend/src/routes/+layout.svelte b/frontend/src/routes/+layout.svelte index 97401ee9..7819730c 100644 --- a/frontend/src/routes/+layout.svelte +++ b/frontend/src/routes/+layout.svelte @@ -30,7 +30,6 @@ BellRing, BadgeDollarSign, Activity, - Fingerprint, PanelLeftClose, PanelLeftOpen, CircleUser, @@ -57,7 +56,6 @@ { href: '/alerting', label: 'Alerts', icon: BellRing }, { href: '/pricing', label: 'Pricing', icon: BadgeDollarSign }, { href: '/ops', label: 'Ops', icon: Activity }, - { href: '/fingerprints', label: 'Fingerprints', icon: Fingerprint } ]; const activePath = $derived(page.url.pathname); diff --git a/frontend/src/routes/alerting/alert-state.test.mjs b/frontend/src/routes/alerting/alert-state.test.mjs index ec3fc17d..8c3379a3 100644 --- a/frontend/src/routes/alerting/alert-state.test.mjs +++ b/frontend/src/routes/alerting/alert-state.test.mjs @@ -63,13 +63,13 @@ function rule(overrides = {}) { function template(overrides = {}) { return { - key: 'oauth-refresh-repeated-v1', - name: 'Repeated OAuth refresh failures', + key: 'request-log-retention-failed-v1', + name: 'Request log retention failures', enabled: false, - category: 'oauth', + category: 'scheduler', severity: 'warning', - eventAction: 'oauth.refresh.automatic.failed', - recoveryAction: 'oauth.refresh.automatic.succeeded', + eventAction: 'scheduler.request_log_retention.failed', + recoveryAction: 'scheduler.request_log_retention.succeeded', aggregationCount: 3, aggregationWindowSeconds: 900, cooldownSeconds: 3600, @@ -117,13 +117,13 @@ test('loadAlertRuleTemplates ignores an older response after a new load starts', test('installAlertRuleTemplate sends only the action and refreshes rules', async () => { const installed = rule({ - templateKey: 'oauth-refresh-repeated-v1', - name: 'Repeated OAuth refresh failures', + templateKey: 'request-log-retention-failed-v1', + name: 'Request log retention failures', enabled: false, - category: 'oauth', + category: 'scheduler', severity: 'warning', - eventAction: 'oauth.refresh.automatic.failed', - recoveryAction: 'oauth.refresh.automatic.succeeded', + eventAction: 'scheduler.request_log_retention.failed', + recoveryAction: 'scheduler.request_log_retention.succeeded', aggregationCount: 3, aggregationWindowSeconds: 900, cooldownSeconds: 3600, @@ -136,14 +136,14 @@ test('installAlertRuleTemplate sends only the action and refreshes rules', async return Response.json({ rules: [installed] }); }; - const result = await installAlertRuleTemplate('oauth-refresh-repeated-v1', 7); + const result = await installAlertRuleTemplate('request-log-retention-failed-v1', 7); - assert.equal(requests[0].path, '/api/admin/alert-rule-templates/oauth-refresh-repeated-v1/install'); + assert.equal(requests[0].path, '/api/admin/alert-rule-templates/request-log-retention-failed-v1/install'); assert.deepEqual(JSON.parse(requests[0].options.body), { actionId: 7 }); assert.equal(requests[1].path, '/api/admin/alert-rules'); assert.equal(result.created, true); assert.equal(result.rule.enabled, false); - assert.equal(alertRules.items[0].templateKey, 'oauth-refresh-repeated-v1'); + assert.equal(alertRules.items[0].templateKey, 'request-log-retention-failed-v1'); assert.equal(alertRuleTemplates.installingKey, ''); }); @@ -181,10 +181,10 @@ test('a late template install response cannot restore state after a 401 reset', return Response.json({ error: 'unauthorized' }, { status: 401 }); }; - const pending = installAlertRuleTemplate('oauth-refresh-repeated-v1', 7); + const pending = installAlertRuleTemplate('request-log-retention-failed-v1', 7); await Promise.resolve(); await loadAlertRules(); - resolveInstall(Response.json({ rule: rule({ templateKey: 'oauth-refresh-repeated-v1' }), created: true }, { status: 201 })); + resolveInstall(Response.json({ rule: rule({ templateKey: 'request-log-retention-failed-v1' }), created: true }, { status: 201 })); const result = await pending; assert.equal(result, null); diff --git a/frontend/src/routes/fingerprints/+page.svelte b/frontend/src/routes/fingerprints/+page.svelte deleted file mode 100644 index 678ca34c..00000000 --- a/frontend/src/routes/fingerprints/+page.svelte +++ /dev/null @@ -1,446 +0,0 @@ - - - - N2API Fingerprint Profiles - - - - {#if fingerprintNotice} -
-
-

{fingerprintNotice.title}

-

{fingerprintNotice.message}

-
- -
- {/if} -
-
-
-

Fingerprint profiles

-

TLS and User-Agent identities applied to provider connections.

-
-
- -
-
- - {#if fingerprintProfiles.error && !showForm && !deletingProfile} -
- {fingerprintProfiles.error} -
- {/if} - -
-
-

System sending defaults

-
-
- - - - - - - - - - - - - - - - - - - - - - - -
Account typeDefault identityHTTP headersTLS
Codex OAuthDefault Codex CLICodex TUI headersDefault transport
API upstreamDefault API upstream (pass-through)Client headers; upstream bearer tokenDefault transport
-
-
- - - {#if showForm} - - {/if} - - {#if deletingProfile} - - {/if} - - -
- {#if fingerprintProfiles.loading} -

Loading fingerprint profiles...

- {:else if fingerprintProfiles.items.length === 0} -

No fingerprint profiles configured.

- {:else} -
- - - - - - - - - - - - - - {#each fingerprintProfiles.items as fp} - - - - - - - - - - {/each} - -
NameUser-AgentTLSHeadersStatusCreatedActions
-
- {fp.name} - {#if fp.systemKey} - System default - {/if} -
- {#if fp.description} -

{fp.description}

- {/if} -
{fp.userAgent || '-'}{fp.tlsFingerprint || 'Default transport'} - {#if fp.headers && Object.keys(fp.headers).length} -
- {#each Object.entries(fp.headers) as [key, value]} -
{key}: {value}
- {/each} -
- {:else} - - - {/if} -
- - {fp.enabled ? 'Enabled' : 'Disabled'} - - {formatDate(fp.createdAt)} - {#if fp.systemKey} - Managed by system - {:else} - - - {/if} -
-
- {/if} -
-
-
diff --git a/frontend/src/routes/fingerprints/fingerprint-page.test.mjs b/frontend/src/routes/fingerprints/fingerprint-page.test.mjs deleted file mode 100644 index 449227f4..00000000 --- a/frontend/src/routes/fingerprints/fingerprint-page.test.mjs +++ /dev/null @@ -1,41 +0,0 @@ -import { test } from 'bun:test'; -import assert from '../../test/assert.js'; - -const source = await Bun.file('src/routes/fingerprints/+page.svelte').text(); - -test('fingerprint profiles expose the system default and seed new profiles from it', () => { - for (const label of [ - 'Fingerprint profiles', - 'New profile', - 'Template', - 'Blank profile', - 'System default ยท Codex CLI', - 'System default', - 'System sending defaults', - 'Codex OAuth', - 'API upstream', - 'Default API upstream (pass-through)', - 'Client headers', - 'Default transport', - 'Managed by system' - ]) { - const pattern = label.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replaceAll(' ', '\\s+'); - assert.match(source, new RegExp(pattern), `fingerprints page should include ${label}`); - } - assert.match(source, /SYSTEM_DEFAULT_KEY\s*=\s*'codex_cli_default'/); - assert.match(source, /profile\.systemKey === SYSTEM_DEFAULT_KEY/); - assert.match(source, /applyTemplate\(systemDefaultProfile\(\) \? SYSTEM_DEFAULT_KEY : ''\)/); - assert.match(source, /if \(fp\.systemKey\) return/); - assert.match(source, /\{#if fp\.systemKey\}[\s\S]*Managed by system/); -}); - -test('fingerprint create edit and delete flows use accessible dialogs', () => { - assert.match(source, /fingerprint-form-title/); - assert.match(source, /delete-fingerprint-title/); - assert.match(source, /role="dialog"/); - assert.match(source, /aria-modal="true"/); - assert.match(source, /formError/); - assert.match(source, /Headers must be valid JSON object syntax/); - assert.match(source, /confirmDeleteProfile/); - assert.doesNotMatch(source, /\bconfirm\(/); -}); diff --git a/frontend/src/routes/mutation-session-state.test.mjs b/frontend/src/routes/mutation-session-state.test.mjs index 6da40d57..e0394ada 100644 --- a/frontend/src/routes/mutation-session-state.test.mjs +++ b/frontend/src/routes/mutation-session-state.test.mjs @@ -5,9 +5,7 @@ globalThis.$state = (value) => value; const { apiKeys, - completeProviderCallback, deleteRevokedKey, - providerOAuth, revokeKey, saveAccountModels, session, @@ -26,16 +24,11 @@ function authenticate() { beforeEach(() => { authenticate(); - providerOAuth.callbackUrl = 'http://localhost/callback?code=test&state=test'; apiKeys.items = [{ id: 7, name: 'test-key', revokedAt: null }]; globalThis.fetch = async () => Response.json({ error: 'unauthorized' }, { status: 401 }); }); test('provider mutations return false when a protected request clears the session', async () => { - assert.equal(await completeProviderCallback(), false); - assert.equal(session.authenticated, false); - - authenticate(); assert.equal(await updateProviderAccount({ id: 3 }, { name: 'updated' }), false); assert.equal(session.authenticated, false); diff --git a/frontend/src/routes/navigation.test.mjs b/frontend/src/routes/navigation.test.mjs index ae3ec670..cddb3da6 100644 --- a/frontend/src/routes/navigation.test.mjs +++ b/frontend/src/routes/navigation.test.mjs @@ -18,7 +18,6 @@ const expectedFiles = [ 'src/routes/alerting/+page.svelte', 'src/routes/pricing/+page.svelte', 'src/routes/ops/+page.svelte', - 'src/routes/fingerprints/+page.svelte' ]; const readText = await preloadTextFiles([ @@ -80,7 +79,7 @@ test('admin UI has focused routes behind a shared sidebar shell', () => { } const layout = readText('src/routes/+layout.svelte'); - for (const label of ['Dashboard', 'Gateway', 'Providers', 'Routing pools', 'API Keys', 'Request Logs', 'System logs', 'Alerts', 'Pricing', 'Ops', 'Fingerprints', 'Active sessions', 'Sign out', 'Change password', 'Save', 'Current password', 'New password']) { + for (const label of ['Dashboard', 'Gateway', 'Providers', 'Routing pools', 'API Keys', 'Request Logs', 'System logs', 'Alerts', 'Pricing', 'Ops', 'Active sessions', 'Sign out', 'Change password', 'Save', 'Current password', 'New password']) { assert.match(layout, new RegExp(label.replace(' ', '\\s+')), `layout should include ${label}`); } assert.doesNotMatch(layout, /label:\s*'Models'/); @@ -185,7 +184,7 @@ test('admin routes share the dashboard page heading hierarchy', () => { alerting: 'Alerts', pricing: 'Pricing', ops: 'Operations monitor', - fingerprints: 'Fingerprint profiles' + }; for (const [route, title] of Object.entries(routeTitles)) { @@ -197,7 +196,7 @@ test('admin routes share the dashboard page heading hierarchy', () => { }); test('dense management tables provide a shared mobile stacked view', () => { - for (const route of ['api-keys', 'providers', 'routing-pools', 'models', 'request-logs', 'system-logs', 'alerting', 'pricing', 'fingerprints']) { + for (const route of ['api-keys', 'providers', 'routing-pools', 'models', 'request-logs', 'system-logs', 'alerting', 'pricing']) { const source = readText(`src/routes/${route}/+page.svelte`); assert.match(source, /ui-table--stacked/, `${route} should opt its primary table into the mobile stacked view`); assert.match(source, /data-label=/, `${route} should label stacked table values`); @@ -526,7 +525,6 @@ test('configuration mutations show blocking progress and transient success feedb ['routing pools', routingPoolsPage, /routingPoolMutationBusy[\s\S]*?ui-loading-overlay[\s\S]*?>thinkingthinkingthinkingthinkingthinking { { name: 'alerting', path: 'src/routes/alerting/+page.svelte' }, { name: 'pricing', path: 'src/routes/pricing/+page.svelte' }, { name: 'ops', path: 'src/routes/ops/+page.svelte' }, - { name: 'fingerprints', path: 'src/routes/fingerprints/+page.svelte' }, ]; for (const page of pages) { const src = readText(page.path); diff --git a/frontend/src/routes/providers/+page.svelte b/frontend/src/routes/providers/+page.svelte index 93b10d14..72010e50 100644 --- a/frontend/src/routes/providers/+page.svelte +++ b/frontend/src/routes/providers/+page.svelte @@ -4,36 +4,26 @@ import { accountLabel, accountTypeLabel, - completeProviderCallback, - connectProvider, - copyAuthorizationURL, createAPIUpstreamAccount, disconnectProviderAccount, formatDate, getAccountModelsState, getAccountTestResultsState, - getProviderStateLabel, isSyncedAccountModel, loadAccountModels, loadProviderAccounts, apiUpstreamForm, - provider, providerAccounts, - providerConnectForm, - providerOAuth, pauseProviderAccount, providerAccountEffectiveStatus, - refreshProviderAccount, removeAccountModel, resetProviderAccountStatus, saveAccountModels, accountModelSummary, accountModelsText, - cancelAccountModelCatalogRefresh, session, sourceBadgeLabel, syncAccountModels, - refreshAccountModelCatalog, setAccountModelEnabled, statusLabel, testAllProviderAccounts, @@ -41,13 +31,10 @@ testProviderAccountModel, toggleAccountTestHistory, updateProviderAccount, - fingerprintProfiles, - loadFingerprintProfiles } from '#lib/admin-state.svelte.js'; import AuthGate from '#lib/AuthGate.svelte'; import { runModelTestsWithConcurrency } from '#lib/model-test-queue.js'; - const DEFAULT_CODEX_FINGERPRINT_SYSTEM_KEY = 'codex_cli_default'; let accountSearch = $state(''); let accountStatusFilter = $state('all'); let accountTypeFilter = $state('all'); @@ -56,12 +43,9 @@ let accountPage = $state(1); let accountPageSize = $state(5); let addAccountModalOpen = $state(false); - /** @type {'oauth' | 'api_upstream'} */ - let addAccountModalTab = $state('oauth'); - let fingerprintProfilesRequested = $state(false); let appliedProviderAccountSearch = $state(''); let editingProviderAccountId = $state(0); - let editingProviderAccountDraft = $state(/** @type {{ name: string, enabled: boolean, priority: number, loadFactor: number, maxConcurrentRequests: number, fingerprintProfileId: number, baseUrl: string, proxyUrl: string, apiKey: string, modelsText: string, modelItems: import('#lib/admin-state.svelte.js').AccountModel[], syncModelsOnSave: boolean } | null} */ (null)); + let editingProviderAccountDraft = $state(/** @type {{ name: string, enabled: boolean, priority: number, loadFactor: number, maxConcurrentRequests: number, baseUrl: string, proxyUrl: string, apiKey: string, modelsText: string, modelItems: import('#lib/admin-state.svelte.js').AccountModel[], syncModelsOnSave: boolean } | null} */ (null)); let editingProviderAccountError = $state(''); let deletingProviderAccountId = $state(0); let modelTestAccountId = $state(0); @@ -73,23 +57,15 @@ /** @type {Record} */ let modelTestRuns = $state({}); let modelTestRunActive = $state(false); - let recoveryTestAccountId = $state(0); let providerMutationBusy = $state(false); let recoveryNotice = $state(/** @type {{ kind: 'success' | 'warning' | 'error', title: string, message: string } | null} */ (null)); - const providerStateLabel = $derived(getProviderStateLabel()); const editingProviderAccount = $derived( providerAccounts.items.find((account) => account.id === editingProviderAccountId) ?? null ); const deletingProviderAccount = $derived( providerAccounts.items.find((account) => account.id === deletingProviderAccountId) ?? null ); - const defaultCodexFingerprintProfile = $derived( - fingerprintProfiles.items.find((profile) => profile.systemKey === DEFAULT_CODEX_FINGERPRINT_SYSTEM_KEY) ?? null - ); - const oauthFingerprintProfiles = $derived( - fingerprintProfiles.items.filter((profile) => profile.systemKey !== DEFAULT_CODEX_FINGERPRINT_SYSTEM_KEY) - ); const modelTestAccount = $derived( providerAccounts.items.find((account) => account.id === modelTestAccountId) ?? null ); @@ -130,18 +106,6 @@ recoveryNotice = { kind: 'success', title, message }; } - $effect(() => { - const defaultProfileID = Number(defaultCodexFingerprintProfile?.id ?? 0); - if ( - defaultProfileID > 0 && - editingProviderAccount && - editingProviderAccountDraft && - isCodexOAuthAccount(editingProviderAccount) && - Number(editingProviderAccountDraft.fingerprintProfileId) === defaultProfileID - ) { - editingProviderAccountDraft.fingerprintProfileId = 0; - } - }); const someFilteredModelTestsSelected = $derived( filteredSelectedModelTestCount > 0 && !allFilteredModelTestsSelected ); @@ -193,9 +157,8 @@ if (!type || type === 'all') { if (status === 'disabled') { accountEnabledFilter = 'disabled'; accountStatusFilter = 'all'; } if (status === 'api_upstream') { accountTypeFilter = 'api_upstream'; accountStatusFilter = 'all'; } - if (status === 'codex_oauth') { accountTypeFilter = 'codex_oauth'; accountStatusFilter = 'all'; } } - if (['all', 'api_upstream', 'codex_oauth'].includes(type)) { + if (['all', 'api_upstream'].includes(type)) { accountTypeFilter = type; } if (['all', 'enabled', 'disabled'].includes(enabled)) { @@ -203,7 +166,6 @@ } } - /** * @param {string | null | undefined} model * @param {import('#lib/admin-state.svelte.js').ProviderAccount} account @@ -216,7 +178,6 @@ $effect(() => { if (!session.authenticated) { - fingerprintProfilesRequested = false; appliedProviderAccountSearch = ''; return; } @@ -224,10 +185,6 @@ appliedProviderAccountSearch = page.url.search; applyProviderAccountURLFilters(page.url.search); } - if (!fingerprintProfilesRequested) { - fingerprintProfilesRequested = true; - void loadFingerprintProfiles(); - } }); $effect(() => { @@ -258,7 +215,6 @@ */ function accountMatchesTypeFilter(account, filter) { if (filter === 'api_upstream') return account.accountType === 'api_upstream'; - if (filter === 'codex_oauth') return isCodexOAuthAccount(account); return true; } @@ -296,7 +252,6 @@ if (key === 'type') return accountTypeLabel(account); if (key === 'status') return statusLabel(providerAccountEffectiveStatus(account)); if (key === 'enabled') return account.enabled ? 0 : 1; - if (key === 'refresh') return Date.parse(account.lastRefreshAt ?? '') || 0; if (key === 'used') return Date.parse(account.lastUsedAt ?? '') || 0; return ''; } @@ -334,37 +289,10 @@ } /** @param {import('#lib/admin-state.svelte.js').ProviderAccount} account */ - function isCodexOAuthAccount(account) { - return account.accountType === 'codex_oauth' || !account.accountType; - } - - /** @param {import('#lib/admin-state.svelte.js').OAuthModelCatalogStatus | null} status */ - function catalogStatusLabel(status) { - if (!status?.lastAttemptAt) return 'Not refreshed'; - if (status.lastError) return `Failed at ${formatDate(status.lastFailureAt)}`; - if (status.source === 'cache') return `Cache hit at ${formatDate(status.lastSuccessAt)}`; - if (status.source === 'upstream') return `Upstream fetched at ${formatDate(status.lastSuccessAt)}`; - return 'Recorded'; - } - - /** @param {import('#lib/admin-state.svelte.js').OAuthModelCatalogStatus | null} status */ - function catalogStatusDetail(status) { - if (!status?.lastAttemptAt) return 'No catalog refresh has been recorded for this account.'; - const timings = []; - if (status.upstreamFetchMs != null) timings.push(`upstream ${status.upstreamFetchMs} ms`); - if (status.localApplyMs != null) timings.push(`apply ${status.localApplyMs} ms`); - return timings.length > 0 ? timings.join(' ยท ') : 'Timing not recorded'; - } - - /** @param {import('#lib/admin-state.svelte.js').ProviderAccount} account */ - function accountEmailLabel(account) { - if (!isCodexOAuthAccount(account)) return ''; - return account.displayName?.includes('@') ? account.displayName : account.subject || ''; - } /** @param {import('#lib/admin-state.svelte.js').ProviderAccount} account */ function accountHoverDetail(account) { - return [accountLabel(account), accountEmailLabel(account)].filter(Boolean).join('\n'); + return accountLabel(account); } /** @param {import('#lib/admin-state.svelte.js').ProviderAccount} account */ @@ -382,7 +310,7 @@ function closeAccountEditor() { const modelState = editingProviderAccountId ? getAccountModelsState(editingProviderAccountId) : null; - if (providerMutationBusy || providerAccounts.saving || modelState?.saving || modelState?.syncing || modelState?.refreshingCatalog) return; + if (providerMutationBusy || providerAccounts.saving || modelState?.saving || modelState?.syncing) return; editingProviderAccountId = 0; editingProviderAccountDraft = null; editingProviderAccountError = ''; @@ -393,18 +321,12 @@ * @param {ReturnType} modelState */ function createAccountDraft(account, modelState) { - const fingerprintProfileID = Number(account.fingerprintProfileId ?? 0); - const defaultProfileID = Number(defaultCodexFingerprintProfile?.id ?? 0); return { name: accountLabel(account), enabled: Boolean(account.enabled), priority: Number(account.priority ?? 0), loadFactor: Number(account.loadFactor || 1), maxConcurrentRequests: Number(account.maxConcurrentRequests || 0), - fingerprintProfileId: - isCodexOAuthAccount(account) && defaultProfileID > 0 && fingerprintProfileID === defaultProfileID - ? 0 - : fingerprintProfileID, baseUrl: account.baseUrl || '', proxyUrl: account.proxyUrlSummary || '', apiKey: '', @@ -414,19 +336,6 @@ }; } - /** - * @param {import('#lib/admin-state.svelte.js').ProviderAccount} account - * @param {number} selectedProfileID - */ - function resolveFingerprintProfileID(account, selectedProfileID) { - const profileID = Number(selectedProfileID) || 0; - if (profileID > 0) return profileID; - if (isCodexOAuthAccount(account)) { - return Number(defaultCodexFingerprintProfile?.id ?? 0) || null; - } - return null; - } - /** @param {number} accountId */ function refreshAccountDraft(accountId) { const account = providerAccounts.items.find((item) => item.id === accountId); @@ -435,22 +344,10 @@ } function addAccountBusy() { - return provider.connecting || providerOAuth.completing || apiUpstreamForm.submitting; + return apiUpstreamForm.submitting; } function resetAddAccountDraft() { - Object.assign(providerConnectForm, { - name: '', - priority: 100, - enabled: true, - fingerprintProfileId: '0' - }); - Object.assign(providerOAuth, { - authorizationUrl: '', - callbackUrl: '', - completing: false, - copied: false - }); Object.assign(apiUpstreamForm, { name: '', baseUrl: '', @@ -459,18 +356,15 @@ priority: 100, loadFactor: 1, enabled: true, - fingerprintProfileId: '0', modelsText: '', submitting: false, error: '' }); - provider.error = ''; providerAccounts.error = ''; } function openAddAccountModal() { resetAddAccountDraft(); - addAccountModalTab = 'oauth'; addAccountModalOpen = true; } @@ -483,32 +377,18 @@ /** @param {SubmitEvent} event */ async function saveAddAccount(event) { event.preventDefault(); - if (addAccountModalTab === 'api_upstream') { - const name = apiUpstreamForm.name.trim() || 'API upstream account'; - if (await createAPIUpstreamAccount()) { - showProviderSuccess('Provider account created', `${name} is ready to configure.`); - } - return; - } - if (providerOAuth.authorizationUrl) { - if (await completeProviderCallback()) { - showProviderSuccess('Provider account connected', 'The Codex OAuth account is ready to use.'); - } - return; + const name = apiUpstreamForm.name.trim() || 'API upstream account'; + if (await createAPIUpstreamAccount()) { + showProviderSuccess('Provider account created', `${name} is ready to configure.`); } - await connectProvider(); } function addAccountSaveLabel() { - if (addAccountModalTab === 'api_upstream') return apiUpstreamForm.submitting ? 'Saving' : 'Save'; - if (providerOAuth.authorizationUrl) return providerOAuth.completing ? 'Saving' : 'Save'; - return provider.connecting ? 'Saving' : 'Save'; + return apiUpstreamForm.submitting ? 'Saving' : 'Save'; } function addAccountSaveDisabled() { - if (addAccountModalTab === 'api_upstream') return apiUpstreamForm.submitting; - if (providerOAuth.authorizationUrl) return providerOAuth.completing || !providerOAuth.callbackUrl.trim(); - return provider.loading || !provider.data?.configured || provider.connecting; + return apiUpstreamForm.submitting; } async function saveEditingProviderAccount() { @@ -544,7 +424,6 @@ priority: Number(draft.priority), loadFactor: Number(draft.loadFactor), maxConcurrentRequests: Number(draft.maxConcurrentRequests), - fingerprintProfileId: resolveFingerprintProfileID(account, draft.fingerprintProfileId) }))) { if (session.authenticated) { editingProviderAccountError = providerAccounts.error || 'Account update failed'; @@ -610,18 +489,17 @@ /** * @param {import('#lib/admin-state.svelte.js').ProviderAccount} account - * @param {'pause' | 'refresh' | 'reset'} action + * @param {'pause' | 'reset'} action */ async function runProviderAccountAction(account, action) { if (providerMutationBusy || providerAccounts.saving) return; providerMutationBusy = true; try { if (action === 'pause') await pauseProviderAccount(account); - else if (action === 'refresh') await refreshProviderAccount(account); else await resetProviderAccountStatus(account); if (!session.authenticated || providerAccounts.error) return; - const actionLabel = action === 'pause' ? 'paused' : action === 'refresh' ? 'refreshed' : 'reset'; + const actionLabel = action === 'pause' ? 'paused' : 'reset'; showProviderSuccess('Provider account updated', `${accountLabel(account)} was ${actionLabel}.`); } finally { providerMutationBusy = false; @@ -814,32 +692,6 @@ } /** @param {import('#lib/admin-state.svelte.js').ProviderAccount} account */ - async function testAccountRecovery(account) { - if (!isCodexOAuthAccount(account) || providerAccounts.saving || recoveryTestAccountId) return; - recoveryTestAccountId = account.id; - recoveryNotice = null; - const tested = await testProviderAccount(account); - if (providerAccounts.error || !tested) { - recoveryNotice = { - kind: 'error', - title: 'Recovery test failed', - message: providerAccounts.error || `${accountLabel(account)} could not be tested.` - }; - } else if (tested.status === 'active') { - recoveryNotice = { - kind: 'success', - title: 'Recovery confirmed', - message: `${accountLabel(account)} is active and its local failure state was cleared.` - }; - } else { - recoveryNotice = { - kind: 'warning', - title: 'Recovery not confirmed', - message: tested.statusReason || tested.lastError || `${accountLabel(account)} is still ${statusLabel(tested.status)}.` - }; - } - recoveryTestAccountId = 0; - } /** @param {import('#lib/admin-state.svelte.js').AccountModel[]} models */ function enabledAccountModelCount(models) { @@ -897,22 +749,10 @@

Provider accounts

-

Codex OAuth and API upstream gateway exits. Last refresh: {formatDate(provider.data?.lastRefreshAt)}.

+

API Key upstream gateway exits.

- - -{provider.loading ? 'Checking' : providerStateLabel} - +
- -
- - -
- - - {#if addAccountModalTab === 'oauth'} -
-

Generate a Codex OAuth authorization link to connect a new account.

- -
-
- - -
- - - - - {#if providerOAuth.authorizationUrl} -
-
-

OAuth authorization link

- -
- - {providerOAuth.authorizationUrl} - -
- {/if} -
- - - {#if providerOAuth.authorizationUrl} -
- -
- {/if} - {#if provider.error} -

{provider.error}

- {/if} -
- - - {:else}

Add an OpenAI-compatible upstream by API key and base URL.

@@ -1165,19 +889,6 @@ class={[

Within the same priority, higher values form a strict preference tier and are considered first; this is not a proportional weight.

- -