Files
Ydisks-Xianyu-Helper/internal/server/login_audit.go
T
Christ 25419c027f 修复登录续期与数据库验证回归
- 修复密码登录、续期和账号编辑链路的边界问题
- 补齐 dbverify、后端与前端单测
- 同步 Docker 与前端构建产物
2026-07-09 16:08:16 +08:00

88 lines
2.5 KiB
Go

package server
import (
"context"
"strings"
"time"
"xianyu-go/internal/db"
)
const (
loginMethodManual = "manual"
loginMethodPassword = "password"
loginMethodQRScan = "qr_scan"
loginStatusSuccess = "success"
loginStatusFailed = "failed"
)
func normalizeLoginMethod(method string) string {
switch strings.ToLower(strings.TrimSpace(method)) {
case "manual", "cookie", "manual_cookie":
return loginMethodManual
case "password", "password_login":
return loginMethodPassword
case "qr", "qr_login", "qr_scan":
return loginMethodQRScan
default:
return strings.ToLower(strings.TrimSpace(method))
}
}
func (s *Server) markSuccessfulLogin(ctx context.Context, cookieID string, userID int64, method, message string) {
method = normalizeLoginMethod(method)
if method == "" {
return
}
at := time.Now().Unix()
if err := s.Store.Cookies.MarkLogin(ctx, cookieID, method, at); err != nil {
if s.Logger != nil {
s.Logger.Warn("记录账号登录方式失败", "cookie_id", cookieID, "method", method, "err", err)
}
return
}
if method == loginMethodPassword || method == loginMethodQRScan {
if err := s.Store.Cookies.SetStatusWithReason(ctx, cookieID, true, ""); err != nil && s.Logger != nil {
s.Logger.Warn("成功登录后启用账号失败", "cookie_id", cookieID, "method", method, "err", err)
}
}
s.addLoginLog(ctx, cookieID, userID, method, loginStatusSuccess, "", message, at)
}
func (s *Server) addLoginLog(ctx context.Context, cookieID string, userID int64, method, status, failureReason, message string, at int64) {
if s.Store == nil || s.Store.LoginLogs == nil {
return
}
if at == 0 {
at = time.Now().Unix()
}
if err := s.Store.LoginLogs.Add(ctx, db.AccountLoginLog{
CookieID: cookieID,
UserID: userID,
OwnerID: userID,
AccountIdentifier: cookieID,
Method: normalizeLoginMethod(method),
Status: status,
Message: truncate(message, 500),
TriggerReason: loginTriggerReason(method),
FailureReason: failureReason,
ErrorMessage: truncate(message, 500),
CreatedAt: at,
}); err != nil && s.Logger != nil {
s.Logger.Warn("记录账号登录日志失败", "cookie_id", cookieID, "method", method, "status", status, "err", err)
}
}
func loginTriggerReason(method string) string {
switch normalizeLoginMethod(method) {
case loginMethodManual:
return "手动Cookie录入"
case loginMethodPassword:
return "账号密码登录"
case loginMethodQRScan:
return "扫码登录"
default:
return ""
}
}