Repository navigation
Expand file tree
/
Copy pathadmin_opencode_go.go
More file actions
75 lines (65 loc) · 2.22 KB
/
Copy pathadmin_opencode_go.go
File metadata and controls
75 lines (65 loc) · 2.22 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func (h *proxyHandler) serveOpencodeGoAdmin(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/admin/opencode-go")
if path == "" {
path = "/"
}
switch {
case path == "/" && r.Method == http.MethodGet:
h.handleAPIKeyList(w, AccountTypeOpencodeGo)
case path == "/add" && r.Method == http.MethodPost:
h.handleOpencodeGoAdd(w, r)
case strings.HasSuffix(path, "/remove") && r.Method == http.MethodPost:
id := strings.TrimPrefix(path, "/")
id = strings.TrimSuffix(id, "/remove")
h.handleAPIKeyRemove(w, AccountTypeOpencodeGo, id)
default:
http.NotFound(w, r)
}
}
func (h *proxyHandler) handleOpencodeGoAdd(w http.ResponseWriter, r *http.Request) {
var req struct {
APIKey string `json:"api_key"`
}
if r.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
respondJSONError(w, http.StatusBadRequest, "invalid json: "+err.Error())
return
}
} else {
req.APIKey = r.FormValue("api_key")
}
apiKey := strings.TrimSpace(req.APIKey)
if apiKey == "" {
respondJSONError(w, http.StatusBadRequest, "api_key is required")
return
}
// Validate the key against the free /usage endpoint: 200 proves the key
// is live, 401/403 means bad key or no Go subscription. No spend.
validationURL := strings.TrimRight(h.cfg.opencodeGoBase.String(), "/") + "/usage"
validReq, _ := http.NewRequest(http.MethodGet, validationURL, nil)
validReq.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := h.transport.RoundTrip(validReq)
if err != nil {
respondJSONError(w, http.StatusBadGateway, "failed to validate key: "+err.Error())
return
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
respondJSONError(w, http.StatusBadRequest, "invalid API key (authentication failed or no Go subscription)")
return
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
respondJSONError(w, http.StatusBadGateway, fmt.Sprintf("key validation returned status %d", resp.StatusCode))
return
}
h.saveAPIKeyAccountFile(w, r, AccountTypeOpencodeGo, "opencode_go", apiKey)
}