File size: 6,653 Bytes
9853396 | 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | package api
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"github.com/looplj/axonhub/internal/log"
"github.com/looplj/axonhub/internal/pkg/xcache"
"github.com/looplj/axonhub/llm/httpclient"
"github.com/looplj/axonhub/llm/oauth"
"github.com/looplj/axonhub/llm/transformer/anthropic/claudecode"
)
type ClaudeCodeHandlersParams struct {
fx.In
CacheConfig xcache.Config
HttpClient *httpclient.HttpClient
}
type ClaudeCodeHandlers struct {
stateCache xcache.Cache[claudeCodeOAuthState]
httpClient *httpclient.HttpClient
}
func NewClaudeCodeHandlers(params ClaudeCodeHandlersParams) *ClaudeCodeHandlers {
return &ClaudeCodeHandlers{
stateCache: xcache.NewFromConfig[claudeCodeOAuthState](params.CacheConfig),
httpClient: params.HttpClient,
}
}
type StartClaudeCodeOAuthRequest struct{}
type StartClaudeCodeOAuthResponse struct {
SessionID string `json:"session_id"`
AuthURL string `json:"auth_url"`
}
type claudeCodeOAuthState struct {
CodeVerifier string `json:"code_verifier"`
CreatedAt int64 `json:"created_at"`
}
func generateClaudeCodeCodeVerifier() (string, error) {
b := make([]byte, 64)
if _, err := rand.Read(b); err != nil {
return "", err
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(b), nil
}
func generateClaudeCodeCodeChallenge(verifier string) string {
hash := sha256.Sum256([]byte(verifier))
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(hash[:])
}
func generateClaudeCodeState() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(b), nil
}
func claudeCodeOAuthCacheKey(sessionID string) string {
return fmt.Sprintf("claudecode:oauth:%s", sessionID)
}
// StartOAuth creates a PKCE session and returns the authorize URL.
// POST /admin/claudecode/oauth/start.
func (h *ClaudeCodeHandlers) StartOAuth(c *gin.Context) {
ctx := c.Request.Context()
var req StartClaudeCodeOAuthRequest
if err := c.ShouldBindJSON(&req); err != nil {
JSONError(c, http.StatusBadRequest, errors.New("invalid request format"))
return
}
state, err := generateClaudeCodeState()
if err != nil {
JSONError(c, http.StatusInternalServerError, fmt.Errorf("failed to generate oauth state: %w", err))
return
}
codeVerifier, err := generateClaudeCodeCodeVerifier()
if err != nil {
JSONError(c, http.StatusInternalServerError, fmt.Errorf("failed to generate code verifier: %w", err))
return
}
codeChallenge := generateClaudeCodeCodeChallenge(codeVerifier)
cacheKey := claudeCodeOAuthCacheKey(state)
if err := h.stateCache.Set(ctx, cacheKey, claudeCodeOAuthState{CodeVerifier: codeVerifier, CreatedAt: time.Now().Unix()}, xcache.WithExpiration(10*time.Minute)); err != nil {
JSONError(c, http.StatusInternalServerError, fmt.Errorf("failed to save oauth state: %w", err))
return
}
params := url.Values{}
params.Set("response_type", "code")
params.Set("client_id", claudecode.ClientID)
params.Set("redirect_uri", claudecode.RedirectURI)
params.Set("scope", claudecode.Scopes)
params.Set("code_challenge", codeChallenge)
params.Set("code_challenge_method", "S256")
params.Set("state", state)
authURL := fmt.Sprintf("%s?%s", claudecode.AuthorizeURL, params.Encode())
c.JSON(http.StatusOK, StartClaudeCodeOAuthResponse{SessionID: state, AuthURL: authURL})
}
type ExchangeClaudeCodeOAuthRequest struct {
SessionID string `json:"session_id" binding:"required"`
CallbackURL string `json:"callback_url" binding:"required"`
}
type ExchangeClaudeCodeOAuthResponse struct {
Credentials string `json:"credentials"`
}
func parseClaudeCodeCallbackURL(callbackURL string) (string, string, error) {
trimmed := strings.TrimSpace(callbackURL)
if !strings.HasPrefix(trimmed, "http://") && !strings.HasPrefix(trimmed, "https://") {
return "", "", fmt.Errorf("callback_url must be a full URL")
}
u, err := url.Parse(trimmed)
if err != nil {
return "", "", fmt.Errorf("invalid callback_url: %w", err)
}
q := u.Query()
code := q.Get("code")
if code == "" {
return "", "", fmt.Errorf("code parameter not found in callback_url")
}
// Claude puts state in URL fragment (after #), not in query params
// Format: http://localhost:54545/callback?code=xxx#state
state := u.Fragment
// Also check query params as fallback
if state == "" {
state = q.Get("state")
}
if state == "" {
return "", "", fmt.Errorf("state parameter not found in callback_url (should be after # or in query)")
}
return code, state, nil
}
// Exchange exchanges callback URL for OAuth credentials JSON.
// POST /admin/claudecode/oauth/exchange.
func (h *ClaudeCodeHandlers) Exchange(c *gin.Context) {
ctx := c.Request.Context()
var req ExchangeClaudeCodeOAuthRequest
if err := c.ShouldBindJSON(&req); err != nil {
JSONError(c, http.StatusBadRequest, errors.New("invalid request format"))
return
}
cacheKey := claudeCodeOAuthCacheKey(req.SessionID)
state, err := h.stateCache.Get(ctx, cacheKey)
if err != nil {
JSONError(c, http.StatusBadRequest, errors.New("invalid or expired oauth session"))
return
}
if err := h.stateCache.Delete(ctx, cacheKey); err != nil {
log.Warn(ctx, "failed to delete used oauth state from cache", log.String("session_id", req.SessionID), log.Cause(err))
}
code, callbackState, err := parseClaudeCodeCallbackURL(req.CallbackURL)
if err != nil {
JSONError(c, http.StatusBadRequest, err)
return
}
if callbackState != req.SessionID {
JSONError(c, http.StatusBadRequest, errors.New("oauth state mismatch"))
return
}
tokenProvider := claudecode.NewTokenProvider(oauth.TokenProviderParams{
HTTPClient: h.httpClient,
})
creds, err := tokenProvider.Exchange(ctx, oauth.ExchangeParams{
Code: code,
State: callbackState,
CodeVerifier: state.CodeVerifier,
ClientID: claudecode.ClientID,
RedirectURI: claudecode.RedirectURI,
})
if err != nil {
JSONError(c, http.StatusBadGateway, fmt.Errorf("token exchange failed: %w", err))
return
}
output, err := creds.ToJSON()
if err != nil {
JSONError(c, http.StatusInternalServerError, fmt.Errorf("failed to encode credentials: %w", err))
return
}
c.JSON(http.StatusOK, ExchangeClaudeCodeOAuthResponse{Credentials: output})
}
|