refactor(auth): export Claude OAuth constants for reuse

This commit is contained in:
hkfires
2026-01-23 21:47:07 +08:00
parent 9aa5344c29
commit 7cb6a9b89a

View File

@@ -18,11 +18,12 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// OAuth configuration constants for Claude/Anthropic
const ( const (
anthropicAuthURL = "https://claude.ai/oauth/authorize" AuthURL = "https://claude.ai/oauth/authorize"
anthropicTokenURL = "https://console.anthropic.com/v1/oauth/token" TokenURL = "https://console.anthropic.com/v1/oauth/token"
anthropicClientID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e" ClientID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
redirectURI = "http://localhost:54545/callback" RedirectURI = "http://localhost:54545/callback"
) )
// tokenResponse represents the response structure from Anthropic's OAuth token endpoint. // tokenResponse represents the response structure from Anthropic's OAuth token endpoint.
@@ -82,16 +83,16 @@ func (o *ClaudeAuth) GenerateAuthURL(state string, pkceCodes *PKCECodes) (string
params := url.Values{ params := url.Values{
"code": {"true"}, "code": {"true"},
"client_id": {anthropicClientID}, "client_id": {ClientID},
"response_type": {"code"}, "response_type": {"code"},
"redirect_uri": {redirectURI}, "redirect_uri": {RedirectURI},
"scope": {"org:create_api_key user:profile user:inference"}, "scope": {"org:create_api_key user:profile user:inference"},
"code_challenge": {pkceCodes.CodeChallenge}, "code_challenge": {pkceCodes.CodeChallenge},
"code_challenge_method": {"S256"}, "code_challenge_method": {"S256"},
"state": {state}, "state": {state},
} }
authURL := fmt.Sprintf("%s?%s", anthropicAuthURL, params.Encode()) authURL := fmt.Sprintf("%s?%s", AuthURL, params.Encode())
return authURL, state, nil return authURL, state, nil
} }
@@ -137,8 +138,8 @@ func (o *ClaudeAuth) ExchangeCodeForTokens(ctx context.Context, code, state stri
"code": newCode, "code": newCode,
"state": state, "state": state,
"grant_type": "authorization_code", "grant_type": "authorization_code",
"client_id": anthropicClientID, "client_id": ClientID,
"redirect_uri": redirectURI, "redirect_uri": RedirectURI,
"code_verifier": pkceCodes.CodeVerifier, "code_verifier": pkceCodes.CodeVerifier,
} }
@@ -154,7 +155,7 @@ func (o *ClaudeAuth) ExchangeCodeForTokens(ctx context.Context, code, state stri
// log.Debugf("Token exchange request: %s", string(jsonBody)) // log.Debugf("Token exchange request: %s", string(jsonBody))
req, err := http.NewRequestWithContext(ctx, "POST", anthropicTokenURL, strings.NewReader(string(jsonBody))) req, err := http.NewRequestWithContext(ctx, "POST", TokenURL, strings.NewReader(string(jsonBody)))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create token request: %w", err) return nil, fmt.Errorf("failed to create token request: %w", err)
} }
@@ -221,7 +222,7 @@ func (o *ClaudeAuth) RefreshTokens(ctx context.Context, refreshToken string) (*C
} }
reqBody := map[string]interface{}{ reqBody := map[string]interface{}{
"client_id": anthropicClientID, "client_id": ClientID,
"grant_type": "refresh_token", "grant_type": "refresh_token",
"refresh_token": refreshToken, "refresh_token": refreshToken,
} }
@@ -231,7 +232,7 @@ func (o *ClaudeAuth) RefreshTokens(ctx context.Context, refreshToken string) (*C
return nil, fmt.Errorf("failed to marshal request body: %w", err) return nil, fmt.Errorf("failed to marshal request body: %w", err)
} }
req, err := http.NewRequestWithContext(ctx, "POST", anthropicTokenURL, strings.NewReader(string(jsonBody))) req, err := http.NewRequestWithContext(ctx, "POST", TokenURL, strings.NewReader(string(jsonBody)))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create refresh request: %w", err) return nil, fmt.Errorf("failed to create refresh request: %w", err)
} }