Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
// cmd/gateway/admin_auth_handlers.go 处理初始化、登录、登出和当前会话查询等嵌入式管理端认证接口。
|
|
|
|
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tursom/mc-gateway/internal/adminhttp"
|
|
"github.com/tursom/mc-gateway/internal/adminuser"
|
|
)
|
|
|
|
func handleAdminSetupStatus(w http.ResponseWriter, r *http.Request) {
|
|
empty, err := usersTableEmpty(r.Context(), adminDB)
|
|
if err != nil {
|
|
adminhttp.WriteAPIError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{"required": empty})
|
|
}
|
|
|
|
func handleAdminSetup(w http.ResponseWriter, r *http.Request) {
|
|
var req adminhttp.SetupRequest
|
|
if !adminhttp.DecodeJSONRequest(w, r, &req) {
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.Username) == "" {
|
|
req.Username = "admin"
|
|
}
|
|
|
|
err := createInitialAdmin(r.Context(), req.Username, req.Password)
|
|
if err != nil {
|
|
recordAudit(r.Context(), "setup", adminhttp.RequestSourceIP(r), "setup", "user", req.Username, false, err.Error())
|
|
status := http.StatusBadRequest
|
|
if strings.Contains(err.Error(), "already") {
|
|
status = http.StatusConflict
|
|
}
|
|
adminhttp.WriteAPIError(w, status, err.Error())
|
|
return
|
|
}
|
|
recordAudit(r.Context(), "setup", adminhttp.RequestSourceIP(r), "setup", "user", req.Username, true, "created initial admin")
|
|
adminhttp.WriteJSON(w, http.StatusCreated, map[string]any{"ok": true})
|
|
}
|
|
|
|
func handleAdminLogin(w http.ResponseWriter, r *http.Request) {
|
|
var req adminhttp.LoginRequest
|
|
if !adminhttp.DecodeJSONRequest(w, r, &req) {
|
|
return
|
|
}
|
|
|
|
user, err := authenticateUser(r.Context(), req.Username, req.Password)
|
|
if err != nil {
|
|
recordAudit(r.Context(), req.Username, adminhttp.RequestSourceIP(r), "login", "user", req.Username, false, err.Error())
|
|
adminhttp.WriteAPIError(w, http.StatusUnauthorized, err.Error())
|
|
return
|
|
}
|
|
|
|
session, err := createSession(user.Username, user.Role)
|
|
if err != nil {
|
|
adminhttp.WriteAPIError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
recordAudit(r.Context(), user.Username, adminhttp.RequestSourceIP(r), "login", "user", user.Username, true, "login success")
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{
|
|
"token": session.Token,
|
|
"expires_at": session.ExpiresAt.Unix(),
|
|
"user": user,
|
|
})
|
|
}
|
|
|
|
func handleAdminLogout(w http.ResponseWriter, r *http.Request) {
|
|
session, ok := requireSession(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
deleteSession(session.Token)
|
|
recordAudit(r.Context(), session.Username, adminhttp.RequestSourceIP(r), "logout", "session", session.Username, true, "logout success")
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
|
|
}
|
|
|
|
func handleAdminMe(w http.ResponseWriter, r *http.Request) {
|
|
session, ok := requireSession(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{
|
|
"username": session.Username,
|
|
"role": session.Role,
|
|
"expires_at": session.ExpiresAt.Unix(),
|
|
"permissions": adminuser.Permissions(session.Role),
|
|
})
|
|
}
|
|
|
|
func handleAdminStatus(w http.ResponseWriter, r *http.Request) {
|
|
if _, ok := requireRole(w, r, adminRoleMember); !ok {
|
|
return
|
|
}
|
|
services, err := listServiceConfigs(r.Context(), adminDB)
|
|
if err != nil {
|
|
adminhttp.WriteAPIError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
adminhttp.WriteJSON(w, http.StatusOK, map[string]any{
|
|
"pid": os.Getpid(),
|
|
"uptime_seconds": int64(time.Since(processStartAt).Seconds()),
|
|
"db_path": adminDBPath,
|
|
"tcp_admin_port": adminStartup.TCPAdminPort,
|
|
"admin_path": adminStartup.AdminPath,
|
|
"admin_api_prefix": adminStartup.AdminAPIPrefix,
|
|
"services": services,
|
|
})
|
|
}
|