fix(test): 修复claude、openai oauth账号test刷新token的bug
This commit is contained in:
@@ -100,7 +100,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|||||||
antigravityTokenProvider := service.NewAntigravityTokenProvider(accountRepository, geminiTokenCache, antigravityOAuthService)
|
antigravityTokenProvider := service.NewAntigravityTokenProvider(accountRepository, geminiTokenCache, antigravityOAuthService)
|
||||||
httpUpstream := repository.NewHTTPUpstream(configConfig)
|
httpUpstream := repository.NewHTTPUpstream(configConfig)
|
||||||
antigravityGatewayService := service.NewAntigravityGatewayService(accountRepository, gatewayCache, antigravityTokenProvider, rateLimitService, httpUpstream)
|
antigravityGatewayService := service.NewAntigravityGatewayService(accountRepository, gatewayCache, antigravityTokenProvider, rateLimitService, httpUpstream)
|
||||||
accountTestService := service.NewAccountTestService(accountRepository, oAuthService, openAIOAuthService, geminiTokenProvider, antigravityGatewayService, httpUpstream)
|
accountTestService := service.NewAccountTestService(accountRepository, geminiTokenProvider, antigravityGatewayService, httpUpstream)
|
||||||
concurrencyCache := repository.ProvideConcurrencyCache(redisClient, configConfig)
|
concurrencyCache := repository.ProvideConcurrencyCache(redisClient, configConfig)
|
||||||
concurrencyService := service.ProvideConcurrencyService(concurrencyCache, accountRepository, configConfig)
|
concurrencyService := service.ProvideConcurrencyService(concurrencyCache, accountRepository, configConfig)
|
||||||
crsSyncService := service.NewCRSSyncService(accountRepository, proxyRepository, oAuthService, openAIOAuthService, geminiOAuthService)
|
crsSyncService := service.NewCRSSyncService(accountRepository, proxyRepository, oAuthService, openAIOAuthService, geminiOAuthService)
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
|
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
|
||||||
"github.com/Wei-Shaw/sub2api/internal/pkg/geminicli"
|
"github.com/Wei-Shaw/sub2api/internal/pkg/geminicli"
|
||||||
@@ -28,7 +27,6 @@ var sseDataPrefix = regexp.MustCompile(`^data:\s*`)
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
testClaudeAPIURL = "https://api.anthropic.com/v1/messages"
|
testClaudeAPIURL = "https://api.anthropic.com/v1/messages"
|
||||||
testOpenAIAPIURL = "https://api.openai.com/v1/responses"
|
|
||||||
chatgptCodexAPIURL = "https://chatgpt.com/backend-api/codex/responses"
|
chatgptCodexAPIURL = "https://chatgpt.com/backend-api/codex/responses"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,8 +42,6 @@ type TestEvent struct {
|
|||||||
// AccountTestService handles account testing operations
|
// AccountTestService handles account testing operations
|
||||||
type AccountTestService struct {
|
type AccountTestService struct {
|
||||||
accountRepo AccountRepository
|
accountRepo AccountRepository
|
||||||
oauthService *OAuthService
|
|
||||||
openaiOAuthService *OpenAIOAuthService
|
|
||||||
geminiTokenProvider *GeminiTokenProvider
|
geminiTokenProvider *GeminiTokenProvider
|
||||||
antigravityGatewayService *AntigravityGatewayService
|
antigravityGatewayService *AntigravityGatewayService
|
||||||
httpUpstream HTTPUpstream
|
httpUpstream HTTPUpstream
|
||||||
@@ -54,16 +50,12 @@ type AccountTestService struct {
|
|||||||
// NewAccountTestService creates a new AccountTestService
|
// NewAccountTestService creates a new AccountTestService
|
||||||
func NewAccountTestService(
|
func NewAccountTestService(
|
||||||
accountRepo AccountRepository,
|
accountRepo AccountRepository,
|
||||||
oauthService *OAuthService,
|
|
||||||
openaiOAuthService *OpenAIOAuthService,
|
|
||||||
geminiTokenProvider *GeminiTokenProvider,
|
geminiTokenProvider *GeminiTokenProvider,
|
||||||
antigravityGatewayService *AntigravityGatewayService,
|
antigravityGatewayService *AntigravityGatewayService,
|
||||||
httpUpstream HTTPUpstream,
|
httpUpstream HTTPUpstream,
|
||||||
) *AccountTestService {
|
) *AccountTestService {
|
||||||
return &AccountTestService{
|
return &AccountTestService{
|
||||||
accountRepo: accountRepo,
|
accountRepo: accountRepo,
|
||||||
oauthService: oauthService,
|
|
||||||
openaiOAuthService: openaiOAuthService,
|
|
||||||
geminiTokenProvider: geminiTokenProvider,
|
geminiTokenProvider: geminiTokenProvider,
|
||||||
antigravityGatewayService: antigravityGatewayService,
|
antigravityGatewayService: antigravityGatewayService,
|
||||||
httpUpstream: httpUpstream,
|
httpUpstream: httpUpstream,
|
||||||
@@ -183,22 +175,6 @@ func (s *AccountTestService) testClaudeAccountConnection(c *gin.Context, account
|
|||||||
if authToken == "" {
|
if authToken == "" {
|
||||||
return s.sendErrorAndEnd(c, "No access token available")
|
return s.sendErrorAndEnd(c, "No access token available")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if token needs refresh
|
|
||||||
needRefresh := false
|
|
||||||
if expiresAt := account.GetCredentialAsTime("expires_at"); expiresAt != nil {
|
|
||||||
if time.Now().Add(5 * time.Minute).After(*expiresAt) {
|
|
||||||
needRefresh = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if needRefresh && s.oauthService != nil {
|
|
||||||
tokenInfo, err := s.oauthService.RefreshAccountToken(ctx, account)
|
|
||||||
if err != nil {
|
|
||||||
return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to refresh token: %s", err.Error()))
|
|
||||||
}
|
|
||||||
authToken = tokenInfo.AccessToken
|
|
||||||
}
|
|
||||||
} else if account.Type == "apikey" {
|
} else if account.Type == "apikey" {
|
||||||
// API Key - use x-api-key header
|
// API Key - use x-api-key header
|
||||||
useBearer = false
|
useBearer = false
|
||||||
@@ -310,15 +286,6 @@ func (s *AccountTestService) testOpenAIAccountConnection(c *gin.Context, account
|
|||||||
return s.sendErrorAndEnd(c, "No access token available")
|
return s.sendErrorAndEnd(c, "No access token available")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if token is expired and refresh if needed
|
|
||||||
if account.IsOpenAITokenExpired() && s.openaiOAuthService != nil {
|
|
||||||
tokenInfo, err := s.openaiOAuthService.RefreshAccountToken(ctx, account)
|
|
||||||
if err != nil {
|
|
||||||
return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to refresh token: %s", err.Error()))
|
|
||||||
}
|
|
||||||
authToken = tokenInfo.AccessToken
|
|
||||||
}
|
|
||||||
|
|
||||||
// OAuth uses ChatGPT internal API
|
// OAuth uses ChatGPT internal API
|
||||||
apiURL = chatgptCodexAPIURL
|
apiURL = chatgptCodexAPIURL
|
||||||
chatgptAccountID = account.GetChatGPTAccountID()
|
chatgptAccountID = account.GetChatGPTAccountID()
|
||||||
|
|||||||
Reference in New Issue
Block a user