Moge-Row commited on
Commit
6ce2d19
·
1 Parent(s): a3b4fd8

fix auth flow for RWPX keys

Browse files
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
- // Extract token from x-api-key or Authorization header
23
- token := r.Header.Get("x-api-key")
24
- if token == "" {
25
- token = strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
26
- }
27
- if token == "" {
28
- writeAnthropicError(w, http.StatusUnauthorized, "authentication_error", "Missing API key")
29
- return
30
- }
31
- ok, reason := CheckAndTrack(token, 0)
32
- if !ok {
33
- writeAnthropicError(w, http.StatusTooManyRequests, "rate_limit_error", reason)
34
- return
35
- }
36
- if token == "free" || strings.HasPrefix(token, "RWPX-") {
37
- anonymousToken, err := auth.GetAnonymousToken()
38
- if err != nil {
39
- logger.LogError("Failed to get anonymous token: %v", err)
40
- writeAnthropicError(w, http.StatusInternalServerError, "api_error", "Failed to get anonymous token")
41
- return
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
- token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
23
- if token == "" {
24
- http.Error(w, "Unauthorized", http.StatusUnauthorized)
25
- return
26
- }
27
-
28
- if token == "free" {
29
- anonymousToken, err := auth.GetAnonymousToken()
30
- if err != nil {
31
- logger.LogError("Failed to get anonymous token: %v", err)
32
- http.Error(w, "Failed to get anonymous token", http.StatusInternalServerError)
33
- return
34
- }
35
- token = anonymousToken
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)