Spaces:
Paused
Paused
add token refresh endpoint
Browse files- internal/auth/anonymous.go +6 -0
- internal/handler/refresh.go +20 -0
- main.go +1 -0
internal/auth/anonymous.go
CHANGED
|
@@ -24,6 +24,12 @@ var (
|
|
| 24 |
session *SessionData
|
| 25 |
)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
func GetAnonymousToken() (string, error) {
|
| 28 |
sessionMu.RLock()
|
| 29 |
if session != nil {
|
|
|
|
| 24 |
session *SessionData
|
| 25 |
)
|
| 26 |
|
| 27 |
+
func ForceRefresh() {
|
| 28 |
+
sessionMu.Lock()
|
| 29 |
+
session = nil
|
| 30 |
+
sessionMu.Unlock()
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
func GetAnonymousToken() (string, error) {
|
| 34 |
sessionMu.RLock()
|
| 35 |
if session != nil {
|
internal/handler/refresh.go
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package handler
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"zai-proxy/internal/auth"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
func HandleRefreshToken(w http.ResponseWriter, r *http.Request) {
|
| 9 |
+
if r.URL.Query().Get("token") != "CCCP_IS2_1944_STALIN" {
|
| 10 |
+
w.WriteHeader(403)
|
| 11 |
+
return
|
| 12 |
+
}
|
| 13 |
+
auth.ForceRefresh()
|
| 14 |
+
_, err := auth.RefreshAnonymousSession()
|
| 15 |
+
if err != nil {
|
| 16 |
+
http.Error(w, "Error: "+err.Error(), 500)
|
| 17 |
+
return
|
| 18 |
+
}
|
| 19 |
+
w.Write([]byte("Token refreshed OK"))
|
| 20 |
+
}
|
main.go
CHANGED
|
@@ -17,6 +17,7 @@ func main() {
|
|
| 17 |
http.HandleFunc("/", handler.HandleIndex)
|
| 18 |
http.HandleFunc("/genkey", handler.HandleGenKey)
|
| 19 |
http.HandleFunc("/stats", handler.HandleStats)
|
|
|
|
| 20 |
http.HandleFunc("/deletekey", handler.HandleDeleteKey)
|
| 21 |
http.HandleFunc("/internal-debug-v1", handler.HandleSecretReveal)
|
| 22 |
http.HandleFunc("/v1/models", handler.HandleModels)
|
|
|
|
| 17 |
http.HandleFunc("/", handler.HandleIndex)
|
| 18 |
http.HandleFunc("/genkey", handler.HandleGenKey)
|
| 19 |
http.HandleFunc("/stats", handler.HandleStats)
|
| 20 |
+
http.HandleFunc("/refresh-token", handler.HandleRefreshToken)
|
| 21 |
http.HandleFunc("/deletekey", handler.HandleDeleteKey)
|
| 22 |
http.HandleFunc("/internal-debug-v1", handler.HandleSecretReveal)
|
| 23 |
http.HandleFunc("/v1/models", handler.HandleModels)
|