Spaces:
Paused
Paused
fix auth flow for RWPX keys
Browse files- internal/handler/anthropic.go +21 -24
- internal/handler/chat.go +21 -16
internal/handler/anthropic.go
CHANGED
|
@@ -19,30 +19,27 @@ import (
|
|
| 19 |
|
| 20 |
// HandleMessages handles Anthropic Messages API requests (/v1/messages)
|
| 21 |
func HandleMessages(w http.ResponseWriter, r *http.Request) {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
token = anonymousToken
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
var req model.AnthropicRequest
|
| 47 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 48 |
writeAnthropicError(w, http.StatusBadRequest, "invalid_request_error", "Invalid request body")
|
|
|
|
| 19 |
|
| 20 |
// HandleMessages handles Anthropic Messages API requests (/v1/messages)
|
| 21 |
func HandleMessages(w http.ResponseWriter, r *http.Request) {
|
| 22 |
+
apiKey := r.Header.Get("x-api-key")
|
| 23 |
+
if apiKey == "" { apiKey = strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") }
|
| 24 |
+
if apiKey == "" {
|
| 25 |
+
writeAnthropicError(w, http.StatusUnauthorized, "authentication_error", "Missing API key")
|
| 26 |
+
return
|
| 27 |
+
}
|
| 28 |
+
ok, reason := CheckAndTrack(apiKey, 0)
|
| 29 |
+
if !ok {
|
| 30 |
+
writeAnthropicError(w, http.StatusTooManyRequests, "rate_limit_error", reason)
|
| 31 |
+
return
|
| 32 |
+
}
|
| 33 |
+
token := apiKey
|
| 34 |
+
if token == "free" || strings.HasPrefix(token, "RWPX-") {
|
| 35 |
+
anonymousToken, err := auth.GetAnonymousToken()
|
| 36 |
+
if err != nil {
|
| 37 |
+
logger.LogError("Failed to get anonymous token: %v", err)
|
| 38 |
+
writeAnthropicError(w, http.StatusInternalServerError, "api_error", "Failed to get anonymous token")
|
| 39 |
+
return
|
| 40 |
+
}
|
| 41 |
+
token = anonymousToken
|
| 42 |
+
}
|
|
|
|
|
|
|
|
|
|
| 43 |
var req model.AnthropicRequest
|
| 44 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 45 |
writeAnthropicError(w, http.StatusBadRequest, "invalid_request_error", "Invalid request body")
|
internal/handler/chat.go
CHANGED
|
@@ -19,22 +19,27 @@ import (
|
|
| 19 |
)
|
| 20 |
|
| 21 |
func HandleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
var req model.ChatRequest
|
| 39 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 40 |
http.Error(w, "Invalid request", http.StatusBadRequest)
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
func HandleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
| 22 |
+
apiKey := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
| 23 |
+
if apiKey == "" { apiKey = r.Header.Get("x-api-key") }
|
| 24 |
+
if apiKey == "" {
|
| 25 |
+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
| 26 |
+
return
|
| 27 |
+
}
|
| 28 |
+
ok, reason := CheckAndTrack(apiKey, 0)
|
| 29 |
+
if !ok {
|
| 30 |
+
http.Error(w, reason, http.StatusTooManyRequests)
|
| 31 |
+
return
|
| 32 |
+
}
|
| 33 |
+
token := apiKey
|
| 34 |
+
if token == "free" || strings.HasPrefix(token, "RWPX-") {
|
| 35 |
+
anonymousToken, err := auth.GetAnonymousToken()
|
| 36 |
+
if err != nil {
|
| 37 |
+
logger.LogError("Failed to get anonymous token: %v", err)
|
| 38 |
+
http.Error(w, "Failed to get anonymous token", http.StatusInternalServerError)
|
| 39 |
+
return
|
| 40 |
+
}
|
| 41 |
+
token = anonymousToken
|
| 42 |
+
}
|
| 43 |
var req model.ChatRequest
|
| 44 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 45 |
http.Error(w, "Invalid request", http.StatusBadRequest)
|