fix: 修复Claude OAuth token交换时authorization code解析错误
原代码中 `parts` 变量被创建但从未使用,导致 `len(parts) == 0` 永远为 true,使得即使成功从 `code#state` 格式中分割出 authCode, 最后也会被覆盖为原始的完整字符串。 这导致传递给Claude Token端点的code包含了 `#state` 部分, Claude返回 "Invalid 'code' in request" 错误。
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sub2api/internal/pkg/oauth"
|
"sub2api/internal/pkg/oauth"
|
||||||
@@ -139,20 +140,12 @@ func (s *claudeOAuthService) GetAuthorizationCode(ctx context.Context, sessionKe
|
|||||||
func (s *claudeOAuthService) ExchangeCodeForToken(ctx context.Context, code, codeVerifier, state, proxyURL string) (*oauth.TokenResponse, error) {
|
func (s *claudeOAuthService) ExchangeCodeForToken(ctx context.Context, code, codeVerifier, state, proxyURL string) (*oauth.TokenResponse, error) {
|
||||||
client := createReqClient(proxyURL)
|
client := createReqClient(proxyURL)
|
||||||
|
|
||||||
|
// Parse code which may contain state in format "authCode#state"
|
||||||
authCode := code
|
authCode := code
|
||||||
codeState := ""
|
codeState := ""
|
||||||
if len(code) > 0 {
|
if idx := strings.Index(code, "#"); idx != -1 {
|
||||||
parts := make([]string, 0, 2)
|
authCode = code[:idx]
|
||||||
for i, part := range []rune(code) {
|
codeState = code[idx+1:]
|
||||||
if part == '#' {
|
|
||||||
authCode = code[:i]
|
|
||||||
codeState = code[i+1:]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(parts) == 0 {
|
|
||||||
authCode = code
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reqBody := map[string]any{
|
reqBody := map[string]any{
|
||||||
|
|||||||
Reference in New Issue
Block a user