refactor(admin): clean LLM artifacts in handler — dedup lease actions, inline types, remove stubs

cognitive committed Mar 5, 2026 at 02:18 UTC bfc993501e1b0451f812e241ef3217920b000fc6
3 files changed +79 -93
portal/admin/handler.go
+78 -90
@@ -3,6 +3,7 @@ package admin
3 import (
4 "encoding/base64"
5 "encoding/json"
6 + "errors"
7 "net/http"
8 "strings"
9
@@ -13,42 +14,34 @@ import (
14 "gosuda.org/portal/types"
15 )
16
16 -type ServeAppStaticFunc func(http.ResponseWriter, *http.Request, string, *portal.RelayServer)
17 -type ListLeasesFunc func(*portal.RelayServer) any
18 -type StatsFunc func(*portal.RelayServer) map[string]any
19 -type DecodeLeaseIDFunc func(string) (string, bool)
20 -type SecureRequestFunc func(*http.Request, bool) bool
21 -type WriteAPIDataFunc func(http.ResponseWriter, int, any)
22 -type WriteAPIOKFunc func(http.ResponseWriter, int)
23 -type WriteAPIErrorFunc func(http.ResponseWriter, int, string, string)
24 -type WriteAPIErrorWithDataFunc func(http.ResponseWriter, int, string, string, any)
17 +var errInvalidLeaseID = errors.New("invalid lease ID")
18
19 type HandlerConfig struct {
20 Service *Service
28 - ServeAppStatic ServeAppStaticFunc
29 - ListLeases ListLeasesFunc
30 - Stats StatsFunc
31 - DecodeLeaseID DecodeLeaseIDFunc
32 - IsSecureRequest SecureRequestFunc
33 - WriteAPIData WriteAPIDataFunc
34 - WriteAPIOK WriteAPIOKFunc
35 - WriteAPIError WriteAPIErrorFunc
36 - WriteAPIErrorWithData WriteAPIErrorWithDataFunc
21 + ServeAppStatic func(http.ResponseWriter, *http.Request, string, *portal.RelayServer)
22 + ListLeases func(*portal.RelayServer) any
23 + Stats func(*portal.RelayServer) map[string]any
24 + DecodeLeaseID func(string) (string, bool)
25 + IsSecureRequest func(*http.Request, bool) bool
26 + WriteAPIData func(http.ResponseWriter, int, any)
27 + WriteAPIOK func(http.ResponseWriter, int)
28 + WriteAPIError func(http.ResponseWriter, int, string, string)
29 + WriteAPIErrorWithData func(http.ResponseWriter, int, string, string, any)
30 TrustProxy bool
31 }
32
33 // Handler routes /admin/* HTTP requests and delegates policy mutations to Service.
34 type Handler struct {
35 service *Service
43 - serveAppStatic ServeAppStaticFunc
44 - listLeases ListLeasesFunc
45 - stats StatsFunc
46 - decodeLeaseID DecodeLeaseIDFunc
47 - isSecureRequest SecureRequestFunc
48 - writeAPIData WriteAPIDataFunc
49 - writeAPIOK WriteAPIOKFunc
50 - writeAPIError WriteAPIErrorFunc
51 - writeAPIErrorWithData WriteAPIErrorWithDataFunc
36 + serveAppStatic func(http.ResponseWriter, *http.Request, string, *portal.RelayServer)
37 + listLeases func(*portal.RelayServer) any
38 + stats func(*portal.RelayServer) map[string]any
39 + decodeLeaseID func(string) (string, bool)
40 + isSecureRequest func(*http.Request, bool) bool
41 + writeAPIData func(http.ResponseWriter, int, any)
42 + writeAPIOK func(http.ResponseWriter, int)
43 + writeAPIError func(http.ResponseWriter, int, string, string)
44 + writeAPIErrorWithData func(http.ResponseWriter, int, string, string, any)
45 trustProxy bool
46 }
47
@@ -83,7 +76,6 @@ func NewHandler(cfg HandlerConfig) *Handler {
76 }
77 return map[string]any{
78 "leases_count": count,
86 - "uptime": "TODO",
79 }
80 }
81 }
@@ -318,43 +310,36 @@ func (h *Handler) handleAuthStatus(w http.ResponseWriter, r *http.Request) {
310 })
311 }
312
321 -type leaseActionRouteStatus uint8
322 -
323 -const (
324 - leaseActionRouteNotFound leaseActionRouteStatus = iota
325 - leaseActionRouteInvalidLeaseID
326 - leaseActionRouteOK
327 -)
328 -
329 -func (h *Handler) parseLeaseActionRoute(route string) (leaseID, action string, status leaseActionRouteStatus) {
313 +func (h *Handler) parseLeaseActionRoute(route string) (leaseID, action string, err error) {
314 parts := strings.Split(route, "/")
315 if len(parts) != 3 || parts[0] != "leases" {
332 - return "", "", leaseActionRouteNotFound
316 + return "", "", errors.New("route not found")
317 }
318
319 action = parts[2]
320 switch action {
321 case "ban", "bps", "approve", "deny":
322 default:
339 - return "", "", leaseActionRouteNotFound
323 + return "", "", errors.New("route not found")
324 }
325
342 - leaseID, ok := h.decodeLeaseID(parts[1])
326 + var ok bool
327 + leaseID, ok = h.decodeLeaseID(parts[1])
328 if !ok {
344 - return "", action, leaseActionRouteInvalidLeaseID
329 + return "", action, errInvalidLeaseID
330 }
331
347 - return leaseID, action, leaseActionRouteOK
332 + return leaseID, action, nil
333 }
334
335 func (h *Handler) handleLeaseActionRouteRequest(w http.ResponseWriter, r *http.Request, serv *portal.RelayServer, route string) bool {
351 - leaseID, action, status := h.parseLeaseActionRoute(route)
352 - switch status {
353 - case leaseActionRouteNotFound:
336 + leaseID, action, err := h.parseLeaseActionRoute(route)
337 + if err != nil {
338 + if errors.Is(err, errInvalidLeaseID) {
339 + h.writeAPIError(w, http.StatusBadRequest, "invalid_lease_id", "invalid lease ID")
340 + return true
341 + }
342 return false
355 - case leaseActionRouteInvalidLeaseID:
356 - h.writeAPIError(w, http.StatusBadRequest, "invalid_lease_id", "invalid lease ID")
357 - return true
343 }
344
345 switch action {
@@ -373,7 +358,17 @@ func (h *Handler) handleLeaseActionRouteRequest(w http.ResponseWriter, r *http.R
358 return true
359 }
360
376 -func (h *Handler) handleLeaseBanRequest(w http.ResponseWriter, r *http.Request, serv *portal.RelayServer, leaseID string) {
361 +// handleLeaseToggleRequest is a generic helper for lease toggle actions (ban, approve, deny).
362 +func (h *Handler) handleLeaseToggleRequest(
363 + w http.ResponseWriter,
364 + r *http.Request,
365 + serv *portal.RelayServer,
366 + leaseID string,
367 + onPost func(),
368 + onDelete func(),
369 + logMsgPost string,
370 + logMsgDelete string,
371 +) {
372 if strings.TrimSpace(leaseID) == "" {
373 h.writeAPIError(w, http.StatusBadRequest, "invalid_lease_id", "invalid lease ID")
374 return
@@ -381,18 +376,33 @@ func (h *Handler) handleLeaseBanRequest(w http.ResponseWriter, r *http.Request,
376
377 switch r.Method {
378 case http.MethodPost:
384 - serv.GetLeaseManager().BanLease(leaseID)
379 + onPost()
380 h.service.SaveSettings(serv)
381 + if logMsgPost != "" {
382 + log.Info().Str("lease_id", leaseID).Msg(logMsgPost)
383 + }
384 h.writeAPIOK(w, http.StatusOK)
385 case http.MethodDelete:
388 - serv.GetLeaseManager().UnbanLease(leaseID)
386 + onDelete()
387 h.service.SaveSettings(serv)
388 + if logMsgDelete != "" {
389 + log.Info().Str("lease_id", leaseID).Msg(logMsgDelete)
390 + }
391 h.writeAPIOK(w, http.StatusOK)
392 default:
393 h.writeAPIError(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
394 }
395 }
396
397 +func (h *Handler) handleLeaseBanRequest(w http.ResponseWriter, r *http.Request, serv *portal.RelayServer, leaseID string) {
398 + h.handleLeaseToggleRequest(
399 + w, r, serv, leaseID,
400 + func() { serv.GetLeaseManager().BanLease(leaseID) },
401 + func() { serv.GetLeaseManager().UnbanLease(leaseID) },
402 + "", "",
403 + )
404 +}
405 +
406 func (h *Handler) handleGetSettings(w http.ResponseWriter) {
407 approveManager := h.service.GetApproveManager()
408 h.writeAPIData(w, http.StatusOK, types.AdminSettingsResponse{
@@ -403,50 +413,28 @@ func (h *Handler) handleGetSettings(w http.ResponseWriter) {
413 }
414
415 func (h *Handler) handleLeaseApproveRequest(w http.ResponseWriter, r *http.Request, serv *portal.RelayServer, leaseID string) {
406 - if strings.TrimSpace(leaseID) == "" {
407 - h.writeAPIError(w, http.StatusBadRequest, "invalid_lease_id", "invalid lease ID")
408 - return
409 - }
410 -
416 approveManager := h.service.GetApproveManager()
412 - switch r.Method {
413 - case http.MethodPost:
414 - approveManager.ApproveLease(leaseID)
415 - approveManager.UndenyLease(leaseID) // Remove from denied if exists.
416 - h.service.SaveSettings(serv)
417 - log.Info().Str("lease_id", leaseID).Msg("[Admin] Lease approved")
418 - h.writeAPIOK(w, http.StatusOK)
419 - case http.MethodDelete:
420 - approveManager.RevokeLease(leaseID)
421 - h.service.SaveSettings(serv)
422 - log.Info().Str("lease_id", leaseID).Msg("[Admin] Lease approval revoked")
423 - h.writeAPIOK(w, http.StatusOK)
424 - default:
425 - h.writeAPIError(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
426 - }
417 + h.handleLeaseToggleRequest(
418 + w, r, serv, leaseID,
419 + func() {
420 + approveManager.ApproveLease(leaseID)
421 + approveManager.UndenyLease(leaseID) // Remove from denied if exists.
422 + },
423 + func() { approveManager.RevokeLease(leaseID) },
424 + "[Admin] Lease approved",
425 + "[Admin] Lease approval revoked",
426 + )
427 }
428
429 func (h *Handler) handleLeaseDenyRequest(w http.ResponseWriter, r *http.Request, serv *portal.RelayServer, leaseID string) {
430 - if strings.TrimSpace(leaseID) == "" {
431 - h.writeAPIError(w, http.StatusBadRequest, "invalid_lease_id", "invalid lease ID")
432 - return
433 - }
434 -
430 approveManager := h.service.GetApproveManager()
436 - switch r.Method {
437 - case http.MethodPost:
438 - approveManager.DenyLease(leaseID)
439 - h.service.SaveSettings(serv)
440 - log.Info().Str("lease_id", leaseID).Msg("[Admin] Lease denied")
441 - h.writeAPIOK(w, http.StatusOK)
442 - case http.MethodDelete:
443 - approveManager.UndenyLease(leaseID)
444 - h.service.SaveSettings(serv)
445 - log.Info().Str("lease_id", leaseID).Msg("[Admin] Lease denial removed")
446 - h.writeAPIOK(w, http.StatusOK)
447 - default:
448 - h.writeAPIError(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
449 - }
431 + h.handleLeaseToggleRequest(
432 + w, r, serv, leaseID,
433 + func() { approveManager.DenyLease(leaseID) },
434 + func() { approveManager.UndenyLease(leaseID) },
435 + "[Admin] Lease denied",
436 + "[Admin] Lease denial removed",
437 + )
438 }
439
440 func (h *Handler) handleLeaseBPSRequest(w http.ResponseWriter, r *http.Request, serv *portal.RelayServer, leaseID string) {
portal/admin/handler_test.go
+1 -1
@@ -113,7 +113,7 @@ func TestHandleAdminRequestLeaseActionInvalidLeaseID(t *testing.T) {
113 }
114 }
115
116 -func newTestHandler(t *testing.T, service *Service, secure bool, listLeases ListLeasesFunc) *Handler {
116 +func newTestHandler(t *testing.T, service *Service, secure bool, listLeases func(*portal.RelayServer) any) *Handler {
117 t.Helper()
118
119 if listLeases == nil {
portal/policy/authenticator.go
-2
@@ -39,8 +39,6 @@ type loginAttempt struct {
39
40 // NewAuthenticator creates a new Authenticator with the given secret key.
41 func NewAuthenticator(secretKey string) *Authenticator {
42 - // Create Authenticator for admin authentication
43 - // Auto-generate secret key if not provided
42 if secretKey == "" {
43 randomBytes := make([]byte, 16)
44 if _, err := rand.Read(randomBytes); err != nil {