feat(admin): 增加登录限流、审计与令牌退出 (#42)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -27,6 +30,7 @@ type Handler struct {
|
||||
basePath string
|
||||
spa fs.FS
|
||||
submitLimit *rateLimiter
|
||||
loginLimit *loginLimiter
|
||||
usage *ratelimit.Limiter
|
||||
}
|
||||
|
||||
@@ -39,6 +43,7 @@ func New(store *adminstore.Store, authn *auth.Authenticator, basePath string, sp
|
||||
basePath: basePath,
|
||||
spa: spa,
|
||||
submitLimit: newRateLimiter(5, 10*time.Minute),
|
||||
loginLimit: newLoginLimiter(10, 20, 15*time.Minute),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +69,7 @@ func (h *Handler) Router() http.Handler {
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(h.authn.Middleware)
|
||||
r.Post("/api/logout", h.Logout)
|
||||
r.Get("/api/me", h.Me)
|
||||
r.Get("/api/stats", h.Stats)
|
||||
r.Get("/api/audit", h.ListAllAudit)
|
||||
@@ -154,14 +160,66 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "invalid body")
|
||||
return
|
||||
}
|
||||
ip := loginIP(r)
|
||||
if blocked, wait := h.loginLimit.blocked(ip, body.Username); blocked {
|
||||
seconds := int(wait / time.Second)
|
||||
if wait%time.Second != 0 {
|
||||
seconds++
|
||||
}
|
||||
if seconds < 1 {
|
||||
seconds = 1
|
||||
}
|
||||
w.Header().Set("Retry-After", strconv.Itoa(seconds))
|
||||
h.logAdminSession(r, body.Username, "login_blocked", ip)
|
||||
writeError(w, http.StatusTooManyRequests, "too_many_requests", "登录尝试过于频繁,请稍后重试")
|
||||
return
|
||||
}
|
||||
token, err := h.authn.Login(body.Username, body.Password)
|
||||
if err != nil {
|
||||
h.loginLimit.recordFailure(ip, body.Username)
|
||||
h.logAdminSession(r, body.Username, "login_failure", ip)
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "用户名或密码错误")
|
||||
return
|
||||
}
|
||||
h.loginLimit.clearIP(ip)
|
||||
h.logAdminSession(r, body.Username, "login_success", ip)
|
||||
writeJSON(w, http.StatusOK, map[string]string{"token": token, "username": body.Username})
|
||||
}
|
||||
|
||||
// Logout revokes the current bearer token.
|
||||
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
||||
if token == "" {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "令牌无效")
|
||||
return
|
||||
}
|
||||
if err := h.authn.Revoke(token); err != nil {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "令牌无效")
|
||||
return
|
||||
}
|
||||
username := auth.UserFrom(r.Context())
|
||||
h.logAdminSession(r, username, "logout", loginIP(r))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func loginIP(r *http.Request) string {
|
||||
ip := realIP(r)
|
||||
if host, _, err := net.SplitHostPort(ip); err == nil {
|
||||
return host
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
func (h *Handler) logAdminSession(r *http.Request, username, action, ip string) {
|
||||
if h.store == nil {
|
||||
return
|
||||
}
|
||||
detail := map[string]any{"ip": ip, "username": username}
|
||||
if err := h.store.LogAdminSession(r.Context(), username, action, detail); err != nil {
|
||||
log.Printf("admin session audit write failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Me returns the current authenticated user.
|
||||
func (h *Handler) Me(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"username": auth.UserFrom(r.Context())})
|
||||
|
||||
Reference in New Issue
Block a user