-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Support mark initial password & force users to change #2003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,20 @@ func Login(c *gin.Context) { | |
|
||
// setup session & cookies and then return user info | ||
func setupLogin(user *model.User, c *gin.Context) { | ||
// 检查是否需要修改初始密码 | ||
if user.IsInitialPassword { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "请立即修改初始密码", | ||
"success": true, | ||
"data": map[string]interface{}{ | ||
"require_change_password": true, | ||
"user_id": user.Id, | ||
"username": user.Username, | ||
}, | ||
}) | ||
return | ||
} | ||
|
||
session := sessions.Default(c) | ||
session.Set("id", user.Id) | ||
session.Set("username", user.Username) | ||
|
@@ -206,11 +220,12 @@ func Register(c *gin.Context) { | |
affCode := user.AffCode // this code is the inviter's code, not the user's own code | ||
inviterId, _ := model.GetUserIdByAffCode(affCode) | ||
cleanUser := model.User{ | ||
Username: user.Username, | ||
Password: user.Password, | ||
DisplayName: user.Username, | ||
InviterId: inviterId, | ||
Role: common.RoleCommonUser, // 明确设置角色为普通用户 | ||
Username: user.Username, | ||
Password: user.Password, | ||
DisplayName: user.Username, | ||
InviterId: inviterId, | ||
Role: common.RoleCommonUser, // 明确设置角色为普通用户 | ||
IsInitialPassword: false, // 用户自行注册不标记为初始密码 | ||
} | ||
if common.EmailVerificationEnabled { | ||
cleanUser.Email = user.Email | ||
|
@@ -639,6 +654,10 @@ func UpdateUser(c *gin.Context) { | |
updatedUser.Password = "" // rollback to what it should be | ||
} | ||
updatePassword := updatedUser.Password != "" | ||
// 如果管理员修改了用户密码,标记为初始密码 | ||
if updatePassword { | ||
updatedUser.IsInitialPassword = true | ||
} | ||
if err := updatedUser.Edit(updatePassword); err != nil { | ||
common.ApiError(c, err) | ||
return | ||
|
@@ -728,22 +747,36 @@ func UpdateSelf(c *gin.Context) { | |
return | ||
} | ||
|
||
cleanUser := model.User{ | ||
Id: c.GetInt("id"), | ||
Username: user.Username, | ||
Password: user.Password, | ||
DisplayName: user.DisplayName, | ||
} | ||
userId := c.GetInt("id") | ||
|
||
if user.Password == "$I_LOVE_U" { | ||
user.Password = "" // rollback to what it should be | ||
cleanUser.Password = "" | ||
} | ||
updatePassword, err := checkUpdatePassword(user.OriginalPassword, user.Password, cleanUser.Id) | ||
updatePassword, err := checkUpdatePassword(user.OriginalPassword, user.Password, userId) | ||
if err != nil { | ||
common.ApiError(c, err) | ||
return | ||
} | ||
if err := cleanUser.Update(updatePassword); err != nil { | ||
|
||
// 构建更新字段的 map | ||
updates := map[string]interface{}{ | ||
"username": user.Username, | ||
"display_name": user.DisplayName, | ||
} | ||
|
||
// 如果正在更新密码,同时清除初始密码标记 | ||
if updatePassword { | ||
hashedPassword, err := common.Password2Hash(user.Password) | ||
if err != nil { | ||
common.ApiError(c, err) | ||
return | ||
} | ||
updates["password"] = hashedPassword | ||
updates["is_initial_password"] = false // 明确设置为 false | ||
} | ||
|
||
// 执行更新 | ||
if err := model.DB.Model(&model.User{}).Where("id = ?", userId).Updates(updates).Error; err != nil { | ||
common.ApiError(c, err) | ||
return | ||
} | ||
|
@@ -856,10 +889,11 @@ func CreateUser(c *gin.Context) { | |
} | ||
// Even for admin users, we cannot fully trust them! | ||
cleanUser := model.User{ | ||
Username: user.Username, | ||
Password: user.Password, | ||
DisplayName: user.DisplayName, | ||
Role: user.Role, // 保持管理员设置的角色 | ||
Username: user.Username, | ||
Password: user.Password, | ||
DisplayName: user.DisplayName, | ||
Role: user.Role, // 保持管理员设置的角色 | ||
IsInitialPassword: true, // 管理员创建的用户标记为初始密码 | ||
} | ||
if err := cleanUser.Insert(0); err != nil { | ||
common.ApiError(c, err) | ||
|
@@ -1109,6 +1143,93 @@ type UpdateUserSettingRequest struct { | |
RecordIpLog bool `json:"record_ip_log"` | ||
} | ||
|
||
type ChangeInitialPasswordRequest struct { | ||
UserId int `json:"user_id" binding:"required"` | ||
Username string `json:"username" binding:"required"` | ||
OldPassword string `json:"old_password" binding:"required"` | ||
NewPassword string `json:"new_password" binding:"required,min=8,max=20"` | ||
} | ||
|
||
func ChangeInitialPassword(c *gin.Context) { | ||
var req ChangeInitialPasswordRequest | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
"message": "无效的参数: " + err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
// 获取用户 | ||
user, err := model.GetUserById(req.UserId, true) | ||
if err != nil { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
"message": "用户不存在", | ||
}) | ||
return | ||
} | ||
|
||
// 验证用户名匹配 | ||
if user.Username != req.Username { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个要用gin自带的bind和validate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已修改为使用binding,并删除.sql |
||
"message": "用户信息不匹配", | ||
}) | ||
return | ||
} | ||
|
||
// 验证旧密码 | ||
if !common.ValidatePasswordAndHash(req.OldPassword, user.Password) { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
"message": "原密码错误", | ||
}) | ||
return | ||
} | ||
|
||
// 验证是否确实需要修改初始密码 | ||
if !user.IsInitialPassword { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
"message": "该用户不需要修改初始密码", | ||
}) | ||
return | ||
} | ||
|
||
// 更新密码并清除初始密码标记 | ||
hashedPassword, err := common.Password2Hash(req.NewPassword) | ||
if err != nil { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
"message": "密码加密失败", | ||
}) | ||
return | ||
} | ||
|
||
// 使用 map 更新以确保 IsInitialPassword = false 能被正确写入 | ||
updates := map[string]interface{}{ | ||
"password": hashedPassword, | ||
"is_initial_password": false, | ||
} | ||
|
||
if err := model.DB.Model(&model.User{}).Where("id = ?", user.Id).Updates(updates).Error; err != nil { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"success": false, | ||
"message": "修改密码失败: " + err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
// 记录日志 | ||
model.RecordLog(user.Id, model.LogTypeSystem, "修改初始密码") | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"message": "密码修改成功,请重新登录", | ||
}) | ||
} | ||
|
||
func UpdateUserSetting(c *gin.Context) { | ||
var req UpdateUserSettingRequest | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ func SetApiRouter(router *gin.Engine) { | |
userRoute.POST("/register", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Register) | ||
userRoute.POST("/login", middleware.CriticalRateLimit(), middleware.TurnstileCheck(), controller.Login) | ||
userRoute.POST("/login/2fa", middleware.CriticalRateLimit(), controller.Verify2FALogin) | ||
userRoute.POST("/change_initial_password", middleware.CriticalRateLimit(), controller.ChangeInitialPassword) | ||
userRoute.POST("/passkey/login/begin", middleware.CriticalRateLimit(), controller.PasskeyLoginBegin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Turnstile check to protect password-change endpoint. Parity with login/register; mitigates brute-force attempts. - userRoute.POST("/change_initial_password", middleware.CriticalRateLimit(), controller.ChangeInitialPassword)
+ userRoute.POST("/change_initial_password",
+ middleware.CriticalRateLimit(),
+ middleware.TurnstileCheck(),
+ controller.ChangeInitialPassword)
🤖 Prompt for AI Agents
|
||
userRoute.POST("/passkey/login/finish", middleware.CriticalRateLimit(), controller.PasskeyLoginFinish) | ||
//userRoute.POST("/tokenlog", middleware.CriticalRateLimit(), controller.TokenLog) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent wiping username/display name on partial updates.
Line 762 unconditionally drops
username
/display_name
into the update map. When the client submits a password-only payload (very common), those fields are omitted in JSON, unmarshal defaults them to""
, and the DB write will blank the username/display name, breaking login. Only include keys that were actually provided in the request.🤖 Prompt for AI Agents