fix(auth): audit-log login success + failure attempts
Login handler hatte h.Audit injiziert (mit Kommentar "login-success/fail ins audit_log fließen") aber nie aufgerufen. Jetzt werden geloggt: - auth.login.failed (reason: invalid_credentials | account_disabled) - auth.login.success (mit role + remote IP) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -86,16 +86,25 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
|
||||
email := strings.TrimSpace(req.Email)
|
||||
actor, role := "", "admin"
|
||||
remote := c.ClientIP()
|
||||
|
||||
// 1. Try DB users table first.
|
||||
if h.Users != nil {
|
||||
u, hash, dbErr := h.Users.FindByEmail(c.Request.Context(), email)
|
||||
if dbErr == nil {
|
||||
if !u.Active {
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), email, "auth.login.failed",
|
||||
email, gin.H{"reason": "account_disabled", "remote": remote}, h.NodeID)
|
||||
}
|
||||
response.Unauthorized(c, errors.New("account_disabled"))
|
||||
return
|
||||
}
|
||||
if !usersvc.VerifyPassword(hash, req.Password) {
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), email, "auth.login.failed",
|
||||
email, gin.H{"reason": "invalid_credentials", "remote": remote}, h.NodeID)
|
||||
}
|
||||
response.Unauthorized(c, errors.New("invalid_credentials"))
|
||||
return
|
||||
}
|
||||
@@ -108,6 +117,10 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
// 2. Fallback: setup-store admin (backwards compat for pre-DB installs).
|
||||
if actor == "" {
|
||||
if !strings.EqualFold(st.AdminEmail, email) || !st.VerifyAdminPassword(req.Password) {
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), email, "auth.login.failed",
|
||||
email, gin.H{"reason": "invalid_credentials", "remote": remote}, h.NodeID)
|
||||
}
|
||||
response.Unauthorized(c, errors.New("invalid_credentials"))
|
||||
return
|
||||
}
|
||||
@@ -127,6 +140,10 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
||||
}
|
||||
setSessionCookie(c, raw, tok.Exp)
|
||||
|
||||
if h.Audit != nil {
|
||||
_ = h.Audit.Log(c.Request.Context(), actor, "auth.login.success",
|
||||
actor, gin.H{"role": role, "remote": remote}, h.NodeID)
|
||||
}
|
||||
response.OK(c, loginResponse{
|
||||
Actor: tok.Actor,
|
||||
Role: tok.Role,
|
||||
|
||||
Reference in New Issue
Block a user