Skip to content

Commit c6d3667

Browse files
committed
fix: don't audit login too early
1 parent 71ae3e0 commit c6d3667

1 file changed

Lines changed: 25 additions & 27 deletions

File tree

internal/controller/user_controller.go

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (controller *UserController) loginHandler(c *gin.Context) {
102102
}
103103

104104
if err := controller.auth.CheckUserPassword(*search, req.Password); err != nil {
105-
tlog.App.Warn().Str("username", req.Username).Msg("Invalid password")
105+
tlog.App.Warn().Err(err).Str("username", req.Username).Msg("Failed to verify password")
106106
controller.auth.RecordLoginAttempt(req.Username, false)
107107
tlog.AuditLoginFailure(c, req.Username, "username", "invalid password")
108108
c.JSON(401, gin.H{
@@ -112,16 +112,20 @@ func (controller *UserController) loginHandler(c *gin.Context) {
112112
return
113113
}
114114

115-
tlog.App.Info().Str("username", req.Username).Msg("Login successful")
116-
tlog.AuditLoginSuccess(c, req.Username, "username")
117-
118-
controller.auth.RecordLoginAttempt(req.Username, true)
119-
120115
var localUser *model.LocalUser
121116

122117
if search.Type == model.UserLocal {
123118
localUser = controller.auth.GetLocalUser(req.Username)
124119

120+
if localUser == nil {
121+
tlog.App.Warn().Str("username", req.Username).Msg("User disappeared during login")
122+
c.JSON(401, gin.H{
123+
"status": 401,
124+
"message": "Unauthorized",
125+
})
126+
return
127+
}
128+
125129
if localUser.TOTPSecret != "" {
126130
tlog.App.Debug().Str("username", req.Username).Msg("User has TOTP enabled, requiring TOTP verification")
127131

@@ -198,6 +202,11 @@ func (controller *UserController) loginHandler(c *gin.Context) {
198202

199203
http.SetCookie(c.Writer, cookie)
200204

205+
tlog.App.Info().Str("username", req.Username).Msg("Login successful")
206+
tlog.AuditLoginSuccess(c, req.Username, "username")
207+
208+
controller.auth.RecordLoginAttempt(req.Username, true)
209+
201210
c.JSON(200, gin.H{
202211
"status": 200,
203212
"message": "Login successful",
@@ -326,29 +335,15 @@ func (controller *UserController) totpHandler(c *gin.Context) {
326335
return
327336
}
328337

329-
tlog.App.Info().Str("username", context.GetUsername()).Msg("TOTP verification successful")
330-
tlog.AuditLoginSuccess(c, context.GetUsername(), "totp")
331-
332338
uuid, err := c.Cookie(controller.config.SessionCookieName)
333339

334-
if err != nil {
335-
tlog.App.Error().Err(err).Msg("Failed to retrieve session cookie in TOTP handler")
336-
c.JSON(500, gin.H{
337-
"status": 500,
338-
"message": "Internal Server Error",
339-
})
340-
return
341-
}
342-
343-
_, err = controller.auth.DeleteSession(c, uuid)
344-
345-
if err != nil {
346-
tlog.App.Error().Err(err).Msg("Failed to delete pending TOTP session")
347-
c.JSON(500, gin.H{
348-
"status": 500,
349-
"message": "Internal Server Error",
350-
})
351-
return
340+
if err == nil {
341+
_, err = controller.auth.DeleteSession(c, uuid)
342+
if err != nil {
343+
tlog.App.Warn().Err(err).Msg("Failed to delete pending TOTP session")
344+
}
345+
} else {
346+
tlog.App.Warn().Err(err).Msg("Failed to retrieve session cookie for pending TOTP session, proceeding without deleting it")
352347
}
353348

354349
controller.auth.RecordLoginAttempt(context.GetUsername(), true)
@@ -382,6 +377,9 @@ func (controller *UserController) totpHandler(c *gin.Context) {
382377

383378
http.SetCookie(c.Writer, cookie)
384379

380+
tlog.App.Info().Str("username", context.GetUsername()).Msg("TOTP verification successful")
381+
tlog.AuditLoginSuccess(c, context.GetUsername(), "totp")
382+
385383
c.JSON(200, gin.H{
386384
"status": 200,
387385
"message": "Login successful",

0 commit comments

Comments
 (0)